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