changeset 32086:3c608abd55f5

Move "same_file" from liboctinterp to liboctave (bug #63803). * libinterp/corefcn/utils.cc, utils.h (same_file): Deprecate function. * libinterp/corefcn/sysdep.cc, sysdep.h (same_file_internal): Remove function. * liboctave/system/lo-sysdep.cc, lo-sysdep.h (same_file): Move function from liboctinterp to liboctave. * libgui/src/m-editor/file-editor.cc (file_editor::find_tab_widget), libinterp/corefcn/fcn-info.cc (file_editor::find_tab_widget), libinterp/corefcn/interpreter.cc (interpreter::execute_startup_files), libinterp/corefcn/load-path.cc (load_path::append): Use function in updated namespace.
author Markus Mützel <markus.muetzel@gmx.de>
date Sat, 06 May 2023 17:48:27 +0200
parents cf03230c0363
children a4fbcbaa0aa2
files libgui/src/m-editor/file-editor.cc libinterp/corefcn/fcn-info.cc libinterp/corefcn/interpreter.cc libinterp/corefcn/load-path.cc libinterp/corefcn/sysdep.cc libinterp/corefcn/sysdep.h libinterp/corefcn/utils.cc libinterp/corefcn/utils.h liboctave/system/lo-sysdep.cc liboctave/system/lo-sysdep.h
diffstat 10 files changed, 95 insertions(+), 91 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor.cc	Tue May 09 18:25:45 2023 +0200
+++ b/libgui/src/m-editor/file-editor.cc	Sat May 06 17:48:27 2023 +0200
@@ -53,6 +53,7 @@
 #include "gui-settings.h"
 #include "main-window.h"
 
+#include "lo-sysdep.h"
 #include "oct-env.h"
 
 #include "event-manager.h"
@@ -2826,7 +2827,8 @@
       //
       // is false
 
-      if (same_file (std_file, tab_file.toStdString ()) || file == tab_file)
+      if (sys::same_file (std_file, tab_file.toStdString ())
+          || file == tab_file)
         return fe_tab;
     }
 
--- a/libinterp/corefcn/fcn-info.cc	Tue May 09 18:25:45 2023 +0200
+++ b/libinterp/corefcn/fcn-info.cc	Sat May 06 17:48:27 2023 +0200
@@ -29,6 +29,7 @@
 
 #include "file-ops.h"
 #include "file-stat.h"
+#include "lo-sysdep.h"
 #include "oct-env.h"
 
 #include "defun.h"
@@ -573,7 +574,7 @@
                         }
 
                       if (! file.empty ())
-                        is_same_file = same_file (file, ff);
+                        is_same_file = sys::same_file (file, ff);
                     }
                   else
                     {
--- a/libinterp/corefcn/interpreter.cc	Tue May 09 18:25:45 2023 +0200
+++ b/libinterp/corefcn/interpreter.cc	Sat May 06 17:48:27 2023 +0200
@@ -1201,7 +1201,7 @@
 
               local_rc = sys::env::make_absolute (initfile);
 
-              home_rc_already_executed = same_file (home_rc, local_rc);
+              home_rc_already_executed = sys::same_file (home_rc, local_rc);
             }
         }
 
