]> git.tdb.fi Git - libs/core.git/blob - source/fs/stat.cpp
Exception rework for fs components
[libs/core.git] / source / fs / stat.cpp
1 #ifdef WIN32
2 #include <io.h>
3 #endif
4 #include <msp/core/systemerror.h>
5 #include "path.h"
6 #include "stat.h"
7
8 namespace Msp {
9 namespace FS {
10
11 int stat(const Path &fn, struct stat &st)
12 {
13         return ::stat(fn.str().c_str(), &st);
14 }
15
16 struct stat stat(const Path &fn)
17 {
18         struct stat st;
19         if(stat(fn, st)==-1)
20                 throw system_error("stat");
21         return st;
22 }
23
24 int lstat(const Path &fn, struct stat &st)
25 {
26 #ifdef WIN32
27         return stat(fn, st);
28 #else
29         return ::lstat(fn.str().c_str(), &st);
30 #endif
31 }
32
33 struct stat lstat(const Path &fn)
34 {
35         struct stat st;
36         if(lstat(fn, st)==-1)
37                 throw system_error("lstat");
38         return st;
39 }
40
41 bool exists(const Path &path)
42 {
43         return access(path.str().c_str(), F_OK)==0;
44 }
45
46 bool is_reg(const Path &path)
47 {
48         struct stat st;
49         if(stat(path, st)==0)
50                 return S_ISREG(st.st_mode);
51         return false;
52 }
53
54 bool is_dir(const Path &path)
55 {
56         struct stat st;
57         if(stat(path, st)==0)
58                 return S_ISDIR(st.st_mode);
59         return false;
60 }
61
62 bool is_link(const Path &path)
63 {
64 #ifdef WIN32
65         (void)path;
66 #else
67         struct stat st;
68         if(lstat(path, st)==0)
69                 return S_ISLNK(st.st_mode);
70 #endif
71         return false;
72 }
73
74 } // namespace FS
75 } // namespace Msp