blob: 70e7e178fd926edc9d5f7311b62ea4a5f5fa430a [file] [log] [blame]
Denys Vlasenko67918b32017-04-11 13:33:54 +02001/* vi: set sw=4 ts=4: */
2/*
3 * Copyright (C) 2017 Denys Vlasenko <vda.linux@googlemail.com>
4 *
5 * Licensed under GPLv2, see file LICENSE in this source tree.
6 */
7
8//config:config FALLOCATE
Denys Vlasenkoae178ce2017-07-19 14:32:54 +02009//config: bool "fallocate (5 kb)"
Denys Vlasenko67918b32017-04-11 13:33:54 +020010//config: default y
11//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020012//config: Preallocate space for files.
Denys Vlasenko67918b32017-04-11 13:33:54 +020013
14//applet:IF_FALLOCATE(APPLET(fallocate, BB_DIR_USR_BIN, BB_SUID_DROP))
15
16//kbuild:lib-$(CONFIG_FALLOCATE) += fallocate.o
17
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"
25// "\n -p Make hole"
26// "\n -z Zero and allocate range"
27// "\n -d Convert zeros to holes"
28// "\n -n Keep size"
29//usage: "\n -o OFS Offset of range"
30//usage: "\n -l LEN Length of range"
31
32//Upstream options:
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.
42//-d, --dig-holes
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,
46//-l, --length length
47// Specifies the length of the range, in bytes.
48//-n, --keep-size
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.
51//-o, --offset offset
52// Specifies the beginning offset of the range, in bytes.
53//-p, --punch-hole
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.
61//-z, --zero-range
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.
72
73#include "libbb.h"
74
75int fallocate_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
76int fallocate_main(int argc UNUSED_PARAM, char **argv)
77{
78 const char *str_l;
79 const char *str_o = "0";
80 off_t ofs, len;
81 unsigned opts;
82 int fd;
83
84 /* exactly one non-option arg */
85 opt_complementary = "=1";
86 opts = getopt32(argv, "l:o:", &str_l, &str_o);
87 if (!(opts & 1))
88 bb_show_usage();
89
90 ofs = xatoull_sfx(str_o, kmg_i_suffixes);
91 len = xatoull_sfx(str_l, kmg_i_suffixes);
92
93 argv += optind;
94 fd = xopen3(*argv, O_RDWR | O_CREAT, 0666);
95
96 /* posix_fallocate has unusual method of returning error */
97 /* maybe use Linux-specific fallocate(int fd, int mode, off_t offset, off_t len) instead? */
98 if ((errno = posix_fallocate(fd, ofs, len)) != 0)
99 bb_perror_msg_and_die("fallocate '%s'", *argv);
100
101 /* util-linux also performs fsync(fd); */
102
103 return EXIT_SUCCESS;
104}