changeset 32080:e9fdfebc6db0

Avoid using file_stat in libinterp/corefcn (bug #59711). * dirfnc.cc (F__mkdir__), file-io.cc (do_stream_open), graphics.cc (Fdrawnow), interpreter.cc (interpreter::execute_startup_files), load-path.cc (load_path::find_file, load_path::find_dir, load_path::find_matching_dirs, load_path::find_first_of, load_path::find_all_first_of, load_path::execute_pkg_add_or_del, load_path::add, load_path::find_private_file, load_path::dir_info::get_file_list, load_path::dir_info::get_method_file_map, genpath), load-save.cc (find_file_to_load), ls-mat5.cc (read_mat5_binary_element), urlwrite (Furlwrite), utils.cc (find_data_file_in_load_path, fcn_file_in_path), __ftp__.cc (F__ftp_mput__), __magick_read__.cc (F__magick_write__): Use functions "dir_exists" or "file_exists" instead of "file_stat".
author Markus Mützel <markus.muetzel@gmx.de>
date Sat, 06 May 2023 13:08:38 +0200
parents b0785d76430a
children 687ea87ccf5d
files libinterp/corefcn/__ftp__.cc libinterp/corefcn/__magick_read__.cc libinterp/corefcn/dirfns.cc libinterp/corefcn/file-io.cc libinterp/corefcn/graphics.cc libinterp/corefcn/interpreter.cc libinterp/corefcn/load-path.cc libinterp/corefcn/load-save.cc libinterp/corefcn/ls-mat5.cc libinterp/corefcn/urlwrite.cc libinterp/corefcn/utils.cc
diffstat 11 files changed, 82 insertions(+), 142 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/__ftp__.cc	Sat May 06 12:57:46 2023 +0200
+++ b/libinterp/corefcn/__ftp__.cc	Sat May 06 13:08:38 2023 +0200
@@ -367,12 +367,10 @@
     {
       std::string file = files(i);
 
-      sys::file_stat fs (file);
-
-      if (! fs.exists ())
+      if (! sys::file_exists (file))
         error ("__ftp__mput: file does not exist");
 
-      if (fs.is_dir ())
+      if (sys::dir_exists (file))
         {
           file_list.append (url_xfer.mput_directory ("", file));
 
--- a/libinterp/corefcn/__magick_read__.cc	Sat May 06 12:57:46 2023 +0200
+++ b/libinterp/corefcn/__magick_read__.cc	Sat May 06 13:08:38 2023 +0200
@@ -1592,7 +1592,7 @@
   // If writemode is set to append, read the image and append to it.  Even
   // if set to append, make sure that something was read at all.
   const std::string writemode = options.getfield ("writemode").string_value ();
-  if (writemode == "append" && sys::file_stat (filename).exists ())
+  if (writemode == "append" && sys::file_exists (filename))
     {
       std::vector<Magick::Image> ini_imvec;
       read_file (filename, ini_imvec);
--- a/libinterp/corefcn/dirfns.cc	Sat May 06 12:57:46 2023 +0200
+++ b/libinterp/corefcn/dirfns.cc	Sat May 06 13:08:38 2023 +0200
@@ -39,6 +39,7 @@
 #include "file-ops.h"
 #include "file-stat.h"
 #include "glob-match.h"
+#include "lo-sysdep.h"
 #include "oct-env.h"
 #include "oct-glob.h"
 #include "pathsearch.h"
@@ -207,9 +208,7 @@
 
   dirname = sys::file_ops::tilde_expand (dirname);
 
-  sys::file_stat fs (dirname);
-
-  if (fs && fs.is_dir ())
+  if (sys::dir_exists (dirname))
     {
       // For Matlab compatibility, return true when directory already exists.
       return ovl (true, "directory exists", "mkdir");
--- a/libinterp/corefcn/file-io.cc	Sat May 06 12:57:46 2023 +0200
+++ b/libinterp/corefcn/file-io.cc	Sat May 06 13:08:38 2023 +0200
@@ -418,12 +418,12 @@
 
   std::string fname = sys::file_ops::tilde_expand (name);
 
-  sys::file_stat fs (fname);
+  bool is_dir = sys::dir_exists (fname);
 
   if (! (md & std::ios::out))
     fname = find_data_file_in_load_path ("fopen", fname);
 
-  if (! fs.is_dir ())
+  if (! is_dir)
     {
 #if defined (HAVE_ZLIB)
       if (use_zlib)
--- a/libinterp/corefcn/graphics.cc	Sat May 06 12:57:46 2023 +0200
+++ b/libinterp/corefcn/graphics.cc	Sat May 06 13:08:38 2023 +0200
@@ -44,6 +44,7 @@
 #include "cmd-edit.h"
 #include "file-ops.h"
 #include "file-stat.h"
+#include "lo-sysdep.h"
 #include "oct-locbuf.h"
 #include "oct-time.h"
 
@@ -13248,9 +13249,7 @@
                 {
                   std::string dirname = file.substr (0, pos+1);
 
-                  octave::sys::file_stat fs (dirname);
-
-                  if (! fs || ! fs.is_dir ())
+                  if (! octave::sys::dir_exists (dirname))
                     error ("drawnow: nonexistent directory '%s'",
                            dirname.c_str ());
 
--- a/libinterp/corefcn/interpreter.cc	Sat May 06 12:57:46 2023 +0200
+++ b/libinterp/corefcn/interpreter.cc	Sat May 06 13:08:38 2023 +0200
@@ -43,6 +43,7 @@
 #include "fpucw-wrappers.h"
 #include "lo-blas-proto.h"
 #include "lo-error.h"
+#include "lo-sysdep.h"
 #include "oct-env.h"
 #include "quit.h"
 #include "str-vec.h"
@@ -1193,9 +1194,7 @@
 
           // Names alone are not enough.
 
-          sys::file_stat fs_home_rc (home_rc);
-
-          if (fs_home_rc)
+          if (sys::file_exists (home_rc))
             {
               // We want to check for curr_dir after executing home_rc
               // because doing that may change the working directory.
--- a/libinterp/corefcn/load-path.cc	Sat May 06 12:57:46 2023 +0200
+++ b/libinterp/corefcn/load-path.cc	Sat May 06 13:08:38 2023 +0200
@@ -33,6 +33,7 @@
 #include "dir-ops.h"
 #include "file-ops.h"
 #include "file-stat.h"
+#include "lo-sysdep.h"
 #include "oct-env.h"
 #include "pathsearch.h"
 
@@ -548,11 +549,7 @@
 
   if (sys::env::absolute_pathname (file)
       || sys::env::rooted_relative_pathname (file))
-    {
-      sys::file_stat fs (file);
-
-      return fs.exists () ? file : retval;
-    }
+    return sys::file_exists (file) ? file : retval;
   else
     {
       std::string tfile = find_private_file (file);
@@ -570,9 +567,7 @@
         {
           std::string tfile = sys::file_ops::concat (di.abs_dir_name, file);
 
-          sys::file_stat fs (tfile);
-
-          if (fs.exists ())
+          if (sys::file_exists (tfile))
             return tfile;
         }
     }
@@ -605,9 +600,7 @@
       && (sys::env::absolute_pathname (dir)
           || sys::env::rooted_relative_pathname (dir)))
     {
-      sys::file_stat fs (dir);
-
-      if (fs.exists () && fs.is_dir ())
+      if (sys::dir_exists (dir))
         return dir;
     }
   else
@@ -630,13 +623,9 @@
 
           if (dname_len > dir_len
               && sys::file_ops::is_dir_sep (dname[dname_len - dir_len - 1])
-              && canon_dir == dname.substr (dname_len - dir_len))
-            {
-              sys::file_stat fs (di.dir_name);
-
-              if (fs.exists () && fs.is_dir ())
-                return di.abs_dir_name;
-            }
+              && canon_dir == dname.substr (dname_len - dir_len)
+              && sys::dir_exists (di.dir_name))
+            return di.abs_dir_name;
         }
     }
 
@@ -652,9 +641,7 @@
       && (sys::env::absolute_pathname (dir)
           || sys::env::rooted_relative_pathname (dir)))
     {
-      sys::file_stat fs (dir);
-
-      if (fs.exists () && fs.is_dir ())
+      if (sys::dir_exists (dir))
         retlist.push_back (dir);
     }
   else
@@ -677,13 +664,9 @@
 
           if (dname_len > dir_len
               && sys::file_ops::is_dir_sep (dname[dname_len - dir_len - 1])
-              && canon_dir == dname.substr (dname_len - dir_len))
-            {
-              sys::file_stat fs (di.dir_name);
-
-              if (fs.exists () && fs.is_dir ())
-                retlist.push_back (di.abs_dir_name);
-            }
+              && canon_dir == dname.substr (dname_len - dir_len)
+              && sys::dir_exists (di.dir_name))
+            retlist.push_back (di.abs_dir_name);
         }
     }
 
