libroot/posix/stdio: Remove unused portions.
[haiku.git] / src / bin / fstrim.cpp
blobb09cd411848d6e23b4b11bcc0b0b9ee7f18a2fda
1 /*
2 * Copyright 2013, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT license.
4 */
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <getopt.h>
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
15 #include <Drivers.h>
17 #include <AutoDeleter.h>
20 static struct option const kLongOptions[] = {
21 {"help", no_argument, 0, 'h'},
22 {NULL}
26 extern const char *__progname;
27 static const char *kProgramName = __progname;
30 void
31 usage(int returnValue)
33 fprintf(stderr, "Usage: %s <path-to-mounted-file-system>\n", kProgramName);
34 exit(returnValue);
38 int
39 main(int argc, char** argv)
41 int c;
42 while ((c = getopt_long(argc, argv, "h", kLongOptions, NULL)) != -1) {
43 switch (c) {
44 case 0:
45 break;
46 case 'h':
47 usage(0);
48 break;
49 default:
50 usage(1);
51 break;
55 if (argc - optind < 1)
56 usage(1);
57 const char* path = argv[optind++];
59 int fd = open(path, O_RDONLY);
60 if (fd < 0) {
61 fprintf(stderr, "%s: Could not access path: %s\n", kProgramName,
62 strerror(errno));
63 return EXIT_FAILURE;
66 FileDescriptorCloser closer(fd);
68 fs_trim_data trimData;
69 trimData.range_count = 1;
70 trimData.ranges[0].offset = 0;
71 trimData.ranges[0].size = UINT64_MAX;
72 trimData.trimmed_size = 0;
74 if (ioctl(fd, B_TRIM_DEVICE, &trimData, sizeof(fs_trim_data)) != 0) {
75 fprintf(stderr, "%s: Trimming failed: %s\n", kProgramName,
76 strerror(errno));
77 return EXIT_FAILURE;
80 printf("Trimmed %" B_PRIu64 " bytes from device.\n", trimData.trimmed_size);
81 return EXIT_SUCCESS;