Revert "Remove ocaml/.depend from git."
[libguestfs.git] / test-tool / test-tool.c
index 3dc0328..c21906f 100644 (file)
@@ -29,6 +29,8 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
+#include <locale.h>
+#include <limits.h>
 
 #include <guestfs.h>
 
 #define N_(str) str
 #endif
 
+#if !ENABLE_NLS
+#undef textdomain
+#define textdomain(Domainname) /* empty */
+#undef bindtextdomain
+#define bindtextdomain(Domainname, Dirname) /* empty */
+#endif
+
+#define STREQ(a,b) (strcmp((a),(b)) == 0)
+#define STRCASEEQ(a,b) (strcasecmp((a),(b)) == 0)
+#define STRNEQ(a,b) (strcmp((a),(b)) != 0)
+#define STRCASENEQ(a,b) (strcasecmp((a),(b)) != 0)
+#define STREQLEN(a,b,n) (strncmp((a),(b),(n)) == 0)
+#define STRCASEEQLEN(a,b,n) (strncasecmp((a),(b),(n)) == 0)
+#define STRNEQLEN(a,b,n) (strncmp((a),(b),(n)) != 0)
+#define STRCASENEQLEN(a,b,n) (strncasecmp((a),(b),(n)) != 0)
+#define STRPREFIX(a,b) (strncmp((a),(b),strlen((b))) == 0)
+
+#ifndef P_tmpdir
+#define P_tmpdir "/tmp"
+#endif
+
 #define DEFAULT_TIMEOUT 120
 
 static const char *helper = DEFAULT_HELPER;
 static int timeout = DEFAULT_TIMEOUT;
-static char tmpf[] = "/tmp/libguestfs-test-tool-sda-XXXXXX";
-static char isof[] = "/tmp/libguestfs-test-tool-iso-XXXXXX";
+static char tmpf[] = P_tmpdir "/libguestfs-test-tool-sda-XXXXXX";
+static char isof[] = P_tmpdir "/libguestfs-test-tool-iso-XXXXXX";
 static guestfs_h *g;
 
 static void preruncheck (void);
@@ -75,7 +98,11 @@ usage (void)
 int
 main (int argc, char *argv[])
 {
-  static const char *options = "?";
+  setlocale (LC_ALL, "");
+  bindtextdomain (PACKAGE, LOCALEBASEDIR);
+  textdomain (PACKAGE);
+
+  static const char *options = "t:?";
   static const struct option long_options[] = {
     { "help", 0, 0, '?' },
     { "helper", 1, 0, 0 },
@@ -100,17 +127,17 @@ main (int argc, char *argv[])
 
     switch (c) {
     case 0:                    /* options which are long only */
-      if (strcmp (long_options[option_index].name, "helper") == 0)
+      if (STREQ (long_options[option_index].name, "helper"))
         helper = optarg;
-      else if (strcmp (long_options[option_index].name, "qemu") == 0)
+      else if (STREQ (long_options[option_index].name, "qemu"))
         set_qemu (optarg, 0);
-      else if (strcmp (long_options[option_index].name, "qemudir") == 0)
+      else if (STREQ (long_options[option_index].name, "qemudir"))
         set_qemu (optarg, 1);
       else {
         fprintf (stderr,
                  _("libguestfs-test-tool: unknown long option: %s (%d)\n"),
                  long_options[option_index].name, option_index);
-        exit (1);
+        exit (EXIT_FAILURE);
       }
       break;
 
@@ -119,19 +146,19 @@ main (int argc, char *argv[])
         fprintf (stderr,
                  _("libguestfs-test-tool: invalid timeout: %s\n"),
                  optarg);
-        exit (1);
+        exit (EXIT_FAILURE);
       }
       break;
 
     case '?':
       usage ();
-      exit (0);
+      exit (EXIT_SUCCESS);
 
     default:
       fprintf (stderr,
                _("libguestfs-test-tool: unexpected command line option 0x%x\n"),
                c);
-      exit (1);
+      exit (EXIT_FAILURE);
     }
   }
 
@@ -145,7 +172,7 @@ main (int argc, char *argv[])
 
   /* Print out any environment variables which may relate to this test. */
   for (i = 0; environ[i] != NULL; ++i)