--- a/libinterp/corefcn/load-path.cc	Tue May 09 18:25:45 2023 +0200
+++ b/libinterp/corefcn/load-path.cc	Sat May 06 17:48:27 2023 +0200
@@ -363,7 +363,7 @@
 
   if (! dir_arg.empty ())
     {
-      if (same_file (dir_arg, "."))
+      if (sys::same_file (dir_arg, "."))
         {
           warning (R"(rmpath: can't remove "." from path)");
 
@@ -429,7 +429,7 @@
 
   for (const auto& d : m_dir_info_list)
     {
-      if (same_file (dir, d.dir_name))
+      if (sys::same_file (dir, d.dir_name))
         {
           retval = true;
           break;
@@ -448,7 +448,7 @@
 
   std::string curr_dir = sys::env::get_current_directory ();
 
-  if (same_file (curr_dir, dir))
+  if (sys::same_file (curr_dir, dir))
     ok = true;
   else
     {
@@ -463,7 +463,7 @@
 
       if (dir_in_load_path)
         {
-          if (same_file (lp_file, file))
+          if (sys::same_file (lp_file, file))
             ok = true;
         }
       else
@@ -474,9 +474,9 @@
           // not enough because the file in the current directory would
           // still be found.
 
-          if (same_file (lp_file, base_file))
+          if (sys::same_file (lp_file, base_file))
             {
-              if (same_file (curr_dir, dir))
+              if (sys::same_file (curr_dir, dir))
                 ok = true;
               else
                 addpath_option = false;
--- a/libinterp/corefcn/sysdep.cc	Tue May 09 18:25:45 2023 +0200
+++ b/libinterp/corefcn/sysdep.cc	Sat May 06 17:48:27 2023 +0200
@@ -380,81 +380,6 @@
 
 #endif
 
-// Return TRUE if FILE1 and FILE2 refer to the same (physical) file.
-
-bool same_file_internal (const std::string& file1, const std::string& file2)
-{
-#if defined (OCTAVE_USE_WINDOWS_API)
-
-  // FIXME: When Octave switches to C++17, consider replacing this function
-  //        by https://en.cppreference.com/w/cpp/filesystem/equivalent.
-
-  bool retval = false;
-
-  std::wstring file1w = sys::u8_to_wstring (file1);
-  std::wstring file2w = sys::u8_to_wstring (file2);
-  const wchar_t *f1 = file1w.c_str ();
-  const wchar_t *f2 = file2w.c_str ();
-
-  bool f1_is_dir = GetFileAttributesW (f1) & FILE_ATTRIBUTE_DIRECTORY;
-  bool f2_is_dir = GetFileAttributesW (f2) & FILE_ATTRIBUTE_DIRECTORY;
-
-  // Windows native code
-  // Reference: http://msdn2.microsoft.com/en-us/library/aa363788.aspx
-
-  DWORD share = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;
-
-  HANDLE hfile1
-    = CreateFileW (f1, 0, share, 0, OPEN_EXISTING,
-                   f1_is_dir ? FILE_FLAG_BACKUP_SEMANTICS : 0, 0);
-
-  if (hfile1 != INVALID_HANDLE_VALUE)
-    {
-      HANDLE hfile2
-        = CreateFileW (f2, 0, share, 0, OPEN_EXISTING,
-                       f2_is_dir ? FILE_FLAG_BACKUP_SEMANTICS : 0, 0);
-
-      if (hfile2 != INVALID_HANDLE_VALUE)
-        {
-          BY_HANDLE_FILE_INFORMATION hfi1;
-          BY_HANDLE_FILE_INFORMATION hfi2;
-
-          if (GetFileInformationByHandle (hfile1, &hfi1)
-              && GetFileInformationByHandle (hfile2, &hfi2))
-            {
-              retval = (hfi1.dwVolumeSerialNumber == hfi2.dwVolumeSerialNumber
-                        && hfi1.nFileIndexHigh == hfi2.nFileIndexHigh
-                        && hfi1.nFileIndexLow == hfi2.nFileIndexLow
-                        && hfi1.nFileSizeHigh == hfi2.nFileSizeHigh
-                        && hfi1.nFileSizeLow == hfi2.nFileSizeLow
-                        && hfi1.ftLastWriteTime.dwLowDateTime
-                        == hfi2.ftLastWriteTime.dwLowDateTime
-                        && hfi1.ftLastWriteTime.dwHighDateTime
-                        == hfi2.ftLastWriteTime.dwHighDateTime);
-            }
-
-          CloseHandle (hfile2);
-        }
-
-      CloseHandle (hfile1);
-    }
-
-  return retval;
-
-#else
-
-  // POSIX Code
-
-  sys::file_stat fs_file1 (file1);
-  sys::file_stat fs_file2 (file2);
-
-  return (fs_file1 && fs_file2
-          && fs_file1.ino () == fs_file2.ino ()
-          && fs_file1.dev () == fs_file2.dev ());
-
-#endif
-}
-
 // Return TRUE if NAME refers to an existing drive letter or UNC share
 
 bool drive_or_unc_share (const std::string& name)
--- a/libinterp/corefcn/sysdep.h	Tue May 09 18:25:45 2023 +0200
+++ b/libinterp/corefcn/sysdep.h	Sat May 06 17:48:27 2023 +0200
@@ -53,9 +53,6 @@
 
 extern OCTINTERP_API std::string get_P_tmpdir ();
 
-extern OCTINTERP_API bool same_file_internal (const std::string&,
-                                              const std::string&);
-
 extern OCTINTERP_API bool drive_or_unc_share (const std::string&);
 
 OCTAVE_END_NAMESPACE(octave)
--- a/libinterp/corefcn/utils.cc	Tue May 09 18:25:45 2023 +0200
+++ b/libinterp/corefcn/utils.cc	Sat May 06 17:48:27 2023 +0200
@@ -294,7 +294,7 @@
 
 bool same_file (const std::string& f, const std::string& g)
 {
-  return same_file_internal (f, g);
+  return sys::same_file (f, g);
 }
 
 DEFUN (is_same_file, args, ,
@@ -337,7 +337,7 @@
       std::string file1 = args(0).string_value ();
       std::string file2 = args(1).string_value ();
 
-      retval = same_file (file1, file2);
+      retval = sys::same_file (file1, file2);
     }
   else if ((s1_string && s2_cellstr) || (s1_cellstr && s2_string))
     {
@@ -360,7 +360,7 @@
       boolNDArray output (cellstr.dims (), false);
 
       for (octave_idx_type idx = 0; idx < cellstr.numel (); idx++)
-        output(idx) = same_file (str, cellstr(idx));
+        output(idx) = sys::same_file (str, cellstr(idx));
 
       retval = output;
     }
@@ -378,7 +378,7 @@
       boolNDArray output (size1, false);
 
       for (octave_idx_type idx = 0; idx < cellstr1.numel (); idx++)
-        output(idx) = same_file (cellstr1(idx), cellstr2(idx));
+        output(idx) = sys::same_file (cellstr1(idx), cellstr2(idx));
 
       retval = output;
     }
--- a/libinterp/corefcn/utils.h	Tue May 09 18:25:45 2023 +0200
+++ b/libinterp/corefcn/utils.h	Sat May 06 17:48:27 2023 +0200
@@ -105,6 +105,7 @@
 extern OCTINTERP_API bool
 make_valid_name (std::string& str, const make_valid_name_options& options);
 
+OCTAVE_DEPRECATED (9, "octave::same_file is obsolete, use octave::sys::same_file")
 extern OCTINTERP_API bool
 same_file (const std::string& f, const std::string& g);
 
--- a/liboctave/system/lo-sysdep.cc	Tue May 09 18:25:45 2023 +0200
+++ b/liboctave/system/lo-sysdep.cc	Sat May 06 17:48:27 2023 +0200
@@ -430,6 +430,81 @@
 #endif
 }
 
+// Return TRUE if FILE1 and FILE2 refer to the same (physical) file.
+
+bool same_file (const std::string& file1, const std::string& file2)
+{
+#if defined (OCTAVE_USE_WINDOWS_API)
+
+  // FIXME: When Octave switches to C++17, consider replacing this function
+  //        by https://en.cppreference.com/w/cpp/filesystem/equivalent.
+
+  bool retval = false;
+
+  std::wstring file1w = sys::u8_to_wstring (file1);
+  std::wstring file2w = sys::u8_to_wstring (file2);
+  const wchar_t *f1 = file1w.c_str ();
+  const wchar_t *f2 = file2w.c_str ();
+
+  bool f1_is_dir = GetFileAttributesW (f1) & FILE_ATTRIBUTE_DIRECTORY;
+  bool f2_is_dir = GetFileAttributesW (f2) & FILE_ATTRIBUTE_DIRECTORY;
+
+  // Windows native code
+  // Reference: http://msdn2.microsoft.com/en-us/library/aa363788.aspx
+
+  DWORD share = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;
+
+  HANDLE hfile1
+    = CreateFileW (f1, 0, share, 0, OPEN_EXISTING,
+                   f1_is_dir ? FILE_FLAG_BACKUP_SEMANTICS : 0, 0);
+
+  if (hfile1 != INVALID_HANDLE_VALUE)
+    {
+      HANDLE hfile2
+        = CreateFileW (f2, 0, share, 0, OPEN_EXISTING,
+                       f2_is_dir ? FILE_FLAG_BACKUP_SEMANTICS : 0, 0);
+
+      if (hfile2 != INVALID_HANDLE_VALUE)
+        {
+          BY_HANDLE_FILE_INFORMATION hfi1;
+          BY_HANDLE_FILE_INFORMATION hfi2;
+
+          if (GetFileInformationByHandle (hfile1, &hfi1)
+              && GetFileInformationByHandle (hfile2, &hfi2))
+            {
+              retval = (hfi1.dwVolumeSerialNumber == hfi2.dwVolumeSerialNumber
+                        && hfi1.nFileIndexHigh == hfi2.nFileIndexHigh
+                        && hfi1.nFileIndexLow == hfi2.nFileIndexLow
+                        && hfi1.nFileSizeHigh == hfi2.nFileSizeHigh
+                        && hfi1.nFileSizeLow == hfi2.nFileSizeLow
+                        && hfi1.ftLastWriteTime.dwLowDateTime
+                        == hfi2.ftLastWriteTime.dwLowDateTime
+                        && hfi1.ftLastWriteTime.dwHighDateTime
+                        == hfi2.ftLastWriteTime.dwHighDateTime);
+            }
+
+          CloseHandle (hfile2);
+        }
+
+      CloseHandle (hfile1);
+    }
+
+  return retval;
+
+#else
+
+  // POSIX Code
+
+  sys::file_stat fs_file1 (file1);
+  sys::file_stat fs_file2 (file2);
+
+  return (fs_file1 && fs_file2
+          && fs_file1.ino () == fs_file2.ino ()
+          && fs_file1.dev () == fs_file2.dev ());
+
+#endif
+}
+
 std::FILE *
 fopen (const std::string& filename, const std::string& mode)
 {
--- a/liboctave/system/lo-sysdep.h	Tue May 09 18:25:45 2023 +0200
+++ b/liboctave/system/lo-sysdep.h	Sat May 06 17:48:27 2023 +0200
@@ -62,6 +62,9 @@
 extern OCTAVE_API bool
 dir_exists (const std::string& dirname, std::string& msg);
 
+extern OCTAVE_API bool
+same_file (const std::string& f, const std::string& g);
+
 extern OCTAVE_API std::FILE *
 fopen (const std::string& name, const std::string& mode);