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