-    if (strncmp (environ[i], "LIBGUESTFS_", 11) == 0)
+    if (STREQLEN (environ[i], "LIBGUESTFS_", 11))
       printf ("%s\n", environ[i]);
 
   /* Create the handle and configure it. */
@@ -153,26 +180,31 @@ main (int argc, char *argv[])
   if (g == NULL) {
     fprintf (stderr,
              _("libguestfs-test-tool: failed to create libguestfs handle\n"));
-    exit (1);
+    exit (EXIT_FAILURE);
   }
-  if (guestfs_add_drive (g, tmpf) == -1) {
+  if (guestfs_add_drive_opts (g, tmpf,
+                              GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw",
+                              -1) == -1) {
     fprintf (stderr,
              _("libguestfs-test-tool: failed to add drive '%s'\n"),
              tmpf);
-    exit (1);
+    exit (EXIT_FAILURE);
   }
-  if (guestfs_add_drive (g, isof) == -1) {
+  if (guestfs_add_drive_opts (g, isof,
+                              GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw",
+                              GUESTFS_ADD_DRIVE_OPTS_READONLY, 1,
+                              -1) == -1) {
     fprintf (stderr,
              _("libguestfs-test-tool: failed to add drive '%s'\n"),
              isof);
-    exit (1);
+    exit (EXIT_FAILURE);
   }
 
   /* Print any version info etc. */
   vers = guestfs_version (g);
   if (vers == NULL) {
     fprintf (stderr, _("libguestfs-test-tool: guestfs_version failed\n"));
-    exit (1);
+    exit (EXIT_FAILURE);
   }
   printf ("library version: %"PRIi64".%"PRIi64".%"PRIi64"%s\n",
           vers->major, vers->minor, vers->release, vers->extra);
@@ -186,21 +218,15 @@ main (int argc, char *argv[])
   printf ("guestfs_get_verbose: %d\n", guestfs_get_verbose (g));
 
   /* Launch the guest handle. */
-  if (guestfs_launch (g) == -1) {
-    fprintf (stderr,
-             _("libguestfs-test-tool: failed to launch appliance\n"));
-    exit (1);
-  }
-
   printf ("Launching appliance, timeout set to %d seconds.\n", timeout);
   fflush (stdout);
 
   alarm (timeout);
 
-  if (guestfs_wait_ready (g) == -1) {
+  if (guestfs_launch (g) == -1) {
     fprintf (stderr,
-             _("libguestfs-test-tool: failed or timed out in 'wait_ready'\n"));
-    exit (1);
+             _("libguestfs-test-tool: failed to launch appliance\n"));
+    exit (EXIT_FAILURE);
   }
 
   alarm (0);
@@ -212,31 +238,31 @@ main (int argc, char *argv[])
   if (guestfs_sfdiskM (g, "/dev/sda", sfdisk_lines) == -1) {
     fprintf (stderr,
              _("libguestfs-test-tool: failed to run sfdisk\n"));
-    exit (1);
+    exit (EXIT_FAILURE);
   }
 
   if (guestfs_mkfs (g, "ext2", "/dev/sda1") == -1) {
     fprintf (stderr,
              _("libguestfs-test-tool: failed to mkfs.ext2\n"));
-    exit (1);
+    exit (EXIT_FAILURE);
   }
 
-  if (guestfs_mount (g, "/dev/sda1", "/") == -1) {
+  if (guestfs_mount_options (g, "", "/dev/sda1", "/") == -1) {
     fprintf (stderr,
              _("libguestfs-test-tool: failed to mount /dev/sda1 on /\n"));
-    exit (1);
+    exit (EXIT_FAILURE);
   }
 
   if (guestfs_mkdir (g, "/iso") == -1) {
     fprintf (stderr,
              _("libguestfs-test-tool: failed to mkdir /iso\n"));
-    exit (1);
+    exit (EXIT_FAILURE);
   }
 
   if (guestfs_mount (g, "/dev/sdb", "/iso") == -1) {
     fprintf (stderr,
              _("libguestfs-test-tool: failed to mount /dev/sdb on /iso\n"));
-    exit (1);
+    exit (EXIT_FAILURE);
   }
 
   /* Let's now run some simple tests using the helper program. */
@@ -244,15 +270,15 @@ main (int argc, char *argv[])
   if (str == NULL) {
     fprintf (stderr,
              _("libguestfs-test-tool: could not run helper program, or helper failed\n"));
-    exit (1);
+    exit (EXIT_FAILURE);
   }
   free (str);
 
   printf ("===== TEST FINISHED OK =====\n");
-  exit (0);
+  exit (EXIT_SUCCESS);
 }
 
-static char qemuwrapper[] = "/tmp/libguestfs-test-tool-wrapper-XXXXXX";
+static char qemuwrapper[] = P_tmpdir "/libguestfs-test-tool-wrapper-XXXXXX";
 
 static void
 cleanup_wrapper (void)
@@ -276,7 +302,7 @@ set_qemu (const char *path, int use_wrapper)
     fprintf (stderr,
     _("LIBGUESTFS_QEMU environment variable is already set, so\n"
       "--qemu/--qemudir options cannot be used.\n"));
-    exit (1);
+    exit (EXIT_FAILURE);
   }
 
   if (!use_wrapper) {
@@ -284,7 +310,7 @@ set_qemu (const char *path, int use_wrapper)
       fprintf (stderr,
                _("Binary '%s' does not exist or is not executable\n"),
                path);
-      exit (1);
+      exit (EXIT_FAILURE);
     }
 
     setenv ("LIBGUESTFS_QEMU", path, 1);
@@ -298,14 +324,14 @@ set_qemu (const char *path, int use_wrapper)
     fprintf (stderr,
              _("%s: does not look like a qemu source directory\n"),
              path);
-    exit (1);
+    exit (EXIT_FAILURE);
   }
 
   /* Make a wrapper script. */
   fd = mkstemp (qemuwrapper);
   if (fd == -1) {
     perror (qemuwrapper);
-    exit (1);
+    exit (EXIT_FAILURE);
   }
 
   fchmod (fd, 0700);
