summaryrefslogtreecommitdiff
path: root/src/ImageMetaData.cxx
diff options
context:
space:
mode:
authorJohannes Hofmann <Johannes.Hofmann@gmx.de>2008-07-08 19:47:49 +0200
committerJohannes Hofmann <Johannes.Hofmann@gmx.de>2008-07-08 19:47:49 +0200
commit029db5ead7c6f95f788047af2446cf302556f9d5 (patch)
treef59b042c47c97f299ae6e6490afbc94efdcf801b /src/ImageMetaData.cxx
parent22774af9a081bc39d827533e0f532259bd395831 (diff)
fix image saving
Diffstat (limited to 'src/ImageMetaData.cxx')
-rw-r--r--src/ImageMetaData.cxx61
1 files changed, 31 insertions, 30 deletions
diff --git a/src/ImageMetaData.cxx b/src/ImageMetaData.cxx
index fb1cc8c..07a5c1b 100644
--- a/src/ImageMetaData.cxx
+++ b/src/ImageMetaData.cxx
@@ -5,7 +5,9 @@
// of the GNU General Public License, incorporated herein by reference.
#include <stdlib.h>
+#include <unistd.h>
#include <stdio.h>
+#include <errno.h>
#include <math.h>
#include <string.h>
#include <sys/types.h>
@@ -204,29 +206,17 @@ ImageMetaData::load_image_jpgcom(char *name) {
int
ImageMetaData::save_image_jpgcom(char *in_img, char *out_img) {
char * args[32];
- FILE *p, *out;
+ FILE *p;
pid_t pid;
- char buf[1024];
- int status;
- size_t n;
- struct stat in_stat, out_stat;
-
- if (stat(in_img, &in_stat) != 0) {
- perror("stat");
- return 1;
- }
-
- if (stat(out_img, &out_stat) == 0) {
- if (in_stat.st_ino == out_stat.st_ino) {
- fprintf(stderr, "Input image %s and output image %s are the same file\n",
- in_img, out_img);
- return 1;
- }
- }
-
- out = fopen(out_img, "w");
- if (out == NULL) {
- perror("fopen");
+ char buf[1024], tmpname[256];
+ int status, err = 0;
+ ssize_t n;
+ int tmp_fd;
+
+ strncpy(tmpname, "/tmp/gipfelXXXXXX", sizeof(tmpname));
+ tmp_fd = mkstemp(tmpname);
+ if (tmp_fd < 0) {
+ perror("mkstemp");
return 1;
}
@@ -253,21 +243,32 @@ ImageMetaData::save_image_jpgcom(char *in_img, char *out_img) {
if (p) {
while ((n = fread(buf, 1, sizeof(buf), p)) != 0) {
- if (fwrite(buf, 1, n, out) != n) {
- perror("fwrite");
- fclose(out);
- fclose(p);
- waitpid(pid, &status, 0);
+ if (write(tmp_fd, buf, n) != n) {
+ perror("write");
+ err++;
+ break;
}
}
fclose(p);
waitpid(pid, &status, 0);
+ if (WEXITSTATUS(status) != 0)
+ err++;
if (WEXITSTATUS(status) == 127 || WEXITSTATUS(status) == 126)
fprintf(stderr, "%s not found\n", args[0]);
- }
+ } else {
+ err++;
+ }
- fclose(out);
- return 0;
+ close(tmp_fd);
+
+ if (!err) {
+ if (rename(tmpname, out_img) != 0) {
+ perror("rename");
+ err++;
+ }
+ }
+
+ return err != 0;
}
void