]> git.tdb.fi Git - libs/core.git/blob - source/fs/dir.cpp
Rewrite the stat functions to expose a platform-independent interface
[libs/core.git] / source / fs / dir.cpp
1 #include <cstdlib>
2 #include <cerrno>
3 #include <dirent.h>
4 #include <sys/stat.h>
5 #ifdef WIN32
6 #include <shlobj.h>
7 #endif
8 #include <msp/core/systemerror.h>
9 #include <msp/strings/regex.h>
10 #include <msp/strings/utils.h>
11 #include "dir.h"
12 #include "path.h"
13 #include "stat.h"
14 #include "utils.h"
15
16 using namespace std;
17
18 namespace Msp {
19 namespace FS {
20
21 namespace
22 {
23
24 /** Helper function to determine the location of the program's executable.
25 Caches the last result to cut down filesystem access with repeated calls. */
26 const Path &get_bin_dir(const string &argv0)
27 {
28         static string last_argv0;
29         static Path bin_dir;
30
31         if(!(argv0==last_argv0))
32         {
33                 Path exe;
34                 if(argv0.find('/')==string::npos)
35                 {
36                         const char *path = getenv("PATH");
37                         vector<string> dirs = split(path, ':');
38                         for(vector<string>::const_iterator i=dirs.begin(); i!=dirs.end(); ++i)
39                                 if(exists(Path(*i)/argv0))
40                                 {
41                                         exe = realpath(Path(*i)/argv0);
42                                         break;
43                                 }
44                 }
45                 else
46                         exe = realpath(argv0);
47
48                 last_argv0 = argv0;
49                 bin_dir = dirname(exe);
50         }
51
52         return bin_dir;
53 }
54
55 }
56
57
58 not_a_directory::not_a_directory(const Path &p):
59         runtime_error(p.str())
60 { }
61
62
63 void mkdir(const Path &path, int mode)
64 {
65         int err;
66 #ifdef WIN32
67         // The win32 version of this function doesn't take the mode argument.  Go figure.
68         (void)mode;
69         err = ::mkdir(path.str().c_str());
70 #else
71         err = ::mkdir(path.str().c_str(), mode);
72 #endif
73
74         if(err==-1)
75                 throw system_error("mkdir");
76 }
77
78 void mkpath(const Path &path, int mode)
79 {
80         Path p;
81         for(Path::Iterator i=path.begin(); i!=path.end(); ++i)
82         {
83                 p /= *i;
84 #ifdef WIN32
85                 if(p.size()==1 && p.is_absolute())
86                         continue;
87 #endif
88                 if(exists(p))
89                 {
90                         if(!is_dir(p))
91                                 throw not_a_directory(p);
92                         continue;
93                 }
94                 else
95                         mkdir(p, mode);
96         }
97 }
98
99 void rmdir(const Path &path)
100 {
101         if(::rmdir(path.str().c_str())==-1)
102                 throw system_error("rmdir");
103 }
104
105 void rmdirs(const Path &path)
106 {
107         list<string> files = list_files(path);
108         for(list<string>::iterator i=files.begin(); i!=files.end(); ++i)
109         {
110                 Path p = path / *i;
111                 if(is_dir(p))
112                         rmdirs(p);
113                 else
114                         unlink(p);
115         }
116
117         rmdir(path);
118 }
119
120 list<string> list_files(const Path &path)
121 {
122         return list_filtered(path, string());
123 }
124
125 list<string> list_filtered(const Path &path, const string &filter)
126 {
127         Regex r_filter(filter);
128
129         list<string> result;
130         DIR *dir = opendir(path.str().c_str());
131         if(dir)
132         {
133                 while(dirent *de = readdir(dir))
134                 {
135                         const char *fn = de->d_name;
136                         if(fn[0]=='.' && (fn[1]==0 || (fn[1]=='.' && fn[2]==0))) 
137                                 continue;
138                         if(r_filter.match(fn))
139                                 result.push_back(fn);
140                 }
141                 closedir(dir);
142         }
143
144         return result;
145 }
146
147 Path getcwd()
148 {
149         char buf[1024];
150         return ::getcwd(buf, sizeof(buf));
151 }
152
153 Path get_home_dir()
154 {
155 #ifdef WIN32
156         char home[MAX_PATH];
157         if(SHGetFolderPath(0, CSIDL_PERSONAL, 0, 0, home)==S_OK)
158                 return home;
159 #else
160         const char *home = getenv("HOME");
161         if(home)
162                 return home;
163 #endif
164         return ".";
165 }
166
167 Path get_user_data_dir(const string &appname)
168 {
169 #ifdef WIN32
170         char datadir[MAX_PATH];
171         if(SHGetFolderPath(0, CSIDL_LOCAL_APPDATA, 0, 0, datadir)==S_OK)
172                 return Path(datadir)/appname;
173         return ".";
174 #else
175         return get_home_dir()/("."+appname);
176 #endif
177 }
178
179 Path get_sys_conf_dir(const string &argv0)
180 {
181         Path dir = get_bin_dir(argv0);
182
183         if(dir[-1]=="bin" || dir[-1]=="sbin")
184         {
185                 dir /= "..";
186                 if(dir[-1]=="usr")
187                         dir /= "..";
188                 return dir/"etc";
189         }
190         else
191                 return dir;
192 }
193
194 Path get_sys_data_dir(const string &argv0, const string &appname)
195 {
196         Path dir = get_bin_dir(argv0);
197
198         if(dir[-1]=="bin" || dir[-1]=="sbin")
199                 return dir/".."/"share"/appname;
200         else
201                 return dir;
202 }
203
204 Path get_sys_lib_dir(const string &argv0, const string &appname)
205 {
206         Path dir = get_bin_dir(argv0);
207
208         if(dir[-1]=="bin" || dir[-1]=="sbin")
209                 return dir/".."/"lib"/appname;
210         else
211                 return dir;
212 }
213
214 void chdir(const Path &path)
215 {
216         if(::chdir(path.str().c_str())==-1)
217                 throw system_error("chdir");
218 }
219
220 } // namespace FS
221 } // namespace Msp