# HG changeset patch # User Markus Mützel # Date 1648919645 -7200 # Sat Apr 02 19:14:05 2022 +0200 # Node ID f9a8bcc620d858d7fe9fb1acf6bdedf934dc6aef # Parent 48b85f28374bfe4cdbfdc7d0c994b71a7ad9c7b1 Unify detection of temporary directory (bug #62215). * libinterp/corefcn/file-io.cc (Ftempdir): Add implementation wrapping around "sys::env::get_temp_directory". (Ftempname): Remove local check of environment variable "TMPDIR". (Ftmpfile): Create temporary file in directory given by "sys::env::get_temp_directory". * liboctave/system/oct-env.cc (sys::env::do_get_temp_directory): Check environment variable "TMPDIR" on all platforms. * scripts/miscellaneous/tempdir.m: Remove file that is replaced by DEFUN. * scripts/miscellaneous/module.mk: Remove deleted file from list. diff -r 48b85f28374b -r f9a8bcc620d8 libinterp/corefcn/file-io.cc --- a/libinterp/corefcn/file-io.cc Sat Apr 02 17:02:49 2022 +0200 +++ b/libinterp/corefcn/file-io.cc Sat Apr 02 19:14:05 2022 +0200 @@ -2883,6 +2883,51 @@ return ovl (streams.remove (args(0), "pclose")); } +DEFUN (tempdir, args, , + doc: /* -*- texinfo -*- +@deftypefn {} {@var{dir} =} tempdir () +Return the name of the host system's directory for temporary files. + +The directory name is taken first from the environment variable @env{TMPDIR}. +If that does not exist, the environment variable @env{TMP} (and on Windows +platforms also with higher priority the environment variable @env{TEMP}) is +checked. If none of those are set, the system default returned by +@code{P_tmpdir} is used. +@seealso{P_tmpdir, tempname, mkstemp, tmpfile} +@end deftypefn */) +{ + int nargin = args.length (); + + if (nargin > 0) + print_usage (); + + std::string tmpdir = sys::env::get_temp_directory (); + + if (! sys::file_ops::is_dir_sep (tmpdir.back ())) + tmpdir += sys::file_ops::dir_sep_str (); + + return ovl (tmpdir); +} + +/* +%!assert (ischar (tempdir ())) + +%!test +%! old_wstate = warning ("off"); +%! old_tmpdir = getenv ("TMPDIR"); +%! unwind_protect +%! setenv ("TMPDIR", "__MY_TMP_DIR__"); +%! assert (tempdir (), ["__MY_TMP_DIR__" filesep()]); +%! unwind_protect_cleanup +%! if (! isempty (old_tmpdir)) +%! setenv ("TMPDIR", old_tmpdir); +%! else +%! unsetenv ("TMPDIR"); +%! endif +%! warning (old_wstate); +%! end_unwind_protect +*/ + DEFUN (tempname, args, , doc: /* -*- texinfo -*- @deftypefn {} {@var{fname} =} tempname () @@ -2912,8 +2957,6 @@ if (nargin > 0) dir = args(0).xstring_value ("tempname: DIR must be a string"); - else - dir = sys::env::getenv ("TMPDIR"); std::string pfx ("oct-"); @@ -2996,7 +3039,17 @@ octave_value_list retval; - FILE *fid = octave_tmpfile_wrapper (); + std::string tmpfile (sys::tempnam (sys::env::get_temp_directory (), "oct-")); + +#if defined (OCTAVE_USE_WINDOWS_API) + FILE *fid = sys::fopen (tmpfile, "w+bD"); +#else + FILE *fid = sys::fopen (tmpfile, "w+b"); + + // From gnulib: This relies on the Unix semantics that a file is not really + // removed until it is closed. + octave_unlink_wrapper (tmpfile.c_str ()); +#endif if (fid) { @@ -3004,7 +3057,7 @@ std::ios::openmode md = fopen_mode_to_ios_mode ("w+b"); - stream s = stdiostream::create (nm, fid, md); + stream s = stdiostream::create (tmpfile, fid, md); if (! s) { diff -r 48b85f28374b -r f9a8bcc620d8 liboctave/system/oct-env.cc --- a/liboctave/system/oct-env.cc Sat Apr 02 17:02:49 2022 +0200 +++ b/liboctave/system/oct-env.cc Sat Apr 02 19:14:05 2022 +0200 @@ -207,11 +207,12 @@ std::string env::do_get_temp_directory (void) const { - std::string tempd; + std::string tempd = do_getenv ("TMPDIR"); #if defined (__MINGW32__) || defined (_MSC_VER) - tempd = do_getenv ("TEMP"); + if (tempd.empty ()) + tempd = do_getenv ("TEMP"); if (tempd.empty ()) tempd = do_getenv ("TMP"); @@ -228,7 +229,8 @@ #else - tempd = do_getenv ("TMP"); + if (tempd.empty ()) + tempd = do_getenv ("TMP"); #if defined (P_tmpdir) if (tempd.empty ()) diff -r 48b85f28374b -r f9a8bcc620d8 scripts/miscellaneous/module.mk --- a/scripts/miscellaneous/module.mk Sat Apr 02 17:02:49 2022 +0200 +++ b/scripts/miscellaneous/module.mk Sat Apr 02 19:14:05 2022 +0200 @@ -94,7 +94,6 @@ %reldir%/swapbytes.m \ %reldir%/symvar.m \ %reldir%/tar.m \ - %reldir%/tempdir.m \ %reldir%/unix.m \ %reldir%/unpack.m \ %reldir%/untar.m \ diff -r 48b85f28374b -r f9a8bcc620d8 scripts/miscellaneous/tempdir.m --- a/scripts/miscellaneous/tempdir.m Sat Apr 02 17:02:49 2022 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,70 +0,0 @@ -######################################################################## -## -## Copyright (C) 2003-2022 The Octave Project Developers -## -## See the file COPYRIGHT.md in the top-level directory of this -## distribution or . -## -## This file is part of Octave. -## -## Octave is free software: you can redistribute it and/or modify it -## under the terms of the GNU General Public License as published by -## the Free Software Foundation, either version 3 of the License, or -## (at your option) any later version. -## -## Octave is distributed in the hope that it will be useful, but -## WITHOUT ANY WARRANTY; without even the implied warranty of -## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -## GNU General Public License for more details. -## -## You should have received a copy of the GNU General Public License -## along with Octave; see the file COPYING. If not, see -## . -## -######################################################################## - -## -*- texinfo -*- -## @deftypefn {} {@var{dir} =} tempdir () -## Return the name of the host system's directory for temporary files. -## -## The directory name is taken first from the environment variable -## @env{TMPDIR}. If that does not exist the system default returned by -## @code{P_tmpdir} is used. -## @seealso{P_tmpdir, tempname, mkstemp, tmpfile} -## @end deftypefn - -function dirname = tempdir () - - dirname = getenv ("TMPDIR"); - if (isempty (dirname)) - dirname = P_tmpdir (); - endif - - if (! strcmp (dirname(end), filesep)) - dirname = [dirname filesep]; - endif - - if (! isfolder (dirname)) - warning ("tempdir: '%s' does not exist or is not a directory", dirname); - endif - -endfunction - - -%!assert (ischar (tempdir ())) - -%!test -%! old_wstate = warning ("query"); -%! warning ("off"); -%! old_tmpdir = getenv ("TMPDIR"); -%! unwind_protect -%! setenv ("TMPDIR", "__MY_TMP_DIR__"); -%! assert (tempdir (), ["__MY_TMP_DIR__" filesep()]); -%! unwind_protect_cleanup -%! if (! isempty (old_tmpdir)) -%! setenv ("TMPDIR", old_tmpdir); -%! else -%! unsetenv ("TMPDIR"); -%! endif -%! warning (old_wstate); -%! end_unwind_protect