Use ftruncate on Unix to create hard disk images quickly

Instead of writing out disk blocks slowly across the entire volume,
just use the ftruncate function to create a file instantly at the
desired size.

Depending on file system, this can either result in identical results
to the old code just faster (eg: ZFS and btrfs with compression
enabled), sparse files (most native Unix file systems without
compression, eg ext4 and UFS), or a full non-sparse file like before
(creating an image on FAT).
This commit is contained in:
Mike Swanson
2024-09-02 22:52:24 -07:00
parent ca880a3bbb
commit 00354749f2
2 changed files with 24 additions and 0 deletions

View File

@@ -26,6 +26,9 @@
#include <time.h>
#include <wchar.h>
#include <errno.h>
#ifdef __unix__
#include <unistd.h>
#endif
#define HAVE_STDARG_H
#include <86box/86box.h>
#include <86box/path.h>
@@ -183,6 +186,7 @@ prepare_new_hard_disk(uint8_t id, uint64_t full_size)
{
uint64_t target_size = (full_size + hdd_images[id].base) - ftello64(hdd_images[id].file);
#ifndef __unix__
uint32_t size;
uint32_t t;
@@ -217,7 +221,16 @@ prepare_new_hard_disk(uint8_t id, uint64_t full_size)
pclog_toggle_suppr();
free(empty_sector_1mb);
#else
pclog("Creating hard disk image: ");
int ret = ftruncate(fileno(hdd_images[id].file), (size_t) target_size);
if (ret) {
pclog("failed\n");
fatal("Could not create hard disk image\n");
}
pclog("OK!\n");
#endif
hdd_images[id].last_sector = (uint32_t) (full_size >> 9) - 1;
hdd_images[id].loaded = 1;