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 <dirent.h> 26 #include <stdio.h> 27 #include <string.h> 28 #include <stdlib.h> 29 30 #include <fsclient/client.h> 31 #include <systypes/fcntl.h> 32 33 34 35 #define DIRENT_CORE_SIZE (sizeof(struct dirent) - sizeof(((struct dirent *) 0)->d_name)) 36 37 int main(int argc, char *argv[]) 38 { 39 if (argc < 2) 40 { 41 printf("Need a directory name and an optional user identifier (if used with a filesystem).\n"); 42 return 1; 43 } 44 45 char *filename = argv[1]; 46 bool have_uid = (argc > 2) && strlen(argv[2]); 47 sys_uid_t uid = have_uid ? atoi(argv[2]) : 0; 48 file_t *file; 49 50 /* With a user, open a user-specific file opener. */ 51 52 if (have_uid) 53 { 54 l4_cap_idx_t opener = client_open_for_user((user_t) {uid, uid, 0022}); 55 56 if (l4_is_invalid_cap(opener)) 57 { 58 printf("Could not obtain opener for file.\n"); 59 return 1; 60 } 61 62 /* Invoke the open method to receive the file reference. */ 63 64 file = client_open_using(filename, O_DIRECTORY, opener); 65 } 66 else 67 { 68 file = client_open(filename, O_DIRECTORY); 69 } 70 71 if (file == NULL) 72 { 73 printf("Could not obtain directory.\n"); 74 return 1; 75 } 76 77 // NOTE: To be replaced by a proper mechanism identifying the nature of each 78 // NOTE: obtained object. 79 80 file->can_mmap = 0; 81 file->has_size = 0; 82 83 /* Register the reader for notification. */ 84 85 long err = client_set_blocking(file, NOTIFY_CONTENT_AVAILABLE); 86 87 if (err) 88 { 89 printf("Could not subscribe to notifications: %s\n", l4sys_errtostr(err)); 90 return 1; 91 } 92 93 char buffer[DIRENT_CORE_SIZE]; 94 offset_t nread = client_read(file, buffer, DIRENT_CORE_SIZE); 95 offset_t total = 0; 96 97 while (nread) 98 { 99 total += nread; 100 101 if (total == DIRENT_CORE_SIZE) 102 { 103 struct dirent *dirent = (struct dirent *) buffer; 104 int remaining = dirent->d_reclen - DIRENT_CORE_SIZE; 105 char entry[DIRENT_CORE_SIZE + remaining], *current; 106 107 memcpy(entry, buffer, DIRENT_CORE_SIZE); 108 current = entry + DIRENT_CORE_SIZE; 109 110 do 111 { 112 nread = client_read(file, current, remaining); 113 remaining -= nread; 114 current += nread; 115 } 116 while (nread && remaining); 117 118 if (remaining) 119 break; 120 121 dirent = (struct dirent *) entry; 122 printf("> %s\n", dirent->d_name); 123 124 total = 0; 125 } 126 127 nread = client_read(file, buffer, DIRENT_CORE_SIZE); 128 } 129 130 printf("Directory shown.\n"); 131 132 return 0; 133 } 134 135 // vim: tabstop=2 expandtab shiftwidth=2