@@ -713,9 +696,7 @@
           if (sys::env::absolute_pathname (file)
               || sys::env::rooted_relative_pathname (file))
             {
-              sys::file_stat fs (file);
-
-              if (fs.exists ())
+              if (sys::file_exists (file))
                 return file;
             }
           else
@@ -725,9 +706,7 @@
                   std::string tfile;
                   tfile = sys::file_ops::concat (di.abs_dir_name, file);
 
-                  sys::file_stat fs (tfile);
-
-                  if (fs.exists ())
+                  if (sys::file_exists (tfile))
                     return tfile;
                 }
             }
@@ -790,9 +769,7 @@
           if (sys::env::absolute_pathname (file)
               || sys::env::rooted_relative_pathname (file))
             {
-              sys::file_stat fs (file);
-
-              if (fs.exists ())
+              if (sys::file_exists (file))
                 retlist.push_back (file);
             }
           else
@@ -802,9 +779,7 @@
                   std::string tfile;
                   tfile = sys::file_ops::concat (di.abs_dir_name, file);
 
-                  sys::file_stat fs (tfile);
-
-                  if (fs.exists ())
+                  if (sys::file_exists (tfile))
                     retlist.push_back (tfile);
                 }
             }
@@ -989,9 +964,7 @@
 
   std::string file = sys::file_ops::concat (dir, script_file);
 
