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