blob: 8542586dcd5ffb8e0cf1dd40cebd98a62103b885 [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 */
Denys Vlasenko67918b32017-04-11 13:33:54 +02007//config:config FALLOCATE
Denys Vlasenkoae178ce2017-07-19 14:32:54 +02008//config: bool "fallocate (5 kb)"
Denys Vlasenko67918b32017-04-11 13:33:54 +02009//config: default y
10//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020011//config: Preallocate space for files.
Denys Vlasenko67918b32017-04-11 13:33:54 +020012
13//applet:IF_FALLOCATE(APPLET(fallocate, BB_DIR_USR_BIN, BB_SUID_DROP))
14
15//kbuild:lib-$(CONFIG_FALLOCATE) += fallocate.o
16
17//usage:#define fallocate_trivial_usage
18//usage: "[-o OFS] -l LEN FILE"
19// fallocate [-c|-p|-z] [-n] [-o OFS] -l LEN FILE
20// fallocate -d [-o OFS] [-l LEN] FILE
21//usage:#define fallocate_full_usage "\n\n"
22//usage: "Preallocate space for FILE\n"
23// "\n -c Remove range"
24// "\n -p Make hole"
25// "\n -z Zero and allocate range"
26// "\n -d Convert zeros to holes"
27// "\n -n Keep size"
28//usage: "\n -o OFS Offset of range"
29//usage: "\n -l LEN Length of range"
30
31//Upstream options:
32//The options --collapse-range, --dig-holes, --punch-hole and --zero-range
33//are mutually exclusive.
34//-c, --collapse-range
35// Removes a byte range from a file, without leaving a hole. The byte range
36// to be collapsed starts at offset and continues for length bytes.
37// At the completion of the operation, the contents of the file starting
38// at the location offset+length will be appended at the location offset,
39// and the file will be length bytes smaller. The option --keep-size may
40// not be specified for the collapse-range operation.
41//-d, --dig-holes
42// Detect and dig holes. This makes the file sparse in-place, without using
43// extra disk space. The minimum size of the hole depends on filesystem I/O
44// block size (usually 4096 bytes). Also,
45//-l, --length length
46// Specifies the length of the range, in bytes.
47//-n, --keep-size
48// Do not modify the apparent length of the file. This may effectively
49// allocate blocks past EOF, which can be removed with a truncate.
50//-o, --offset offset
51// Specifies the beginning offset of the range, in bytes.
52//-p, --punch-hole
53// Deallocates space (i.e., creates a hole) in the byte range starting
54// at offset and continuing for length bytes. Within the specified range,
55// partial filesystem blocks are zeroed, and whole
56// filesystem blocks are removed from the file. After a successful call,
57// subsequent reads from this range will return zeroes. This option may not
58// be specified at the same time as the
59// --zero-range option. Also, when using this option, --keep-size is implied.
60//-z, --zero-range
61// Zeroes space in the byte range starting at offset and continuing for
62// length bytes. Within the specified range, blocks are preallocated for
63// the regions that span the holes in the file. After
64// a successful call, subsequent reads from this range will return zeroes.
65// Zeroing is done within the filesystem preferably by converting the range
66// into unwritten extents. This approach means that the specified range
67// will not be physically zeroed out on the device (except for partial
68// blocks at the either end of the range), and I/O is (otherwise) required
69// only to update metadata.
70// Option --keep-size can be specified to prevent file length modification.
71
72#include "libbb.h"
73
74int fallocate_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
75int fallocate_main(int argc UNUSED_PARAM, char **argv)
76{
77 const char *str_l;
78 const char *str_o = "0";
79 off_t ofs, len;
80 unsigned opts;
81 int fd;
82
83 /* exactly one non-option arg */
Denys Vlasenko22542ec2017-08-08 21:55:02 +020084 opts = getopt32(argv, "^" "l:o:" "\0" "=1", &str_l, &str_o);
Denys Vlasenko67918b32017-04-11 13:33:54 +020085 if (!(opts & 1))
86 bb_show_usage();
87
88 ofs = xatoull_sfx(str_o, kmg_i_suffixes);
89 len = xatoull_sfx(str_l, kmg_i_suffixes);
90
91 argv += optind;
92 fd = xopen3(*argv, O_RDWR | O_CREAT, 0666);
93
94 /* posix_fallocate has unusual method of returning error */
95 /* maybe use Linux-specific fallocate(int fd, int mode, off_t offset, off_t len) instead? */
96 if ((errno = posix_fallocate(fd, ofs, len)) != 0)
97 bb_perror_msg_and_die("fallocate '%s'", *argv);
98
99 /* util-linux also performs fsync(fd); */
100
101 return EXIT_SUCCESS;
102}