paul@158 | 1 | /* |
paul@160 | 2 | * Test directory reading operations. |
paul@158 | 3 | * |
paul@160 | 4 | * Copyright (C) 2020, 2021 Paul Boddie <paul@boddie.org.uk> |
paul@158 | 5 | * |
paul@158 | 6 | * This program is free software; you can redistribute it and/or |
paul@158 | 7 | * modify it under the terms of the GNU General Public License as |
paul@158 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@158 | 9 | * the License, or (at your option) any later version. |
paul@158 | 10 | * |
paul@158 | 11 | * This program is distributed in the hope that it will be useful, |
paul@158 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@158 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@158 | 14 | * GNU General Public License for more details. |
paul@158 | 15 | * |
paul@158 | 16 | * You should have received a copy of the GNU General Public License |
paul@158 | 17 | * along with this program; if not, write to the Free Software |
paul@158 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@158 | 19 | * Boston, MA 02110-1301, USA |
paul@158 | 20 | */ |
paul@158 | 21 | |
paul@160 | 22 | #include <l4/re/env.h> |
paul@160 | 23 | #include <l4/sys/err.h> |
paul@160 | 24 | |
paul@158 | 25 | #include <stdio.h> |
paul@160 | 26 | #include <string.h> |
paul@160 | 27 | #include <stdlib.h> |
paul@160 | 28 | |
paul@160 | 29 | #include <fsclient/client.h> |
paul@160 | 30 | #include <systypes/fcntl.h> |
paul@158 | 31 | |
paul@158 | 32 | |
paul@158 | 33 | |
paul@175 | 34 | static file_t *open_directory(char *filename, bool have_uid, sys_uid_t uid) |
paul@170 | 35 | { |
paul@170 | 36 | /* With a user, open a user-specific file opener. */ |
paul@170 | 37 | |
paul@170 | 38 | if (have_uid) |
paul@170 | 39 | { |
paul@170 | 40 | l4_cap_idx_t opener = client_open_for_user((user_t) {uid, uid, 0022}); |
paul@170 | 41 | |
paul@170 | 42 | if (l4_is_invalid_cap(opener)) |
paul@170 | 43 | { |
paul@170 | 44 | printf("Could not obtain opener for file.\n"); |
paul@170 | 45 | return NULL; |
paul@170 | 46 | } |
paul@170 | 47 | |
paul@170 | 48 | /* Invoke the open method to receive the file reference. */ |
paul@170 | 49 | |
paul@175 | 50 | return client_opendir_using(filename, opener); |
paul@170 | 51 | } |
paul@170 | 52 | else |
paul@170 | 53 | { |
paul@175 | 54 | return client_opendir(filename); |
paul@170 | 55 | } |
paul@170 | 56 | } |
paul@170 | 57 | |
paul@170 | 58 | |
paul@170 | 59 | |
paul@158 | 60 | int main(int argc, char *argv[]) |
paul@158 | 61 | { |
paul@158 | 62 | if (argc < 2) |
paul@158 | 63 | { |
paul@160 | 64 | printf("Need a directory name and an optional user identifier (if used with a filesystem).\n"); |
paul@160 | 65 | return 1; |
paul@160 | 66 | } |
paul@160 | 67 | |
paul@160 | 68 | char *filename = argv[1]; |
paul@160 | 69 | bool have_uid = (argc > 2) && strlen(argv[2]); |
paul@160 | 70 | sys_uid_t uid = have_uid ? atoi(argv[2]) : 0; |
paul@178 | 71 | |
paul@178 | 72 | printf("Opening %s...\n", filename); |
paul@178 | 73 | |
paul@175 | 74 | file_t *file = open_directory(filename, have_uid, uid); |
paul@160 | 75 | |
paul@160 | 76 | if (file == NULL) |
paul@160 | 77 | { |
paul@160 | 78 | printf("Could not obtain directory.\n"); |
paul@158 | 79 | return 1; |
paul@158 | 80 | } |
paul@158 | 81 | |
paul@178 | 82 | printf("Reading...\n"); |
paul@178 | 83 | |
paul@170 | 84 | struct dirent *dirent; |
paul@160 | 85 | |
paul@174 | 86 | while ((dirent = client_readdir(file)) != NULL) |
paul@170 | 87 | { |
paul@170 | 88 | printf("> %s\n", dirent->d_name); |
paul@170 | 89 | free(dirent); |
paul@160 | 90 | } |
paul@158 | 91 | |
paul@158 | 92 | printf("Directory shown.\n"); |
paul@158 | 93 | |
paul@170 | 94 | /* Open again, reading a single entry only. */ |
paul@170 | 95 | |
paul@175 | 96 | file = open_directory(filename, have_uid, uid); |
paul@170 | 97 | |
paul@170 | 98 | if (file == NULL) |
paul@170 | 99 | { |
paul@170 | 100 | printf("Could not obtain directory.\n"); |
paul@170 | 101 | return 1; |
paul@170 | 102 | } |
paul@170 | 103 | |
paul@174 | 104 | dirent = client_readdir(file); |
paul@170 | 105 | |
paul@170 | 106 | if (dirent != NULL) |
paul@170 | 107 | { |
paul@170 | 108 | printf("> %s\n", dirent->d_name); |
paul@170 | 109 | free(dirent); |
paul@170 | 110 | } |
paul@170 | 111 | |
paul@170 | 112 | printf("Entry shown.\n"); |
paul@170 | 113 | |
paul@158 | 114 | return 0; |
paul@158 | 115 | } |
paul@158 | 116 | |
paul@158 | 117 | // vim: tabstop=2 expandtab shiftwidth=2 |