--- jpeg-6b/cjpeg.c.bak 1998-03-21 03:21:49.000000000 +0200 +++ jpeg-6b/cjpeg.c 2007-08-14 14:56:31.675979339 +0300 @@ -36,6 +36,8 @@ #endif #endif +#include +#include /* Create the add-on message string table. */ @@ -75,6 +77,7 @@ static const char * const cdjpeg_message static boolean is_targa; /* records user -targa switch */ +static boolean do_fsync; LOCAL(cjpeg_source_ptr) select_file_type (j_compress_ptr cinfo, FILE * infile) @@ -179,6 +182,7 @@ usage (void) #endif fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n"); fprintf(stderr, " -outfile name Specify name for output file\n"); + fprintf(stderr, " -fsync fsync output file prior to closing\n"); fprintf(stderr, " -verbose or -debug Emit debug output\n"); fprintf(stderr, "Switches for wizards:\n"); #ifdef C_ARITH_CODING_SUPPORTED @@ -316,6 +320,10 @@ parse_switches (j_compress_ptr cinfo, in usage(); outfilename = argv[argn]; /* save it away for later use */ + } else if (keymatch(arg, "fsync", 2)) { + /* Set fsync flag */ + do_fsync = 1; + } else if (keymatch(arg, "progressive", 1)) { /* Select simple progressive mode. */ #ifdef C_PROGRESSIVE_SUPPORTED @@ -470,6 +478,7 @@ main (int argc, char **argv) FILE * input_file; FILE * output_file; JDIMENSION num_scanlines; + int ioerror = 0; /* On Mac, fetch a command line. */ #ifdef USE_CCOMMAND @@ -593,14 +602,30 @@ main (int argc, char **argv) /* Close files, if we opened them */ if (input_file != stdin) fclose(input_file); + + if (fflush(output_file) == EOF) { + fprintf(stderr, "%s: failed to write to %s: %s\n", + progname, outfilename, strerror(errno)); + ioerror = 1; + } + if (do_fsync && (fsync(fileno(output_file)) == -1)) { + if (errno != EINVAL) { + fprintf(stderr, "%s: failed to fsync %s: %s\n", + progname, outfilename, strerror(errno)); + ioerror = 1; + } + } if (output_file != stdout) - fclose(output_file); + if (fclose(output_file) == EOF) { + fprintf(stderr, "%s: failed to close %s: %s\n", + progname, outfilename, strerror(errno)); + ioerror = 1; + } #ifdef PROGRESS_REPORT end_progress_monitor((j_common_ptr) &cinfo); #endif /* All done. */ - exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS); - return 0; /* suppress no-return-value warnings */ + exit((jerr.num_warnings || ioerror) ? EXIT_WARNING : EXIT_SUCCESS); } --- jpeg-6b/djpeg.c.bak 1997-10-12 01:29:07.000000000 +0300 +++ jpeg-6b/djpeg.c 2007-08-14 15:01:49.525689810 +0300 @@ -38,6 +38,8 @@ #endif #endif +#include +#include /* Create the add-on message string table. */ @@ -48,6 +50,7 @@ static const char * const cdjpeg_message NULL }; +static boolean do_fsync; /* * This list defines the known output image formats @@ -153,6 +156,7 @@ usage (void) #endif fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n"); fprintf(stderr, " -outfile name Specify name for output file\n"); + fprintf(stderr, " -fsync fsync output file prior to closing\n"); fprintf(stderr, " -verbose or -debug Emit debug output\n"); exit(EXIT_FAILURE); } @@ -314,6 +318,10 @@ parse_switches (j_decompress_ptr cinfo, usage(); outfilename = argv[argn]; /* save it away for later use */ + } else if (keymatch(arg, "fsync", 2)) { + /* Set fsync flag */ + do_fsync = 1; + } else if (keymatch(arg, "pnm", 1) || keymatch(arg, "ppm", 1)) { /* PPM/PGM output format. */ requested_fmt = FMT_PPM; @@ -433,6 +441,7 @@ main (int argc, char **argv) FILE * input_file; FILE * output_file; JDIMENSION num_scanlines; + int ioerror = 0; /* On Mac, fetch a command line. */ #ifdef USE_CCOMMAND @@ -603,14 +612,29 @@ main (int argc, char **argv) /* Close files, if we opened them */ if (input_file != stdin) fclose(input_file); + if (fflush(output_file) == EOF) { + fprintf(stderr, "%s: failed to write to %s: %s\n", + progname, outfilename, strerror(errno)); + ioerror = 1; + } + if (do_fsync && (fsync(fileno(output_file)) == -1)) { + if (errno != EINVAL) { + fprintf(stderr, "%s: failed to fsync %s: %s\n", + progname, outfilename, strerror(errno)); + ioerror = 1; + } + } if (output_file != stdout) - fclose(output_file); + if (fclose(output_file) == EOF) { + fprintf(stderr, "%s: failed to close %s: %s\n", + progname, outfilename, strerror(errno)); + ioerror = 1; + } #ifdef PROGRESS_REPORT end_progress_monitor((j_common_ptr) &cinfo); #endif /* All done. */ - exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS); - return 0; /* suppress no-return-value warnings */ + exit((jerr.num_warnings || ioerror) ? EXIT_WARNING : EXIT_SUCCESS); } --- libjpeg-turbo-1.0.90/jpegtran.c.orig 2011-01-06 01:42:59.000000000 +0200 +++ libjpeg-turbo-1.0.90/jpegtran.c 2011-02-04 11:15:46.146749424 +0200 @@ -27,6 +27,8 @@ #endif #endif +#include +#include /* * Argument-parsing code. @@ -41,7 +43,8 @@ static const char * progname; /* program static char * outfilename; /* for -outfile switch */ static JCOPY_OPTION copyoption; /* -copy switch */ static jpeg_transform_info transformoption; /* image transformation options */ - +static int do_fsync; +static int do_temp; LOCAL(void) usage (void) @@ -81,6 +84,8 @@ usage (void) fprintf(stderr, " -restart N Set restart interval in rows, or in blocks with B\n"); fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n"); fprintf(stderr, " -outfile name Specify name for output file\n"); + fprintf(stderr, " -fsync fsync output file prior to closing\n"); + fprintf(stderr, " -temp write output to temp file and replace original input\n"); fprintf(stderr, " -verbose or -debug Emit debug output\n"); fprintf(stderr, "Switches for wizards:\n"); #ifdef C_ARITH_CODING_SUPPORTED @@ -257,7 +262,12 @@ parse_switches (j_compress_ptr cinfo, in if (++argn >= argc) /* advance to next argument */ usage(); outfilename = argv[argn]; /* save it away for later use */ - + } else if (keymatch(arg, "fsync", 2)) { + /* Set fsync flag */ + do_fsync = 1; + } else if (keymatch(arg, "temp", 2)) { + /* Set temp flag */ + do_temp = 1; } else if (keymatch(arg, "perfect", 2)) { /* Fail if there is any partial edge MCUs that the transform can't * handle. */ @@ -372,10 +382,13 @@ main (int argc, char **argv) jvirt_barray_ptr * src_coef_arrays; jvirt_barray_ptr * dst_coef_arrays; int file_index; + int inputfile_index; /* We assume all-in-memory processing and can therefore use only a * single file pointer for sequential input and output operation. */ FILE * fp; + int ioerror = 0; + char outtemp[4096]; /* On Mac, fetch a command line. */ #ifdef USE_CCOMMAND @@ -442,7 +455,13 @@ main (int argc, char **argv) fprintf(stderr, "%s: can't open %s for reading\n", progname, argv[file_index]); exit(EXIT_FAILURE); } + inputfile_index = file_index; } else { + if (do_temp) { + fprintf(stderr, "%s: temp mode can not be used when reading " + "from stdin\n", progname); + exit(EXIT_FAILURE); + } /* default input file is stdin */ fp = read_stdin(); } @@ -501,13 +520,35 @@ main (int argc, char **argv) /* Open the output file. */ if (outfilename != NULL) { - if ((fp = fopen(outfilename, WRITE_BINARY)) == NULL) { + if (do_temp) { + fprintf(stderr, "%s: outfile parameter incompatible with temp mode\n", progname); + exit(EXIT_FAILURE); + } else if ((fp = fopen(outfilename, WRITE_BINARY)) == NULL) { fprintf(stderr, "%s: can't open %s for writing\n", progname, outfilename); exit(EXIT_FAILURE); } } else { - /* default output file is stdout */ - fp = write_stdout(); + if (do_temp) { + int fd; + + snprintf(outtemp, sizeof(outtemp), "%s.tmp.XXXXXX", argv[inputfile_index]); + fd = mkstemp(outtemp); + if (fd == -1) { + fprintf(stderr, "%s: can't open temporary file: %s\n", + progname, strerror(errno)); + exit(EXIT_FAILURE); + } + fp = fdopen(fd, WRITE_BINARY); + outfilename = outtemp; + if (fp == NULL) { + fprintf(stderr, "%s: can't fdopen temporary file: %s\n", + progname, strerror(errno)); + exit(EXIT_FAILURE); + } + } else { + /* default output file is stdout */ + fp = write_stdout(); + } } /* Adjust default compression parameters by re-parsing the options */ @@ -535,9 +576,32 @@ main (int argc, char **argv) (void) jpeg_finish_decompress(&srcinfo); jpeg_destroy_decompress(&srcinfo); - /* Close output file, if we opened it */ - if (fp != stdout) - fclose(fp); + if (fflush(fp) == EOF) { + fprintf(stderr, "%s: failed to write to %s: %s\n", + progname, outfilename, strerror(errno)); + ioerror = 1; + } + if (do_fsync && (fsync(fileno(fp)) == -1)) { + if (errno != EINVAL) { + fprintf(stderr, "%s: failed to fsync %s: %s\n", + progname, outfilename, strerror(errno)); + ioerror = 1; + } + } + if (fp != stdout) { + if (fclose(fp) == EOF) { + fprintf(stderr, "%s: failed to close %s: %s\n", + progname, outfilename, strerror(errno)); + ioerror = 1; + } + if (do_temp) { + if (rename(outtemp, argv[inputfile_index]) == -1) { + fprintf(stderr, "%s: failed to rename %s to %s: %s\n", + progname, outtemp, argv[inputfile_index], strerror(errno)); + ioerror = 1; + } + } + } #ifdef PROGRESS_REPORT end_progress_monitor((j_common_ptr) &dstinfo);