1 /* 2 * Test directory reading operations, exploring concurrency issues. 3 * 4 * Copyright (C) 2020, 2021, 2022 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 int main(int argc, char *argv[]) 35 { 36 if (argc < 2) 37 { 38 printf("Need a directory name.\n"); 39 return 1; 40 } 41 42 char *filename = argv[1]; 43 44 printf("Opening %s...\n", filename); 45 46 file_t *reader = client_opendir(filename); 47 48 if (reader == NULL) 49 { 50 printf("Could not read from directory.\n"); 51 return 1; 52 } 53 54 printf("Reading...\n"); 55 56 struct dirent *dirent; 57 int filenum = 1000, remaining = 100; 58 char buffer[strlen(filename) + strlen("/file-XXXX.txt") + 10]; 59 char data[256]; 60 int found = 0; 61 file_t *files[100]; 62 63 while ((dirent = client_readdir(reader)) != NULL) 64 { 65 free(dirent); 66 found++; 67 68 /* Create files while getting the listing. */ 69 70 if (remaining) 71 { 72 sprintf(buffer, "%s/file-%d.txt", filename, filenum++); 73 74 file_t *file = client_open(buffer, O_RDWR | O_CREAT); 75 76 if (file == NULL) 77 { 78 printf("Could not open file: %s\n", buffer); 79 return 1; 80 } 81 82 /* Write something to each file. */ 83 84 sprintf(data, "Data in file-%d.txt", filenum - 1); 85 client_write(file, data, strlen(data)); 86 87 /* Remember the file for later. */ 88 89 files[remaining - 1] = file; 90 91 remaining--; 92 } 93 } 94 95 printf("Files found: %d\n", found); 96 97 /* Re-read, counting files. */ 98 99 reader = client_opendir(filename); 100 101 if (reader == NULL) 102 { 103 printf("Could not read from directory.\n"); 104 return 1; 105 } 106 107 found = 0; 108 109 while ((dirent = client_readdir(reader)) != NULL) 110 { 111 free(dirent); 112 found++; 113 } 114 115 printf("Files found: %d\n", found); 116 117 /* Remove some files. */ 118 119 for (filenum = 1000, remaining = 100; remaining > 0; remaining--) 120 { 121 sprintf(buffer, "%s/file-%d.txt", filename, filenum++); 122 123 long err = client_remove(buffer); 124 125 if (err) 126 { 127 printf("Could not remove file: %s\n", buffer); 128 return 1; 129 } 130 } 131 132 /* Re-read, counting files. */ 133 134 reader = client_opendir(filename); 135 136 if (reader == NULL) 137 { 138 printf("Could not read from directory.\n"); 139 return 1; 140 } 141 142 found = 0; 143 144 while ((dirent = client_readdir(reader)) != NULL) 145 { 146 free(dirent); 147 found++; 148 } 149 150 printf("Files found: %d\n", found); 151 152 /* Read from the still-open but now removed files. */ 153 154 for (remaining = 0; remaining < 100; remaining++) 155 { 156 file_t *file = files[remaining]; 157 158 client_seek(file, SEEK_SET, 0); 159 offset_t nread = client_read(file, data, 256); 160 161 data[nread] = '\0'; 162 printf("Read: %s\n", data); 163 } 164 165 return 0; 166 } 167 168 // vim: tabstop=2 expandtab shiftwidth=2