fallocate.c 4.0 KB

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