[Pkg-libvirt-commits] [libguestfs] 146/233: fish: Use disk-create API to implement guestfish 'alloc' and 'sparse' commands.

Hilko Bengen bengen at moszumanska.debian.org
Wed Feb 19 21:11:50 UTC 2014


This is an automated email from the git hooks/post-receive script.

bengen pushed a commit to branch experimental
in repository libguestfs.

commit 2794eac7390f8d7eb7b76cb0c374b4b2266d98a9
Author: Richard W.M. Jones <rjones at redhat.com>
Date:   Tue Jan 28 15:04:09 2014 +0000

    fish: Use disk-create API to implement guestfish 'alloc' and 'sparse' commands.
---
 fish/Makefile.am   |  1 +
 fish/alloc.c       | 60 ++++--------------------------------------------------
 fish/test-alloc.sh | 47 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 52 insertions(+), 56 deletions(-)

diff --git a/fish/Makefile.am b/fish/Makefile.am
index 54ba875..c39c968 100644
--- a/fish/Makefile.am
+++ b/fish/Makefile.am
@@ -301,6 +301,7 @@ EXTRA_DIST += \
 	test-a.sh \
 	test-add-domain.sh \
 	test-add-uri.sh \
+	test-alloc.sh \
 	test-copy.sh \
 	test-d.sh \
 	test-edit.sh \
diff --git a/fish/alloc.c b/fish/alloc.c
index 18597f7..06dd8cf 100644
--- a/fish/alloc.c
+++ b/fish/alloc.c
@@ -66,67 +66,15 @@ int
 alloc_disk (const char *filename, const char *size_str, int add, int sparse)
 {
   off_t size;
-  int fd;
-  char c = 0;
+  const char *prealloc = sparse ? "sparse" : "full";
 
   if (parse_size (size_str, &size) == -1)
     return -1;
 
-  fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_TRUNC|O_CLOEXEC, 0666);
-  if (fd == -1) {
-    perror (filename);
+  if (guestfs_disk_create (g, filename, "raw", (int64_t) size,
+                           GUESTFS_DISK_CREATE_PREALLOCATION, prealloc,
+                           -1) == -1)
     return -1;
-  }
-
-  if (!sparse) {                /* Not sparse */
-#ifdef HAVE_POSIX_FALLOCATE
-    int err = posix_fallocate (fd, 0, size);
-    if (err != 0) {
-      errno = err;
-      perror ("fallocate");
-      close (fd);
-      unlink (filename);
-      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 (filename);
-        return -1;
-      }
-      remaining -= r;
-    }
-#endif
-  } else {                      /* Sparse */
-    if (lseek (fd, size-1, SEEK_SET) == (off_t) -1) {
-      perror ("lseek");
-      close (fd);
-      unlink (filename);
-      return -1;
-    }
-
-    if (write (fd, &c, 1) != 1) {
-      perror ("write");
-      close (fd);
-      unlink (filename);
-      return -1;
-    }
-  }
-
-  if (close (fd) == -1) {
-    perror (filename);
-    unlink (filename);
-    return -1;
-  }
 
   if (add) {
     if (guestfs_add_drive_opts (g, filename,
diff --git a/fish/test-alloc.sh b/fish/test-alloc.sh
new file mode 100755
index 0000000..3f46f8b
--- /dev/null
+++ b/fish/test-alloc.sh
@@ -0,0 +1,47 @@
+#!/bin/bash -
+# libguestfs
+# Copyright (C) 2014 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
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+# Test guestfish alloc and sparse commands.
+
+set -e
+
+rm -f test-alloc.img
+
+$VG ./guestfish alloc test-alloc.img 200000
+if [ "$(stat -c '%s' test-alloc.img)" -ne 200000 ]; then
+    echo "$0: alloc command failed to create file of the correct size"
+    exit 1
+fi
+
+if [ "$(stat -c '%b' test-alloc.img)" -eq 0 ]; then
+    echo "$0: alloc command failed to create a fully allocated file"
+    exit 1
+fi
+
+$VG ./guestfish sparse test-alloc.img 100000
+if [ "$(stat -c '%s' test-alloc.img)" -ne 100000 ]; then
+    echo "$0: sparse command failed to create file of the correct size"
+    exit 1
+fi
+
+if [ "$(stat -c '%b' test-alloc.img)" -ne 0 ]; then
+    echo "$0: sparse command failed to create a sparse file"
+    exit 1
+fi
+
+rm test-alloc.img

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-libvirt/libguestfs.git



More information about the Pkg-libvirt-commits mailing list