X-Git-Url: https://git.sven.stormbind.net/?p=sven%2Fexfat-utils.git;a=blobdiff_plain;f=mkfs%2Fmain.c;h=885dd71263a5dd5b9ec69064d2ebe9d66e7b89fe;hp=8deca1597a02dfc3d0ab8c507d1beb44690aabc1;hb=1f5c4d900674e89576886f971ba292ad14f947f7;hpb=dbe4348df2ba88986e13163ef13d09fb7247589d diff --git a/mkfs/main.c b/mkfs/main.c index 8deca15..885dd71 100644 --- a/mkfs/main.c +++ b/mkfs/main.c @@ -46,7 +46,7 @@ struct exfat_structure int order; off_t (*get_alignment)(void); off_t (*get_size)(void); - int (*write_data)(off_t, int); + int (*write_data)(struct exfat_dev*, off_t); }; static int init_sb(off_t volume_size, int sector_bits, int spc_bits, @@ -99,7 +99,7 @@ static int init_sb(off_t volume_size, int sector_bits, int spc_bits, return 0; } -static int erase_device(int fd) +static int erase_device(struct exfat_dev* dev) { off_t erase_size; off_t erase_blocks; @@ -123,7 +123,7 @@ static int erase_device(int fd) erase_blocks = DIV_ROUND_UP(erase_size, block_size); - if (lseek(fd, 0, SEEK_SET) == (off_t) -1) + if (exfat_seek(dev, 0, SEEK_SET) == (off_t) -1) { exfat_error("seek failed"); return 1; @@ -139,7 +139,7 @@ static int erase_device(int fd) for (i = 0; i < erase_blocks; i++) { - if (write(fd, block, block_size) == -1) + if (exfat_write(dev, block, block_size) < 0) { free(block); exfat_error("failed to erase block %"PRIu64, i); @@ -183,20 +183,20 @@ static struct exfat_structure structures[] = }; #undef FS_OBJECT -static off_t write_structure(int fd, struct exfat_structure* structure, - off_t current) +static off_t write_structure(struct exfat_dev* dev, + struct exfat_structure* structure, off_t current) { off_t alignment = structure->get_alignment(); off_t base = ROUND_UP(current, alignment); - if (lseek(fd, base, SEEK_SET) == (off_t) -1) + if (exfat_seek(dev, base, SEEK_SET) == (off_t) -1) { exfat_error("seek to %"PRIu64" failed", base); return -1; } if (structure->order > 0) { - int rc = structure->write_data(base, fd); + int rc = structure->write_data(dev, base); if (rc != 0) { exfat_error("%s creation failed: %s", structure->name, @@ -208,7 +208,7 @@ static off_t write_structure(int fd, struct exfat_structure* structure, return base + structure->get_size(); } -static int write_structures(int fd) +static int write_structures(struct exfat_dev* dev) { off_t current; size_t i; @@ -220,7 +220,7 @@ static int write_structures(int fd) remainder = 0; for (i = 0; i < sizeof(structures) / sizeof(structures[0]); i++) { - current = write_structure(fd, &structures[i], current); + current = write_structure(dev, &structures[i], current); if (current == (off_t) -1) return 1; remainder += structures[i].order; @@ -243,7 +243,7 @@ static int get_spc_bits(int user_defined, off_t volume_size) return 8; /* 128 KB */ } -static int set_volume_label(int fd, const char* volume_label) +static int set_volume_label(const char* volume_label) { le16_t tmp[EXFAT_ENAME_MAX + 1]; @@ -253,10 +253,7 @@ static int set_volume_label(int fd, const char* volume_label) memset(tmp, 0, sizeof(tmp)); if (utf8_to_utf16(tmp, volume_label, EXFAT_ENAME_MAX, strlen(volume_label)) != 0) - { - close(fd); return 1; - } memcpy(label_entry.name, tmp, EXFAT_ENAME_MAX * sizeof(le16_t)); label_entry.length = utf16_length(tmp); label_entry.type |= EXFAT_ENTRY_VALID; @@ -278,32 +275,32 @@ static uint32_t get_volume_serial(uint32_t user_defined) static int mkfs(const char* spec, int sector_bits, int spc_bits, const char* volume_label, uint32_t volume_serial, int first_sector) { - int fd; + struct exfat_dev* dev; off_t volume_size; - fd = exfat_open(spec, 0); - if (fd < 0) + dev = exfat_open(spec, 0); + if (dev == NULL) return 1; - volume_size = lseek(fd, 0, SEEK_END); + volume_size = exfat_seek(dev, 0, SEEK_END); if (volume_size == (off_t) -1) { - close(fd); + exfat_close(dev); exfat_error("seek failed"); return 1; } spc_bits = get_spc_bits(spc_bits, volume_size); - if (set_volume_label(fd, volume_label) != 0) + if (set_volume_label(volume_label) != 0) { - close(fd); + exfat_close(dev); return 1; } volume_serial = get_volume_serial(volume_serial); if (volume_serial == 0) { - close(fd); + exfat_close(dev); exfat_error("failed to get current time to form volume id"); return 1; } @@ -311,38 +308,34 @@ static int mkfs(const char* spec, int sector_bits, int spc_bits, if (init_sb(volume_size, sector_bits, spc_bits, volume_serial, first_sector) != 0) { - close(fd); + exfat_close(dev); return 1; } printf("Creating... %2u%%", 0); fflush(stdout); - if (erase_device(fd) != 0) + if (erase_device(dev) != 0) { - close(fd); + exfat_close(dev); return 1; } - if (write_structures(fd) != 0) + if (write_structures(dev) != 0) { - close(fd); + exfat_close(dev); return 1; } puts("\b\b\b\bdone."); printf("Flushing... "); fflush(stdout); - if (fsync(fd) < 0) + if (exfat_fsync(dev) != 0) { - close(fd); - exfat_error("fsync failed"); + exfat_close(dev); return 1; } puts("done."); - if (close(fd) < 0) - { - exfat_error("close failed"); + if (exfat_close(dev) != 0) return 1; - } printf("File system created successfully.\n"); return 0; }