-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Create temporary files w/o race conditions whenever possible (fixes #1145) #1177
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
%% Copyright (C) 2022 Alex Vong | ||
%% | ||
%% This file is part of OctSymPy. | ||
%% | ||
%% OctSymPy 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. | ||
%% | ||
%% This software 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 this software; see the file COPYING. | ||
%% If not, see <https://www.gnu.org/licenses/>. | ||
|
||
%% -*- texinfo -*- | ||
%% @documentencoding UTF-8 | ||
%% @deftypefun {@var{path} =} make_temp_dir__ (@var{tmpdir}, @var{prefix}) | ||
%% Try to create temporary directory in temporary directory @var{tmpdir} with | ||
%% prefix @var{prefix} in the most secure and portable way possible. | ||
%% | ||
%% This function is not really intended for end users. | ||
%% | ||
%% @end deftypefun | ||
|
||
|
||
function path = make_temp_dir__ (tmpdir, prefix) | ||
cmd = {'import tempfile' | ||
'(tmpdir, prefix) = _ins' | ||
'return tempfile.mkdtemp(dir=tmpdir, prefix=prefix)'}; | ||
path = pycall_sympy__ (cmd, tmpdir, prefix); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not a huge fan of having temp dir creation depend on Python...! Is this really not do-able on Octave? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAIK octave has no function to create temporary directory. If java is enabled, we can just use java. For *nix, we can use the shell command There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Java isn't included in the installer of Octave for Windows. Octave just tries to detect if a compatible JRE "happens" to be installed that it can use. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. Since cygwin / msys2 is part of octave in windows (as indicated in #1182), I think we can use the |
||
end | ||
|
||
|
||
%!test | ||
%! % general test | ||
%! path = make_temp_dir__ (tempdir (), 'octsympy-'); | ||
%! assert (~isempty (strfind (path, tempdir ()))); | ||
%! assert (~isempty (strfind (path, 'octsympy-'))); | ||
%! assert (isfolder (path)); | ||
%! assert (isequal (ls (path), '')); | ||
%! assert (rmdir (path)); | ||
|
||
%!error <Python exception: FileNotFoundError> | ||
%! if (exist ('OCTAVE_VERSION', 'builtin')) | ||
%! make_temp_dir__ ('/nonexistent', ''); | ||
%! end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bikeshedding here but seems to me this should verify that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea. Debian-based distros use |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
%% Copyright (C) 2022 Alex Vong | ||
%% | ||
%% This file is part of OctSymPy. | ||
%% | ||
%% OctSymPy 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. | ||
%% | ||
%% This software 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 this software; see the file COPYING. | ||
%% If not, see <https://www.gnu.org/licenses/>. | ||
|
||
%% -*- texinfo -*- | ||
%% @documentencoding UTF-8 | ||
%% @deftypefun {[@var{fd}, @var{filename}] =} make_temp_file__ (@var{tmpdir}, @var{prefix}) | ||
%% Try to create temporary file in temporary directory @var{tmpdir} with prefix | ||
%% @var{prefix} in the most secure and portable way possible. | ||
%% | ||
%% This function is not really intended for end users. | ||
%% | ||
%% @end deftypefun | ||
|
||
|
||
%% This function is used in private/python_ipc_*.m | ||
%% Assume Python IPC is not initialized yet | ||
|
||
function [fd, filename] = make_temp_file__ (tmpdir, prefix) | ||
if (exist ('OCTAVE_VERSION', 'builtin')) | ||
template = [tmpdir '/' prefix 'XXXXXX']; | ||
[fd, filename, msg] = mkstemp (template); | ||
if (fd == -1 || isequal (filename, '')) | ||
error ('make_temp_file__: cannot create temp file: %s', msg); | ||
end | ||
|
||
elseif (usejava ('jvm')) % java is required when not running octave | ||
attrs = javaArray ('java.nio.file.attribute.FileAttribute', 0); | ||
path = javaMethod ('createTempFile', 'java.nio.file.Files', | ||
prefix, '', | ||
attrs); | ||
filename = javaMethod ('toString', path); | ||
fd = fopen (filename, 'r+'); | ||
|
||
else | ||
error ('make_temp_file__: cannot create temp file: please enable java'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this is "please enable java or use GNU Octave"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, we should recommend Octave as well |
||
end | ||
end | ||
|
||
|
||
%!test | ||
%! % general test | ||
%! [fd, filename] = make_temp_file__ (tempdir (), 'octsympy-'); | ||
%! assert (~isempty (strfind (filename, tempdir ()))); | ||
%! assert (~isempty (strfind (filename, 'octsympy-'))); | ||
%! assert (mod (fd, 1) == 0 && fd > 2); | ||
%! s = 'hello, world'; | ||
%! fprintf (fd, s); | ||
%! assert (fclose (fd) == 0); | ||
%! fd2 = fopen (filename); | ||
%! s2 = fgets (fd2); | ||
%! assert (isequal (s, s2)); | ||
%! assert (fgets (fd2) == -1); | ||
%! assert (fclose (fd2) == 0); | ||
%! if (exist ('OCTAVE_VERSION', 'builtin')) | ||
%! assert (unlink (filename) == 0); | ||
%! else | ||
%! delete (filename); | ||
%! end | ||
|
||
%!error <cannot create temp file> | ||
%! if (exist ('OCTAVE_VERSION', 'builtin')) | ||
%! make_temp_file__ ('/nonexistent', ''); | ||
%! end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks scary: fd is an open file id...? not closed? does
save
open another fd on the same file...?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, should have closed the unused fd here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but then this is no better than any other racy way... i.e., does this offer any benefit over
tempname
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is better than
tempname
becausetempname
does not choose the filename and create the file in a single step (aka atomic operation). The fd is opened for convenience but it's not essential. Themktemp
shell command similarly returns a filename instead of a fd, but it's still secure.