]> git.tdb.fi Git - libs/core.git/blob - source/stat.cpp
New functions in utils.h: readlink realpath
[libs/core.git] / source / stat.cpp
1 /* $Id$
2
3 This file is part of libmspfs
4 Copyright © 2006-2008  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <cerrno>
9 #ifdef WIN32
10 #include <io.h>
11 #endif
12 #include <msp/core/except.h>
13 #include "path.h"
14 #include "stat.h"
15
16 namespace Msp {
17 namespace FS {
18
19 int stat(const Path &fn, struct stat &st)
20 {
21         return ::stat(fn.str().c_str(), &st);
22 }
23
24 struct stat stat(const Path &fn)
25 {
26         struct stat st;
27         if(stat(fn, st)==-1)
28                 throw SystemError("stat failed", errno);
29         return st;
30 }
31
32 int lstat(const Path &fn, struct stat &st)
33 {
34 #ifdef WIN32
35         return stat(fn, st);
36 #else
37         return ::lstat(fn.str().c_str(), &st);
38 #endif
39 }
40
41 struct stat lstat(const Path &fn)
42 {
43         struct stat st;
44         if(lstat(fn, st)==-1)
45                 throw SystemError("lstat failed", errno);
46         return st;
47 }
48
49 bool exists(const Path &path)
50 {
51         return access(path.str().c_str(), F_OK)==0;
52 }
53
54 bool is_reg(const Path &path)
55 {
56         struct stat st;
57         if(stat(path, st)==0)
58                 return S_ISREG(st.st_mode);
59         return false;
60 }
61
62 bool is_dir(const Path &path)
63 {
64         struct stat st;
65         if(stat(path, st)==0)
66                 return S_ISDIR(st.st_mode);
67         return false;
68 }
69
70 bool is_link(const Path &path)
71 {
72         struct stat st;
73         if(lstat(path, st)==0)
74                 return S_ISLNK(st.st_mode);
75         return false;
76 }
77
78 } // namespace FS
79 } // namespace Msp