Skip to content

Commit

Permalink
replace stat with std::filesystem
Browse files Browse the repository at this point in the history
Signed-off-by: Rosen Penev <[email protected]>
  • Loading branch information
neheb committed Aug 3, 2023
1 parent 06cf19a commit 4b886a2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 37 deletions.
4 changes: 0 additions & 4 deletions samples/geotag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ namespace fs = std::experimental::filesystem;

#ifdef _WIN32
#include <windows.h>
char* realpath(const char* file, char* path);
#define lstat stat
#if _MSC_VER < 1400
#define strcpy_s(d, l, s) strcpy(d, s)
#define strcat_s(d, l, s) strcat(d, s)
Expand Down Expand Up @@ -464,8 +462,6 @@ bool readDir(const char* path, Options& options) {
// print all the files and directories within directory
while ((ent = readdir(dir)) != nullptr) {
std::string pathName = makePath(path, ent->d_name);
struct stat buf;
lstat(path, &buf);
if (ent->d_name[0] != '.') {
// printf("reading %s => %s\n",ent->d_name,pathName.c_str());
if (getFileType(pathName, options) == typeImage) {
Expand Down
47 changes: 18 additions & 29 deletions src/basicio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
#include <iostream>

// + standard includes
#include <fcntl.h> // _O_BINARY in FileIo::FileIo
#include <sys/stat.h> // for stat, chmod
#include <fcntl.h> // _O_BINARY in FileIo::FileIo

#if __has_include(<sys/mman.h>)
#include <sys/mman.h> // for mmap and munmap
Expand All @@ -37,7 +36,6 @@
#endif

#ifdef _WIN32
using mode_t = unsigned short;
#include <io.h>
#include <windows.h>
#endif
Expand Down Expand Up @@ -101,8 +99,8 @@ class FileIo::Impl {
// TYPES
//! Simple struct stat wrapper for internal use
struct StructStat {
mode_t st_mode{0}; //!< Permissions
off_t st_size{0}; //!< Size
fs::perms st_mode{}; //!< Permissions
std::uintmax_t st_size{}; //!< Size
};
// #endif
// METHODS
Expand Down Expand Up @@ -181,13 +179,13 @@ int FileIo::Impl::switchMode(OpMode opMode) {
} // FileIo::Impl::switchMode

int FileIo::Impl::stat(StructStat& buf) const {
struct stat st;
auto ret = ::stat(path_.c_str(), &st);
if (ret == 0) {
buf.st_size = st.st_size;
buf.st_mode = st.st_mode;
try {
buf.st_size = fs::file_size(path_);
buf.st_mode = fs::status(path_).permissions();
return 0;
} catch (fs::filesystem_error&) {
return -1;
}
return ret;
} // FileIo::Impl::stat

FileIo::FileIo(const std::string& path) : p_(std::make_unique<Impl>(path)) {
Expand Down Expand Up @@ -356,8 +354,8 @@ void FileIo::transfer(BasicIo& src) {
close();

bool statOk = true;
mode_t origStMode = 0;
auto pf = path().c_str();
std::filesystem::perms origStMode = {};
auto pf = path();

Impl::StructStat buf1;
if (p_->stat(buf1) == -1) {
Expand All @@ -377,8 +375,8 @@ void FileIo::transfer(BasicIo& src) {
if (hKernel) {
auto pfcn_ReplaceFileA = reinterpret_cast<ReplaceFileA_t>(GetProcAddress(hKernel, "ReplaceFileA"));
if (pfcn_ReplaceFileA) {
BOOL ret =
pfcn_ReplaceFileA(pf, fileIo->path().c_str(), nullptr, REPLACEFILE_IGNORE_MERGE_ERRORS, nullptr, nullptr);
BOOL ret = pfcn_ReplaceFileA(pf.c_str(), fileIo->path().c_str(), nullptr, REPLACEFILE_IGNORE_MERGE_ERRORS,
nullptr, nullptr);
if (ret == 0) {
if (GetLastError() == ERROR_FILE_NOT_FOUND) {
fs::rename(fileIo->path(), pf);
Expand All @@ -388,7 +386,7 @@ void FileIo::transfer(BasicIo& src) {
}
}
} else {
if (fileExists(pf) && ::remove(pf) != 0) {
if (fileExists(pf) && fs::remove(pf) != 0) {
throw Error(ErrorCode::kerCallFailed, pf, strError(), "fs::remove");
}
fs::rename(fileIo->path(), pf);
Expand All @@ -403,15 +401,10 @@ void FileIo::transfer(BasicIo& src) {
fs::remove(fileIo->path());
#endif
// Check permissions of new file
struct stat buf2;
if (statOk && ::stat(pf, &buf2) == -1) {
statOk = false;
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << Error(ErrorCode::kerCallFailed, pf, strError(), "::stat") << "\n";
#endif
}
auto newStMode = fs::status(pf).permissions();
// Set original file permissions
if (statOk && origStMode != buf2.st_mode && ::chmod(pf, origStMode) == -1) {
if (statOk && origStMode != newStMode) {
fs::permissions(pf, origStMode);
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << Error(ErrorCode::kerCallFailed, pf, strError(), "::chmod") << "\n";
#endif
Expand Down Expand Up @@ -1729,11 +1722,7 @@ DataBuf readFile(const std::string& path) {
if (file.open("rb") != 0) {
throw Error(ErrorCode::kerFileOpenFailed, path, "rb", strError());
}
struct stat st;
if (0 != ::stat(path.c_str(), &st)) {
throw Error(ErrorCode::kerCallFailed, path, strError(), "::stat");
}
DataBuf buf(st.st_size);
DataBuf buf(fs::file_size(path));
if (file.read(buf.data(), buf.size()) != buf.size()) {
throw Error(ErrorCode::kerCallFailed, path, strError(), "FileIo::read");
}
Expand Down
4 changes: 0 additions & 4 deletions src/futils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ namespace fs = std::experimental::filesystem;
#include <libproc.h>
#endif

#if __has_include(<unistd.h>)
#include <unistd.h> // for stat()
#endif

#if defined(__FreeBSD__)
// clang-format off
#include <sys/mount.h>
Expand Down

0 comments on commit 4b886a2

Please sign in to comment.