📄 common.c
字号:
n = strlen(temp); name = (char *) calloc(n,sizeof(char)); strncpy(name,temp,n); if (!uiquery->getstring(_("Partition name"), &name, NULL, NULL, 0)) goto error; } if (!name) goto error; if (!ped_partition_set_name (part, name)) goto error_free_name; free (name); /*if (!ped_disk_commit (disk)) goto error_destroy_disk;*/ uiquery->need_commit = 1; return 1;error_free_name: free (name);error: return 0;}static PedPartitionType_disk_get_part_type_for_sector (PedDisk* disk, PedSector sector){ PedPartition* extended; extended = ped_disk_extended_partition (disk); if (!extended || !ped_geometry_test_sector_inside (&extended->geom, sector)) return 0; return PED_PARTITION_LOGICAL;}/* This function checks if "part" contains a file system, and returs * 0 if either no file system was found, or the user declined to add it. * 1 if a file system was found, and the user chose to add it. * -1 if the user chose to cancel the entire search. */static int_rescue_add_partition (PedPartition* part){ const PedFileSystemType* fs_type; PedGeometry* probed; PedExceptionOption ex_opt; PedConstraint* constraint; char* found_start; char* found_end; fs_type = ped_file_system_probe (&part->geom); if (!fs_type) return 0; probed = ped_file_system_probe_specific (fs_type, &part->geom); if (!probed) return 0; if (!ped_geometry_test_inside (&part->geom, probed)) { ped_geometry_destroy (probed); return 0; } constraint = ped_constraint_exact (probed); if (!ped_disk_set_partition_geom (part->disk, part, constraint, probed->start, probed->end)) { ped_constraint_destroy (constraint); return 0; } ped_constraint_destroy (constraint); found_start = ped_unit_format (probed->dev, probed->start); found_end = ped_unit_format (probed->dev, probed->end); ex_opt = ped_exception_throw ( PED_EXCEPTION_INFORMATION, PED_EXCEPTION_YES_NO_CANCEL, _("A %s %s partition was found at %s -> %s. " "Do you want to add it to the partition table?"), fs_type->name, ped_partition_type_get_name (part->type), found_start, found_end); ped_geometry_destroy (probed); ped_free (found_start); ped_free (found_end); switch (ex_opt) { case PED_EXCEPTION_CANCEL: return -1; case PED_EXCEPTION_NO: return 0; } ped_partition_set_system (part, fs_type); ped_disk_commit (part->disk); uiquery->need_commit = 0; return 1;}/* hack: we only iterate through the start, since most (all) fs's have their * superblocks at the start. We'll need to change this if we generalize * for RAID, or something... */static int_rescue_pass (PedDisk* disk, PedGeometry* start_range, PedGeometry* end_range){ PedSector start; PedGeometry start_geom_exact; PedGeometry entire_dev; PedConstraint constraint; PedPartition* part; PedPartitionType part_type; part_type = _disk_get_part_type_for_sector ( disk, (start_range->start + end_range->end) / 2); ped_geometry_init (&entire_dev, disk->dev, 0, disk->dev->length); ped_timer_reset (uiquery->timer); ped_timer_set_state_name (uiquery->timer, _("searching for file systems")); for (start = start_range->start; start <= start_range->end; start++) { ped_timer_update (uiquery->timer, 1.0 * (start - start_range->start) / start_range->length); ped_geometry_init (&start_geom_exact, disk->dev, start, 1); ped_constraint_init ( &constraint, ped_alignment_any, ped_alignment_any, &start_geom_exact, &entire_dev, 1, disk->dev->length); part = ped_partition_new (disk, part_type, NULL, start, end_range->end); if (!part) { ped_constraint_done (&constraint); continue; } ped_exception_fetch_all (); if (ped_disk_add_partition (disk, part, &constraint)) { ped_exception_leave_all (); switch (_rescue_add_partition (part)) { case 1: ped_constraint_done (&constraint); return 1; case 0: ped_disk_remove_partition (disk, part); break; case -1: goto error_remove_partition; } } else { ped_exception_leave_all (); } ped_partition_destroy (part); ped_constraint_done (&constraint); } ped_timer_update (uiquery->timer, 1.0); return 1;error_remove_partition: ped_disk_remove_partition (disk, part);error_partition_destroy: ped_partition_destroy (part);error_constraint_done: ped_constraint_done (&constraint);error: return 0;}/* TODO: This might be ok without commiting */intperform_rescue (PedDisk* disk, PedSector start, PedSector end, UIOpts opts){ PedSector fuzz; PedGeometry probe_start_region; PedGeometry probe_end_region; if (!disk) goto error; if (opts & UI_WARN_COMMIT) if (!ask_boolean_question (_("WARNING: This writes all data to disk automatically, continue?"))) return 0; /* Why the hell I use two seperate ifs, instead of one? I wish I knew */ if (uiquery->need_commit) if (!perform_commit(disk,UI_DEFAULT)) goto error; if (opts & UI_CUSTOM_VALUES) { PartPos pos; pos.start.sector = start; pos.end.sector = end; if (!get_position(&pos, disk, GET_SECT | NO_RANGE )) goto error; start = pos.start.sector; end = pos.end.sector; } fuzz = PED_MAX (PED_MIN ((end - start) / 10, MEGABYTE_SECTORS(disk->dev)), MEGABYTE_SECTORS(disk->dev) * 16); ped_geometry_init (&probe_start_region, disk->dev, PED_MAX(start - fuzz, 0), PED_MIN(2 * fuzz, (disk->dev)->length - (start - fuzz))); ped_geometry_init (&probe_end_region, disk->dev, PED_MAX(end - fuzz, 0), PED_MIN(2 * fuzz, (disk->dev)->length - (end - fuzz))); /* I don't believe this is dangerous, but still */ uiquery->need_commit = 1; if (!_rescue_pass (disk, &probe_start_region, &probe_end_region)) goto error; return 1;error: return 0;}intperform_resize (PedDisk *disk, PedPartition *part, PartPos *pos, UIOpts opts){ PedFileSystem *fs; PedConstraint *constraint; PedGeometry new_geom; if (!disk) goto error; if (!part) if (!get_partition (_("Partition"), disk, &part)) goto error; if (!(part->type == PED_PARTITION_EXTENDED || (opts & UI_NO_FS_RESIZE))) { if (opts & UI_WARN_COMMIT && !ask_boolean_question (_("WARNING: This writes all data to disk automatically, continue?"))) return 0; if (uiquery->need_commit) if (!perform_commit(disk,UI_DEFAULT)) goto error; } if (part->type != PED_PARTITION_EXTENDED) { if (!_partition_warn_busy (part)) goto error; } if (!get_position(pos, disk,opts & UI_CUSTOM_VALUES)) goto error; /* FIXME: This is most likely not needed */ //if (!(opts & UI_CUSTOM_VALUES) && (opts & UI_NO_FS_RESIZE)) { if (opts & UI_RESIZE_INEXACT) { if (part->geom.start != pos->start.sector) fuzzify(pos->start.range,disk->dev,NULL, 127, 127); if (part->geom.end != pos->end.sector) fuzzify(pos->end.range,disk->dev,NULL, 127, 127); } //} if (!ped_geometry_init (&new_geom, disk->dev, pos->start.sector, pos->end.sector - pos->start.sector + 1LL)) goto error; snap_to_boundaries (&new_geom, &part->geom, disk, pos->start.range, pos->end.range); /* If the partition is extended or we have UI_NO_FS_RESIZE, don't try to resize the fs... FIXME? */ if (part->type == PED_PARTITION_EXTENDED || (opts & UI_NO_FS_RESIZE)) { /* I really hope this part isn't dangerous */ constraint = constraint_from_start_end (disk->dev, pos->start.range, pos->end.range); if (!ped_disk_set_partition_geom (disk, part, constraint, new_geom.start, new_geom.end)) goto error_destroy_constraint; if (part->type == PED_PARTITION_EXTENDED) ped_partition_set_system (part, NULL); uiquery->need_commit = 1; } else { fs = ped_file_system_open (&part->geom); if (!fs) goto error; constraint = constraint_intersect_and_destroy ( ped_file_system_get_resize_constraint (fs), constraint_from_start_end (disk->dev, pos->start.range, pos->end.range)); /* We will play with the geometry */ uiquery->need_commit = 1; if (!ped_disk_set_partition_geom (disk, part, constraint, new_geom.start, new_geom.end)) goto error_close_fs; if (!ped_file_system_resize (fs, &part->geom, uiquery->timer)) goto error_close_fs; /* may have changed... eg fat16 -> fat32 */ ped_partition_set_system (part, fs->type); ped_file_system_close (fs); ped_disk_commit (disk); uiquery->need_commit = 0; } ped_constraint_destroy (constraint); if (pos->start.range != NULL) ped_geometry_destroy (pos->start.range); if (pos->end.range != NULL) ped_geometry_destroy (pos->end.range); return 1;error_close_fs: ped_file_system_close (fs);error_destroy_constraint: ped_constraint_destroy (constraint);error: if (pos->start.range != NULL) ped_geometry_destroy (pos->start.range); if (pos->end.range != NULL) ped_geometry_destroy (pos->end.range); return 0;}intperform_rm (PedDisk* disk, PedPartition* part){ if (!disk) goto error; if (!part) if (!get_partition (_("Partition"), disk, &part)) goto error; if (!_partition_warn_busy (part)) goto error; if (!_warn_ext_not_empty (part)) goto error; ped_disk_delete_partition (disk, part); uiquery->need_commit = 1; return 1;error: return 0;}intperform_maximize (PedDisk* disk, PedPartition* part){ PedConstraint *constraint = NULL; if (!disk) return 0; if (!part) if (!get_partition(_("Partition"), disk, &part)) return 0; if (!_partition_warn_busy(part)) return 0; constraint = ped_constraint_any(disk->dev); if (!constraint) return 0; if (!ped_disk_maximize_partition(disk,part,constraint)) { ped_constraint_destroy (constraint); return 0; } ped_constraint_destroy(constraint); return 1;}intperform_set_system (PedDisk* disk, PedPartition* part, const PedFileSystemType *type){ if (!disk) return 0; if (!part) if (!get_partition(_("Partition"), disk, &part)) return 0; if (!type) { if (!get_fs_type (_("File system type"), &type, 0)) return 0; } /* FIXME: Is this needed? We don't touch the partition itself */ if (!_partition_warn_busy(part)) return 0; if (!ped_partition_set_system(part,type)) return 0; uiquery->need_commit = 1; return 1;}intperform_set (PedDisk *disk, PedPartition *part, PedPartitionFlag flag, UIOpts opts){ int state; if (!disk) goto error; if (!part) if (!get_partition (_("Partition"), disk, &part)) goto error; if (!flag) if (!get_part_flag (_("Flag to change"), part, &flag)) goto error; state = (ped_partition_get_flag (part, flag) == 0 ? 1 : 0); if (!(opts & UI_FLAG_TOGGLE)) { if (!uiquery->getbool (_("New state"), &state)) goto error; } if (!ped_partition_set_flag (part, flag, state)) goto error; uiquery->need_commit = 1; return 1;error: return 0;}intperform_commit (PedDisk* disk, UIOpts opts){ if (opts & UI_WARN_COMMIT) if (!ask_boolean_question (_("WARNING: This writes all data to disk, continue?"))) return 0; if (ped_disk_commit(disk)) { uiquery->need_commit = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -