changeset 31847:318dbb0ce30d

load-save.cc: Use temp file only when not in append mode (bug #63803) load-save.cc in function load_save_system::save (): Previously we were writing to a temp file so that if saving crashed we weren't losing data already in the file from earlier. That logic breaks down for append mode, so this changeset does the temp file technique only for non-append mode.
author Arun Giridhar <arungiridhar@gmail.com>
date Fri, 24 Feb 2023 09:36:30 -0500
parents 5f0b8101234e
children 1f3f7e874203
files libinterp/corefcn/load-save.cc
diffstat 1 files changed, 14 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/load-save.cc	Tue Feb 21 17:45:34 2023 -0500
+++ b/libinterp/corefcn/load-save.cc	Fri Feb 24 09:36:30 2023 -0500
@@ -1465,14 +1465,16 @@
     print_usage ();
   else
     {
-      // We make a new temporary filename, write to that instead of the
-      // file specified, then try renaming it at the end.
+      // For non-append mode, we make a new temporary filename, write to that
+      // instead of the file specified, then rename it at the end.
       // That way, if something goes wrong during the save like OOM,
       // we won't overwrite already-saved data in a file.
       // See bug #63803 for context.
+      // In append mode, this kind of guard is counterproductive so we write
+      // directly to the specified file.
 
       std::string desiredname = sys::file_ops::tilde_expand (argv[i]);
-      std::string fname = desiredname + ".saving_in_progress";
+      std::string fname = desiredname + (append ? "" : ".saving_in_progress");
 
       i++;
 
@@ -1553,13 +1555,16 @@
       // If we are all the way here without Octave crashing or running
       // out of memory etc, then we can say that writing to the
       // temporary file was successful. So now we try to rename it to
-      // the actual file that was specified.
-
-      std::string msg;
+      // the actual file that was specified, unless we were in append mode
+      // in which case we take no action.
 
-      if (octave::sys::rename (fname, desiredname, msg) < 0)
-        error ("save: unable to save to %s  %s",
-               desiredname.c_str (), msg.c_str ());
+      if (! append)
+        {
+          std::string msg;
+          if (octave::sys::rename (fname, desiredname, msg) < 0)
+            error ("save: unable to save to %s  %s",
+                   desiredname.c_str (), msg.c_str ());
+        }
     }
 
   return retval;