-  sys::file_stat fs (file);
-
-  if (fs.exists ())
+  if (sys::file_exists (file))
     source_file (file, "base");
 }
 
@@ -1103,34 +1076,27 @@
     move (i, at_end);
   else
     {
-      sys::file_stat fs (dir);
-
-      if (fs)
+      std::string msg;
+
+      if (sys::dir_exists (dir, msg))
         {
-          if (fs.is_dir ())
-            {
-              read_dir_config (dir);
-
-              dir_info di (dir);
-
-              if (at_end)
-                m_dir_info_list.push_back (di);
-              else
-                m_dir_info_list.push_front (di);
-
-              add (di, at_end);
-
-              if (m_add_hook)
-                m_add_hook (dir);
-            }
-          else if (warn)
-            warning ("addpath: %s: not a directory", dir_arg.c_str ());
+          read_dir_config (dir);
+
+          dir_info di (dir);
+
+          if (at_end)
+            m_dir_info_list.push_back (di);
+          else
+            m_dir_info_list.push_front (di);
+
+          add (di, at_end);
+
+          if (m_add_hook)
+            m_add_hook (dir);
         }
-      else if (warn)
-        {
-          std::string msg = fs.error ();
-          warning ("addpath: %s: %s", dir_arg.c_str (), msg.c_str ());
-        }
+
+      if (warn && ! msg.empty ())
+        warning ("addpath: %s: %s", dir_arg.c_str (), msg.c_str ());
     }
 
   // FIXME: is there a better way to do this?
@@ -1323,9 +1289,7 @@
                                + "private" + sys::file_ops::dir_sep_str ()
                                + fname;
 
-          sys::file_stat fs (pfname);
-
-          if (fs.exists () && fs.is_reg ())
+          if (sys::file_exists (pfname, false))
             retval = pfname;
         }
     }
@@ -1553,36 +1517,31 @@
 
       std::string full_name = sys::file_ops::concat (d, fname);
 
-      sys::file_stat fs (full_name);
-
-      if (fs)
+      if (sys::dir_exists (full_name))
         {
-          if (fs.is_dir ())
-            {
-              if (fname == "private")
-                get_private_file_map (full_name);
-              else if (fname[0] == '@')
-                get_method_file_map (full_name, fname.substr (1));
-              else if (fname[0] == '+')
-                get_package_dir (full_name, fname.substr (1));
-            }
-          else
+          if (fname == "private")
+            get_private_file_map (full_name);
+          else if (fname[0] == '@')
+            get_method_file_map (full_name, fname.substr (1));
+          else if (fname[0] == '+')
+            get_package_dir (full_name, fname.substr (1));
+        }
+      else if (sys::file_exists (full_name))
+        {
+          all_files[all_files_count++] = fname;
+
+          std::size_t pos = fname.rfind ('.');
+
+          if (pos != std::string::npos)
             {
-              all_files[all_files_count++] = fname;
-
-              std::size_t pos = fname.rfind ('.');
-
-              if (pos != std::string::npos)
+              std::string ext = fname.substr (pos);
+
+              if (ext == ".m" || ext == ".oct" || ext == ".mex")
                 {
-                  std::string ext = fname.substr (pos);
-
-                  if (ext == ".m" || ext == ".oct" || ext == ".mex")
-                    {
-                      std::string base = fname.substr (0, pos);
-
-                      if (valid_identifier (base))
-                        fcn_files[fcn_files_count++] = fname;
-                    }
+                  std::string base = fname.substr (0, pos);
+
+                  if (valid_identifier (base))
+                    fcn_files[fcn_files_count++] = fname;
                 }
             }
         }
@@ -1606,9 +1565,7 @@
 
   std::string pd = sys::file_ops::concat (d, "private");
 
-  sys::file_stat fs (pd);
-
-  if (fs && fs.is_dir ())
+  if (sys::dir_exists (pd))
     method_file_map[class_name].private_file_map = get_fcn_files (pd);
 }
 
