# HG changeset patch
# User Markus Mützel <markus.muetzel@gmx.de>
# Date 1683388107 -7200
#      Sat May 06 17:48:27 2023 +0200
# Node ID af2f59e4ea8a61b90d71577588af73a7d670577e
# Parent  e9fdfebc6db0983ba636db29e9b2ed7146f51d6a
Move "same_file" to liboctave.

diff -r e9fdfebc6db0 -r af2f59e4ea8a libgui/src/m-editor/file-editor.cc
--- a/libgui/src/m-editor/file-editor.cc	Sat May 06 13:08:38 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;
     }
 
diff -r e9fdfebc6db0 -r af2f59e4ea8a libinterp/corefcn/fcn-info.cc
--- a/libinterp/corefcn/fcn-info.cc	Sat May 06 13:08:38 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
                     {
diff -r e9fdfebc6db0 -r af2f59e4ea8a libinterp/corefcn/interpreter.cc
--- a/libinterp/corefcn/interpreter.cc	Sat May 06 13:08:38 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);
             }
         }
 
diff -r e9fdfebc6db0 -r af2f59e4ea8a libinterp/corefcn/load-path.cc
--- a/libinterp/corefcn/load-path.cc	Sat May 06 13:08:38 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 (octave::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 (octave::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 (octave::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;
diff -r e9fdfebc6db0 -r af2f59e4ea8a libinterp/corefcn/sysdep.cc
--- a/libinterp/corefcn/sysdep.cc	Sat May 06 13:08:38 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)
diff -r e9fdfebc6db0 -r af2f59e4ea8a libinterp/corefcn/sysdep.h
--- a/libinterp/corefcn/sysdep.h	Sat May 06 13:08:38 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)
diff -r e9fdfebc6db0 -r af2f59e4ea8a libinterp/corefcn/utils.cc
--- a/libinterp/corefcn/utils.cc	Sat May 06 13:08:38 2023 +0200
+++ b/libinterp/corefcn/utils.cc	Sat May 06 17:48:27 2023 +0200
@@ -290,13 +290,6 @@
     error ("makeValidName: STR must be a string or cellstr");
 }
 
-// Return TRUE if F and G are both names for the same file.
-
-bool same_file (const std::string& f, const std::string& g)
-{
-  return same_file_internal (f, g);
-}
-
 DEFUN (is_same_file, args, ,
        doc: /* -*- texinfo -*-
 @deftypefn {} {@var{same} =} is_same_file (@var{filepath1}, @var{filepath2})
@@ -337,7 +330,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 +353,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 +371,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;
     }
diff -r e9fdfebc6db0 -r af2f59e4ea8a libinterp/corefcn/utils.h
--- a/libinterp/corefcn/utils.h	Sat May 06 13:08:38 2023 +0200
+++ b/libinterp/corefcn/utils.h	Sat May 06 17:48:27 2023 +0200
@@ -105,9 +105,6 @@
 extern OCTINTERP_API bool
 make_valid_name (std::string& str, const make_valid_name_options& options);
 
-extern OCTINTERP_API bool
-same_file (const std::string& f, const std::string& g);
-
 extern OCTINTERP_API int almost_match (const std::string& std,
                                        const std::string& s,
                                        int min_match_len = 1,
diff -r e9fdfebc6db0 -r af2f59e4ea8a liboctave/system/lo-sysdep.cc
--- a/liboctave/system/lo-sysdep.cc	Sat May 06 13:08:38 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)
 {
diff -r e9fdfebc6db0 -r af2f59e4ea8a liboctave/system/lo-sysdep.h
--- a/liboctave/system/lo-sysdep.h	Sat May 06 13:08:38 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);
 
