1 /* vi: set sw=4 ts=4: */
3 * Copyright (C) 2017 Denys Vlasenko <vda.linux@googlemail.com>
5 * Licensed under GPLv2, see file LICENSE in this source tree.
8 //config:config FALLOCATE
9 //config: bool "fallocate (5 kb)"
12 //config: Preallocate space for files.
14 //applet:IF_FALLOCATE(APPLET(fallocate, BB_DIR_USR_BIN, BB_SUID_DROP))
16 //kbuild:lib-$(CONFIG_FALLOCATE) += fallocate.o
18 //usage:#define fallocate_trivial_usage
19 //usage: "[-o OFS] -l LEN FILE"
20 // fallocate [-c|-p|-z] [-n] [-o OFS] -l LEN FILE
21 // fallocate -d [-o OFS] [-l LEN] FILE
22 //usage:#define fallocate_full_usage "\n\n"
23 //usage: "Preallocate space for FILE\n"
24 // "\n -c Remove range"
26 // "\n -z Zero and allocate range"
27 // "\n -d Convert zeros to holes"
29 //usage: "\n -o OFS Offset of range"
30 //usage: "\n -l LEN Length of range"
33 //The options --collapse-range, --dig-holes, --punch-hole and --zero-range
34 //are mutually exclusive.
35 //-c, --collapse-range
36 // Removes a byte range from a file, without leaving a hole. The byte range
37 // to be collapsed starts at offset and continues for length bytes.
38 // At the completion of the operation, the contents of the file starting
39 // at the location offset+length will be appended at the location offset,
40 // and the file will be length bytes smaller. The option --keep-size may
41 // not be specified for the collapse-range operation.
43 // Detect and dig holes. This makes the file sparse in-place, without using
44 // extra disk space. The minimum size of the hole depends on filesystem I/O
45 // block size (usually 4096 bytes). Also,
47 // Specifies the length of the range, in bytes.
49 // Do not modify the apparent length of the file. This may effectively
50 // allocate blocks past EOF, which can be removed with a truncate.
52 // Specifies the beginning offset of the range, in bytes.
54 // Deallocates space (i.e., creates a hole) in the byte range starting
55 // at offset and continuing for length bytes. Within the specified range,
56 // partial filesystem blocks are zeroed, and whole
57 // filesystem blocks are removed from the file. After a successful call,
58 // subsequent reads from this range will return zeroes. This option may not
59 // be specified at the same time as the
60 // --zero-range option. Also, when using this option, --keep-size is implied.
62 // Zeroes space in the byte range starting at offset and continuing for
63 // length bytes. Within the specified range, blocks are preallocated for
64 // the regions that span the holes in the file. After
65 // a successful call, subsequent reads from this range will return zeroes.
66 // Zeroing is done within the filesystem preferably by converting the range
67 // into unwritten extents. This approach means that the specified range
68 // will not be physically zeroed out on the device (except for partial
69 // blocks at the either end of the range), and I/O is (otherwise) required
70 // only to update metadata.
71 // Option --keep-size can be specified to prevent file length modification.
75 int fallocate_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
76 int fallocate_main(int argc UNUSED_PARAM, char **argv)
79 const char *str_o = "0";
84 /* exactly one non-option arg */
85 opts = getopt32(argv, "^" "l:o:" "\0" "=1", &str_l, &str_o);
89 ofs = xatoull_sfx(str_o, kmg_i_suffixes);
90 len = xatoull_sfx(str_l, kmg_i_suffixes);
93 fd = xopen3(*argv, O_RDWR | O_CREAT, 0666);
95 /* posix_fallocate has unusual method of returning error */
96 /* maybe use Linux-specific fallocate(int fd, int mode, off_t offset, off_t len) instead? */
97 if ((errno = posix_fallocate(fd, ofs, len)) != 0)
98 bb_perror_msg_and_die("fallocate '%s'", *argv);
100 /* util-linux also performs fsync(fd); */