Skip to content

Commit

Permalink
More partition fixer fixes
Browse files Browse the repository at this point in the history
Add a missing fsync() and inline a function into its only caller.
  • Loading branch information
DemiMarie committed Nov 17, 2024
1 parent b2587f4 commit b3e1f4d
Showing 1 changed file with 24 additions and 29 deletions.
53 changes: 24 additions & 29 deletions gptfixer/gpt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit b3e1f4d

Please sign in to comment.