[hackers] [ubase] Refactor fallocate(1) || FRIGN

From: <git_AT_suckless.org>
Date: Mon, 14 Sep 2015 11:16:01 +0200 (CEST)

commit e3b20bbda0a7a23141702b3250dc64f4fd2d8f87
Author: FRIGN <dev_AT_frign.de>
AuthorDate: Fri Sep 11 00:27:29 2015 +0200
Commit: sin <sin_AT_2f30.org>
CommitDate: Mon Sep 14 10:15:55 2015 +0100

    Refactor fallocate(1)
    
    1) Simplify the manpage. Just refer to fallocate(2) and stop trying
       to list supported file systems. This can change and everbody
       with common sense can bring up the relevant manpages of a given
       operating system himself.
       Use the num-semantics.
    2) Use estrtonum() instead of estrtol().
    3) Allow multiple arguments.

diff --git a/fallocate.1 b/fallocate.1
index ecac614..2fad8d2 100644
--- a/fallocate.1
+++ b/fallocate.1
_AT_@ -1,27 +1,33 @@
-.Dd February 2, 2015
+.Dd September 11, 2015
 .Dt FALLOCATE 1
 .Os ubase
 .Sh NAME
 .Nm fallocate
-.Nd preallocate blocks to a file
+.Nd reallocate files
 .Sh SYNOPSIS
 .Nm
-.Op Fl o Ar offset
-.Fl l Ar length Ar file
+.Op Fl o Ar num
+.Fl l Ar num
+.Ar file ...
 .Sh DESCRIPTION
 .Nm
-preallocates blocks to a file. Only certain filesystems support the
-.Xr fallocate 2
-system call. This is a very fast operation to allocate uninitialized blocks
-in a file without doing any IO. As of the Linux kernel v2.6.31, the
-.Xr fallocate 2
-system call is supported by the btrfs, ext4, ocfs2, and xfs filesystems.
+if necessary creates and reallocates each
+.Ar file
+resulting in expansion or truncation.
+.sp
+Given the filesystem supports
+.Xr fallocate 2 ,
+it is a very fast method of reallocation.
 .Sh OPTIONS
 .Bl -tag -width Ds
-.It Fl l Ar length
-Specifies the length of the allocation, in bytes.
-.It Fl o
-Specifies the beginning offset of the allocation, in bytes.
+.It Fl l Ar num
+Allocate
+.Ar num
+bytes.
+.It Fl o Ar num
+Offset allocation by
+.Ar num
+bytes.
 .El
 .Sh SEE ALSO
 .Xr fallocate 2
diff --git a/fallocate.c b/fallocate.c
index 8e20cbb..47eb470 100644
--- a/fallocate.c
+++ b/fallocate.c
_AT_@ -2,6 +2,8 @@
 #include <sys/stat.h>
 
 #include <fcntl.h>
+#include <limits.h>
+#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
_AT_@ -11,36 +13,43 @@
 static void
 usage(void)
 {
- eprintf("usage: %s [-o offset] -l length file\n", argv0);
+ eprintf("usage: %s [-o num] -l num file ...\n", argv0);
 }
 
 int
 main(int argc, char *argv[])
 {
- int fd;
+ int fd, ret = 0;
         off_t size = 0, offset = 0;
 
         ARGBEGIN {
         case 'l':
- size = estrtol(EARGF(usage()), 10);
+ size = estrtonum(EARGF(usage()), 1, MIN(LLONG_MAX, SIZE_MAX));
                 break;
         case 'o':
- offset = estrtol(EARGF(usage()), 10);
+ offset = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX));
                 break;
         default:
                 usage();
         } ARGEND;
 
- if (argc != 1 || !size)
+ if (!argc || !size)
                 usage();
 
- fd = open(argv[0], O_RDWR | O_CREAT, 0644);
- if (fd < 0)
- eprintf("open %s:", argv[0]);
-
- if (posix_fallocate(fd, offset, size) < 0)
- eprintf("posix_fallocate:");
-
- close(fd);
- return 0;
+ for (; *argv; argc--, argv++) {
+ if ((fd = open(*argv, O_RDWR | O_CREAT, 0644)) < 0) {
+ weprintf("open %s:", *argv);
+ ret = 1;
+ } else if (posix_fallocate(fd, offset, size) < 0) {
+ weprintf("posix_fallocate %s:", *argv);
+ ret = 1;
+ }
+
+ if (fd >= 0 && close(fd) < 0) {
+ weprintf("close %s:", *argv);
+ ret = 1;
+ }
+ }
+
+ return ret;
 }
diff --git a/util.h b/util.h
index 073156d..6ab609e 100644
--- a/util.h
+++ b/util.h
_AT_@ -2,6 +2,14 @@
 #include "arg.h"
 
 #define UTF8_POINT(c) (((c) & 0xc0) != 0x80)
+
+#undef MIN
+#define MIN(x,y) ((x) < (y) ? (x) : (y))
+#undef MAX
+#define MAX(x,y) ((x) > (y) ? (x) : (y))
+#undef LIMIT
+#define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
+
 #define LEN(x) (sizeof (x) / sizeof *(x))
 
 /* eprintf.c */
Received on Mon Sep 14 2015 - 11:16:01 CEST

This archive was generated by hypermail 2.3.0 : Mon Sep 14 2015 - 11:24:10 CEST