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 /* Register the reader for notification. */ 113 114 return client_set_blocking(file, NOTIFY_CONTENT_AVAILABLE | NOTIFY_PEER_CLOSED); 115 } 116 117 118 119 int main(int argc, char *argv[]) 120 { 121 if (argc < 2) 122 { 123 printf("Need a directory name and an optional user identifier (if used with a filesystem).\n"); 124 return 1; 125 } 126 127 char *filename = argv[1]; 128 bool have_uid = (argc > 2) && strlen(argv[2]); 129 sys_uid_t uid = have_uid ? atoi(argv[2]) : 0; 130 file_t *file = open_file(filename, have_uid, uid); 131 132 if (file == NULL) 133 { 134 printf("Could not obtain directory.\n"); 135 return 1; 136 } 137 138 long err = open_directory(file); 139 140 if (err) 141 { 142 printf("Could not subscribe to notifications: %s\n", l4sys_errtostr(err)); 143 return 1; 144 } 145 146 struct dirent *dirent; 147 148 while ((dirent = read_directory_entry(file)) != NULL) 149 { 150 printf("> %s\n", dirent->d_name); 151 free(dirent); 152 } 153 154 printf("Directory shown.\n"); 155 156 /* Open again, reading a single entry only. */ 157 158 file = open_file(filename, have_uid, uid); 159 160 if (file == NULL) 161 { 162 printf("Could not obtain directory.\n"); 163 return 1; 164 } 165 166 err = open_directory(file); 167 168 if (err) 169 { 170 printf("Could not subscribe to notifications: %s\n", l4sys_errtostr(err)); 171 return 1; 172 } 173 174 dirent = read_directory_entry(file); 175 176 if (dirent != NULL) 177 { 178 printf("> %s\n", dirent->d_name); 179 free(dirent); 180 } 181 182 printf("Entry shown.\n"); 183 184 return 0; 185 } 186 187 // vim: tabstop=2 expandtab shiftwidth=2