@@ -350,19 +376,19 @@ preruncheck (void)
       "\n"
       "Use the --helper option to specify the location of this program.\n"),
              helper);
-    exit (1);
+    exit (EXIT_FAILURE);
   }
 
   snprintf (cmd, sizeof cmd, "file '%s'", helper);
   fp = popen (cmd, "r");
   if (fp == NULL) {
     perror (cmd);
-    exit (1);
+    exit (EXIT_FAILURE);
   }
   r = fread (buffer, 1, sizeof buffer - 1, fp);
   if (r == 0) {
     fprintf (stderr, _("command failed: %s"), cmd);
-    exit (1);
+    exit (EXIT_FAILURE);
   }
   pclose (fp);
   buffer[r] = '\0';
@@ -373,7 +399,7 @@ preruncheck (void)
       "is not statically linked.  This is a build error when this test tool\n"
       "was built.\n"),
              helper);
-    exit (1);
+    exit (EXIT_FAILURE);
   }
 }
 
@@ -394,7 +420,7 @@ make_files (void)
   fd = mkstemp (isof);
   if (fd == -1) {
     perror (isof);
-    exit (1);
+    exit (EXIT_FAILURE);
   }
   close (fd);
 
@@ -404,7 +430,7 @@ make_files (void)
   if (r == -1 || WEXITSTATUS(r) != 0) {
     fprintf (stderr,
              _("mkisofs command failed: %s\n"), cmd);
-    exit (1);
+    exit (EXIT_FAILURE);
   }
 
   /* Allocate the sparse file for /dev/sda. */
@@ -412,7 +438,7 @@ make_files (void)
   if (fd == -1) {
     perror (tmpf);
     unlink (isof);
-    exit (1);
+    exit (EXIT_FAILURE);
   }
 
   if (lseek (fd, 100 * 1024 * 1024 - 1, SEEK_SET) == -1) {
@@ -420,7 +446,7 @@ make_files (void)
     close (fd);
     unlink (tmpf);
     unlink (isof);
-    exit (1);
+    exit (EXIT_FAILURE);
   }
 
   if (write (fd, "\0", 1) == -1) {
@@ -428,7 +454,7 @@ make_files (void)
     close (fd);
     unlink (tmpf);
     unlink (isof);
-    exit (1);
+    exit (EXIT_FAILURE);
   }
 
   close (fd);