From 5408e20d80b87731e90840b5cc983a6717c851a5 Mon Sep 17 00:00:00 2001 From: Johannes Hofmann Date: Thu, 17 Jul 2008 21:12:10 +0200 Subject: copy instead of rename - in case /tmp is another filesystem --- src/ImageMetaData.cxx | 28 ++++++++++++++++++++++++---- src/gipfel.cxx | 3 ++- 2 files changed, 26 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/ImageMetaData.cxx b/src/ImageMetaData.cxx index 07a5c1b..e29bd03 100644 --- a/src/ImageMetaData.cxx +++ b/src/ImageMetaData.cxx @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -259,15 +260,34 @@ ImageMetaData::save_image_jpgcom(char *in_img, char *out_img) { err++; } - close(tmp_fd); - if (!err) { - if (rename(tmpname, out_img) != 0) { - perror("rename"); + int out_fd = open(out_img, O_WRONLY|O_TRUNC); + + lseek(tmp_fd, 0, SEEK_SET); + + if (out_fd == -1) { + perror("open"); err++; + } else { + while ((n = read(tmp_fd, buf, sizeof(buf)))) { + if (write(out_fd, buf, n) != n) { + perror("write"); + fprintf(stderr, "ERROR: Image %s might be corrupted. " + "A copy is still available at %s\n", out_img, tmpname); + err++; + break; + } + } + + close(out_fd); } } + close(tmp_fd); + + if (!err) + unlink(tmpname); + return err != 0; } diff --git a/src/gipfel.cxx b/src/gipfel.cxx index bcf4816..788d22b 100644 --- a/src/gipfel.cxx +++ b/src/gipfel.cxx @@ -118,7 +118,8 @@ void track_cb() { void save_cb() { char *file = fl_file_chooser("Save Image As?", NULL, img_file); if (file) - gipf->save_image(file); + if (gipf->save_image(file)) + fl_message("ERROR: Saving image %s failed.", file); } void focal_length_cb(Fl_Slider* o, void*) { -- cgit v1.2.3