@@ -2444,9 +2401,7 @@
             {
               std::string nm = sys::file_ops::concat (dirname, elt);
 
-              sys::file_stat fs (nm);
-
-              if (fs && fs.is_dir ())
+              if (sys::dir_exists (nm))
                 retval += (directory_path::path_sep_str ()
                            + genpath (nm, skip));
             }
--- a/libinterp/corefcn/load-save.cc	Sat May 06 12:57:46 2023 +0200
+++ b/libinterp/corefcn/load-save.cc	Sat May 06 13:08:38 2023 +0200
@@ -43,6 +43,7 @@
 #include "file-stat.h"
 #include "glob-match.h"
 #include "lo-mappers.h"
+#include "lo-sysdep.h"
 #include "mach-info.h"
 #include "oct-env.h"
 #include "oct-locbuf.h"
@@ -193,16 +194,12 @@
       // Either no '.' in name or no '.' appears after last directory
       // separator.
 
-      sys::file_stat fs (fname);
-
-      if (! (fs.exists () && fs.is_reg ()))
+      if (! (sys::file_exists (fname, false)))
         fname = find_file_to_load (fname + ".mat", orig_name);
     }
   else
     {
-      sys::file_stat fs (fname);
-
-      if (! (fs.exists () && fs.is_reg ()))
+      if (! (sys::file_exists (fname, false)))
         {
           fname = "";
 
--- a/libinterp/corefcn/ls-mat5.cc	Sat May 06 12:57:46 2023 +0200
+++ b/libinterp/corefcn/ls-mat5.cc	Sat May 06 13:08:38 2023 +0200
@@ -44,6 +44,7 @@
 #include "file-stat.h"
 #include "glob-match.h"
 #include "lo-mappers.h"
+#include "lo-sysdep.h"
 #include "mach-info.h"
 #include "oct-env.h"
 #include "oct-locbuf.h"
@@ -897,9 +898,8 @@
                     std::string str
                       = (octave::config::octave_exec_home ()
                          + fpath.substr (mroot.length ()));
-                    octave::sys::file_stat fs (str);
-
-                    if (fs.exists ())
+
+                    if (octave::sys::file_exists (str))
                       {
                         std::size_t xpos
                           = str.find_last_of (octave::sys::file_ops::dir_sep_chars ());
--- a/libinterp/corefcn/urlwrite.cc	Sat May 06 12:57:46 2023 +0200
+++ b/libinterp/corefcn/urlwrite.cc	Sat May 06 13:08:38 2023 +0200
@@ -136,8 +136,6 @@
   // create it, and the download fails.  We use unwind_protect to do
   // it so that the deletion happens no matter how we exit the function.
 
-  sys::file_stat fs (filename);
-
   std::ofstream ofile =
     sys::ofstream (filename.c_str (), std::ios::out | std::ios::binary);
 
--- a/libinterp/corefcn/utils.cc	Sat May 06 12:57:46 2023 +0200
+++ b/libinterp/corefcn/utils.cc	Sat May 06 13:08:38 2023 +0200
@@ -39,6 +39,7 @@
 #include "file-ops.h"
 #include "file-stat.h"
 #include "lo-mappers.h"
+#include "lo-sysdep.h"
 #include "lo-utils.h"
 #include "nanosleep-wrapper.h"
 #include "oct-cmplx.h"
@@ -710,10 +711,8 @@
       // Load path will also search "." first, but we don't want to
       // issue a warning if the file is found in the current directory,
       // so do an explicit check for that.
-      sys::file_stat fs (fname);
-
       bool local_file_ok
-        = fs.exists () && (fs.is_reg () || ! require_regular_file);
+        = sys::file_exists (fname, ! require_regular_file);
 
       if (! local_file_ok)
         {
@@ -734,7 +733,7 @@
   return fname;
 }
 
-// See if there is an function file in the path.
+// See if there is a function file in the path.
 // If so, return the full path to the file.
 
 std::string fcn_file_in_path (const std::string& name)
@@ -747,9 +746,7 @@
     {
       if (sys::env::absolute_pathname (name))
         {
-          sys::file_stat fs (name);
-
-          if (fs.exists () && ! fs.is_dir ())
+          if (sys::file_exists (name, false))
             retval = name;
         }
       else if (len > 2 && name[len - 2] == '.' && name[len - 1] == 'm')
@@ -788,9 +785,7 @@
       std::string tcontents
         = sys::file_ops::concat (lp.find_dir (dir), "Contents.m");
 
-      sys::file_stat fs (tcontents);
-
-      if (fs.exists ())
+      if (sys::file_exists (tcontents))
         retval = sys::env::make_absolute (tcontents);
     }