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 file_t *file = open_directory(filename, have_uid, uid); 72 73 if (file == NULL) 74 { 75 printf("Could not obtain directory.\n"); 76 return 1; 77 } 78 79 struct dirent *dirent; 80 81 while ((dirent = client_readdir(file)) != NULL) 82 { 83 printf("> %s\n", dirent->d_name); 84 free(dirent); 85 } 86 87 printf("Directory shown.\n"); 88 89 /* Open again, reading a single entry only. */ 90 91 file = open_directory(filename, have_uid, uid); 92 93 if (file == NULL) 94 { 95 printf("Could not obtain directory.\n"); 96 return 1; 97 } 98 99 dirent = client_readdir(file); 100 101 if (dirent != NULL) 102 { 103 printf("> %s\n", dirent->d_name); 104 free(dirent); 105 } 106 107 printf("Entry shown.\n"); 108 109 return 0; 110 } 111 112 // vim: tabstop=2 expandtab shiftwidth=2