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 83 84 int main(int argc, char *argv[]) 85 { 86 if (argc < 2) 87 { 88 printf("Need a directory name and an optional user identifier (if used with a filesystem).\n"); 89 return 1; 90 } 91 92 char *filename = argv[1]; 93 bool have_uid = (argc > 2) && strlen(argv[2]); 94 sys_uid_t uid = have_uid ? atoi(argv[2]) : 0; 95 96 printf("Opening %s...\n", filename); 97 98 file_t *reader = open_directory(filename, have_uid, uid); 99 100 if (reader == NULL) 101 { 102 printf("Could not read from directory.\n"); 103 return 1; 104 } 105 106 printf("Reading...\n"); 107 108 struct dirent *dirent; 109 int filenum = 1000, remaining = 100; 110 char buffer[strlen(filename) + strlen("/file-XXXX.txt") + 10]; 111 int found = 0; 112 113 while ((dirent = client_readdir(reader)) != NULL) 114 { 115 free(dirent); 116 found++; 117 118 /* Create files while getting the listing. */ 119 120 if (remaining) 121 { 122 sprintf(buffer, "%s/file-%d.txt", filename, filenum++); 123 124 file_t *file = open_file(buffer, have_uid, uid); 125 126 if (file == NULL) 127 { 128 printf("Could not open file: %s\n", buffer); 129 return 1; 130 } 131 132 remaining--; 133 } 134 } 135 136 printf("Files found: %d\n", found); 137 138 /* Re-read, counting files. */ 139 140 reader = open_directory(filename, have_uid, uid); 141 142 if (reader == NULL) 143 { 144 printf("Could not read from directory.\n"); 145 return 1; 146 } 147 148 found = 0; 149 150 while ((dirent = client_readdir(reader)) != NULL) 151 { 152 free(dirent); 153 found++; 154 } 155 156 printf("Files found: %d\n", found); 157 158 return 0; 159 } 160 161 // vim: tabstop=2 expandtab shiftwidth=2