diff --git a/gptfixer/gpt.c b/gptfixer/gpt.c index 341e3487..ec305fcd 100644 --- a/gptfixer/gpt.c +++ b/gptfixer/gpt.c @@ -397,31 +397,6 @@ struct GPT *gpt_shrink_sectors(uint64_t sector_offset, uint32_t sector_size, return new_gpt; } -static void write_and_free_gpt(int fd, struct GPT *gpt, uint32_t sector_size, - uint64_t size) { - if (size == 0) - abort(); - void *entries = gpt->entries; - assert(entries != NULL); - gpt->used_entries = 0; - gpt->entries = NULL; - ssize_t res = - pwrite64(fd, gpt, sector_size, (off_t)(gpt->header.MyLBA * sector_size)); - if (res != sector_size) - err(1, "short write or write error"); - if (fsync(fd)) - err(1, "fsync() failed"); - warnx("Writing 0x%" PRIX64 " bytes", size); - res = pwrite64(fd, entries, size, - (off_t)(gpt->header.PartitionEntryLBA * sector_size)); - if (res < 0) - err(1, "write error"); - if ((size_t)res != size) - err(1, "short write or write error"); - free(gpt); - free(entries); -} - /* Next up: Process GUID partition table (GPT) */ static uint64_t check_gpt_header(const struct GPTHeader *header, uint64_t sector_offset, uint32_t sector_size, @@ -578,9 +553,8 @@ static uint64_t check_gpt_header(const struct GPTHeader *header, return partition_table_bytes; } -static struct GPT * -read_and_check_gpt(int fd, uint64_t sector_offset, uint32_t sector_size, - uint64_t sectors) { +static struct GPT *read_and_check_gpt(int fd, uint64_t sector_offset, + uint32_t sector_size, uint64_t sectors) { char *header_buffer = NULL, *entries_buffer = NULL; if (sector_offset >= sectors) { @@ -782,7 +756,28 @@ static void create_new_gpt(int fd, bool used_primary, uint64_t sectors, assert(new_gpt->entries); print_gpts(new_gpt); assert(new_gpt->entries); - write_and_free_gpt(fd, new_gpt, sector_size, to_write); + if (to_write == 0) + abort(); + void *entries = new_gpt->entries; + assert(entries != NULL); + new_gpt->used_entries = 0; + new_gpt->entries = NULL; + ssize_t res = pwrite64(fd, new_gpt, sector_size, + (off_t)(new_gpt->header.MyLBA * sector_size)); + if (res != sector_size) + err(1, "short write or write error"); + if (fsync(fd)) + err(1, "fsync() failed"); + res = pwrite64(fd, entries, to_write, + (off_t)(new_gpt->header.PartitionEntryLBA * sector_size)); + if (res < 0) + err(1, "write error"); + if ((size_t)res != to_write) + err(1, "short write or write error"); + if (fsync(fd)) + err(1, "fsync() failed"); + free(new_gpt); + free(entries); } int main(int argc, char **argv) {