diff liboctave/system/oct-time.cc @ 32090:7d663f770c5a

load-path: Avoid using file_stat on Windows (bug #59711). * liboctave/system/oct-time.h, oct-time.cc (octave::sys::file_time): New class to handle an efficient native file time type per platform. * libinterp/corefcn/load-path.h (load_path::dir_info::dir_mtime, load_path::dir_info::dir_time_last_checked): Change type to new class. * libinterp/corefcn/load-path.cc (subdirs_modified, load_path::dir_info::update, load_path::dir_info::initialize): Avoid using file_stat on Windows.
author Markus Mützel <markus.muetzel@gmx.de>
date Sat, 13 May 2023 13:33:57 +0200
parents 21f9b34eb893
children 53e15e754725
line wrap: on
line diff
--- a/liboctave/system/oct-time.cc	Fri May 12 08:03:14 2023 +0200
+++ b/liboctave/system/oct-time.cc	Sat May 13 13:33:57 2023 +0200
@@ -36,6 +36,8 @@
 
 #if defined (OCTAVE_USE_WINDOWS_API)
 #  include <windows.h>
+#else
+#  include "file-stat.h"
 #endif
 
 #include "lo-error.h"
@@ -370,5 +372,42 @@
   m_cpu = cpu_time (usr_sec, sys_sec, usr_usec, sys_usec);
 }
 
+file_time::file_time ()
+{
+#if defined (OCTAVE_USE_WINDOWS_API)
+  FILETIME curr_file_time;
+  GetSystemTimeAsFileTime (&curr_file_time);
+  m_time
+    = (static_cast<OCTAVE_TIME_T> (curr_file_time.dwHighDateTime)) >> 32
+      | curr_file_time.dwLowDateTime;
+#else
+  time_t ot_unix_time;
+  time_t ot_usec;
+  octave_gettimeofday_wrapper (&ot_unix_time, &ot_usec);
+  // Discard usec.  We are assuming a 1 second resolution anyway.
+  m_time = ot_unix_time;
+#endif
+}
+
+file_time::file_time (const std::string& filename)
+{
+#if defined (OCTAVE_USE_WINDOWS_API)
+  std::wstring wfull_name = sys::u8_to_wstring (filename);
+  HANDLE h_file
+    = CreateFile (wfull_name.c_str (), GENERIC_READ,
+                  FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0,
+                  nullptr);
+  FILETIME last_access_time;
+  GetFileTime (h_file, nullptr, &last_access_time, nullptr);
+
+  m_time
+    = (static_cast<OCTAVE_TIME_T> (last_access_time.dwHighDateTime)) >> 32
+      | last_access_time.dwLowDateTime;
+#else
+  file_stat fs = file_stat (filename);
+  m_time = fs.mtime ().unix_time ();
+#endif
+}
+
 OCTAVE_END_NAMESPACE(sys)
 OCTAVE_END_NAMESPACE(octave)