1.1 --- a/tests/dstest_file_readdir_concurrent.cc Fri Feb 18 00:13:37 2022 +0100
1.2 +++ b/tests/dstest_file_readdir_concurrent.cc Fri Feb 18 00:14:05 2022 +0100
1.3 @@ -31,6 +31,26 @@
1.4
1.5
1.6
1.7 +static int count_files(const char *filename, int *found)
1.8 +{
1.9 + file_t *reader = client_opendir(filename);
1.10 + struct dirent *dirent;
1.11 +
1.12 + if (reader == NULL)
1.13 + return 1;
1.14 +
1.15 + *found = 0;
1.16 +
1.17 + while ((dirent = client_readdir(reader)) != NULL)
1.18 + {
1.19 + free(dirent);
1.20 + (*found)++;
1.21 + }
1.22 +
1.23 + client_close(reader);
1.24 + return 0;
1.25 +}
1.26 +
1.27 int main(int argc, char *argv[])
1.28 {
1.29 if (argc < 2)
1.30 @@ -40,6 +60,23 @@
1.31 }
1.32
1.33 char *filename = argv[1];
1.34 + int original;
1.35 +
1.36 + if (count_files(filename, &original))
1.37 + {
1.38 + printf("Could not read from directory.\n");
1.39 + return 1;
1.40 + }
1.41 +
1.42 + printf("Files found: %d\n", original);
1.43 +
1.44 + struct dirent *dirent;
1.45 + int filenum_base = 1000, filenum = filenum_base;
1.46 + int to_create = 100, remaining = to_create;
1.47 + char buffer[strlen(filename) + strlen("/file-XXXX.txt") + 10];
1.48 + char data[256];
1.49 + int found = 0;
1.50 + file_t *files[to_create];
1.51
1.52 printf("Opening %s...\n", filename);
1.53
1.54 @@ -53,13 +90,6 @@
1.55
1.56 printf("Reading...\n");
1.57
1.58 - struct dirent *dirent;
1.59 - int filenum = 1000, remaining = 100;
1.60 - char buffer[strlen(filename) + strlen("/file-XXXX.txt") + 10];
1.61 - char data[256];
1.62 - int found = 0;
1.63 - file_t *files[100];
1.64 -
1.65 while ((dirent = client_readdir(reader)) != NULL)
1.66 {
1.67 free(dirent);
1.68 @@ -69,7 +99,7 @@
1.69
1.70 if (remaining)
1.71 {
1.72 - sprintf(buffer, "%s/file-%d.txt", filename, filenum++);
1.73 + sprintf(buffer, "%s/file-%d.txt", filename, filenum);
1.74
1.75 file_t *file = client_open(buffer, O_RDWR | O_CREAT);
1.76
1.77 @@ -81,14 +111,15 @@
1.78
1.79 /* Write something to each file. */
1.80
1.81 - sprintf(data, "Data in file-%d.txt", filenum - 1);
1.82 + sprintf(data, "Data in file-%d.txt", filenum);
1.83 client_write(file, data, strlen(data));
1.84
1.85 /* Remember the file for later. */
1.86
1.87 - files[remaining - 1] = file;
1.88 + files[filenum - filenum_base] = file;
1.89
1.90 remaining--;
1.91 + filenum++;
1.92 }
1.93 }
1.94
1.95 @@ -96,29 +127,19 @@
1.96
1.97 /* Re-read, counting files. */
1.98
1.99 - reader = client_opendir(filename);
1.100 -
1.101 - if (reader == NULL)
1.102 + if (count_files(filename, &found))
1.103 {
1.104 printf("Could not read from directory.\n");
1.105 return 1;
1.106 }
1.107
1.108 - found = 0;
1.109 -
1.110 - while ((dirent = client_readdir(reader)) != NULL)
1.111 - {
1.112 - free(dirent);
1.113 - found++;
1.114 - }
1.115 -
1.116 printf("Files found: %d\n", found);
1.117
1.118 /* Remove some files. */
1.119
1.120 - for (filenum = 1000, remaining = 100; remaining > 0; remaining--)
1.121 + for (filenum = 1000, remaining = to_create; remaining > 0; remaining--, filenum++)
1.122 {
1.123 - sprintf(buffer, "%s/file-%d.txt", filename, filenum++);
1.124 + sprintf(buffer, "%s/file-%d.txt", filename, filenum);
1.125
1.126 long err = client_remove(buffer);
1.127
1.128 @@ -131,37 +152,55 @@
1.129
1.130 /* Re-read, counting files. */
1.131
1.132 - reader = client_opendir(filename);
1.133 -
1.134 - if (reader == NULL)
1.135 + if (count_files(filename, &found))
1.136 {
1.137 printf("Could not read from directory.\n");
1.138 return 1;
1.139 }
1.140
1.141 - found = 0;
1.142 -
1.143 - while ((dirent = client_readdir(reader)) != NULL)
1.144 - {
1.145 - free(dirent);
1.146 - found++;
1.147 - }
1.148 -
1.149 printf("Files found: %d\n", found);
1.150
1.151 /* Read from the still-open but now removed files. */
1.152
1.153 - for (remaining = 0; remaining < 100; remaining++)
1.154 + found = 0;
1.155 +
1.156 + for (filenum = 1000, remaining = 0; remaining < to_create; remaining++, filenum++)
1.157 {
1.158 - file_t *file = files[remaining];
1.159 + file_t *file = files[filenum - filenum_base];
1.160 + char read_data[256];
1.161
1.162 client_seek(file, SEEK_SET, 0);
1.163 - offset_t nread = client_read(file, data, 256);
1.164 + offset_t nread = client_read(file, read_data, 256);
1.165 +
1.166 + if (nread)
1.167 + {
1.168 + read_data[nread] = '\0';
1.169 + sprintf(data, "Data in file-%d.txt", filenum);
1.170 +
1.171 + if (!strcmp(data, read_data))
1.172 + found++;
1.173 + }
1.174 + }
1.175 +
1.176 + printf("Files still open: %d (%d)\n", found, to_create);
1.177
1.178 - data[nread] = '\0';
1.179 - printf("Read: %s\n", data);
1.180 + /* Close the files. */
1.181 +
1.182 + for (remaining = 0; remaining < to_create; remaining++)
1.183 + client_close(files[remaining]);
1.184 +
1.185 + printf("Closed files.\n");
1.186 +
1.187 + /* Re-read, counting files. */
1.188 +
1.189 + if (count_files(filename, &found))
1.190 + {
1.191 + printf("Could not read from directory.\n");
1.192 + return 1;
1.193 }
1.194
1.195 + printf("Files found: %d (%d)\n", found, original);
1.196 +
1.197 return 0;
1.198 }
1.199