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_open_using(filename, O_DIRECTORY, opener); 51 } 52 else 53 { 54 return client_open(filename, O_DIRECTORY); 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 file_t *reader = client_opendir(file); 83 84 if (reader == NULL) 85 { 86 printf("Could not read from directory.\n"); 87 return 1; 88 } 89 90 printf("Reading...\n"); 91 92 struct dirent *dirent; 93 94 while ((dirent = client_readdir(reader)) != NULL) 95 { 96 printf("> %s\n", dirent->d_name); 97 free(dirent); 98 } 99 100 printf("Directory shown.\n"); 101 102 /* Open again, reading a single entry only. */ 103 104 file = open_directory(filename, have_uid, uid); 105 106 if (file == NULL) 107 { 108 printf("Could not obtain directory.\n"); 109 return 1; 110 } 111 112 reader = client_opendir(file); 113 114 if (reader == NULL) 115 { 116 printf("Could not read from directory.\n"); 117 return 1; 118 } 119 120 dirent = client_readdir(reader); 121 122 if (dirent != NULL) 123 { 124 printf("> %s\n", dirent->d_name); 125 free(dirent); 126 } 127 128 printf("Entry shown.\n"); 129 130 return 0; 131 } 132 133 // vim: tabstop=2 expandtab shiftwidth=2