1 #define _FILE_OFFSET_BITS 64
7 #include <msp/core/systemerror.h>
8 #include <msp/strings/format.h>
10 #include "stat_private.h"
15 Stat::Private::Private(const Private &other):
16 owner_id(other.owner_id),
17 group_id(other.group_id)
20 Stat::Private::~Private()
23 Stat Stat::Private::from_struct_stat(const struct stat &st)
27 if(S_ISREG(st.st_mode))
28 result.type = REGULAR;
29 else if(S_ISDIR(st.st_mode))
30 result.type = DIRECTORY;
31 else if(S_ISLNK(st.st_mode))
32 result.type = SYMLINK;
34 result.type = UNKNOWN;
35 result.size = st.st_size;
36 result.alloc_size = st.st_blocks*512;
37 result.mtime = Time::TimeStamp::from_unixtime(st.st_mtime);
39 result.priv = new Private;
40 result.priv->owner_id = st.st_uid;
41 result.priv->group_id = st.st_gid;
46 void Stat::Private::fill_owner_info(Stat::OwnerInfo &result)
52 if(!getpwuid_r(owner_id, &pw, buf, sizeof(buf), &owner) && owner)
53 result.owner = owner->pw_name;
55 result.owner = format("%d", owner_id);
59 if(!getgrgid_r(group_id, &gr, buf, sizeof(buf), &group) && group)
60 result.group = group->gr_name;
62 result.group = format("%d", group_id);
66 Stat Stat::stat(const Path &path)
69 int ret = ::stat(path.str().c_str(), &st);
75 throw system_error("stat");
78 return Private::from_struct_stat(st);
81 Stat Stat::lstat(const Path &path)
84 int ret = ::lstat(path.str().c_str(), &st);
90 throw system_error("lstat");
93 return Private::from_struct_stat(st);
96 bool exists(const Path &path)
98 return access(path.str().c_str(), F_OK)==0;