diff options
author | Richard W.M. Jones <rjones@redhat.com> | 2012-08-17 13:57:37 +0100 |
---|---|---|
committer | Richard W.M. Jones <rjones@redhat.com> | 2012-08-17 16:08:13 +0100 |
commit | 1b4d8e2cca7819b4c6e14bb052f40a9fc40205d3 (patch) | |
tree | 156912d33df89aa422c4639bb374c3b7d41d9343 /daemon | |
parent | 8ee51907685d6db92031e84d249ea3bb4cdc2d67 (diff) | |
download | libguestfs-1b4d8e2cca7819b4c6e14bb052f40a9fc40205d3.tar.gz libguestfs-1b4d8e2cca7819b4c6e14bb052f40a9fc40205d3.tar.xz libguestfs-1b4d8e2cca7819b4c6e14bb052f40a9fc40205d3.zip |
New API: ls0 - List files, separated by \0 characters.
This API is not especially useful on its own. It will be used to
reimplement guestfs_ls to work without protocol limits.
Diffstat (limited to 'daemon')
-rw-r--r-- | daemon/ls.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/daemon/ls.c b/daemon/ls.c index 5c76d7e2..0f60a4ea 100644 --- a/daemon/ls.c +++ b/daemon/ls.c @@ -25,10 +25,74 @@ #include <fcntl.h> #include <dirent.h> #include <sys/stat.h> +#include <errno.h> #include "daemon.h" #include "actions.h" +/* Has one FileOut parameter. */ +int +do_ls0 (const char *path) +{ + DIR *dir; + struct dirent *d; + size_t len; + + CHROOT_IN; + dir = opendir (path); + CHROOT_OUT; + + if (dir == NULL) { + reply_with_perror ("opendir: %s", path); + return -1; + } + + /* Now we must send the reply message, before the filenames. After + * this there is no opportunity in the protocol to send any error + * message back. Instead we can only cancel the transfer. + */ + reply (NULL, NULL); + + while (1) { + errno = 0; + d = readdir (dir); + if (d == NULL) break; + + /* Ignore . and .. */ + if (STREQ (d->d_name, ".") || STREQ (d->d_name, "..")) + continue; + + /* Send the name in a single chunk. XXX Needs to be fixed if + * names can be longer than the chunk size. Note we use 'len+1' + * because we want to include the \0 terminating character in the + * output. + */ + len = strlen (d->d_name); + if (send_file_write (d->d_name, len+1) < 0) { + closedir (dir); + return -1; + } + } + + if (errno != 0) { + perror (path); + send_file_end (1); /* Cancel. */ + closedir (dir); + return -1; + } + + if (closedir (dir) == -1) { + perror (path); + send_file_end (1); /* Cancel. */ + return -1; + } + + if (send_file_end (0)) /* Normal end of file. */ + return -1; + + return 0; +} + char ** do_ls (const char *path) { |