Added fstrim command.

Later, there should be a service that runs this from time to time for all
devices that support it.

Conflicts:
	build/jam/HaikuImage
This commit is contained in:
Axel Dörfler 2013-08-05 00:14:52 +02:00
parent 29a8450843
commit a352b43896
3 changed files with 83 additions and 1 deletions

View File

@ -13,7 +13,7 @@ SYSTEM_BIN = [ FFilterByBuildFeatures
diskimage draggers driveinfo dstcheck du dumpcatalog
echo eject env error expand expr
factor false fdinfo ffm filepanel find finddir findpaths FirstBootPrompt fmt
fold fortune frcode ftp ftpd funzip fwcontrol@x86
fold fortune frcode fstrim ftp ftpd funzip fwcontrol@x86
gawk gdb@x86 getlimits groupadd groupdel groupmod groups gzip gzexe
hd head hey hostname
id ident ifconfig <bin>install installsound iroster isvolume

View File

@ -35,6 +35,7 @@ StdBinCommands
error.c
fortune.c
finddir.c
fstrim.cpp
hd.c
idestatus.c
listarea.c

81
src/bin/fstrim.cpp Normal file
View File

@ -0,0 +1,81 @@
/*
* Copyright 2013, Axel Dörfler, axeld@pinc-software.de.
* Distributed under the terms of the MIT license.
*/
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Drivers.h>
#include <AutoDeleter.h>
static struct option const kLongOptions[] = {
{"help", no_argument, 0, 'h'},
{NULL}
};
extern const char *__progname;
static const char *kProgramName = __progname;
void
usage(int returnValue)
{
fprintf(stderr, "Usage: %s <path-to-mounted-file-system>\n", kProgramName);
exit(returnValue);
}
int
main(int argc, char** argv)
{
int c;
while ((c = getopt_long(argc, argv, "h", kLongOptions, NULL)) != -1) {
switch (c) {
case 0:
break;
case 'h':
usage(0);
break;
default:
usage(1);
break;
}
}
if (argc - optind < 1)
usage(1);
const char* path = argv[optind++];
int fd = open(path, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "%s: Could not access path: %s\n", kProgramName,
strerror(errno));
return EXIT_FAILURE;
}
FileDescriptorCloser closer(fd);
fs_trim_data trimData;
trimData.offset = 0;
trimData.size = OFF_MAX;
trimData.trimmed_size = 0;
if (ioctl(fd, B_TRIM_DEVICE, &trimData, sizeof(fs_trim_data)) != 0) {
fprintf(stderr, "%s: Trimming failed: %s\n", kProgramName,
strerror(errno));
return EXIT_FAILURE;
}
printf("Trimmed %lld bytes from device.\n", trimData.trimmed_size);
return EXIT_SUCCESS;
}