summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBen Boeckel <ben.boeckel@kitware.com>2023-12-02 03:36:00 (GMT)
committerBen Boeckel <ben.boeckel@kitware.com>2023-12-02 03:58:05 (GMT)
commit0639a32d3a27b9a89b82d915f58cfd59a634bb44 (patch)
tree3f9715f9bc42a1115fc74342b5561bdbcf64111b
parent01deb5809854b1ce1fdaa8fd8e31adf31a134904 (diff)
downloadCMake-0639a32d3a27b9a89b82d915f58cfd59a634bb44.zip
CMake-0639a32d3a27b9a89b82d915f58cfd59a634bb44.tar.gz
CMake-0639a32d3a27b9a89b82d915f58cfd59a634bb44.tar.bz2
cmFileTimes: return status codes from APIs
This avoids accidentally overwriting the global error state before fetching the intended error code.
-rw-r--r--Source/cmFileTimes.cxx42
-rw-r--r--Source/cmFileTimes.h9
2 files changed, 34 insertions, 17 deletions
diff --git a/Source/cmFileTimes.cxx b/Source/cmFileTimes.cxx
index 0aaab1d..6835e46 100644
--- a/Source/cmFileTimes.cxx
+++ b/Source/cmFileTimes.cxx
@@ -6,6 +6,8 @@
#include <cm/memory>
+#include "cmsys/Status.hxx"
+
#include "cm_sys_stat.h"
#if defined(_WIN32)
@@ -13,6 +15,8 @@
# include "cmSystemTools.h"
#else
+# include <cerrno>
+
# include <utime.h>
#endif
@@ -66,7 +70,7 @@ cmFileTimes::cmFileTimes(std::string const& fileName)
}
cmFileTimes::~cmFileTimes() = default;
-bool cmFileTimes::Load(std::string const& fileName)
+cmsys::Status cmFileTimes::Load(std::string const& fileName)
{
std::unique_ptr<Times> ptr;
if (this->IsValid()) {
@@ -82,29 +86,29 @@ bool cmFileTimes::Load(std::string const& fileName)
GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, 0);
if (!handle) {
- return false;
+ return cmsys::Status::Windows_GetLastError();
}
if (!GetFileTime(handle, &ptr->timeCreation, &ptr->timeLastAccess,
&ptr->timeLastWrite)) {
- return false;
+ return cmsys::Status::Windows_GetLastError();
}
#else
struct stat st;
if (stat(fileName.c_str(), &st) < 0) {
- return false;
+ return cmsys::Status::POSIX_errno();
}
ptr->timeBuf.actime = st.st_atime;
ptr->timeBuf.modtime = st.st_mtime;
#endif
// Accept times
this->times = std::move(ptr);
- return true;
+ return cmsys::Status::Success();
}
-bool cmFileTimes::Store(std::string const& fileName) const
+cmsys::Status cmFileTimes::Store(std::string const& fileName) const
{
if (!this->IsValid()) {
- return false;
+ return cmsys::Status::POSIX(EINVAL);
}
#if defined(_WIN32) && !defined(__CYGWIN__)
@@ -112,18 +116,28 @@ bool cmFileTimes::Store(std::string const& fileName) const
cmSystemTools::ConvertToWindowsExtendedPath(fileName).c_str(),
FILE_WRITE_ATTRIBUTES, 0, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
if (!handle) {
- return false;
+ return cmsys::Status::Windows_GetLastError();
+ }
+ if (SetFileTime(handle, &this->times->timeCreation,
+ &this->times->timeLastAccess,
+ &this->times->timeLastWrite) == 0) {
+ return cmsys::Status::Windows_GetLastError();
}
- return SetFileTime(handle, &this->times->timeCreation,
- &this->times->timeLastAccess,
- &this->times->timeLastWrite) != 0;
#else
- return utime(fileName.c_str(), &this->times->timeBuf) >= 0;
+ if (utime(fileName.c_str(), &this->times->timeBuf) < 0) {
+ return cmsys::Status::POSIX_errno();
+ }
#endif
+ return cmsys::Status::Success();
}
-bool cmFileTimes::Copy(std::string const& fromFile, std::string const& toFile)
+cmsys::Status cmFileTimes::Copy(std::string const& fromFile,
+ std::string const& toFile)
{
cmFileTimes fileTimes;
- return (fileTimes.Load(fromFile) && fileTimes.Store(toFile));
+ cmsys::Status load_status = fileTimes.Load(fromFile);
+ if (!load_status) {
+ return load_status;
+ }
+ return fileTimes.Store(toFile);
}
diff --git a/Source/cmFileTimes.h b/Source/cmFileTimes.h
index 50d64fd..58d24bc 100644
--- a/Source/cmFileTimes.h
+++ b/Source/cmFileTimes.h
@@ -7,6 +7,8 @@
#include <memory>
#include <string>
+#include "cmsys/Status.hxx"
+
/** \class cmFileTimes
* \brief Loads and stores file times.
*/
@@ -21,12 +23,13 @@ public:
//! @return true, if file times were loaded successfully
bool IsValid() const { return (this->times != nullptr); }
//! Try to load the file times from @a fileName and @return IsValid()
- bool Load(std::string const& fileName);
+ cmsys::Status Load(std::string const& fileName);
//! Stores the file times at @a fileName (if IsValid())
- bool Store(std::string const& fileName) const;
+ cmsys::Status Store(std::string const& fileName) const;
//! Copies the file times of @a fromFile to @a toFile
- static bool Copy(std::string const& fromFile, std::string const& toFile);
+ static cmsys::Status Copy(std::string const& fromFile,
+ std::string const& toFile);
private:
#ifdef _WIN32