f5e914eada807aa0ee356c736fd6a63fbecb3be0
[libs/core.git] / source / fs / stat.cpp
1 #ifdef WIN32
2 #include <windows.h>
3 #include <aclapi.h>
4 #else
5 #define _FILE_OFFSET_BITS 64
6 #include <cerrno>
7 #include <unistd.h>
8 #include <sys/stat.h>
9 #include <grp.h>
10 #include <pwd.h>
11 #endif
12 #include <msp/core/systemerror.h>
13 #include <msp/strings/format.h>
14 #include <msp/time/rawtime_private.h>
15 #include "path.h"
16 #include "stat.h"
17
18 using namespace std;
19
20 namespace {
21
22 #ifdef WIN32
23 string get_account_name(PSID sid)
24 {
25         char name[1024];
26         DWORD nlen = sizeof(name);
27         char domain[1024];
28         DWORD dlen = sizeof(domain);
29         SID_NAME_USE use;
30         if(!LookupAccountSid(0, sid, name, &nlen, domain, &dlen, &use))
31                 throw Msp::system_error("LookupAccountSid");
32         return Msp::format("%s/%s", name, domain);
33 }
34 #endif
35
36 }
37
38
39 namespace Msp {
40 namespace FS {
41
42 #ifndef WIN32
43 struct Stat::Private
44 {
45         static Stat from_struct_stat(const struct stat &);
46 };
47
48 Stat Stat::Private::from_struct_stat(const struct stat &st)
49 {
50         Stat result;
51         result.exists = true;
52         if(S_ISREG(st.st_mode))
53                 result.type = REGULAR;
54         else if(S_ISDIR(st.st_mode))
55                 result.type = DIRECTORY;
56         else if(S_ISLNK(st.st_mode))
57                 result.type = SYMLINK;
58         else
59                 result.type = UNKNOWN;
60         result.size = st.st_size;
61         result.alloc_size = st.st_blocks*512;
62         result.mtime = Time::TimeStamp::from_unixtime(st.st_mtime);
63
64         char buf[1024];
65
66         struct passwd pw;
67         struct passwd *owner;
68         if(!getpwuid_r(st.st_uid, &pw, buf, sizeof(buf), &owner) && owner)
69                 result.owner = owner->pw_name;
70         else
71                 result.owner = format("%d", st.st_uid);
72
73         struct group gr;
74         struct group *group;
75         if(!getgrgid_r(st.st_gid, &gr, buf, sizeof(buf), &group) && group)
76                 result.group = group->gr_name;
77         else
78                 result.group = format("%d", st.st_gid);
79
80         return result;
81 }
82 #endif
83
84 Stat::Stat():
85         exists(false),
86         type(UNKNOWN),
87         size(0),
88         alloc_size(0)
89 { }
90
91 Stat Stat::stat(const Path &path)
92 {
93 #ifdef WIN32
94         HANDLE handle;
95         handle = CreateFile(path.str().c_str(), 0, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
96         if(handle==INVALID_HANDLE_VALUE)
97                 throw system_error("CreateFile");
98
99         BY_HANDLE_FILE_INFORMATION info;
100         if(!GetFileInformationByHandle(handle, &info))
101         {
102                 CloseHandle(handle);
103                 throw system_error("GetFileInformationByHandle");
104         }
105
106         Stat result;
107         if(info.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
108                 result.type = DIRECTORY;
109         else
110                 result.type = REGULAR;
111
112         result.size = FileSize(info.nFileSizeHigh)<<32 | info.nFileSizeLow;
113         result.alloc_size = (result.size+511)&~511;
114         result.mtime = Time::TimeStamp(Time::filetime_to_rawtime(info.ftLastWriteTime));
115
116         PSECURITY_DESCRIPTOR sec_desc;
117         PSID owner = 0;
118         PSID group = 0;
119         if(!GetSecurityInfo(handle, SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION, &owner, &group, 0, 0, &sec_desc))
120         {
121                 CloseHandle(handle);
122                 throw system_error("GetSecurityInfo");
123         }
124
125         if(owner)
126                 result.owner = get_account_name(owner);
127         if(group)
128                 result.group = get_account_name(group);
129
130         LocalFree(sec_desc);
131
132         CloseHandle(handle);
133
134         return result;
135 #else
136         struct stat st;
137         int ret = ::stat(path.str().c_str(), &st);
138         if(ret==-1)
139         {
140                 if(errno==ENOENT)
141                         return Stat();
142                 else
143                         throw system_error("stat");
144         }
145
146         return Private::from_struct_stat(st);
147 #endif
148 }
149
150 Stat Stat::lstat(const Path &path)
151 {
152 #ifdef WIN32
153         return stat(path);
154 #else
155         struct stat st;
156         int ret = ::lstat(path.str().c_str(), &st);
157         if(ret==-1)
158         {
159                 if(errno==ENOENT)
160                         return Stat();
161                 else
162                         throw system_error("lstat");
163         }
164
165         return Private::from_struct_stat(st);
166 #endif
167 }
168
169 bool exists(const Path &path)
170 {
171 #ifdef WIN32
172         return GetFileAttributes(path.str().c_str())!=INVALID_FILE_ATTRIBUTES;
173 #else
174         return access(path.str().c_str(), F_OK)==0;
175 #endif
176 }
177
178 } // namespace FS
179 } // namespace Msp