From 0e6e32025885286e2de39efca34ced2aaf26b3a3 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Fri, 31 Jul 2009 08:57:10 +0100 Subject: [PATCH] New command: 'fallocate' to (pre-)allocate sized files. --- TODO | 1 - daemon/Makefile.am | 1 + daemon/fallocate.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ po/POTFILES.in | 1 + src/MAX_PROC_NR | 2 +- src/generator.ml | 14 +++++++++++++ 6 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 daemon/fallocate.c diff --git a/TODO b/TODO index 2b9aae1..0326ad9 100644 --- a/TODO +++ b/TODO @@ -147,7 +147,6 @@ Ideas for extra commands dd (?) utime / utimes / futimes / futimens / l.. more mk*temp calls - some sort of alloc/fallocate/posix_fallocate call to create empty space trunc[ate??] ext2 properties: diff --git a/daemon/Makefile.am b/daemon/Makefile.am index 52482d0..6da485b 100644 --- a/daemon/Makefile.am +++ b/daemon/Makefile.am @@ -35,6 +35,7 @@ guestfsd_SOURCES = \ dropcaches.c \ du.c \ ext2.c \ + fallocate.c \ file.c \ find.c \ fsck.c \ diff --git a/daemon/fallocate.c b/daemon/fallocate.c new file mode 100644 index 0000000..47e84c6 --- /dev/null +++ b/daemon/fallocate.c @@ -0,0 +1,59 @@ +/* libguestfs - the guestfsd daemon + * 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 + * 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., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include + +#include +#include +#include +#include +#include + +#include "daemon.h" +#include "actions.h" + +int +do_fallocate (char *path, int len) +{ + int fd, r; + + NEED_ROOT (-1); + ABS_PATH (path, -1); + + CHROOT_IN; + fd = open (path, O_WRONLY | O_CREAT | O_TRUNC | O_NOCTTY, 0666); + CHROOT_OUT; + if (fd == -1) { + reply_with_perror (path); + return -1; + } + + r = posix_fallocate (fd, 0, len); + if (r == -1) { + reply_with_perror ("posix_fallocate: %s", path); + close (fd); + return -1; + } + + if (close (fd) == -1) { + reply_with_perror ("close: %s", path); + return -1; + } + + return 0; +} diff --git a/po/POTFILES.in b/po/POTFILES.in index fc6c9f0..c7be238 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -13,6 +13,7 @@ daemon/dmesg.c daemon/dropcaches.c daemon/du.c daemon/ext2.c +daemon/fallocate.c daemon/file.c daemon/find.c daemon/fsck.c diff --git a/src/MAX_PROC_NR b/src/MAX_PROC_NR index de8febe..fb402ef 100644 --- a/src/MAX_PROC_NR +++ b/src/MAX_PROC_NR @@ -1 +1 @@ -168 +169 diff --git a/src/generator.ml b/src/generator.ml index 3472193..e30f5eb 100755 --- a/src/generator.ml +++ b/src/generator.ml @@ -3187,6 +3187,20 @@ The C<-f> option removes the link (C) if it exists already."); "\ This command reads the target of a symbolic link."); + ("fallocate", (RErr, [String "path"; Int "len"]), 169, [], + [InitBasicFS, Always, TestOutputStruct ( + [["fallocate"; "/a"; "1000000"]; + ["stat"; "/a"]], [CompareWithInt ("size", 1_000_000)])], + "preallocate a file in the guest filesystem", + "\ +This command preallocates a file (containing zero bytes) named +C of size C bytes. If the file exists already, it +is overwritten. + +Do not confuse this with the guestfish-specific +C command which allocates a file in the host and +attaches it as a device."); + ] let all_functions = non_daemon_functions @ daemon_functions -- 1.8.3.1