build: Add ./configure --disable-fuse option.
[libguestfs.git] / daemon / ext2.c
index 3a075e5..725352e 100644 (file)
 #include <string.h>
 #include <unistd.h>
 
-#include "../src/guestfs_protocol.h"
+#include "guestfs_protocol.h"
 #include "daemon.h"
 #include "c-ctype.h"
 #include "actions.h"
 
+/* Confirmed this is true up to ext4 from the Linux sources. */
+#define EXT2_LABEL_MAX 16
+
 /* Choose which tools like mke2fs to use.  For RHEL 5 (only) there
  * is a special set of tools which support ext2/3/4.  eg. On RHEL 5,
  * mke2fs only supports ext2/3, but mke4fs supports ext2/3/4.
@@ -156,6 +159,12 @@ do_set_e2label (const char *device, const char *label)
   if (e2prog (prog) == -1)
     return -1;
 
+  if (strlen (label) > EXT2_LABEL_MAX) {
+    reply_with_error ("%s: ext2 labels are limited to %d bytes",
+                      label, EXT2_LABEL_MAX);
+    return -1;
+  }
+
   r = command (NULL, &err, prog, device, label, NULL);
   if (r == -1) {
     reply_with_error ("%s", err);
@@ -170,29 +179,7 @@ do_set_e2label (const char *device, const char *label)
 char *
 do_get_e2label (const char *device)
 {
-  int r, len;
-  char *out, *err;
-
-  char prog[] = "e2label";
-  if (e2prog (prog) == -1)
-    return NULL;
-
-  r = command (&out, &err, prog, device, NULL);
-  if (r == -1) {
-    reply_with_error ("%s", err);
-    free (out);
-    free (err);
-    return NULL;
-  }
-
-  free (err);
-
-  /* Remove any trailing \n from the label. */
-  len = strlen (out);
-  if (len > 0 && out[len-1] == '\n')
-    out[len-1] = '\0';
-
-  return out;                  /* caller frees */
+  return do_vfs_label (device);
 }
 
 int
@@ -219,65 +206,7 @@ do_set_e2uuid (const char *device, const char *uuid)
 char *
 do_get_e2uuid (const char *device)
 {
-  int r;
-  char *out, *err, *p, *q;
-
-  /* It's not so straightforward to get the volume UUID.  We have
-   * to use tune2fs -l and then look for a particular string in
-   * the output.
-   */
-  char prog[] = "tune2fs";
-  if (e2prog (prog) == -1)
-    return NULL;
-
-  r = command (&out, &err, prog, "-l", device, NULL);
-  if (r == -1) {
-    reply_with_error ("%s", err);
-    free (out);
-    free (err);
-    return NULL;
-  }
-
-  free (err);
-
-  /* Look for /\nFilesystem UUID:\s+/ in the output. */
-  p = strstr (out, "\nFilesystem UUID:");
-  if (p == NULL) {
-    reply_with_error ("no Filesystem UUID in the output of tune2fs -l");
-    free (out);
-    return NULL;
-  }
-
-  p += 17;
-  while (*p && c_isspace (*p))
-    p++;
-  if (!*p) {
-    reply_with_error ("malformed Filesystem UUID in the output of tune2fs -l");
-    free (out);
-    return NULL;
-  }
-
-  /* Now 'p' hopefully points to the start of the UUID. */
-  q = p;
-  while (*q && (c_isxdigit (*q) || *q == '-'))
-    q++;
-  if (!*q) {
-    reply_with_error ("malformed Filesystem UUID in the output of tune2fs -l");
-    free (out);
-    return NULL;
-  }
-
-  *q = '\0';
-
-  p = strdup (p);
-  if (!p) {
-    reply_with_perror ("strdup");
-    free (out);
-    return NULL;
-  }
-
-  free (out);
-  return p;                    /* caller frees */
+  return do_vfs_uuid (device);
 }
 
 int
@@ -400,6 +329,12 @@ do_mke2journal_L (int blocksize, const char *label, const char *device)
   if (e2prog (prog) == -1)
     return -1;
 
+  if (strlen (label) > EXT2_LABEL_MAX) {
+    reply_with_error ("%s: ext2 labels are limited to %d bytes",
+                      label, EXT2_LABEL_MAX);
+    return -1;
+  }
+
   char blocksize_s[32];
   snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
 
@@ -486,6 +421,12 @@ do_mke2fs_JL (const char *fstype, int blocksize, const char *device,
   if (e2prog (prog) == -1)
     return -1;
 
+  if (strlen (label) > EXT2_LABEL_MAX) {
+    reply_with_error ("%s: ext2 labels are limited to %d bytes",
+                      label, EXT2_LABEL_MAX);
+    return -1;
+  }
+
   char blocksize_s[32];
   snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);