From: Karel Zak Date: Fri, 31 Jan 2020 10:29:12 +0000 (+0100) Subject: libfdisk: (gpt) cleanup and consolidate write code X-Git-Tag: v2.36-rc1~235 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=afb27a8e4c311e621f9258f6e415fb8266fbd88f;p=thirdparty%2Futil-linux.git libfdisk: (gpt) cleanup and consolidate write code - add fsync after write - use write_all() - use the same code for all write steps Signed-off-by: Karel Zak --- diff --git a/libfdisk/src/gpt.c b/libfdisk/src/gpt.c index fe778104f3..68d346ce8e 100644 --- a/libfdisk/src/gpt.c +++ b/libfdisk/src/gpt.c @@ -1894,7 +1894,20 @@ static int gpt_set_partition(struct fdisk_context *cxt, size_t n, return rc; } +static int gpt_write(struct fdisk_context *cxt, off_t offset, void *buf, size_t count) +{ + if (offset != lseek(cxt->dev_fd, offset, SEEK_SET)) + return -errno; + + if (write_all(cxt->dev_fd, buf, count)) + return -errno; + + fsync(cxt->dev_fd); + DBG(LABEL, ul_debug("GPT write OK [offset=%zu, size=%zu]", + (size_t) offset, count)); + return 0; +} /* * Write partitions. @@ -1903,23 +1916,16 @@ static int gpt_set_partition(struct fdisk_context *cxt, size_t n, static int gpt_write_partitions(struct fdisk_context *cxt, struct gpt_header *header, unsigned char *ents) { - off_t offset = (off_t) le64_to_cpu(header->partition_entry_lba) * cxt->sector_size; - size_t towrite = 0; - ssize_t ssz; + size_t esz = 0; int rc; - rc = gpt_sizeof_entries(header, &towrite); + rc = gpt_sizeof_entries(header, &esz); if (rc) return rc; - if (offset != lseek(cxt->dev_fd, offset, SEEK_SET)) - return -errno; - - ssz = write(cxt->dev_fd, ents, towrite); - if (ssz < 0 || (ssize_t) towrite != ssz) - return -errno; - - return 0; + return gpt_write(cxt, + (off_t) le64_to_cpu(header->partition_entry_lba) * cxt->sector_size, + ents, esz); } /* @@ -1933,15 +1939,7 @@ static int gpt_write_partitions(struct fdisk_context *cxt, static int gpt_write_header(struct fdisk_context *cxt, struct gpt_header *header, uint64_t lba) { - off_t offset = lba * cxt->sector_size; - - if (offset != lseek(cxt->dev_fd, offset, SEEK_SET)) - goto fail; - if (cxt->sector_size == - (size_t) write(cxt->dev_fd, header, cxt->sector_size)) - return 0; -fail: - return -errno; + return gpt_write(cxt, lba * cxt->sector_size, header, cxt->sector_size); } /* @@ -1950,7 +1948,6 @@ fail: */ static int gpt_write_pmbr(struct fdisk_context *cxt) { - off_t offset; struct gpt_legacy_mbr *pmbr; assert(cxt); @@ -1980,16 +1977,9 @@ static int gpt_write_pmbr(struct fdisk_context *cxt) pmbr->partition_record[0].size_in_lba = cpu_to_le32((uint32_t) (cxt->total_sectors - 1ULL)); - offset = GPT_PMBR_LBA * cxt->sector_size; - if (offset != lseek(cxt->dev_fd, offset, SEEK_SET)) - goto fail; - /* pMBR covers the first sector (LBA) of the disk */ - if (write_all(cxt->dev_fd, pmbr, cxt->sector_size)) - goto fail; - return 0; -fail: - return -errno; + return gpt_write(cxt, GPT_PMBR_LBA * cxt->sector_size, + pmbr, cxt->sector_size); } /*