From 5a006c4fce0893b63f881a6c9ecd010f15770428 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 6 Sep 2011 14:38:48 +0300 Subject: [PATCH] Change stat functions so that it's again possible to determine file existence --- source/fs/stat.cpp | 17 +++++++++++++++-- source/fs/stat.h | 6 +++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/source/fs/stat.cpp b/source/fs/stat.cpp index d11867d..716a7d6 100644 --- a/source/fs/stat.cpp +++ b/source/fs/stat.cpp @@ -3,6 +3,7 @@ #include #else #define _FILE_OFFSET_BITS 64 +#include #include #include #include @@ -46,6 +47,7 @@ struct Stat::Private Stat Stat::Private::from_struct_stat(const struct stat &st) { Stat result; + result.exists = true; if(S_ISREG(st.st_mode)) result.type = REGULAR; else if(S_ISDIR(st.st_mode)) @@ -79,6 +81,7 @@ Stat Stat::Private::from_struct_stat(const struct stat &st) #endif Stat::Stat(): + exists(false), type(UNKNOWN), size(0), alloc_size(0) @@ -132,7 +135,12 @@ Stat Stat::stat(const Path &path) struct stat st; int ret = ::stat(path.str().c_str(), &st); if(ret==-1) - throw system_error("stat"); + { + if(errno==ENOENT) + return Stat(); + else + throw system_error("stat"); + } return Private::from_struct_stat(st); #endif @@ -146,7 +154,12 @@ Stat Stat::lstat(const Path &path) struct stat st; int ret = ::lstat(path.str().c_str(), &st); if(ret==-1) - throw system_error("lstat"); + { + if(errno==ENOENT) + return Stat(); + else + throw system_error("lstat"); + } return Private::from_struct_stat(st); #endif diff --git a/source/fs/stat.h b/source/fs/stat.h index 0d5f6de..6ae551f 100644 --- a/source/fs/stat.h +++ b/source/fs/stat.h @@ -30,6 +30,7 @@ class Stat private: struct Private; + bool exists; FileType type; FileSize size; FileSize alloc_size; @@ -37,8 +38,9 @@ private: std::string owner; std::string group; - Stat(); public: + Stat(); + FileType get_type() const { return type; } bool is_regular() const { return type==REGULAR; } bool is_directory() const { return type==DIRECTORY; } @@ -49,6 +51,8 @@ public: const std::string &get_owner() const { return owner; } const std::string &get_group() const { return group; } + operator bool() const { return exists; } + /// Returns a Stat object describing a file. static Stat stat(const Path &); static Stat lstat(const Path &); -- 2.43.0