# HG changeset patch # User Paul Boddie # Date 1628091014 -7200 # Node ID 0bf39e6d7142b7d97711f4007fb1acb97f696c19 # Parent 68da6f0c940fd24554bbf974234f3bc5fe5a1ab5 Added initial support for directory listing retrieval from filesystem objects. diff -r 68da6f0c940f -r 0bf39e6d7142 conf/dstest_host_readdir.cfg --- a/conf/dstest_host_readdir.cfg Wed Aug 04 17:13:47 2021 +0200 +++ b/conf/dstest_host_readdir.cfg Wed Aug 04 17:30:14 2021 +0200 @@ -4,11 +4,22 @@ local l = L4.default_loader; -local server = l:new_channel(); +local pipe_server = l:new_channel(); l:startv({ caps = { - server = server:svr(), + server = pipe_server:svr(), + }, + log = { "pipes", "r" }, + }, + "rom/dstest_pipe_server", "10"); + +local host_server = l:new_channel(); + +l:startv({ + caps = { + pipes = pipe_server, + server = host_server:svr(), }, log = { "server", "r" }, }, @@ -16,7 +27,7 @@ l:startv({ caps = { - server = server, + server = host_server, }, log = { "client", "g" }, }, diff -r 68da6f0c940f -r 0bf39e6d7142 conf/dstest_host_readdir.list --- a/conf/dstest_host_readdir.list Wed Aug 04 17:13:47 2021 +0200 +++ b/conf/dstest_host_readdir.list Wed Aug 04 17:30:14 2021 +0200 @@ -5,6 +5,7 @@ module ned module dstest_host_readdir module dstest_host_server +module dstest_pipe_server module lib4re-c.so module lib4re-c-util.so module lib4re.so diff -r 68da6f0c940f -r 0bf39e6d7142 libfsserver/include/fsserver/host_file_opener.h --- a/libfsserver/include/fsserver/host_file_opener.h Wed Aug 04 17:13:47 2021 +0200 +++ b/libfsserver/include/fsserver/host_file_opener.h Wed Aug 04 17:30:14 2021 +0200 @@ -54,6 +54,10 @@ virtual bool accessing_file(const char *path, flags_t flags, fileid_t fileid); + /* Convenience methods obtaining different pager types. */ + + virtual long get_directory(const char *path, flags_t flags, fileid_t fileid, offset_t *size, l4_cap_idx_t *cap); + public: explicit HostFileOpener(FilePaging *paging) : OpenerResource(paging) diff -r 68da6f0c940f -r 0bf39e6d7142 libfsserver/lib/files/host_file_opener.cc --- a/libfsserver/lib/files/host_file_opener.cc Wed Aug 04 17:13:47 2021 +0200 +++ b/libfsserver/lib/files/host_file_opener.cc Wed Aug 04 17:30:14 2021 +0200 @@ -19,11 +19,50 @@ * Boston, MA 02110-1301, USA */ +#include + +#include #include +#include + #include "host_file_accessor.h" #include "host_file_opener.h" + + +/* Thread payload. */ + +static void read_directory(const char *path, file_t *writer) +{ + DIR *dir = opendir(path); + struct dirent *dirent; + + /* Subscribe to space and closure notifications on the pipe. */ + + long err = client_set_blocking(writer, NOTIFY_SPACE_AVAILABLE); + + if (err) + { + client_close(writer); + return; + } + + /* Write directory entries to the pipe, closing the pipe when finished. */ + + while ((dirent = readdir(dir)) != NULL) + { + offset_t nwritten = 0; + + while (nwritten < dirent->d_reclen) + nwritten += client_write(writer, (const void *) (dirent + nwritten), dirent->d_reclen - nwritten); + } + + client_close(writer); +} + + + HostFileOpener::~HostFileOpener() { } @@ -50,6 +89,33 @@ return (st.st_mode & S_IFREG) ? true : false; } +long HostFileOpener::get_directory(const char *path, flags_t flags, fileid_t fileid, offset_t *size, l4_cap_idx_t *cap) +{ + file_t *reader, *writer; + + // NOTE: Might be more appropriate to use lower-level file operations to + // NOTE: avoid unnecessary mapping of the reader's memory region. + + long err = client_pipe(&reader, &writer); + + if (err) + return err; + + *size = reader->size; + *cap = reader->ref; + + /* Discard the reader structure but do not close the reader itself. */ + + delete reader; + + /* Spawn a independent thread for reading the directory details and writing + them to the pipe. */ + + std::thread(read_directory, path, writer).detach(); + + return L4_EOK; +} + /* Return a file identifier for the given 'path'. */ long HostFileOpener::get_fileid(const char *path, flags_t flags, fileid_t *fileid) diff -r 68da6f0c940f -r 0bf39e6d7142 tests/dstest_host_readdir.cc --- a/tests/dstest_host_readdir.cc Wed Aug 04 17:13:47 2021 +0200 +++ b/tests/dstest_host_readdir.cc Wed Aug 04 17:30:14 2021 +0200 @@ -1,7 +1,7 @@ /* - * Test directory reading. + * Test directory reading operations. * - * Copyright (C) 2021 Paul Boddie + * Copyright (C) 2020, 2021 Paul Boddie * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -19,34 +19,113 @@ * Boston, MA 02110-1301, USA */ +#include +#include + #include #include +#include +#include + +#include +#include +#define DIRENT_CORE_SIZE (sizeof(struct dirent) - sizeof(((struct dirent *) 0)->d_name)) + int main(int argc, char *argv[]) { if (argc < 2) { - printf("Need directory name.\n"); + printf("Need a directory name and an optional user identifier (if used with a filesystem).\n"); + return 1; + } + + char *filename = argv[1]; + bool have_uid = (argc > 2) && strlen(argv[2]); + sys_uid_t uid = have_uid ? atoi(argv[2]) : 0; + file_t *file; + + /* With a user, open a user-specific file opener. */ + + if (have_uid) + { + l4_cap_idx_t opener = client_open_for_user((user_t) {uid, uid, 0022}); + + if (l4_is_invalid_cap(opener)) + { + printf("Could not obtain opener for file.\n"); + return 1; + } + + /* Invoke the open method to receive the file reference. */ + + file = client_open_using(filename, O_DIRECTORY, opener); + } + else + { + file = client_open(filename, O_DIRECTORY); + } + + if (file == NULL) + { + printf("Could not obtain directory.\n"); return 1; } - /* Obtain directory name. */ + // NOTE: To be replaced by a proper mechanism identifying the nature of each + // NOTE: obtained object. + + file->can_mmap = 0; + file->has_size = 0; - char *filename = argv[1]; - DIR *dir = opendir(filename); + /* Register the reader for notification. */ + + long err = client_set_blocking(file, NOTIFY_CONTENT_AVAILABLE); - if (dir == NULL) + if (err) { - printf("Could not obtain directory: %s\n", filename); + printf("Could not subscribe to notifications: %s\n", l4sys_errtostr(err)); return 1; } - struct dirent *dirent; + char buffer[DIRENT_CORE_SIZE]; + offset_t nread = client_read(file, buffer, DIRENT_CORE_SIZE); + offset_t total = 0; + + while (nread) + { + total += nread; + + if (total == DIRENT_CORE_SIZE) + { + struct dirent *dirent = (struct dirent *) buffer; + int remaining = dirent->d_reclen - DIRENT_CORE_SIZE; + char entry[DIRENT_CORE_SIZE + remaining], *current; + + memcpy(entry, buffer, DIRENT_CORE_SIZE); + current = entry + DIRENT_CORE_SIZE; - while ((dirent = readdir(dir)) != NULL) - printf("> %s\n", dirent->d_name); + do + { + nread = client_read(file, current, remaining); + remaining -= nread; + current += nread; + } + while (nread && remaining); + + if (remaining) + break; + + dirent = (struct dirent *) entry; + printf("> %s\n", dirent->d_name); + + total = 0; + } + + nread = client_read(file, buffer, DIRENT_CORE_SIZE); + } printf("Directory shown.\n");