[Libguestfs] [PATCH 3/5] daemon: debug: new "exists" subcommand

Pino Toscano ptoscano at redhat.com
Tue Dec 6 15:29:20 UTC 2016


Easy way to check whether a file in the appliance exists.
---
 daemon/debug.c | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/daemon/debug.c b/daemon/debug.c
index c646be7..2193fe6 100644
--- a/daemon/debug.c
+++ b/daemon/debug.c
@@ -67,6 +67,7 @@ static char *debug_core_pattern (const char *subcmd, size_t argc, char *const *c
 static char *debug_device_speed (const char *subcmd, size_t argc, char *const *const argv);
 static char *debug_env (const char *subcmd, size_t argc, char *const *const argv);
 static char *debug_error (const char *subcmd, size_t argc, char *const *const argv);
+static char *debug_exists (const char *subcmd, size_t argc, char *const *const argv);
 static char *debug_fds (const char *subcmd, size_t argc, char *const *const argv);
 static char *debug_ldd (const char *subcmd, size_t argc, char *const *const argv);
 static char *debug_ls (const char *subcmd, size_t argc, char *const *const argv);
@@ -90,6 +91,7 @@ static struct cmd cmds[] = {
   { "device_speed", debug_device_speed },
   { "env", debug_env },
   { "error", debug_error },
+  { "exists", debug_exists },
   { "fds", debug_fds },
   { "ldd", debug_ldd },
   { "ls", debug_ls },
@@ -933,6 +935,43 @@ do_debug_upload (const char *filename, int mode)
   return 0;
 }
 
+/* Check whether a file in the appliance exists.
+ * An empty string is returned if the file does not exist,
+ * an "ok" string when the file exists, and an error in case of other
+ * stat errors.
+ */
+static char *
+debug_exists (const char *subcmd, size_t argc, char *const *const argv)
+{
+  char *ret;
+  int r;
+  struct stat buf;
+
+  if (argc != 1) {
+    reply_with_error ("exists: one argument expected");
+    return NULL;
+  }
+
+  r = stat (argv[0], &buf);
+
+  if (r == -1) {
+    if (errno != ENOENT && errno != ENOTDIR) {
+      reply_with_perror ("stat: %s", argv[0]);
+      return NULL;
+    }
+    else
+      ret = strdup ("");
+  } else
+    ret = strdup ("ok");
+
+  if (NULL == ret) {
+    reply_with_perror ("strdup");
+    return NULL;
+  }
+
+  return ret;
+}
+
 /* This function is identical to debug_upload. */
 /* Has one FileIn parameter. */
 int
-- 
2.7.4




More information about the Libguestfs mailing list