1 /* 2 * Test directory reading operations, exploring concurrency issues. 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_opendir_using(filename, opener); 51 } 52 else 53 { 54 return client_opendir(filename); 55 } 56 } 57 58 static file_t *open_file(char *filename, bool have_uid, sys_uid_t uid) 59 { 60 /* With a user, open a user-specific file opener. */ 61 62 if (have_uid) 63 { 64 l4_cap_idx_t opener = client_open_for_user((user_t) {uid, uid, 0022}); 65 66 if (l4_is_invalid_cap(opener)) 67 { 68 printf("Could not obtain opener for file.\n"); 69 return NULL; 70 } 71 72 /* Invoke the open method to receive the file reference. */ 73 74 return client_open_using(filename, O_RDWR | O_CREAT, opener); 75 } 76 else 77 { 78 return client_open(filename, O_RDWR | O_CREAT); 79 } 80 } 81 82 static long remove_file(char *filename, bool have_uid, sys_uid_t uid) 83 { 84 /* With a user, open a user-specific file opener. */ 85 86 if (have_uid) 87 { 88 l4_cap_idx_t opener = client_open_for_user((user_t) {uid, uid, 0022}); 89 90 if (l4_is_invalid_cap(opener)) 91 { 92 printf("Could not obtain opener for file.\n"); 93 return -L4_EIO; 94 } 95 96 /* Invoke the remove method to remove the file. */ 97 98 return client_remove_using(filename, opener); 99 } 100 else 101 { 102 return client_remove(filename); 103 } 104 } 105 106 107 108 int main(int argc, char *argv[]) 109 { 110 if (argc < 2) 111 { 112 printf("Need a directory name and an optional user identifier (if used with a filesystem).\n"); 113 return 1; 114 } 115 116 char *filename = argv[1]; 117 bool have_uid = (argc > 2) && strlen(argv[2]); 118 sys_uid_t uid = have_uid ? atoi(argv[2]) : 0; 119 120 printf("Opening %s...\n", filename); 121 122 file_t *reader = open_directory(filename, have_uid, uid); 123 124 if (reader == NULL) 125 { 126 printf("Could not read from directory.\n"); 127 return 1; 128 } 129 130 printf("Reading...\n"); 131 132 struct dirent *dirent; 133 int filenum = 1000, remaining = 100; 134 char buffer[strlen(filename) + strlen("/file-XXXX.txt") + 10]; 135 char data[256]; 136 int found = 0; 137 file_t *files[100]; 138 139 while ((dirent = client_readdir(reader)) != NULL) 140 { 141 free(dirent); 142 found++; 143 144 /* Create files while getting the listing. */ 145 146 if (remaining) 147 { 148 sprintf(buffer, "%s/file-%d.txt", filename, filenum++); 149 150 file_t *file = open_file(buffer, have_uid, uid); 151 152 if (file == NULL) 153 { 154 printf("Could not open file: %s\n", buffer); 155 return 1; 156 } 157 158 /* Write something to each file. */ 159 160 sprintf(data, "Data in file-%d.txt", filenum - 1); 161 client_write(file, data, strlen(data)); 162 163 /* Remember the file for later. */ 164 165 files[remaining - 1] = file; 166 167 remaining--; 168 } 169 } 170 171 printf("Files found: %d\n", found); 172 173 /* Re-read, counting files. */ 174 175 reader = open_directory(filename, have_uid, uid); 176 177 if (reader == NULL) 178 { 179 printf("Could not read from directory.\n"); 180 return 1; 181 } 182 183 found = 0; 184 185 while ((dirent = client_readdir(reader)) != NULL) 186 { 187 free(dirent); 188 found++; 189 } 190 191 printf("Files found: %d\n", found); 192 193 /* Remove some files. */ 194 195 for (filenum = 1000, remaining = 100; remaining > 0; remaining--) 196 { 197 sprintf(buffer, "%s/file-%d.txt", filename, filenum++); 198 199 long err = remove_file(buffer, have_uid, uid); 200 201 if (err) 202 { 203 printf("Could not remove file: %s\n", buffer); 204 return 1; 205 } 206 } 207 208 /* Re-read, counting files. */ 209 210 reader = open_directory(filename, have_uid, uid); 211 212 if (reader == NULL) 213 { 214 printf("Could not read from directory.\n"); 215 return 1; 216 } 217 218 found = 0; 219 220 while ((dirent = client_readdir(reader)) != NULL) 221 { 222 free(dirent); 223 found++; 224 } 225 226 printf("Files found: %d\n", found); 227 228 /* Read from the still-open but now removed files. */ 229 230 for (remaining = 0; remaining < 100; remaining++) 231 { 232 file_t *file = files[remaining]; 233 234 client_seek(file, SEEK_SET, 0); 235 offset_t nread = client_read(file, data, 256); 236 237 data[nread] = '\0'; 238 printf("Read: %s\n", data); 239 } 240 241 return 0; 242 } 243 244 // vim: tabstop=2 expandtab shiftwidth=2