int main(int argc, char **argv) { int c; int fd; int mode = 0; int dig = 0; loff_t length = -2LL; loff_t offset = 0; static const struct option longopts[] = { { "help", 0, 0, 'h' }, { "version", 0, 0, 'V' }, { "keep-size", 0, 0, 'n' }, { "punch-hole", 0, 0, 'p' }, { "collapse-range", 0, 0, 'c' }, { "dig-holes", 0, 0, 'd' }, { "insert-range", 0, 0, 'i' }, { "zero-range", 0, 0, 'z' }, { "offset", 1, 0, 'o' }, { "length", 1, 0, 'l' }, { "verbose", 0, 0, 'v' }, { NULL, 0, 0, 0 } }; static const ul_excl_t excl[] = { /* rows and cols in in ASCII order */ { 'c', 'd', 'p', 'z' }, { 'c', 'n' }, { 0 } }; int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); while ((c = getopt_long(argc, argv, "hvVncpdizl:o:", longopts, NULL)) != -1) { err_exclusive_options(c, longopts, excl, excl_st); switch(c) { case 'h': usage(stdout); break; case 'c': mode |= FALLOC_FL_COLLAPSE_RANGE; break; case 'd': dig = 1; break; case 'i': mode |= FALLOC_FL_INSERT_RANGE; break; case 'l': length = cvtnum(optarg); break; case 'n': mode |= FALLOC_FL_KEEP_SIZE; break; case 'o': offset = cvtnum(optarg); break; case 'p': mode |= FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE; break; case 'z': mode |= FALLOC_FL_ZERO_RANGE; break; case 'v': verbose++; break; case 'V': printf(UTIL_LINUX_VERSION); return EXIT_SUCCESS; default: usage(stderr); break; } } if (optind == argc) errx(EXIT_FAILURE, _("no filename specified")); filename = argv[optind++]; if (optind != argc) errx(EXIT_FAILURE, _("unexpected number of arguments")); if (dig) { /* for --dig-holes the default is analyze all file */ if (length == -2LL) length = 0; if (length < 0) errx(EXIT_FAILURE, _("invalid length value specified")); } else { /* it's safer to require the range specification (--length --offset) */ if (length == -2LL) errx(EXIT_FAILURE, _("no length argument specified")); if (length <= 0) errx(EXIT_FAILURE, _("invalid length value specified")); } if (offset < 0) errx(EXIT_FAILURE, _("invalid offset value specified")); /* O_CREAT makes sense only for the default fallocate(2) behavior * when mode is no specified and new space is allocated */ fd = open(filename, O_RDWR | (!dig && !mode ? O_CREAT : 0), S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); if (fd < 0) err(EXIT_FAILURE, _("cannot open %s"), filename); if (dig) dig_holes(fd, offset, length); else xfallocate(fd, mode, offset, length); if (close_fd(fd) != 0) err(EXIT_FAILURE, _("write failed: %s"), filename); return EXIT_SUCCESS; }
int main(int argc, char **argv) { int c; int fd; int mode = 0; int dig = 0; loff_t length = -2LL; loff_t offset = 0; static const struct option longopts[] = { { "help", 0, 0, 'h' }, { "version", 0, 0, 'V' }, { "keep-size", 0, 0, 'n' }, { "punch-hole", 0, 0, 'p' }, { "dig-holes", 0, 0, 'd' }, { "offset", 1, 0, 'o' }, { "length", 1, 0, 'l' }, { "verbose", 0, 0, 'v' }, { NULL, 0, 0, 0 } }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); while ((c = getopt_long(argc, argv, "hvVnpdl:o:", longopts, NULL)) != -1) { switch(c) { case 'h': usage(stdout); break; case 'V': printf(UTIL_LINUX_VERSION); return EXIT_SUCCESS; case 'p': mode |= FALLOC_FL_PUNCH_HOLE; /* fall through */ case 'n': mode |= FALLOC_FL_KEEP_SIZE; break; case 'd': dig = 1; break; case 'l': length = cvtnum(optarg); break; case 'o': offset = cvtnum(optarg); break; case 'v': verbose++; break; default: usage(stderr); break; } } if (dig) { if (mode != 0) errx(EXIT_FAILURE, _("Can't use -p or -n with --dig-holes")); if (length == -2LL) length = 0; if (length < 0) errx(EXIT_FAILURE, _("invalid length value specified")); } else { if (length == -2LL) errx(EXIT_FAILURE, _("no length argument specified")); if (length <= 0) errx(EXIT_FAILURE, _("invalid length value specified")); } if (offset < 0) errx(EXIT_FAILURE, _("invalid offset value specified")); if (optind == argc) errx(EXIT_FAILURE, _("no filename specified.")); filename = argv[optind++]; if (optind != argc) { warnx(_("unexpected number of arguments")); usage(stderr); } fd = open(filename, O_RDWR|O_CREAT, 0644); if (fd < 0) err(EXIT_FAILURE, _("cannot open %s"), filename); if (dig) dig_holes(fd, offset, length); else xfallocate(fd, mode, offset, length); if (close_fd(fd) != 0) err(EXIT_FAILURE, _("write failed: %s"), filename); return EXIT_SUCCESS; }