changeset 19462:eee9f111c164

Check if tempfile name exists when changing path of temporary file name (Bug #43872) * liboctave/system/file-ops.cc (octave_tempnam): if path name was sepcified for tempory filename, verify (and regenerate) a name if file already exists.
author John Donoghue
date Tue, 23 Dec 2014 10:30:26 -0500
parents e23aedcc70a7
children aee5fea8a03e
files liboctave/system/file-ops.cc
diffstat 1 files changed, 14 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/system/file-ops.cc	Tue Dec 23 15:43:48 2014 -0800
+++ b/liboctave/system/file-ops.cc	Tue Dec 23 10:30:26 2014 -0500
@@ -705,6 +705,20 @@
             file_stat fs (dirarg, false);
             if (fs && fs.is_dir ())
               retval.replace (0, pos, dirarg);
+
+            // since we have changed the directory, it is possible that the name
+            // we are using is no longer unique, so check/modify
+            std::string tmppath = retval;
+            int attempt = 0;
+            while (++attempt < TMP_MAX && file_stat (tmppath, false).exists ())
+              {
+                char file_postfix[16];
+
+                sprintf(file_postfix, "t%d", attempt);
+
+                tmppath = retval + file_postfix;
+              }
+            retval = tmppath;
           }
         }
     }