1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tests/dstest_file_readdir.cc Fri Aug 06 19:30:20 2021 +0200
1.3 @@ -0,0 +1,138 @@
1.4 +/*
1.5 + * Test directory reading operations.
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 <dirent.h>
1.29 +#include <stdio.h>
1.30 +#include <string.h>
1.31 +#include <stdlib.h>
1.32 +
1.33 +#include <fsclient/client.h>
1.34 +#include <systypes/fcntl.h>
1.35 +
1.36 +
1.37 +
1.38 +#define DIRENT_CORE_SIZE (sizeof(struct dirent) - sizeof(((struct dirent *) 0)->d_name))
1.39 +
1.40 +int main(int argc, char *argv[])
1.41 +{
1.42 + if (argc < 2)
1.43 + {
1.44 + printf("Need a directory name and an optional user identifier (if used with a filesystem).\n");
1.45 + return 1;
1.46 + }
1.47 +
1.48 + char *filename = argv[1];
1.49 + bool have_uid = (argc > 2) && strlen(argv[2]);
1.50 + sys_uid_t uid = have_uid ? atoi(argv[2]) : 0;
1.51 + file_t *file;
1.52 +
1.53 + /* With a user, open a user-specific file opener. */
1.54 +
1.55 + if (have_uid)
1.56 + {
1.57 + l4_cap_idx_t opener = client_open_for_user((user_t) {uid, uid, 0022});
1.58 +
1.59 + if (l4_is_invalid_cap(opener))
1.60 + {
1.61 + printf("Could not obtain opener for file.\n");
1.62 + return 1;
1.63 + }
1.64 +
1.65 + /* Invoke the open method to receive the file reference. */
1.66 +
1.67 + file = client_open_using(filename, O_DIRECTORY, opener);
1.68 + }
1.69 + else
1.70 + {
1.71 + file = client_open(filename, O_DIRECTORY);
1.72 + }
1.73 +
1.74 + if (file == NULL)
1.75 + {
1.76 + printf("Could not obtain directory.\n");
1.77 + return 1;
1.78 + }
1.79 +
1.80 + // NOTE: To be replaced by a proper mechanism identifying the nature of each
1.81 + // NOTE: obtained object.
1.82 +
1.83 + file->can_mmap = 0;
1.84 + file->has_size = 0;
1.85 +
1.86 + /* Register the reader for notification. */
1.87 +
1.88 + long err = client_set_blocking(file, NOTIFY_CONTENT_AVAILABLE | NOTIFY_PEER_CLOSED);
1.89 +
1.90 + if (err)
1.91 + {
1.92 + printf("Could not subscribe to notifications: %s\n", l4sys_errtostr(err));
1.93 + return 1;
1.94 + }
1.95 +
1.96 + char buffer[DIRENT_CORE_SIZE];
1.97 + offset_t nread = client_read(file, buffer, DIRENT_CORE_SIZE);
1.98 + offset_t total = 0;
1.99 +
1.100 + while (1)
1.101 + {
1.102 + if (!nread && (file->notifications & NOTIFY_PEER_CLOSED))
1.103 + break;
1.104 +
1.105 + total += nread;
1.106 +
1.107 + if (total == DIRENT_CORE_SIZE)
1.108 + {
1.109 + struct dirent *dirent = (struct dirent *) buffer;
1.110 + int remaining = dirent->d_reclen - DIRENT_CORE_SIZE;
1.111 + char entry[DIRENT_CORE_SIZE + remaining], *current;
1.112 +
1.113 + memcpy(entry, buffer, DIRENT_CORE_SIZE);
1.114 + current = entry + DIRENT_CORE_SIZE;
1.115 +
1.116 + do
1.117 + {
1.118 + nread = client_read(file, current, remaining);
1.119 + remaining -= nread;
1.120 + current += nread;
1.121 + }
1.122 + while (nread && remaining);
1.123 +
1.124 + if (remaining)
1.125 + break;
1.126 +
1.127 + dirent = (struct dirent *) entry;
1.128 + printf("> %s\n", dirent->d_name);
1.129 +
1.130 + total = 0;
1.131 + }
1.132 +
1.133 + nread = client_read(file, buffer, DIRENT_CORE_SIZE);
1.134 + }
1.135 +
1.136 + printf("Directory shown.\n");
1.137 +
1.138 + return 0;
1.139 +}
1.140 +
1.141 +// vim: tabstop=2 expandtab shiftwidth=2