1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- #include "libbb.h"
- #include <linux/fs.h>
- #ifndef BLKDISCARD
- #define BLKDISCARD 0x1277
- #endif
- #ifndef BLKSECDISCARD
- #define BLKSECDISCARD 0x127d
- #endif
- int blkdiscard_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int blkdiscard_main(int argc UNUSED_PARAM, char **argv)
- {
- unsigned opts;
- const char *offset_str = "0";
- const char *length_str;
- uint64_t offset;
- uint64_t length;
- uint64_t range[2];
- int fd;
- enum {
- OPT_OFFSET = (1 << 0),
- OPT_LENGTH = (1 << 1),
- OPT_SECURE = (1 << 2),
- };
- opts = getopt32(argv, "^" "o:l:s" "\0" "=1", &offset_str, &length_str);
- argv += optind;
- fd = xopen(argv[0], O_RDWR|O_EXCL);
- offset = xatoull_sfx(offset_str, kMG_suffixes);
- if (opts & OPT_LENGTH)
- length = xatoull_sfx(length_str, kMG_suffixes);
- else {
- xioctl(fd, BLKGETSIZE64, &length);
- length -= offset;
- }
- range[0] = offset;
- range[1] = length;
- ioctl_or_perror_and_die(fd,
- (opts & OPT_SECURE) ? BLKSECDISCARD : BLKDISCARD,
- &range,
- "%s: %s failed",
- argv[0],
- (opts & OPT_SECURE) ? "BLKSECDISCARD" : "BLKDISCARD"
- );
- if (ENABLE_FEATURE_CLEAN_UP)
- close(fd);
- return EXIT_SUCCESS;
- }
|