mirror of
https://github.com/Kitware/CMake.git
synced 2025-10-17 07:11:52 +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 <cm/memory>
|
||||||
|
|
||||||
|
#include "cmsys/Status.hxx"
|
||||||
|
|
||||||
#include "cm_sys_stat.h"
|
#include "cm_sys_stat.h"
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
@@ -13,6 +15,8 @@
|
|||||||
|
|
||||||
# include "cmSystemTools.h"
|
# include "cmSystemTools.h"
|
||||||
#else
|
#else
|
||||||
|
# include <cerrno>
|
||||||
|
|
||||||
# include <utime.h>
|
# include <utime.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -66,7 +70,7 @@ cmFileTimes::cmFileTimes(std::string const& fileName)
|
|||||||
}
|
}
|
||||||
cmFileTimes::~cmFileTimes() = default;
|
cmFileTimes::~cmFileTimes() = default;
|
||||||
|
|
||||||
bool cmFileTimes::Load(std::string const& fileName)
|
cmsys::Status cmFileTimes::Load(std::string const& fileName)
|
||||||
{
|
{
|
||||||
std::unique_ptr<Times> ptr;
|
std::unique_ptr<Times> ptr;
|
||||||
if (this->IsValid()) {
|
if (this->IsValid()) {
|
||||||
@@ -82,29 +86,29 @@ bool cmFileTimes::Load(std::string const& fileName)
|
|||||||
GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING,
|
GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING,
|
||||||
FILE_FLAG_BACKUP_SEMANTICS, 0);
|
FILE_FLAG_BACKUP_SEMANTICS, 0);
|
||||||
if (!handle) {
|
if (!handle) {
|
||||||
return false;
|
return cmsys::Status::Windows_GetLastError();
|
||||||
}
|
}
|
||||||
if (!GetFileTime(handle, &ptr->timeCreation, &ptr->timeLastAccess,
|
if (!GetFileTime(handle, &ptr->timeCreation, &ptr->timeLastAccess,
|
||||||
&ptr->timeLastWrite)) {
|
&ptr->timeLastWrite)) {
|
||||||
return false;
|
return cmsys::Status::Windows_GetLastError();
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
struct stat st;
|
struct stat st;
|
||||||
if (stat(fileName.c_str(), &st) < 0) {
|
if (stat(fileName.c_str(), &st) < 0) {
|
||||||
return false;
|
return cmsys::Status::POSIX_errno();
|
||||||
}
|
}
|
||||||
ptr->timeBuf.actime = st.st_atime;
|
ptr->timeBuf.actime = st.st_atime;
|
||||||
ptr->timeBuf.modtime = st.st_mtime;
|
ptr->timeBuf.modtime = st.st_mtime;
|
||||||
#endif
|
#endif
|
||||||
// Accept times
|
// Accept times
|
||||||
this->times = std::move(ptr);
|
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()) {
|
if (!this->IsValid()) {
|
||||||
return false;
|
return cmsys::Status::POSIX(EINVAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(_WIN32) && !defined(__CYGWIN__)
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
||||||
@@ -112,18 +116,28 @@ bool cmFileTimes::Store(std::string const& fileName) const
|
|||||||
cmSystemTools::ConvertToWindowsExtendedPath(fileName).c_str(),
|
cmSystemTools::ConvertToWindowsExtendedPath(fileName).c_str(),
|
||||||
FILE_WRITE_ATTRIBUTES, 0, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
|
FILE_WRITE_ATTRIBUTES, 0, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
|
||||||
if (!handle) {
|
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->timeLastAccess,
|
||||||
&this->times->timeLastWrite) != 0;
|
&this->times->timeLastWrite) == 0) {
|
||||||
|
return cmsys::Status::Windows_GetLastError();
|
||||||
|
}
|
||||||
#else
|
#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
|
#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;
|
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 <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include "cmsys/Status.hxx"
|
||||||
|
|
||||||
/** \class cmFileTimes
|
/** \class cmFileTimes
|
||||||
* \brief Loads and stores file times.
|
* \brief Loads and stores file times.
|
||||||
*/
|
*/
|
||||||
@@ -21,12 +23,13 @@ public:
|
|||||||
//! @return true, if file times were loaded successfully
|
//! @return true, if file times were loaded successfully
|
||||||
bool IsValid() const { return (this->times != nullptr); }
|
bool IsValid() const { return (this->times != nullptr); }
|
||||||
//! Try to load the file times from @a fileName and @return IsValid()
|
//! 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())
|
//! 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
|
//! 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:
|
private:
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
Reference in New Issue
Block a user