comparison liboctave/system/oct-time.h @ 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 1daf8bfceac3
children ae4e19c0a2b1 2e484f9f1f18
comparison
equal deleted inserted replaced
32089:212145b8e5f0 32090:7d663f770c5a
28 28
29 #include "octave-config.h" 29 #include "octave-config.h"
30 30
31 #include <iosfwd> 31 #include <iosfwd>
32 #include <string> 32 #include <string>
33
34 #if defined (OCTAVE_USE_WINDOWS_API)
35 // Some Windows headers must be included in a certain order.
36 // Don't include "windows.h" here to avoid potential issues due to that.
37 // Instead just define the one type we need for the interface of one function.
38 struct OCTAVE_WIN_FILETIME
39 {
40 uint32_t dwLowDateTime;
41 uint32_t dwHighDateTime;
42 };
43 #endif
44
33 45
34 static inline double 46 static inline double
35 as_double (OCTAVE_TIME_T sec, long usec) 47 as_double (OCTAVE_TIME_T sec, long usec)
36 { 48 {
37 // Unix time will be exactly representable as a double for more than 49 // Unix time will be exactly representable as a double for more than
463 long m_nsignals; 475 long m_nsignals;
464 long m_nvcsw; 476 long m_nvcsw;
465 long m_nivcsw; 477 long m_nivcsw;
466 }; 478 };
467 479
480 // class to handle file time efficiently on different platforms
481
482 class OCTAVE_API file_time
483 {
484 public:
485
486 file_time ();
487
488 file_time (OCTAVE_TIME_T t)
489 : m_time (t)
490 { }
491
492 #if defined (OCTAVE_USE_WINDOWS_API)
493 file_time (OCTAVE_WIN_FILETIME& t)
494 {
495 m_time = (static_cast<OCTAVE_TIME_T> (t.dwHighDateTime)) >> 32
496 | t.dwLowDateTime;
497 }
498 #endif
499
500 file_time (const std::string& filename);
501
502 file_time (const file_time& ot)
503 {
504 m_time = ot.time ();
505 }
506
507 file_time& operator = (const file_time& ot)
508 {
509 if (this != &ot)
510 m_time = ot.time ();
511
512 return *this;
513 }
514
515 ~file_time () = default;
516
517 inline static file_time time_resolution ()
518 {
519 #if defined (OCTAVE_USE_WINDOWS_API)
520 // FAT file systems have 2 seconds resolution for the modification time.
521 static OCTAVE_TIME_T time_resolution = 20000;
522 #else
523 // Assume 1 second (see file_stat)
524 static OCTAVE_TIME_T time_resolution = 1;
525 #endif
526 return time_resolution;
527 }
528
529 inline bool
530 operator == (const file_time& t2) const
531 {
532 return time () == t2.time ();
533 }
534
535 inline bool
536 operator != (const file_time& t2) const
537 {
538 return ! (*this == t2);
539 }
540
541 inline bool
542 operator < (const file_time& t2) const
543 {
544 return time () < t2.time ();
545 }
546
547 inline bool
548 operator <= (const file_time& t2) const
549 {
550 return (*this < t2 || *this == t2);
551 }
552
553 inline bool
554 operator > (const file_time& t2) const
555 {
556 return time () > t2.time ();
557 }
558
559 inline bool
560 operator >= (const file_time& t2) const
561 {
562 return (*this > t2 || *this == t2);
563 }
564
565 inline file_time
566 operator + (const file_time& t2) const
567 {
568 return file_time (time () + t2.time ());
569 }
570
571 inline file_time
572 operator + (const OCTAVE_TIME_T t2) const
573 {
574 return file_time (time () + t2);
575 }
576
577 OCTAVE_TIME_T time () const { return m_time; }
578
579 private:
580
581 // The native file time type differs per platform.
582 // On POSIX, this is the number of 1 second intervals since the epoch.
583 // On Windows, this is the number of 0.1 ms intervals since a different epoch.
584 OCTAVE_TIME_T m_time;
585 };
586
468 OCTAVE_END_NAMESPACE(sys) 587 OCTAVE_END_NAMESPACE(sys)
469 OCTAVE_END_NAMESPACE(octave) 588 OCTAVE_END_NAMESPACE(octave)
470 589
471 #endif 590 #endif