1.1 --- a/libfsclient/lib/src/client.cc Tue Aug 10 23:41:21 2021 +0200
1.2 +++ b/libfsclient/lib/src/client.cc Tue Aug 10 23:41:57 2021 +0200
1.3 @@ -23,6 +23,7 @@
1.4
1.5 #include <stdio.h>
1.6 #include <stdlib.h>
1.7 +#include <string.h>
1.8
1.9 #include "client.h"
1.10
1.11 @@ -32,6 +33,10 @@
1.12
1.13 const offset_t DEFAULT_PIPE_SIZE = 4096;
1.14
1.15 +/* Size of the core member region of a directory entry structure. */
1.16 +
1.17 +const offset_t DIRENT_CORE_SIZE = (sizeof(struct dirent) - sizeof(((struct dirent *) 0)->d_name));
1.18 +
1.19
1.20
1.21 /* Access the given position and synchronise state with the file object. Pipe
1.22 @@ -271,6 +276,18 @@
1.23
1.24
1.25
1.26 +/* Obtain the current region of a pipe. */
1.27 +
1.28 +long client_current_region(file_t *file)
1.29 +{
1.30 + if (file == NULL)
1.31 + return -L4_EINVAL;
1.32 +
1.33 + return pipe_current(file);
1.34 +}
1.35 +
1.36 +
1.37 +
1.38 /* Flush data explicitly to the filesystem object. */
1.39
1.40 long client_flush(file_t *file)
1.41 @@ -297,18 +314,6 @@
1.42
1.43
1.44
1.45 -/* Obtain the current region of a pipe. */
1.46 -
1.47 -long client_current_region(file_t *file)
1.48 -{
1.49 - if (file == NULL)
1.50 - return -L4_EINVAL;
1.51 -
1.52 - return pipe_current(file);
1.53 -}
1.54 -
1.55 -
1.56 -
1.57 /* Obtain the next region of a pipe. */
1.58
1.59 long client_next_region(file_t *file)
1.60 @@ -321,6 +326,51 @@
1.61
1.62
1.63
1.64 +/* Read a directory entry. This must be freed by the caller after use. */
1.65 +
1.66 +struct dirent *client_readdir(file_t *file)
1.67 +{
1.68 + char buffer[DIRENT_CORE_SIZE];
1.69 + offset_t nread = client_read(file, buffer, DIRENT_CORE_SIZE);
1.70 +
1.71 + /* Stop if no new structure can be successfully read. */
1.72 +
1.73 + if (nread != DIRENT_CORE_SIZE)
1.74 + return NULL;
1.75 +
1.76 + struct dirent *dirent = (struct dirent *) buffer;
1.77 + offset_t remaining = dirent->d_reclen - DIRENT_CORE_SIZE;
1.78 +
1.79 + /* Allocate a buffer for the complete structure. */
1.80 +
1.81 + char *entry = (char *) calloc(DIRENT_CORE_SIZE + remaining, sizeof(char));
1.82 +
1.83 + if (entry == NULL)
1.84 + return NULL;
1.85 +
1.86 + /* Copy the start of the entry into a new buffer. */
1.87 +
1.88 + memcpy(entry, buffer, DIRENT_CORE_SIZE);
1.89 +
1.90 + /* Append to the entry buffer. */
1.91 +
1.92 + char *current = entry + DIRENT_CORE_SIZE;
1.93 +
1.94 + nread = client_read(file, current, remaining);
1.95 +
1.96 + /* Stop if no complete structure can be successfully read. */
1.97 +
1.98 + if (nread != remaining)
1.99 + {
1.100 + free(entry);
1.101 + return NULL;
1.102 + }
1.103 +
1.104 + return (struct dirent *) entry;
1.105 +}
1.106 +
1.107 +
1.108 +
1.109 /* Read from the filesystem object into the buffer provided. */
1.110
1.111 offset_t client_read(file_t *file, void *buf, offset_t count)