1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tests/dstest_file_readdir_concurrent.cc Wed Oct 13 00:51:40 2021 +0200
1.3 @@ -0,0 +1,161 @@
1.4 +/*
1.5 + * Test directory reading operations, exploring concurrency issues.
1.6 + *
1.7 + * Copyright (C) 2020, 2021 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#include <l4/re/env.h>
1.26 +#include <l4/sys/err.h>
1.27 +
1.28 +#include <stdio.h>
1.29 +#include <string.h>
1.30 +#include <stdlib.h>
1.31 +
1.32 +#include <fsclient/client.h>
1.33 +#include <systypes/fcntl.h>
1.34 +
1.35 +
1.36 +
1.37 +static file_t *open_directory(char *filename, bool have_uid, sys_uid_t uid)
1.38 +{
1.39 + /* With a user, open a user-specific file opener. */
1.40 +
1.41 + if (have_uid)
1.42 + {
1.43 + l4_cap_idx_t opener = client_open_for_user((user_t) {uid, uid, 0022});
1.44 +
1.45 + if (l4_is_invalid_cap(opener))
1.46 + {
1.47 + printf("Could not obtain opener for file.\n");
1.48 + return NULL;
1.49 + }
1.50 +
1.51 + /* Invoke the open method to receive the file reference. */
1.52 +
1.53 + return client_opendir_using(filename, opener);
1.54 + }
1.55 + else
1.56 + {
1.57 + return client_opendir(filename);
1.58 + }
1.59 +}
1.60 +
1.61 +static file_t *open_file(char *filename, bool have_uid, sys_uid_t uid)
1.62 +{
1.63 + /* With a user, open a user-specific file opener. */
1.64 +
1.65 + if (have_uid)
1.66 + {
1.67 + l4_cap_idx_t opener = client_open_for_user((user_t) {uid, uid, 0022});
1.68 +
1.69 + if (l4_is_invalid_cap(opener))
1.70 + {
1.71 + printf("Could not obtain opener for file.\n");
1.72 + return NULL;
1.73 + }
1.74 +
1.75 + /* Invoke the open method to receive the file reference. */
1.76 +
1.77 + return client_open_using(filename, O_RDWR | O_CREAT, opener);
1.78 + }
1.79 + else
1.80 + {
1.81 + return client_open(filename, O_RDWR | O_CREAT);
1.82 + }
1.83 +}
1.84 +
1.85 +
1.86 +
1.87 +int main(int argc, char *argv[])
1.88 +{
1.89 + if (argc < 2)
1.90 + {
1.91 + printf("Need a directory name and an optional user identifier (if used with a filesystem).\n");
1.92 + return 1;
1.93 + }
1.94 +
1.95 + char *filename = argv[1];
1.96 + bool have_uid = (argc > 2) && strlen(argv[2]);
1.97 + sys_uid_t uid = have_uid ? atoi(argv[2]) : 0;
1.98 +
1.99 + printf("Opening %s...\n", filename);
1.100 +
1.101 + file_t *reader = open_directory(filename, have_uid, uid);
1.102 +
1.103 + if (reader == NULL)
1.104 + {
1.105 + printf("Could not read from directory.\n");
1.106 + return 1;
1.107 + }
1.108 +
1.109 + printf("Reading...\n");
1.110 +
1.111 + struct dirent *dirent;
1.112 + int filenum = 1000, remaining = 100;
1.113 + char buffer[strlen(filename) + strlen("/file-XXXX.txt") + 10];
1.114 + int found = 0;
1.115 +
1.116 + while ((dirent = client_readdir(reader)) != NULL)
1.117 + {
1.118 + free(dirent);
1.119 + found++;
1.120 +
1.121 + /* Create files while getting the listing. */
1.122 +
1.123 + if (remaining)
1.124 + {
1.125 + sprintf(buffer, "%s/file-%d.txt", filename, filenum++);
1.126 +
1.127 + file_t *file = open_file(buffer, have_uid, uid);
1.128 +
1.129 + if (file == NULL)
1.130 + {
1.131 + printf("Could not open file: %s\n", buffer);
1.132 + return 1;
1.133 + }
1.134 +
1.135 + remaining--;
1.136 + }
1.137 + }
1.138 +
1.139 + printf("Files found: %d\n", found);
1.140 +
1.141 + /* Re-read, counting files. */
1.142 +
1.143 + reader = open_directory(filename, have_uid, uid);
1.144 +
1.145 + if (reader == NULL)
1.146 + {
1.147 + printf("Could not read from directory.\n");
1.148 + return 1;
1.149 + }
1.150 +
1.151 + found = 0;
1.152 +
1.153 + while ((dirent = client_readdir(reader)) != NULL)
1.154 + {
1.155 + free(dirent);
1.156 + found++;
1.157 + }
1.158 +
1.159 + printf("Files found: %d\n", found);
1.160 +
1.161 + return 0;
1.162 +}
1.163 +
1.164 +// vim: tabstop=2 expandtab shiftwidth=2