Mac OS X: provide alternate implementation of posix_fallocate.
[libguestfs.git] / fish / alloc.c
index 9ef7b49..28c990f 100644 (file)
@@ -1,5 +1,5 @@
 /* guestfish - the filesystem interactive shell
- * Copyright (C) 2009 Red Hat Inc. 
+ * Copyright (C) 2009 Red Hat Inc.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -36,7 +36,7 @@ do_alloc (const char *cmd, int argc, char *argv[])
   int fd;
 
   if (argc != 2) {
-    fprintf (stderr, "use 'alloc file size' to create an image\n");
+    fprintf (stderr, _("use 'alloc file size' to create an image\n"));
     return -1;
   }
 
@@ -44,7 +44,7 @@ do_alloc (const char *cmd, int argc, char *argv[])
     return -1;
 
   if (!guestfs_is_config (g)) {
-    fprintf (stderr, "can't allocate or add disks after launching\n");
+    fprintf (stderr, _("can't allocate or add disks after launching\n"));
     return -1;
   }
 
@@ -54,12 +54,85 @@ do_alloc (const char *cmd, int argc, char *argv[])
     return -1;
   }
 
+#ifdef HAVE_POSIX_FALLOCATE
   if (posix_fallocate (fd, 0, size) == -1) {
     perror ("fallocate");
     close (fd);
     unlink (argv[0]);
     return -1;
   }
+#else
+  /* Slow emulation of posix_fallocate on platforms which don't have it. */
+  char buffer[BUFSIZ];
+  memset (buffer, 0, sizeof buffer);
+
+  size_t remaining = size;
+  while (remaining > 0) {
+    size_t n = remaining > sizeof buffer ? sizeof buffer : remaining;
+    ssize_t r = write (fd, buffer, n);
+    if (r == -1) {
+      perror ("write");
+      close (fd);
+      unlink (argv[0]);
+      return -1;
+    }
+    remaining -= r;
+  }
+#endif
+
+  if (close (fd) == -1) {
+    perror (argv[0]);
+    unlink (argv[0]);
+    return -1;
+  }
+
+  if (guestfs_add_drive (g, argv[0]) == -1) {
+    unlink (argv[0]);
+    return -1;
+  }
+
+  return 0;
+}
+
+int
+do_sparse (const char *cmd, int argc, char *argv[])
+{
+  off_t size;
+  int fd;
+  char c = 0;
+
+  if (argc != 2) {
+    fprintf (stderr, _("use 'sparse file size' to create a sparse image\n"));
+    return -1;
+  }
+
+  if (parse_size (argv[1], &size) == -1)
+    return -1;
+
+  if (!guestfs_is_config (g)) {
+    fprintf (stderr, _("can't allocate or add disks after launching\n"));
+    return -1;
+  }
+
+  fd = open (argv[0], O_WRONLY|O_CREAT|O_NOCTTY|O_TRUNC, 0666);
+  if (fd == -1) {
+    perror (argv[0]);
+    return -1;
+  }
+
+  if (lseek (fd, size-1, SEEK_SET) == (off_t) -1) {
+    perror ("lseek");
+    close (fd);
+    unlink (argv[0]);
+    return -1;
+  }
+
+  if (write (fd, &c, 1) != 1) {
+    perror ("write");
+    close (fd);
+    unlink (argv[0]);
+    return -1;
+  }
 
   if (close (fd) == -1) {
     perror (argv[0]);
@@ -86,19 +159,22 @@ parse_size (const char *str, off_t *size_rtn)
    */
   if (sscanf (str, "%"SCNu64"%c", &size, &type) == 2) {
     switch (type) {
-    case 'k': case 'K': size *= 1024; break;
-    case 'm': case 'M': size *= 1024 * 1024; break;
-    case 'g': case 'G': size *= 1024 * 1024 * 1024; break;
+    case 'k': case 'K': size *= 1024ULL; break;
+    case 'm': case 'M': size *= 1024ULL * 1024ULL; break;
+    case 'g': case 'G': size *= 1024ULL * 1024ULL * 1024ULL; break;
+    case 't': case 'T': size *= 1024ULL * 1024ULL * 1024ULL * 1024ULL; break;
+    case 'p': case 'P': size *= 1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL; break;
+    case 'e': case 'E': size *= 1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL; break;
     case 's': size *= 512; break;
     default:
-      fprintf (stderr, "could not parse size specification '%s'\n", str);
+      fprintf (stderr, _("could not parse size specification '%s'\n"), str);
       return -1;
     }
   }
   else if (sscanf (str, "%"SCNu64, &size) == 1)
-    size *= 1024;
+    size *= 1024ULL;
   else {
-    fprintf (stderr, "could not parse size specification '%s'\n", str);
+    fprintf (stderr, _("could not parse size specification '%s'\n"), str);
     return -1;
   }