summaryrefslogtreecommitdiffstats
path: root/sys-utils/fallocate.c
diff options
context:
space:
mode:
authorRodrigo Campos2014-01-25 20:17:27 +0100
committerKarel Zak2014-02-14 11:00:41 +0100
commitbcd9315d4ba62a225028c47b0423e390e19381f6 (patch)
treee8b05dda1c4f5df01695b22d9dcc07aed0c61967 /sys-utils/fallocate.c
parentfallocate: Clarify that space can also be deallocated (diff)
downloadkernel-qcow2-util-linux-bcd9315d4ba62a225028c47b0423e390e19381f6.tar.gz
kernel-qcow2-util-linux-bcd9315d4ba62a225028c47b0423e390e19381f6.tar.xz
kernel-qcow2-util-linux-bcd9315d4ba62a225028c47b0423e390e19381f6.zip
fallocate: Hide #ifdef tricks to call fallocate in a function
Future patches will add more calls to fallocate(), so it will be useful to have all these tricks inside a function. The error message when fallocate is not supported is slightly changed: the file name is not printed as a prefix because is not available in the context of the function. Also, to only print one of the two possible errors (as happens when using directly exit()), an else clause was added. Signed-off-by: Rodrigo Campos <rodrigo@sdfg.com.ar>
Diffstat (limited to 'sys-utils/fallocate.c')
-rw-r--r--sys-utils/fallocate.c43
1 files changed, 28 insertions, 15 deletions
diff --git a/sys-utils/fallocate.c b/sys-utils/fallocate.c
index 55e841115..5c665535a 100644
--- a/sys-utils/fallocate.c
+++ b/sys-utils/fallocate.c
@@ -82,6 +82,30 @@ static loff_t cvtnum(char *s)
return x;
}
+static int xfallocate(int fd, int mode, off_t offset, off_t length)
+{
+ int error;
+
+#ifdef HAVE_FALLOCATE
+ error = fallocate(fd, mode, offset, length);
+#else
+ error = syscall(SYS_fallocate, fd, mode, offset, length);
+#endif
+ /*
+ * EOPNOTSUPP: The FALLOC_FL_KEEP_SIZE is unsupported
+ * ENOSYS: The filesystem does not support sys_fallocate
+ */
+ if (error < 0) {
+ if ((mode & FALLOC_FL_KEEP_SIZE) && errno == EOPNOTSUPP) {
+ fputs(_("keep size mode (-n option) unsupported\n"),
+ stderr);
+ } else {
+ fputs(_("fallocate failed\n"), stderr);
+ }
+ }
+ return error;
+}
+
int main(int argc, char **argv)
{
char *fname;
@@ -153,21 +177,10 @@ int main(int argc, char **argv)
if (fd < 0)
err(EXIT_FAILURE, _("cannot open %s"), fname);
-#ifdef HAVE_FALLOCATE
- error = fallocate(fd, mode, offset, length);
-#else
- error = syscall(SYS_fallocate, fd, mode, offset, length);
-#endif
- /*
- * EOPNOTSUPP: The FALLOC_FL_KEEP_SIZE is unsupported
- * ENOSYS: The filesystem does not support sys_fallocate
- */
- if (error < 0) {
- if ((mode & FALLOC_FL_KEEP_SIZE) && errno == EOPNOTSUPP)
- errx(EXIT_FAILURE,
- _("keep size mode (-n option) unsupported"));
- err(EXIT_FAILURE, _("%s: fallocate failed"), fname);
- }
+ error = xfallocate(fd, mode, offset, length);
+
+ if (error < 0)
+ exit(EXIT_FAILURE);
if (close_fd(fd) != 0)
err(EXIT_FAILURE, _("write failed: %s"), fname);