mirror of
https://github.com/Kitware/CMake.git
synced 2025-10-15 12:16:40 +08:00
cmFileTimes: return status codes from APIs
This avoids accidentally overwriting the global error state before fetching the intended error code.
This commit is contained in:
@@ -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();
|
||||
}
|
||||
return SetFileTime(handle, &this->times->timeCreation,
|
||||
if (SetFileTime(handle, &this->times->timeCreation,
|
||||
&this->times->timeLastAccess,
|
||||
&this->times->timeLastWrite) != 0;
|
||||
&this->times->timeLastWrite) == 0) {
|
||||
return cmsys::Status::Windows_GetLastError();
|
||||
}
|
||||
#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);
|
||||
}
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user