diff options
author | Andreas Gruenbacher <agruen@suse.de> | 2010-09-16 13:25:32 +0200 |
---|---|---|
committer | Andreas Gruenbacher <agruen@suse.de> | 2010-09-17 02:06:25 +0200 |
commit | fdafb60cc0d920fe3f31965013235fbd7fc9d592 (patch) | |
tree | df1acf269ae90100a022c8b5682b708419db5054 | |
parent | a1a66e6032c293c9ea8885d2ff303061331e3cd6 (diff) | |
download | patch-fdafb60cc0d920fe3f31965013235fbd7fc9d592.tar.gz |
Stop using mktemp() for TMPOUTNAME
* src/patch.c (main): Compute TMPOUTNAME with make_tempfile().
(make_temp): Remove obsolete function.
-rw-r--r-- | ChangeLog | 3 | ||||
-rw-r--r-- | src/patch.c | 68 |
2 files changed, 23 insertions, 48 deletions
@@ -22,6 +22,9 @@ (abort_hunk, init_reject): Pass through the output file name, and base TMPREJNAME on it. + * src/patch.c (main): Compute TMPOUTNAME with make_tempfile(). + (make_temp): Remove obsolete function. + 2010-07-27 Tim Waugh <twaugh@redhat.com> * src/patch.c: Stops "patch --get 1" from segfaulting. diff --git a/src/patch.c b/src/patch.c index acc4f56..ebf2a26 100644 --- a/src/patch.c +++ b/src/patch.c @@ -39,7 +39,6 @@ static lin locate_hunk (lin); static bool apply_hunk (struct outstate *, lin); static bool patch_match (lin, lin, lin, lin); static bool spew_output (struct outstate *, struct stat *); -static char const *make_temp (char); static int numeric_string (char const *, bool, char const *); static void cleanup (void); static void get_some_switches (void); @@ -104,6 +103,7 @@ main (int argc, char **argv) bool written_to_rejname = false; bool apply_empty_patch = false; mode_t file_type; + int outfd = -1; exit_failure = 2; program_name = argv[0]; @@ -137,10 +137,6 @@ main (int argc, char **argv) else if ((version_control = getenv ("VERSION_CONTROL"))) version_control_context = "$VERSION_CONTROL"; - /* Cons up the names of the global temporary files. - Do this before `cleanup' can possibly be called (e.g. by `pfatal'). */ - TMPOUTNAME = make_temp ('o'); - /* parse switches */ Argc = argc; Argv = argv; @@ -192,6 +188,15 @@ main (int argc, char **argv) } remove_if_needed (TMPREJNAME, &TMPREJNAME_needs_removal); } + if (TMPOUTNAME_needs_removal) + { + if (outfd != -1) + { + close (outfd); + outfd = -1; + } + remove_if_needed (TMPOUTNAME, &TMPOUTNAME_needs_removal); + } if (! skip_rest_of_patch && ! file_type) { @@ -233,6 +238,9 @@ main (int argc, char **argv) } } + outfd = make_tempfile (&TMPOUTNAME, 'o', outname, + O_WRONLY | binary_transput, instat.st_mode); + TMPOUTNAME_needs_removal = 1; if (diff_type == ED_DIFF) { outstate.zero_output = false; somefailed |= skip_rest_of_patch; @@ -241,10 +249,12 @@ main (int argc, char **argv) if (! dry_run && ! outfile && ! skip_rest_of_patch) { struct stat statbuf; - if (stat (TMPOUTNAME, &statbuf) != 0) + if (fstat (outfd, &statbuf) != 0) pfatal ("%s", TMPOUTNAME); outstate.zero_output = statbuf.st_size == 0; } + close (outfd); + outfd = -1; } else { int got_hunk; bool apply_anyway = merge; /* don't try to reverse when merging */ @@ -258,10 +268,10 @@ main (int argc, char **argv) /* initialize the patched file */ if (! skip_rest_of_patch && ! outfile) { - int exclusive = TMPOUTNAME_needs_removal ? 0 : O_EXCL; - TMPOUTNAME_needs_removal = 1; init_output (&outstate); - outstate.ofp = create_output_file (TMPOUTNAME, exclusive); + outstate.ofp = fdopen(outfd, binary_transput ? "wb" : "w"); + if (! outstate.ofp) + pfatal ("%s", TMPOUTNAME); } /* find out where all the lines are */ @@ -337,6 +347,7 @@ main (int argc, char **argv) { fclose (outstate.ofp); outstate.ofp = 0; + outfd = -1; } } } @@ -1582,45 +1593,6 @@ similar (char const *a, size_t alen, char const *b, size_t blen) } } -/* Make a temporary file. */ - -#if HAVE_MKTEMP && ! HAVE_DECL_MKTEMP && ! defined mktemp -char *mktemp (char *); -#endif - -#ifndef TMPDIR -#define TMPDIR "/tmp" -#endif - -static char const * -make_temp (char letter) -{ - char *r; -#if HAVE_MKTEMP - char const *tmpdir = getenv ("TMPDIR"); /* Unix tradition */ - if (!tmpdir) tmpdir = getenv ("TMP"); /* DOS tradition */ - if (!tmpdir) tmpdir = getenv ("TEMP"); /* another DOS tradition */ - if (!tmpdir) tmpdir = TMPDIR; - r = xmalloc (strlen (tmpdir) + 10); - sprintf (r, "%s/p%cXXXXXX", tmpdir, letter); - - /* It is OK to use mktemp here, since the rest of the code always - opens temp files with O_EXCL. It might be better to use mkstemp - to avoid some DoS problems, but simply substituting mkstemp for - mktemp here will not fix the DoS problems; a more extensive - change would be needed. */ - mktemp (r); - - if (!*r) - pfatal ("mktemp"); -#else - r = xmalloc (L_tmpnam); - if (! (tmpnam (r) == r && *r)) - pfatal ("tmpnam"); -#endif - return r; -} - /* Fatal exit with cleanup. */ void |