llvm.org GIT mirror llvm / f0b29b8
[Support] NFC: Allow modifying access/modification times independently in sys::fs::setLastModificationAndAccessTime. Summary: Add an overload to sys::fs::setLastModificationAndAccessTime that allows setting last access and modification times separately. This will allow tools to use this API when they want to preserve both the access and modification times from an input file, which may be different. Also note that both the POSIX (futimens/futimes) and Windows (SetFileTime) APIs take the two timestamps in the order of (1) access (2) modification time, so this renames the method to "setLastAccessAndModificationTime" to make it clear which timestamp is which. For existing callers, the 1-arg overload just sets both timestamps to the same thing. Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D50521 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@339628 91177308-0d34-0410-b5e6-96231b3b80d8 Jordan Rupprecht 1 year, 7 days ago
5 changed file(s) with 25 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
665665 /// @returns errc::success if the file times were successfully set, otherwise a
666666 /// platform-specific error_code or errc::function_not_supported on
667667 /// platforms where the functionality isn't available.
668 std::error_code setLastModificationAndAccessTime(int FD, TimePoint<> Time);
668 std::error_code setLastAccessAndModificationTime(int FD, TimePoint<> AccessTime,
669 TimePoint<> ModificationTime);
670
671 /// Simpler version that sets both file modification and access time to the same
672 /// time.
673 inline std::error_code setLastAccessAndModificationTime(int FD,
674 TimePoint<> Time) {
675 return setLastAccessAndModificationTime(FD, Time, Time);
676 }
669677
670678 /// Is status available?
671679 ///
582582 return std::error_code();
583583 }
584584
585 std::error_code setLastModificationAndAccessTime(int FD, TimePoint<> Time) {
585 std::error_code setLastAccessAndModificationTime(int FD, TimePoint<> AccessTime,
586 TimePoint<> ModificationTime) {
586587 #if defined(HAVE_FUTIMENS)
587588 timespec Times[2];
588 Times[0] = Times[1] = sys::toTimeSpec(Time);
589 Times[0] = sys::toTimeSpec(AccessTime);
590 Times[1] = sys::toTimeSpec(ModificationTime);
589591 if (::futimens(FD, Times))
590592 return std::error_code(errno, std::generic_category());
591593 return std::error_code();
592594 #elif defined(HAVE_FUTIMES)
593595 timeval Times[2];
594 Times[0] = Times[1] = sys::toTimeVal(
595 std::chrono::time_point_cast(Time));
596 Times[0] = sys::toTimeVal(
597 std::chrono::time_point_cast(AccessTime));
598 Times[1] =
599 sys::toTimeVal(std::chrono::time_point_cast(
600 ModificationTime));
596601 if (::futimes(FD, Times))
597602 return std::error_code(errno, std::generic_category());
598603 return std::error_code();
765765 return std::error_code();
766766 }
767767
768 std::error_code setLastModificationAndAccessTime(int FD, TimePoint<> Time) {
769 FILETIME FT = toFILETIME(Time);
768 std::error_code setLastAccessAndModificationTime(int FD, TimePoint<> AccessTime,
769 TimePoint<> ModificationTime) {
770 FILETIME AccessFT = toFILETIME(AccessTime);
771 FILETIME ModifyFT = toFILETIME(ModificationTime);
770772 HANDLE FileHandle = reinterpret_cast(_get_osfhandle(FD));
771 if (!SetFileTime(FileHandle, NULL, &FT, &FT))
773 if (!SetFileTime(FileHandle, NULL, &AccessFT, &ModifyFT))
772774 return mapWindowsError(::GetLastError());
773775 return std::error_code();
774776 }
409409 auto ModTimeOrErr = C.getLastModified();
410410 failIfError(ModTimeOrErr.takeError());
411411 failIfError(
412 sys::fs::setLastModificationAndAccessTime(FD, ModTimeOrErr.get()));
412 sys::fs::setLastAccessAndModificationTime(FD, ModTimeOrErr.get()));
413413 }
414414
415415 if (close(FD))
12961296 ASSERT_NO_ERROR(sys::fs::openFileForWrite(Twine(TempPath), FileDescriptor,
12971297 fs::CD_OpenExisting));
12981298 TimePoint<> Epoch(std::chrono::milliseconds(0));
1299 ASSERT_NO_ERROR(fs::setLastModificationAndAccessTime(FileDescriptor, Epoch));
1299 ASSERT_NO_ERROR(fs::setLastAccessAndModificationTime(FileDescriptor, Epoch));
13001300 ::close(FileDescriptor);
13011301
13021302 // Open the file and ensure access time is updated, when forced.