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_file(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 static long open_directory(file_t *file) 61 { 62 /* Register the reader for notification. */ 63 64 return client_set_blocking(file, NOTIFY_CONTENT_AVAILABLE | NOTIFY_PEER_CLOSED); 65 } 66 67 68 69 int main(int argc, char *argv[]) 70 { 71 if (argc < 2) 72 { 73 printf("Need a directory name and an optional user identifier (if used with a filesystem).\n"); 74 return 1; 75 } 76 77 char *filename = argv[1]; 78 bool have_uid = (argc > 2) && strlen(argv[2]); 79 sys_uid_t uid = have_uid ? atoi(argv[2]) : 0; 80 file_t *file = open_file(filename, have_uid, uid); 81 82 if (file == NULL) 83 { 84 printf("Could not obtain directory.\n"); 85 return 1; 86 } 87 88 long err = open_directory(file); 89 90 if (err) 91 { 92 printf("Could not subscribe to notifications: %s\n", l4sys_errtostr(err)); 93 return 1; 94 } 95 96 struct dirent *dirent; 97 98 while ((dirent = client_readdir(file)) != NULL) 99 { 100 printf("> %s\n", dirent->d_name); 101 free(dirent); 102 } 103 104 printf("Directory shown.\n"); 105 106 /* Open again, reading a single entry only. */ 107 108 file = open_file(filename, have_uid, uid); 109 110 if (file == NULL) 111 { 112 printf("Could not obtain directory.\n"); 113 return 1; 114 } 115 116 err = open_directory(file); 117 118 if (err) 119 { 120 printf("Could not subscribe to notifications: %s\n", l4sys_errtostr(err)); 121 return 1; 122 } 123 124 dirent = client_readdir(file); 125 126 if (dirent != NULL) 127 { 128 printf("> %s\n", dirent->d_name); 129 free(dirent); 130 } 131 132 printf("Entry shown.\n"); 133 134 return 0; 135 } 136 137 // vim: tabstop=2 expandtab shiftwidth=2