]> git.tdb.fi Git - libs/core.git/blob - source/fs/stat.cpp
Return an empty Stat object for nonexistent files on Windows
[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         {
98                 DWORD err = GetLastError();
99                 if(err==ERROR_FILE_NOT_FOUND)
100                         return Stat();
101                 else
102                         throw system_error("CreateFile", err);
103         }
104
105         BY_HANDLE_FILE_INFORMATION info;
106         if(!GetFileInformationByHandle(handle, &info))
107         {
108                 CloseHandle(handle);
109                 throw system_error("GetFileInformationByHandle");
110         }
111
112         Stat result;
113         if(info.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
114                 result.type = DIRECTORY;
115         else
116                 result.type = REGULAR;
117
118         result.size = FileSize(info.nFileSizeHigh)<<32 | info.nFileSizeLow;
119         result.alloc_size = (result.size+511)&~511;
120         result.mtime = Time::TimeStamp(Time::filetime_to_rawtime(info.ftLastWriteTime));
121
122         PSECURITY_DESCRIPTOR sec_desc;
123         PSID owner = 0;
124         PSID group = 0;
125         if(!GetSecurityInfo(handle, SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION, &owner, &group, 0, 0, &sec_desc))
126         {
127                 CloseHandle(handle);
128                 throw system_error("GetSecurityInfo");
129         }
130
131         if(owner)
132                 result.owner = get_account_name(owner);
133         if(group)
134                 result.group = get_account_name(group);
135
136         LocalFree(sec_desc);
137
138         CloseHandle(handle);
139
140         return result;
141 #else
142         struct stat st;
143         int ret = ::stat(path.str().c_str(), &st);
144         if(ret==-1)
145         {
146                 if(errno==ENOENT)
147                         return Stat();
148                 else
149                         throw system_error("stat");
150         }
151
152         return Private::from_struct_stat(st);
153 #endif
154 }
155
156 Stat Stat::lstat(const Path &path)
157 {
158 #ifdef WIN32
159         return stat(path);
160 #else
161         struct stat st;
162         int ret = ::lstat(path.str().c_str(), &st);
163         if(ret==-1)
164         {
165                 if(errno==ENOENT)
166                         return Stat();
167                 else
168                         throw system_error("lstat");
169         }
170
171         return Private::from_struct_stat(st);
172 #endif
173 }
174
175 bool exists(const Path &path)
176 {
177 #ifdef WIN32
178         return GetFileAttributes(path.str().c_str())!=INVALID_FILE_ATTRIBUTES;
179 #else
180         return access(path.str().c_str(), F_OK)==0;
181 #endif
182 }
183
184 } // namespace FS
185 } // namespace Msp