123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- #include "libbb.h"
- #include <linux/fs.h>
- #ifndef FITRIM
- struct fstrim_range {
- uint64_t start;
- uint64_t len;
- uint64_t minlen;
- };
- #define FITRIM _IOWR('X', 121, struct fstrim_range)
- #endif
- int fstrim_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int fstrim_main(int argc UNUSED_PARAM, char **argv)
- {
- struct fstrim_range range;
- char *arg_o, *arg_l, *arg_m, *mp;
- unsigned opts;
- int fd;
- enum {
- OPT_o = (1 << 0),
- OPT_l = (1 << 1),
- OPT_m = (1 << 2),
- OPT_v = (1 << 3),
- };
- #if ENABLE_LONG_OPTS
- static const char fstrim_longopts[] ALIGN1 =
- "offset\0" Required_argument "o"
- "length\0" Required_argument "l"
- "minimum\0" Required_argument "m"
- "verbose\0" No_argument "v"
- ;
- #endif
- opts = getopt32long(argv, "^"
- "o:l:m:v"
- "\0" "=1", fstrim_longopts,
- &arg_o, &arg_l, &arg_m
- );
- memset(&range, 0, sizeof(range));
- range.len = ULLONG_MAX;
- if (opts & OPT_o)
- range.start = xatoull_sfx(arg_o, kmg_i_suffixes);
- if (opts & OPT_l)
- range.len = xatoull_sfx(arg_l, kmg_i_suffixes);
- if (opts & OPT_m)
- range.minlen = xatoull_sfx(arg_m, kmg_i_suffixes);
- mp = argv[optind];
- fd = xopen_nonblocking(mp);
-
- xioctl(fd, FITRIM, &range);
- if (ENABLE_FEATURE_CLEAN_UP)
- close(fd);
- if (opts & OPT_v)
- printf("%s: %llu bytes trimmed\n", mp, (unsigned long long)range.len);
- return EXIT_SUCCESS;
- return EXIT_FAILURE;
- }
|