1 /* 2 * Copyright 2013, Axel Dörfler, axeld@pinc-software.de. 3 * Distributed under the terms of the MIT license. 4 */ 5 6 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> 14 15 #include <Drivers.h> 16 17 #include <AutoDeleter.h> 18 19 20 static struct option const kLongOptions[] = { 21 {"help", no_argument, 0, 'h'}, 22 {NULL} 23 }; 24 25 26 extern const char *__progname; 27 static const char *kProgramName = __progname; 28 29 30 void 31 usage(int returnValue) 32 { 33 fprintf(stderr, "Usage: %s <path-to-mounted-file-system>\n", kProgramName); 34 exit(returnValue); 35 } 36 37 38 int 39 main(int argc, char** argv) 40 { 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; 52 } 53 } 54 55 if (argc - optind < 1) 56 usage(1); 57 const char* path = argv[optind++]; 58 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; 64 } 65 66 FileDescriptorCloser closer(fd); 67 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; 73 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; 78 } 79 80 printf("Trimmed %" B_PRIu64 " bytes from device.\n", trimData.trimmed_size); 81 return EXIT_SUCCESS; 82 } 83