Skip to content

Commit 6ea6e2c

Browse files
committed
replace stat with std::filesystem
Signed-off-by: Rosen Penev <[email protected]>
1 parent 3b15b6f commit 6ea6e2c

File tree

3 files changed

+19
-33
lines changed

3 files changed

+19
-33
lines changed

samples/geotag.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ namespace fs = std::experimental::filesystem;
2020

2121
#ifdef _WIN32
2222
#include <windows.h>
23-
char* realpath(const char* file, char* path);
24-
#define lstat stat
2523
#if _MSC_VER < 1400
2624
#define strcpy_s(d, l, s) strcpy(d, s)
2725
#define strcat_s(d, l, s) strcat(d, s)
@@ -464,8 +462,6 @@ bool readDir(const char* path, Options& options) {
464462
// print all the files and directories within directory
465463
while ((ent = readdir(dir)) != nullptr) {
466464
std::string pathName = makePath(path, ent->d_name);
467-
struct stat buf;
468-
lstat(path, &buf);
469465
if (ent->d_name[0] != '.') {
470466
// printf("reading %s => %s\n",ent->d_name,pathName.c_str());
471467
if (getFileType(pathName, options) == typeImage) {

src/basicio.cpp

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
#include <iostream>
2020

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

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

3938
#ifdef _WIN32
40-
using mode_t = unsigned short;
4139
#include <io.h>
4240
#include <windows.h>
4341
#endif
@@ -101,8 +99,8 @@ class FileIo::Impl {
10199
// TYPES
102100
//! Simple struct stat wrapper for internal use
103101
struct StructStat {
104-
mode_t st_mode{0}; //!< Permissions
105-
off_t st_size{0}; //!< Size
102+
fs::perms st_mode{}; //!< Permissions
103+
std::uintmax_t st_size{}; //!< Size
106104
};
107105
// #endif
108106
// METHODS
@@ -181,13 +179,13 @@ int FileIo::Impl::switchMode(OpMode opMode) {
181179
} // FileIo::Impl::switchMode
182180

183181
int FileIo::Impl::stat(StructStat& buf) const {
184-
struct stat st;
185-
auto ret = ::stat(path_.c_str(), &st);
186-
if (ret == 0) {
187-
buf.st_size = st.st_size;
188-
buf.st_mode = st.st_mode;
182+
try {
183+
buf.st_size = fs::file_size(path_);
184+
buf.st_mode = fs::status(path_).permissions();
185+
return 0;
186+
} catch (const fs::filesystem_error&) {
187+
return -1;
189188
}
190-
return ret;
191189
} // FileIo::Impl::stat
192190

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

358356
bool statOk = true;
359-
mode_t origStMode = 0;
360-
auto pf = path().c_str();
357+
fs::perms origStMode = {};
358+
auto pf = path();
361359

362360
Impl::StructStat buf1;
363361
if (p_->stat(buf1) == -1) {
@@ -372,14 +370,15 @@ void FileIo::transfer(BasicIo& src) {
372370
// that file has been opened with FILE_SHARE_DELETE by another process,
373371
// like a virus scanner or disk indexer
374372
// (see also http://stackoverflow.com/a/11023068)
375-
auto ret = ReplaceFileA(pf, fileIo->path().c_str(), nullptr, REPLACEFILE_IGNORE_MERGE_ERRORS, nullptr, nullptr);
373+
auto ret =
374+
ReplaceFileA(pf.c_str(), fileIo->path().c_str(), nullptr, REPLACEFILE_IGNORE_MERGE_ERRORS, nullptr, nullptr);
376375
if (ret == 0) {
377376
if (GetLastError() != ERROR_FILE_NOT_FOUND)
378377
throw Error(ErrorCode::kerFileRenameFailed, fileIo->path(), pf, strError());
379378
fs::rename(fileIo->path(), pf);
380379
fs::remove(fileIo->path());
381380
} else {
382-
if (fileExists(pf) && ::remove(pf) != 0)
381+
if (fileExists(pf) && fs::remove(pf) != 0)
383382
throw Error(ErrorCode::kerCallFailed, pf, strError(), "fs::remove");
384383
fs::rename(fileIo->path(), pf);
385384
fs::remove(fileIo->path());
@@ -392,15 +391,10 @@ void FileIo::transfer(BasicIo& src) {
392391
fs::remove(fileIo->path());
393392
#endif
394393
// Check permissions of new file
395-
struct stat buf2;
396-
if (statOk && ::stat(pf, &buf2) == -1) {
397-
statOk = false;
398-
#ifndef SUPPRESS_WARNINGS
399-
EXV_WARNING << Error(ErrorCode::kerCallFailed, pf, strError(), "::stat") << "\n";
400-
#endif
401-
}
394+
auto newStMode = fs::status(pf).permissions();
402395
// Set original file permissions
403-
if (statOk && origStMode != buf2.st_mode && ::chmod(pf, origStMode) == -1) {
396+
if (statOk && origStMode != newStMode) {
397+
fs::permissions(pf, origStMode);
404398
#ifndef SUPPRESS_WARNINGS
405399
EXV_WARNING << Error(ErrorCode::kerCallFailed, pf, strError(), "::chmod") << "\n";
406400
#endif
@@ -1718,11 +1712,7 @@ DataBuf readFile(const std::string& path) {
17181712
if (file.open("rb") != 0) {
17191713
throw Error(ErrorCode::kerFileOpenFailed, path, "rb", strError());
17201714
}
1721-
struct stat st;
1722-
if (0 != ::stat(path.c_str(), &st)) {
1723-
throw Error(ErrorCode::kerCallFailed, path, strError(), "::stat");
1724-
}
1725-
DataBuf buf(st.st_size);
1715+
DataBuf buf(fs::file_size(path));
17261716
if (file.read(buf.data(), buf.size()) != buf.size()) {
17271717
throw Error(ErrorCode::kerCallFailed, path, strError(), "FileIo::read");
17281718
}

src/futils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace fs = std::experimental::filesystem;
3535
#endif
3636

3737
#if __has_include(<unistd.h>)
38-
#include <unistd.h> // for stat()
38+
#include <unistd.h> // for getpid()
3939
#endif
4040

4141
#if defined(__FreeBSD__)

0 commit comments

Comments
 (0)