Mercurial > octave-nkf
changeset 19454:82f2a3437e02
Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
* file-ops.cc (octave_tempnam): Use C++ std::string functions to check
if function's dir argument has been overwritten by TMPDIR environment variable.
If it has, replace the TMPDIR string with the the function's dir argument.
* file-io.cc (Ftempname): Add BIST tests for correct behavior.
author | Rik <rik@octave.org> |
---|---|
date | Sun, 21 Dec 2014 21:00:51 -0800 |
parents | c029983a0389 |
children | 8b785ca93de7 |
files | libinterp/corefcn/file-io.cc liboctave/system/file-ops.cc |
diffstat | 2 files changed, 67 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/libinterp/corefcn/file-io.cc Sun Dec 21 19:52:41 2014 -0800 +++ b/libinterp/corefcn/file-io.cc Sun Dec 21 21:00:51 2014 -0800 @@ -1983,6 +1983,54 @@ return retval; } +/* +%!test +%! if (ispc ()) +%! envname = "TMP"; +%! else +%! envname = "TMPDIR"; +%! endif +%! envdir = getenv (envname); +%! unsetenv (envname); +%! unwind_protect +%! ## Test 0-argument form +%! fname = tempname (); +%! [tmpdir, tmpfname] = fileparts (fname); +%! assert (tmpdir, P_tmpdir); +%! assert (tmpfname (1:4), "oct-"); +%! ## Test 1-argument form +%! tmp_tmpdir = [P_tmpdir filesep() substr(tmpfname, -5)]; +%! mkdir (tmp_tmpdir) || error ("Unable to create tmp dir"); +%! setenv (envname, P_tmpdir); +%! fname = tempname (tmp_tmpdir); +%! [tmpdir, tmpfname] = fileparts (fname); +%! assert (tmpdir, tmp_tmpdir); +%! assert (tmpfname (1:4), "oct-"); +%! ## Test 1-argument form w/null tmpdir +%! fname = tempname (""); +%! [tmpdir, tmpfname] = fileparts (fname); +%! assert (tmpdir, P_tmpdir); +%! assert (tmpfname (1:4), "oct-"); +%! ## Test 2-argument form +%! fname = tempname (tmp_tmpdir, "pfx-"); +%! [tmpdir, tmpfname] = fileparts (fname); +%! assert (tmpdir, tmp_tmpdir); +%! assert (tmpfname (1:4), "pfx-"); +%! ## Test 2-argument form w/null prefix +%! fname = tempname (tmp_tmpdir, ""); +%! [tmpdir, tmpfname] = fileparts (fname); +%! assert (tmpdir, tmp_tmpdir); +%! assert (tmpfname (1:4), "file"); +%! unwind_protect_cleanup +%! rmdir (tmp_tmpdir); +%! if (isempty (envdir)) +%! unsetenv (envname); +%! else +%! setenv (envname, envdir); +%! endif +%! end_unwind_protect +*/ + DEFUN (tmpfile, args, , "-*- texinfo -*-\n\ @deftypefn {Built-in Function} {[@var{fid}, @var{msg}] =} tmpfile ()\n\
--- a/liboctave/system/file-ops.cc Sun Dec 21 19:52:41 2014 -0800 +++ b/liboctave/system/file-ops.cc Sun Dec 21 21:00:51 2014 -0800 @@ -687,8 +687,26 @@ if (tmp) { retval = tmp; + free (tmp); - free (tmp); + if (! dir.empty ()) + { + // Check that environment variable hasn't overridden dir argument + size_t pos = retval.rfind (file_ops::dir_sep_char ()); + std::string tmpdir = retval.substr (0, pos); + std::string dirarg = dir; + if (*dirarg.rbegin () == file_ops::dir_sep_char ()) + dirarg.erase (--dirarg.end ()); + + if (tmpdir != dirarg) + { + // A different TMPDIR was used. + // Replace TMPDIR with given dir if is valid + file_stat fs (dirarg, false); + if (fs && fs.is_dir ()) + retval.replace (0, pos, dirarg); + } + } } else msg = gnulib::strerror (errno);