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

@@ -20,6 +20,9 @@
#include "ui_qt_harddiskdialog.h"
extern "C" {
#ifdef __unix__
#include <unistd.h>
#endif
#include <86box/86box.h>
#include <86box/hdd.h>
#include "../disk/minivhd/minivhd.h"
@@ -447,6 +450,7 @@ HarddiskDialog::onCreateNewFile()
}
// formats 0, 1 and 2
#ifndef __unix__
connect(this, &HarddiskDialog::fileProgress, this, [this](int value) { ui->progressBar->setValue(value); QApplication::processEvents(); });
ui->progressBar->setVisible(true);
[size, &file, this] {
@@ -469,6 +473,13 @@ HarddiskDialog::onCreateNewFile()
}
emit fileProgress(100);
}();
#else
int ret = ftruncate(file.handle(), (size_t) size);
if (ret) {
QMessageBox::critical(this, tr("Unable to write file"), tr("Make sure the file is being saved to a writable directory."));
}
#endif
QMessageBox::information(this, tr("Disk image created"), tr("Remember to partition and format the newly-created drive."));
setResult(QDialog::Accepted);