1.1 --- a/tests/dstest_file_readdir_concurrent.cc Tue Jan 04 23:27:53 2022 +0100
1.2 +++ b/tests/dstest_file_readdir_concurrent.cc Tue Jan 04 23:43:56 2022 +0100
1.3 @@ -79,6 +79,30 @@
1.4 }
1.5 }
1.6
1.7 +static long remove_file(char *filename, bool have_uid, sys_uid_t uid)
1.8 +{
1.9 + /* With a user, open a user-specific file opener. */
1.10 +
1.11 + if (have_uid)
1.12 + {
1.13 + l4_cap_idx_t opener = client_open_for_user((user_t) {uid, uid, 0022});
1.14 +
1.15 + if (l4_is_invalid_cap(opener))
1.16 + {
1.17 + printf("Could not obtain opener for file.\n");
1.18 + return -L4_EIO;
1.19 + }
1.20 +
1.21 + /* Invoke the remove method to remove the file. */
1.22 +
1.23 + return client_remove_using(filename, opener);
1.24 + }
1.25 + else
1.26 + {
1.27 + return client_remove(filename);
1.28 + }
1.29 +}
1.30 +
1.31
1.32
1.33 int main(int argc, char *argv[])
1.34 @@ -108,7 +132,9 @@
1.35 struct dirent *dirent;
1.36 int filenum = 1000, remaining = 100;
1.37 char buffer[strlen(filename) + strlen("/file-XXXX.txt") + 10];
1.38 + char data[256];
1.39 int found = 0;
1.40 + file_t *files[100];
1.41
1.42 while ((dirent = client_readdir(reader)) != NULL)
1.43 {
1.44 @@ -129,6 +155,15 @@
1.45 return 1;
1.46 }
1.47
1.48 + /* Write something to each file. */
1.49 +
1.50 + sprintf(data, "Data in file-%d.txt", filenum - 1);
1.51 + client_write(file, data, strlen(data));
1.52 +
1.53 + /* Remember the file for later. */
1.54 +
1.55 + files[remaining - 1] = file;
1.56 +
1.57 remaining--;
1.58 }
1.59 }
1.60 @@ -155,6 +190,54 @@
1.61
1.62 printf("Files found: %d\n", found);
1.63
1.64 + /* Remove some files. */
1.65 +
1.66 + for (filenum = 1000, remaining = 100; remaining > 0; remaining--)
1.67 + {
1.68 + sprintf(buffer, "%s/file-%d.txt", filename, filenum++);
1.69 +
1.70 + long err = remove_file(buffer, have_uid, uid);
1.71 +
1.72 + if (err)
1.73 + {
1.74 + printf("Could not remove file: %s\n", buffer);
1.75 + return 1;
1.76 + }
1.77 + }
1.78 +
1.79 + /* Re-read, counting files. */
1.80 +
1.81 + reader = open_directory(filename, have_uid, uid);
1.82 +
1.83 + if (reader == NULL)
1.84 + {
1.85 + printf("Could not read from directory.\n");
1.86 + return 1;
1.87 + }
1.88 +
1.89 + found = 0;
1.90 +
1.91 + while ((dirent = client_readdir(reader)) != NULL)
1.92 + {
1.93 + free(dirent);
1.94 + found++;
1.95 + }
1.96 +
1.97 + printf("Files found: %d\n", found);
1.98 +
1.99 + /* Read from the still-open but now removed files. */
1.100 +
1.101 + for (remaining = 0; remaining < 100; remaining++)
1.102 + {
1.103 + file_t *file = files[remaining];
1.104 +
1.105 + client_seek(file, SEEK_SET, 0);
1.106 + offset_t nread = client_read(file, data, 256);
1.107 +
1.108 + data[nread] = '\0';
1.109 + printf("Read: %s\n", data);
1.110 + }
1.111 +
1.112 return 0;
1.113 }
1.114