From b4d1c6165b3f59a43cb93a3edbd7860b456bb9a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Pierre=20Andr=C3=A9?= Date: Wed, 23 Mar 2011 09:35:24 +0100 Subject: [PATCH] Used a stream to produce aligned writes in image creation When creating a partition image, ntfsclone write an extra byte to describe each cluster, this causes two unneeded ntfs-3g calls per cluster, and inefficiency when imaging to ntfs. --- ntfsprogs/ntfsclone.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/ntfsprogs/ntfsclone.c b/ntfsprogs/ntfsclone.c index 67864ca6..41a866ff 100644 --- a/ntfsprogs/ntfsclone.c +++ b/ntfsprogs/ntfsclone.c @@ -153,6 +153,7 @@ static struct bitmap lcn_bitmap; static int fd_in; static int fd_out; +static FILE *stream_out = (FILE*)NULL; static FILE *msg_out = NULL; static int wipe = 0; @@ -539,9 +540,12 @@ static int io_all(void *fd, void *buf, int count, int do_write) struct ntfs_device *dev = fd; while (count > 0) { - if (do_write) - i = write(*(int *)fd, buf, count); - else if (opt.restore_image) + if (do_write) { + if (opt.save_image) + i = fwrite(buf, 1, count, stream_out); + else + i = write(*(int *)fd, buf, count); + } else if (opt.restore_image) i = read(*(int *)fd, buf, count); else i = dev->d_ops->read(dev, buf, count); @@ -1553,6 +1557,8 @@ static s64 device_size_get(int fd) static void fsync_clone(int fd) { Printf("Syncing ...\n"); + if (opt.save_image && stream_out && fflush(stream_out)) + perr_exit("fflush"); if (fsync(fd) && errno != EINVAL) perr_exit("fsync"); } @@ -1877,6 +1883,7 @@ int main(int argc, char **argv) if (opt.std_out) { if ((fd_out = fileno(stdout)) == -1) perr_exit("fileno for stdout failed"); + stream_out = stdout; } else { /* device_size_get() might need to read() */ int flags = O_RDWR; @@ -1887,8 +1894,17 @@ int main(int argc, char **argv) flags |= O_EXCL; } - if ((fd_out = open(opt.output, flags, S_IRUSR | S_IWUSR)) == -1) - perr_exit("Opening file '%s' failed", opt.output); + if (opt.save_image) { + stream_out = fopen(opt.output,"w"); + if (!stream_out) + perr_exit("Opening file '%s' failed", + opt.output); + fd_out = fileno(stream_out); + } else + if ((fd_out = open(opt.output, flags, + S_IRUSR | S_IWUSR)) == -1) + perr_exit("Opening file '%s' failed", + opt.output); if (!opt.save_image) check_output_device(ntfs_size); @@ -1924,6 +1940,8 @@ int main(int argc, char **argv) clone_ntfs(nr_clusters_to_save); fsync_clone(fd_out); + if (opt.save_image) + fclose(stream_out); ntfs_umount(vol,FALSE); free(lcn_bitmap.bm); exit(0);