void resize_map(unsigned long new_size, partition_map_header *map) { partition_map * entry; partition_map * next; unsigned int incr; // find map entry entry = find_entry_by_type(kMapType, map); if (entry == NULL) { printf("Couldn't find entry for map!\n"); return; } next = entry->next_by_base; // same size if (new_size == entry->data->dpme_pblocks) { // do nothing return; } // make it smaller if (new_size < entry->data->dpme_pblocks) { if (next == NULL || istrncmp(next->data->dpme_type, kFreeType, DPISTRLEN) != 0) { incr = 1; } else { incr = 0; } if (new_size < map->blocks_in_map + incr) { printf("New size would be too small\n"); return; } goto doit; } // make it larger if (next == NULL || istrncmp(next->data->dpme_type, kFreeType, DPISTRLEN) != 0) { printf("No free space to expand into\n"); return; } if (entry->data->dpme_pblock_start + entry->data->dpme_pblocks != next->data->dpme_pblock_start) { printf("No contiguous free space to expand into\n"); return; } if (new_size > entry->data->dpme_pblocks + next->data->dpme_pblocks) { printf("No enough free space\n"); return; } doit: entry->data->dpme_type[0] = 0; delete_partition_from_map(entry); add_partition_to_map("Apple", kMapType, 1, new_size, map); map->maximum_in_map = new_size; }
partition_map_header * init_partition_map(char *name, partition_map_header* oldmap) { partition_map_header *map; if (oldmap != NULL) { printf("map already exists\n"); if (get_okay("do you want to reinit? [n/y]: ", 0) != 1) { return oldmap; } } map = create_partition_map(name, oldmap); if (map == NULL) { return oldmap; } close_partition_map(oldmap); add_partition_to_map("Apple", kMapType, 1, (map->media_size <= 128? 2: 63), map); return map; }
void do_create_partition(partition_map_header *map, int get_type) { long base; long length; char *name = 0; char *type_name = 0; if (map == NULL) { bad_input("No partition map exists"); return; } if (!rflag && map->writable == 0) { printf("The map is not writable.\n"); } // XXX add help feature (i.e. '?' in any argument routine prints help string) if (get_base_argument(&base, map) == 0) { return; } if (get_size_argument(&length, map) == 0) { return; } if (get_string_argument("Name of partition: ", &name, 1) == 0) { bad_input("Bad name"); return; } if (get_type == 0) { add_partition_to_map(name, kUnixType, base, length, map); #if 0 /* this check is not found in linux fdisk-0.1 */ if (map->blocks_in_map > MAX_LINUX_MAP) { error(-1, "Map contains more than %d blocks - Linux may have trouble", MAX_LINUX_MAP); } goto xit1; #endif } else if (get_string_argument("Type of partition: ", &type_name, 1) == 0) { bad_input("Bad type"); goto xit1; } else { if (istrncmp(type_name, kFreeType, DPISTRLEN) == 0) { bad_input("Can't create a partition with the Free type"); goto xit2; } if (istrncmp(type_name, kMapType, DPISTRLEN) == 0) { bad_input("Can't create a partition with the Map type"); goto xit2; } add_partition_to_map(name, type_name, base, length, map); #if 0 /* this check is not found in linux fdisk-0.1 */ if (map->blocks_in_map > MAX_LINUX_MAP) { error(-1, "Map contains more than %d blocks - Linux may have trouble", MAX_LINUX_MAP); } #endif } do_update_dpme(find_entry_by_base(base,map)); xit2: if (type_name) free(type_name); xit1: if (name) free(name); return; }