diff --git a/fish/Makefile.am b/fish/Makefile.am index 54ba8755a..c39c9685b 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 18597f7b2..06dd8cf4d 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 000000000..3f46f8b4f --- /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