]> git.tdb.fi Git - libs/core.git/blob - source/fs/unix/stat.cpp
1ba62cd1c3f8a752032c6f1a1baca95ce9a28051
[libs/core.git] / source / fs / unix / stat.cpp
1 #define _FILE_OFFSET_BITS 64
2 #include <cerrno>
3 #include <unistd.h>
4 #include <sys/stat.h>
5 #include <msp/core/systemerror.h>
6 #include "stat.h"
7 #include "stat_private.h"
8
9 namespace Msp {
10 namespace FS {
11
12 Stat::Private::Private(const Private &other):
13         owner_id(other.owner_id),
14         group_id(other.group_id)
15 { }
16
17 Stat::Private::~Private()
18 { }
19
20 Stat Stat::Private::from_struct_stat(const struct stat &st)
21 {
22         Stat result;
23         result.exists = true;
24         if(S_ISREG(st.st_mode))
25                 result.type = REGULAR;
26         else if(S_ISDIR(st.st_mode))
27                 result.type = DIRECTORY;
28         else if(S_ISLNK(st.st_mode))
29                 result.type = SYMLINK;
30         else
31                 result.type = UNKNOWN;
32         result.size = st.st_size;
33         result.alloc_size = st.st_blocks*512;
34         result.mtime = Time::TimeStamp::from_unixtime(st.st_mtime);
35
36         result.priv = new Private;
37         result.priv->owner_id = st.st_uid;
38         result.priv->group_id = st.st_gid;
39
40         return result;
41 }
42
43
44 Stat Stat::stat(const Path &path)
45 {
46         struct stat st;
47         int ret = ::stat(path.str().c_str(), &st);
48         if(ret==-1)
49         {
50                 if(errno==ENOENT)
51                         return Stat();
52                 else
53                         throw system_error("stat");
54         }
55
56         return Private::from_struct_stat(st);
57 }
58
59 Stat Stat::lstat(const Path &path)
60 {
61         struct stat st;
62         int ret = ::lstat(path.str().c_str(), &st);
63         if(ret==-1)
64         {
65                 if(errno==ENOENT)
66                         return Stat();
67                 else
68                         throw system_error("lstat");
69         }
70
71         return Private::from_struct_stat(st);
72 }
73
74 bool exists(const Path &path)
75 {
76         return access(path.str().c_str(), F_OK)==0;
77 }
78
79 } // namespace FS
80 } // namespace Msp