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