]> git.tdb.fi Git - libs/core.git/commitdiff
Change stat functions so that it's again possible to determine file existence
authorMikko Rasa <tdb@tdb.fi>
Tue, 6 Sep 2011 11:38:48 +0000 (14:38 +0300)
committerMikko Rasa <mikko.rasa@movial.com>
Tue, 6 Sep 2011 11:38:48 +0000 (14:38 +0300)
source/fs/stat.cpp
source/fs/stat.h

index d11867d99b6af41a427dba423673acc622390a65..716a7d6a4f82707587aed2a9b452aa4bde87715c 100644 (file)
@@ -3,6 +3,7 @@
 #include <aclapi.h>
 #else
 #define _FILE_OFFSET_BITS 64
+#include <cerrno>
 #include <sys/stat.h>
 #include <grp.h>
 #include <pwd.h>
@@ -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
index 0d5f6de184b6f7a725e648119172573125799375..6ae551fd13836955bbd6a11f347a2b236abe0022 100644 (file)
@@ -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 &);