From 1b4d8e2cca7819b4c6e14bb052f40a9fc40205d3 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Fri, 17 Aug 2012 13:57:37 +0100 Subject: 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. --- daemon/ls.c | 64 ++++++++++++++++++++++++++++++++++++++++++ generator/generator_actions.ml | 14 +++++++++ src/MAX_PROC_NR | 2 +- 3 files changed, 79 insertions(+), 1 deletion(-) 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 #include #include +#include #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) { diff --git a/generator/generator_actions.ml b/generator/generator_actions.ml index 9c45a663..be2db82a 100644 --- a/generator/generator_actions.ml +++ b/generator/generator_actions.ml @@ -9425,6 +9425,20 @@ so the target must be set up not to require one. The optional arguments are the same as those of C." }; + { defaults with + name = "ls0"; + style = RErr, [Pathname "dir"; FileOut "filenames"], []; + proc_nr = Some 347; + shortdesc = "get list of files in a directory"; + longdesc = "\ +This specialized command is used to get a listing of +the filenames in the directory C. The list of filenames +is written to the local file C (on the host). + +In the output file, the filenames are separated by C<\\0> characters. + +C<.> and C<..> are not returned. The filenames are not sorted." }; + ] (* Non-API meta-commands available only in guestfish. diff --git a/src/MAX_PROC_NR b/src/MAX_PROC_NR index 99ca0d5f..538ad4bc 100644 --- a/src/MAX_PROC_NR +++ b/src/MAX_PROC_NR @@ -1 +1 @@ -346 +347 -- cgit