paul@215 | 1 | /* |
paul@215 | 2 | * Test directory reading operations, exploring concurrency issues. |
paul@215 | 3 | * |
paul@237 | 4 | * Copyright (C) 2020, 2021, 2022 Paul Boddie <paul@boddie.org.uk> |
paul@215 | 5 | * |
paul@215 | 6 | * This program is free software; you can redistribute it and/or |
paul@215 | 7 | * modify it under the terms of the GNU General Public License as |
paul@215 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@215 | 9 | * the License, or (at your option) any later version. |
paul@215 | 10 | * |
paul@215 | 11 | * This program is distributed in the hope that it will be useful, |
paul@215 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@215 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@215 | 14 | * GNU General Public License for more details. |
paul@215 | 15 | * |
paul@215 | 16 | * You should have received a copy of the GNU General Public License |
paul@215 | 17 | * along with this program; if not, write to the Free Software |
paul@215 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@215 | 19 | * Boston, MA 02110-1301, USA |
paul@215 | 20 | */ |
paul@215 | 21 | |
paul@215 | 22 | #include <l4/re/env.h> |
paul@215 | 23 | #include <l4/sys/err.h> |
paul@215 | 24 | |
paul@215 | 25 | #include <stdio.h> |
paul@215 | 26 | #include <string.h> |
paul@215 | 27 | #include <stdlib.h> |
paul@215 | 28 | |
paul@215 | 29 | #include <fsclient/client.h> |
paul@215 | 30 | #include <systypes/fcntl.h> |
paul@215 | 31 | |
paul@215 | 32 | |
paul@215 | 33 | |
paul@215 | 34 | int main(int argc, char *argv[]) |
paul@215 | 35 | { |
paul@215 | 36 | if (argc < 2) |
paul@215 | 37 | { |
paul@237 | 38 | printf("Need a directory name.\n"); |
paul@215 | 39 | return 1; |
paul@215 | 40 | } |
paul@215 | 41 | |
paul@215 | 42 | char *filename = argv[1]; |
paul@215 | 43 | |
paul@215 | 44 | printf("Opening %s...\n", filename); |
paul@215 | 45 | |
paul@237 | 46 | file_t *reader = client_opendir(filename); |
paul@215 | 47 | |
paul@215 | 48 | if (reader == NULL) |
paul@215 | 49 | { |
paul@215 | 50 | printf("Could not read from directory.\n"); |
paul@215 | 51 | return 1; |
paul@215 | 52 | } |
paul@215 | 53 | |
paul@215 | 54 | printf("Reading...\n"); |
paul@215 | 55 | |
paul@215 | 56 | struct dirent *dirent; |
paul@215 | 57 | int filenum = 1000, remaining = 100; |
paul@215 | 58 | char buffer[strlen(filename) + strlen("/file-XXXX.txt") + 10]; |
paul@232 | 59 | char data[256]; |
paul@215 | 60 | int found = 0; |
paul@232 | 61 | file_t *files[100]; |
paul@215 | 62 | |
paul@215 | 63 | while ((dirent = client_readdir(reader)) != NULL) |
paul@215 | 64 | { |
paul@215 | 65 | free(dirent); |
paul@215 | 66 | found++; |
paul@215 | 67 | |
paul@215 | 68 | /* Create files while getting the listing. */ |
paul@215 | 69 | |
paul@215 | 70 | if (remaining) |
paul@215 | 71 | { |
paul@215 | 72 | sprintf(buffer, "%s/file-%d.txt", filename, filenum++); |
paul@215 | 73 | |
paul@237 | 74 | file_t *file = client_open(buffer, O_RDWR | O_CREAT); |
paul@215 | 75 | |
paul@215 | 76 | if (file == NULL) |
paul@215 | 77 | { |
paul@215 | 78 | printf("Could not open file: %s\n", buffer); |
paul@215 | 79 | return 1; |
paul@215 | 80 | } |
paul@215 | 81 | |
paul@232 | 82 | /* Write something to each file. */ |
paul@232 | 83 | |
paul@232 | 84 | sprintf(data, "Data in file-%d.txt", filenum - 1); |
paul@232 | 85 | client_write(file, data, strlen(data)); |
paul@232 | 86 | |
paul@232 | 87 | /* Remember the file for later. */ |
paul@232 | 88 | |
paul@232 | 89 | files[remaining - 1] = file; |
paul@232 | 90 | |
paul@215 | 91 | remaining--; |
paul@215 | 92 | } |
paul@215 | 93 | } |
paul@215 | 94 | |
paul@215 | 95 | printf("Files found: %d\n", found); |
paul@215 | 96 | |
paul@215 | 97 | /* Re-read, counting files. */ |
paul@215 | 98 | |
paul@237 | 99 | reader = client_opendir(filename); |
paul@215 | 100 | |
paul@215 | 101 | if (reader == NULL) |
paul@215 | 102 | { |
paul@215 | 103 | printf("Could not read from directory.\n"); |
paul@215 | 104 | return 1; |
paul@215 | 105 | } |
paul@215 | 106 | |
paul@215 | 107 | found = 0; |
paul@215 | 108 | |
paul@215 | 109 | while ((dirent = client_readdir(reader)) != NULL) |
paul@215 | 110 | { |
paul@215 | 111 | free(dirent); |
paul@215 | 112 | found++; |
paul@215 | 113 | } |
paul@215 | 114 | |
paul@215 | 115 | printf("Files found: %d\n", found); |
paul@215 | 116 | |
paul@232 | 117 | /* Remove some files. */ |
paul@232 | 118 | |
paul@232 | 119 | for (filenum = 1000, remaining = 100; remaining > 0; remaining--) |
paul@232 | 120 | { |
paul@232 | 121 | sprintf(buffer, "%s/file-%d.txt", filename, filenum++); |
paul@232 | 122 | |
paul@237 | 123 | long err = client_remove(buffer); |
paul@232 | 124 | |
paul@232 | 125 | if (err) |
paul@232 | 126 | { |
paul@232 | 127 | printf("Could not remove file: %s\n", buffer); |
paul@232 | 128 | return 1; |
paul@232 | 129 | } |
paul@232 | 130 | } |
paul@232 | 131 | |
paul@232 | 132 | /* Re-read, counting files. */ |
paul@232 | 133 | |
paul@237 | 134 | reader = client_opendir(filename); |
paul@232 | 135 | |
paul@232 | 136 | if (reader == NULL) |
paul@232 | 137 | { |
paul@232 | 138 | printf("Could not read from directory.\n"); |
paul@232 | 139 | return 1; |
paul@232 | 140 | } |
paul@232 | 141 | |
paul@232 | 142 | found = 0; |
paul@232 | 143 | |
paul@232 | 144 | while ((dirent = client_readdir(reader)) != NULL) |
paul@232 | 145 | { |
paul@232 | 146 | free(dirent); |
paul@232 | 147 | found++; |
paul@232 | 148 | } |
paul@232 | 149 | |
paul@232 | 150 | printf("Files found: %d\n", found); |
paul@232 | 151 | |
paul@232 | 152 | /* Read from the still-open but now removed files. */ |
paul@232 | 153 | |
paul@232 | 154 | for (remaining = 0; remaining < 100; remaining++) |
paul@232 | 155 | { |
paul@232 | 156 | file_t *file = files[remaining]; |
paul@232 | 157 | |
paul@232 | 158 | client_seek(file, SEEK_SET, 0); |
paul@232 | 159 | offset_t nread = client_read(file, data, 256); |
paul@232 | 160 | |
paul@232 | 161 | data[nread] = '\0'; |
paul@232 | 162 | printf("Read: %s\n", data); |
paul@232 | 163 | } |
paul@232 | 164 | |
paul@215 | 165 | return 0; |
paul@215 | 166 | } |
paul@215 | 167 | |
paul@215 | 168 | // vim: tabstop=2 expandtab shiftwidth=2 |