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 38 39 /* Return a directory entry. This must be freed by the caller after use. */ 40 41 static struct dirent *read_directory_entry(file_t *file) 42 { 43 char buffer[DIRENT_CORE_SIZE]; 44 offset_t nread = client_read(file, buffer, DIRENT_CORE_SIZE); 45 46 /* Stop if no new structure can be successfully read. */ 47 48 if (nread != DIRENT_CORE_SIZE) 49 return NULL; 50 51 struct dirent *dirent = (struct dirent *) buffer; 52 offset_t remaining = dirent->d_reclen - DIRENT_CORE_SIZE; 53 54 /* Allocate a buffer for the complete structure. */ 55 56 char *entry = (char *) calloc(DIRENT_CORE_SIZE + remaining, sizeof(char)); 57 58 if (entry == NULL) 59 return NULL; 60 61 /* Copy the start of the entry into a new buffer. */ 62 63 memcpy(entry, buffer, DIRENT_CORE_SIZE); 64 65 /* Append to the entry buffer. */ 66 67 char *current = entry + DIRENT_CORE_SIZE; 68 69 nread = client_read(file, current, remaining); 70 71 /* Stop if no complete structure can be successfully read. */ 72 73 if (nread != remaining) 74 { 75 free(entry); 76 return NULL; 77 } 78 79 return (struct dirent *) entry; 80 } 81 82 83 84 static file_t *open_file(char *filename, bool have_uid, sys_uid_t uid) 85 { 86 /* With a user, open a user-specific file opener. */ 87 88 if (have_uid) 89 { 90 l4_cap_idx_t opener = client_open_for_user((user_t) {uid, uid, 0022}); 91 92 if (l4_is_invalid_cap(opener)) 93 { 94 printf("Could not obtain opener for file.\n"); 95 return NULL; 96 } 97 98 /* Invoke the open method to receive the file reference. */ 99 100 return client_open_using(filename, O_DIRECTORY, opener); 101 } 102 else 103 { 104 return client_open(filename, O_DIRECTORY); 105 } 106 } 107 108 109 110 static long open_directory(file_t *file) 111 { 112 // NOTE: To be replaced by a proper mechanism identifying the nature of each 113 // NOTE: obtained object. 114 115 file->can_mmap = 0; 116 file->has_size = 0; 117 118 /* Register the reader for notification. */ 119 120 return client_set_blocking(file, NOTIFY_CONTENT_AVAILABLE | NOTIFY_PEER_CLOSED); 121 } 122 123 124 125 int main(int argc, char *argv[]) 126 { 127 if (argc < 2) 128 { 129 printf("Need a directory name and an optional user identifier (if used with a filesystem).\n"); 130 return 1; 131 } 132 133 char *filename = argv[1]; 134 bool have_uid = (argc > 2) && strlen(argv[2]); 135 sys_uid_t uid = have_uid ? atoi(argv[2]) : 0; 136 file_t *file = open_file(filename, have_uid, uid); 137 138 if (file == NULL) 139 { 140 printf("Could not obtain directory.\n"); 141 return 1; 142 } 143 144 long err = open_directory(file); 145 146 if (err) 147 { 148 printf("Could not subscribe to notifications: %s\n", l4sys_errtostr(err)); 149 return 1; 150 } 151 152 struct dirent *dirent; 153 154 while ((dirent = read_directory_entry(file)) != NULL) 155 { 156 printf("> %s\n", dirent->d_name); 157 free(dirent); 158 } 159 160 printf("Directory shown.\n"); 161 162 /* Open again, reading a single entry only. */ 163 164 file = open_file(filename, have_uid, uid); 165 166 if (file == NULL) 167 { 168 printf("Could not obtain directory.\n"); 169 return 1; 170 } 171 172 err = open_directory(file); 173 174 if (err) 175 { 176 printf("Could not subscribe to notifications: %s\n", l4sys_errtostr(err)); 177 return 1; 178 } 179 180 dirent = read_directory_entry(file); 181 182 if (dirent != NULL) 183 { 184 printf("> %s\n", dirent->d_name); 185 free(dirent); 186 } 187 188 printf("Entry shown.\n"); 189 190 return 0; 191 } 192 193 // vim: tabstop=2 expandtab shiftwidth=2