2 * Copyright 2008, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
10 #include <DiskDevice.h>
11 #include <DiskDeviceRoster.h>
12 #include <DiskSystem.h>
15 extern "C" const char* __progname
;
16 static const char* kProgramName
= __progname
;
23 "Usage: %s <options> <device|volume name>\n"
26 " -h, --help - print this help text\n"
27 " -c, --check-only - do not make any changes to the file system\n",
33 main(int argc
, char** argv
)
35 const struct option kLongOptions
[] = {
36 { "help", 0, NULL
, 'h' },
37 { "check-only", 0, NULL
, 'c' },
40 const char* kShortOptions
= "hc";
42 // parse argument list
43 bool checkOnly
= false;
46 int nextOption
= getopt_long(argc
, argv
, kShortOptions
, kLongOptions
,
55 case 'c': // --check-only
58 default: // everything else
64 // the device name should be the only non-option element
65 if (optind
!= argc
- 1) {
70 const char* path
= argv
[optind
];
71 //UnregisterFileDevice unregisterFileDevice;
73 BDiskDeviceRoster roster
;
74 BPartition
* partition
;
77 status_t status
= roster
.GetPartitionForPath(path
, &device
,
80 if (strncmp(path
, "/dev", 4)) {
82 status
= roster
.FindPartitionByMountPoint(path
, &device
, &partition
)
86 // TODO: try to register file device
89 fprintf(stderr
, "%s: Failed to get disk device for path \"%s\": "
90 "%s\n", kProgramName
, path
, strerror(status
));
95 // Prepare the device for modifications
97 status
= device
.PrepareModifications();
99 fprintf(stderr
, "%s: Could not prepare the device for modifications: "
100 "%s\n", kProgramName
, strerror(status
));
104 // Check if the partition supports repairing
106 bool canRepairWhileMounted
;
107 bool canRepair
= partition
->CanRepair(checkOnly
, &canRepairWhileMounted
);
108 if (!canRepair
&& !canRepairWhileMounted
) {
109 fprintf(stderr
, "%s: The disk system does not support repairing.\n",
114 if (partition
->IsMounted() && !canRepairWhileMounted
) {
115 fprintf(stderr
, "%s: The disk system does not support repairing a "
116 "mounted volume.\n", kProgramName
);
119 if (!partition
->IsMounted() && !canRepair
) {
120 fprintf(stderr
, "%s: The disk system does not support repairing a "
121 "volume that is not mounted.\n", kProgramName
);
125 BDiskSystem diskSystem
;
126 status
= partition
->GetDiskSystem(&diskSystem
);
127 if (status
!= B_OK
) {
128 fprintf(stderr
, "%s: Failed to get disk system for partition: %s\n",
129 kProgramName
, strerror(status
));
135 status
= partition
->Repair(checkOnly
);
136 if (status
!= B_OK
) {
137 fprintf(stderr
, "%s: Repairing failed: %s\n", kProgramName
,
142 status
= device
.CommitModifications();