summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--daemon/ls.c64
-rw-r--r--generator/generator_actions.ml14
-rw-r--r--src/MAX_PROC_NR2
3 files changed, 79 insertions, 1 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)
{
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<guestfs_rsync>." };
+ { 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<dir>. The list of filenames
+is written to the local file C<filenames> (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