# HG changeset patch # User Paul Boddie # Date 1628635869 -7200 # Node ID 8bd29fae338b9f98bad797813190d32f09af2ec4 # Parent 10bc1e2cb24460870932f9ab2c53b859d59b7541 Introduced convenience functions for opening directories. diff -r 10bc1e2cb244 -r 8bd29fae338b libfsclient/include/fsclient/client.h --- a/libfsclient/include/fsclient/client.h Tue Aug 10 23:41:57 2021 +0200 +++ b/libfsclient/include/fsclient/client.h Wed Aug 11 00:51:09 2021 +0200 @@ -39,6 +39,9 @@ file_t *client_open(const char *name, flags_t flags); file_t *client_open_using(const char *name, flags_t flags, l4_cap_idx_t server); +file_t *client_opendir(const char *name); +file_t *client_opendir_using(const char *name, l4_cap_idx_t server); + long client_pipe(file_t **reader, file_t **writer); long client_pipe_using(file_t **reader, file_t **writer, l4_cap_idx_t server); diff -r 10bc1e2cb244 -r 8bd29fae338b libfsclient/lib/src/client.cc --- a/libfsclient/lib/src/client.cc Tue Aug 10 23:41:57 2021 +0200 +++ b/libfsclient/lib/src/client.cc Wed Aug 11 00:51:09 2021 +0200 @@ -25,6 +25,8 @@ #include #include +#include + #include "client.h" @@ -236,6 +238,38 @@ +/* Open a directory. */ + +file_t *client_opendir(const char *name) +{ + l4_cap_idx_t server = l4re_env_get_cap("server"); + + return client_opendir_using(name, server); +} + +/* Open a directory using a named capability. */ + +file_t *client_opendir_using(const char *name, l4_cap_idx_t server) +{ + file_t *file = client_open_using(name, O_DIRECTORY, server); + + if (file == NULL) + return NULL; + + /* Set blocking read mode to be able to conveniently read directory entries + from the stream. */ + + if (client_set_blocking(file, NOTIFY_CONTENT_AVAILABLE | NOTIFY_PEER_CLOSED)) + { + client_close(file); + return NULL; + } + + return file; +} + + + /* Open a pipe object. */ long client_pipe(file_t **reader, file_t **writer) diff -r 10bc1e2cb244 -r 8bd29fae338b tests/dstest_file_readdir.cc --- a/tests/dstest_file_readdir.cc Tue Aug 10 23:41:57 2021 +0200 +++ b/tests/dstest_file_readdir.cc Wed Aug 11 00:51:09 2021 +0200 @@ -31,7 +31,7 @@ -static file_t *open_file(char *filename, bool have_uid, sys_uid_t uid) +static file_t *open_directory(char *filename, bool have_uid, sys_uid_t uid) { /* With a user, open a user-specific file opener. */ @@ -47,25 +47,16 @@ /* Invoke the open method to receive the file reference. */ - return client_open_using(filename, O_DIRECTORY, opener); + return client_opendir_using(filename, opener); } else { - return client_open(filename, O_DIRECTORY); + return client_opendir(filename); } } -static long open_directory(file_t *file) -{ - /* Register the reader for notification. */ - - return client_set_blocking(file, NOTIFY_CONTENT_AVAILABLE | NOTIFY_PEER_CLOSED); -} - - - int main(int argc, char *argv[]) { if (argc < 2) @@ -77,7 +68,7 @@ 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 = open_file(filename, have_uid, uid); + file_t *file = open_directory(filename, have_uid, uid); if (file == NULL) { @@ -85,14 +76,6 @@ return 1; } - long err = open_directory(file); - - if (err) - { - printf("Could not subscribe to notifications: %s\n", l4sys_errtostr(err)); - return 1; - } - struct dirent *dirent; while ((dirent = client_readdir(file)) != NULL) @@ -105,7 +88,7 @@ /* Open again, reading a single entry only. */ - file = open_file(filename, have_uid, uid); + file = open_directory(filename, have_uid, uid); if (file == NULL) { @@ -113,14 +96,6 @@ return 1; } - err = open_directory(file); - - if (err) - { - printf("Could not subscribe to notifications: %s\n", l4sys_errtostr(err)); - return 1; - } - dirent = client_readdir(file); if (dirent != NULL)