Skip to content

Commit 65fb3ba

Browse files
authored
Update f3fix.c
1 parent 5869ab1 commit 65fb3ba

File tree

1 file changed

+115
-4
lines changed

1 file changed

+115
-4
lines changed

f3fix.c

Lines changed: 115 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ struct args {
2929
bool list_fs_types;
3030
bool boot;
3131
const char *dev_filename;
32-
const char *disk_type;
33-
const char *fs_type;
32+
PedDiskType *disk_type;
33+
PedFileSystemType *fs_type;
3434
long long first_sec;
3535
long long last_sec;
3636
};
@@ -121,6 +121,115 @@ static void parse_args(int argc, char **argv, struct args *args) {
121121

122122
#else // Linux реализация
123123

124+
static long long arg_to_long_long(const struct argp_state *state, const char *arg);
125+
static void list_disk_types(void);
126+
static void list_fs_types(void);
127+
static int fix_disk(PedDevice *dev, PedDiskType *type, PedFileSystemType *fs_type, int boot, PedSector start, PedSector end);
128+
129+
static char adoc[] = "<DISK_DEV>";
130+
static char doc[] = "F3 Fix -- edit the partition table...";
131+
static struct argp_option options[] = {
132+
{"disk-type", 'd', "TYPE", 0, "Disk type", 2},
133+
// ... остальные опции из оригинального кода ...
134+
};
135+
136+
static void list_disk_types(void)
137+
{
138+
PedDiskType *type;
139+
int i = 0;
140+
printf("Disk types:\n");
141+
for (type = ped_disk_type_get_next(NULL); type;
142+
type = ped_disk_type_get_next(type)) {
143+
printf("%s\t", type->name);
144+
i++;
145+
if (i == 5) {
146+
printf("\n");
147+
i = 0;
148+
}
149+
}
150+
if (i > 0)
151+
printf("\n");
152+
printf("\n");
153+
}
154+
155+
static void list_fs_types(void)
156+
{
157+
PedFileSystemType *fs_type;
158+
int i = 0;
159+
printf("File system types:\n");
160+
for (fs_type = ped_file_system_type_get_next(NULL); fs_type;
161+
fs_type = ped_file_system_type_get_next(fs_type)) {
162+
printf("%s\t", fs_type->name);
163+
i++;
164+
if (i == 5) {
165+
printf("\n");
166+
i = 0;
167+
}
168+
}
169+
if (i > 0)
170+
printf("\n");
171+
printf("\n");
172+
}
173+
174+
static long long arg_to_long_long(const struct argp_state *state,
175+
const char *arg)
176+
{
177+
char *end;
178+
long long ll = strtoll(arg, &end, 0);
179+
if (!arg)
180+
argp_error(state, "An integer must be provided");
181+
if (!*arg || *end)
182+
argp_error(state, "`%s' is not an integer", arg);
183+
return ll;
184+
}
185+
186+
static int fix_disk(PedDevice *dev, PedDiskType *type,
187+
PedFileSystemType *fs_type, int boot, PedSector start, PedSector end)
188+
{
189+
PedDisk *disk;
190+
PedPartition *part;
191+
PedGeometry *geom;
192+
PedConstraint *constraint;
193+
int ret = 0;
194+
195+
disk = ped_disk_new_fresh(dev, type);
196+
if (!disk)
197+
goto out;
198+
199+
start = map_sector_to_logical_sector(start, dev->sector_size);
200+
end = map_sector_to_logical_sector(end, dev->sector_size);
201+
part = ped_partition_new(disk, PED_PARTITION_NORMAL,
202+
fs_type, start, end);
203+
if (!part)
204+
goto disk;
205+
if (boot && !ped_partition_set_flag(part, PED_PARTITION_BOOT, 1))
206+
goto part;
207+
208+
geom = ped_geometry_new(dev, start, end - start + 1);
209+
if (!geom)
210+
goto part;
211+
constraint = ped_constraint_exact(geom);
212+
ped_geometry_destroy(geom);
213+
if (!constraint)
214+
goto part;
215+
216+
ret = ped_disk_add_partition(disk, part, constraint);
217+
ped_constraint_destroy(constraint);
218+
if (!ret)
219+
goto part;
220+
/* ped_disk_print(disk); */
221+
222+
ret = ped_disk_commit(disk);
223+
goto disk;
224+
225+
part:
226+
ped_partition_destroy(part);
227+
disk:
228+
ped_disk_destroy(disk);
229+
out:
230+
return ret;
231+
}
232+
124233
// Оригинальные Linux-функции с argp
125234
static error_t parse_opt(int key, char *arg, struct argp_state *state) {
126235
struct args *args = state->input;
@@ -252,6 +361,7 @@ int main(int argc, char *argv[]) {
252361
#ifdef _WIN32
253362
// Windows: преобразование пути
254363
char win_path[MAX_PATH];
364+
char drive_letter;
255365
if (sscanf(args.dev_filename, "/dev/sd%c", &drive_letter) == 1) {
256366
int drive_num = tolower(drive_letter) - 'a';
257367
snprintf(win_path, MAX_PATH, "\\\\.\\PhysicalDrive%d", drive_num);
@@ -288,12 +398,13 @@ int main(int argc, char *argv[]) {
288398
/* XXX If @dev is a partition, refer the user to
289399
* the disk of this partition.
290400
*/
291-
dev = ped_device_get(args.dev_filename);
401+
//dev = ped_device_get(args.dev_filename);
292402
if (!dev)
293403
return 1;
294404

295405
ret = !fix_disk(dev, args.disk_type, args.fs_type, args.boot,
296-
args.first_sec, args.last_sec);
406+
args.first_sec, args.last_sec);
407+
return ret;
297408
#endif
298409

299410
printf("Drive %s was successfully fixed\n", args.dev_filename);

0 commit comments

Comments
 (0)