Make EnumWriter exceptions a bit more informative.
[ardour.git] / libs / pbd / filesystem.cc
1 /*
2         Copyright (C) 2007 Tim Mayberry 
3
4         This program is free software; you can redistribute it and/or modify
5         it under the terms of the GNU General Public License as published by
6         the Free Software Foundation; either version 2 of the License, or
7         (at your option) any later version.
8
9         This program is distributed in the hope that it will be useful,
10         but WITHOUT ANY WARRANTY; without even the implied warranty of
11         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12         GNU General Public License for more details.
13
14         You should have received a copy of the GNU General Public License
15         along with this program; if not, write to the Free Software
16         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <sys/stat.h>
20
21 #include <glib.h>
22 #include <glib/gstdio.h>
23
24 #include <giomm/file.h>
25
26 #include <cerrno>
27 #include <fstream>
28
29 #include <glibmm/fileutils.h>
30 #include <glibmm/miscutils.h>
31
32 #include "pbd/filesystem.h"
33 #include "pbd/error.h"
34 #include "pbd/compose.h"
35 #include "pbd/pathscanner.h"
36
37 #include "i18n.h"
38
39 using namespace std;
40
41 namespace PBD {
42
43 namespace sys {
44         
45 path&
46 path::operator/=(const path& rhs)
47 {
48         m_path = Glib::build_filename(m_path, rhs.m_path);
49         return *this;
50 }
51
52 path&
53 path::operator/=(const string& rhs)
54 {
55         m_path = Glib::build_filename(m_path, rhs);
56         return *this;
57 }
58
59 path&
60 path::operator/=(const char* rhs)
61 {
62         m_path = Glib::build_filename(m_path, rhs);
63         return *this;
64 }
65
66 string
67 path::leaf () const
68 {
69         return Glib::path_get_basename(m_path);
70 }
71
72 path
73 path::branch_path () const
74 {
75         string dir = Glib::path_get_dirname (m_path);
76
77         /*
78          * glib returns "." to signify that the path
79          * has no directory components(branch path)
80          * whereas boost::filesystem returns an empty
81          * string
82          */
83         if(dir == ".")
84         {
85                 return "";
86         }
87         return dir;
88 }
89
90 bool
91 exists (const path & p)
92 {
93         return Glib::file_test (p.to_string(), Glib::FILE_TEST_EXISTS);
94 }
95
96 bool
97 exists_and_writable (const path & p)
98 {
99         /* writable() really reflects the whole folder, but if for any
100            reason the session state file can't be written to, still
101            make us unwritable.
102         */
103
104         struct stat statbuf;
105
106         if (g_stat (p.to_string().c_str(), &statbuf) != 0) {
107                 /* doesn't exist - not writable */
108                 return false;
109         } else {
110                 if (!(statbuf.st_mode & S_IWUSR)) {
111                         /* exists and is not writable */
112                         return false;
113                 }
114                 /* filesystem may be mounted read-only, so even though file
115                  * permissions permit access, the mount status does not.
116                  * access(2) seems like the best test for this.
117                  */
118                 if (g_access (p.to_string().c_str(), W_OK) != 0) {
119                         return false;
120                 }
121         }
122
123         return true;
124 }
125
126
127 bool
128 is_directory (const path & p)
129 {
130         return Glib::file_test (p.to_string(), Glib::FILE_TEST_IS_DIR);
131 }
132
133 bool
134 create_directory(const path & p)
135 {
136         if(is_directory(p)) return false;
137
138         int error = g_mkdir (p.to_string().c_str(), S_IRWXU|S_IRWXG|S_IRWXO);
139
140         if(error == -1)
141         {
142                 throw filesystem_error(g_strerror(errno), errno);
143         }
144         return true;
145 }
146
147 bool
148 create_directories(const path & p)
149 {
150         if(is_directory(p)) return false;
151
152         int error = g_mkdir_with_parents (p.to_string().c_str(), S_IRWXU|S_IRWXG|S_IRWXO);
153
154         if(error == -1)
155         {
156                 throw filesystem_error(g_strerror(errno), errno);
157         }
158         return true;
159 }
160
161 bool
162 remove(const path & p)
163 {
164         if(!exists(p)) return false;
165
166         int error = g_unlink (p.to_string().c_str());
167
168         if(error == -1)
169         {
170                 throw filesystem_error(g_strerror(errno), errno);
171         }
172         return true;
173 }
174
175 void
176 rename (const path & from_path, const path & to_path)
177 {
178         // g_rename is a macro that evaluates to ::rename on
179         // POSIX systems, without the global namespace qualifier
180         // it would evaluate to a recursive call(if it compiled)
181         if ( ::g_rename( from_path.to_string().c_str(),
182                                 to_path.to_string().c_str() ) == -1 )
183         {
184                 throw filesystem_error(g_strerror(errno), errno);
185         }
186 }
187
188 // XXX character encoding.
189 void
190 copy_file(const path & from_path, const path & to_path)
191 {
192         std::ifstream in(from_path.to_string().c_str());
193         std::ofstream out(to_path.to_string().c_str());
194         
195         if (!in || !out) {
196                 throw filesystem_error(string_compose(_("Could not open files %1 and %2 for copying"),
197                                         from_path.to_string(), to_path.to_string()));
198         }
199         
200         out << in.rdbuf();
201         
202         if (!in || !out) {
203                 remove (to_path);
204                 throw filesystem_error(string_compose(_("Could not copy existing file %1 to %2"),
205                                         from_path.to_string(), to_path.to_string()));
206         }
207 }
208
209 static
210 bool accept_all_files (string const &, void *)
211 {
212         return true;
213 }
214         
215 void
216 copy_files(const path & from_path, const path & to_dir)
217 {
218         PathScanner scanner;
219         vector<string*>* files = scanner (from_path.to_string(), accept_all_files, 0, true, false);
220         for (vector<string*>::iterator i = files->begin(); i != files->end(); ++i) {
221                 sys::path from = from_path;
222                 from /= **i;
223                 sys::path to = to_dir;
224                 to /= **i;
225
226                 copy_file (from, to);
227         }
228 }
229
230 string
231 basename (const path & p)
232 {
233         string base(p.leaf());
234
235         string::size_type n = base.rfind ('.');
236
237         return base.substr (0, n);
238 }
239
240 string
241 extension (const path & p)
242 {
243         string base(p.leaf());
244
245         string::size_type n = base.rfind ('.');
246
247         if (n != string::npos)
248         {
249                 return base.substr(n);
250         }
251
252         return string();
253
254 }
255
256 /** Take a (possibly) relative path and make it absolute */
257 path
258 get_absolute_path (const path & p)
259 {
260         Glib::RefPtr<Gio::File> f = Gio::File::create_for_path (p.to_string ());
261         return f->get_path ();
262 }
263
264 /** @return true if a and b have the same inode */
265 bool
266 inodes_same (const path& a, const path& b)
267 {
268         struct stat bA;
269         int const rA = stat (a.to_string().c_str(), &bA);
270         struct stat bB;
271         int const rB = stat (b.to_string().c_str(), &bB);
272
273         return (rA == 0 && rB == 0 && bA.st_ino == bB.st_ino);
274 }
275
276 /** Find out if `needle' is a file or directory within the
277  *  directory `haystack'.
278  *  @return true if it is.
279  */
280 bool
281 path_is_within (path const & haystack, path needle)
282 {
283         while (1) {
284                 if (inodes_same (haystack, needle)) {
285                         return true;
286                 }
287
288                 needle = needle.branch_path ();
289                 if (needle.to_string().empty() || needle.to_string() == "/") {
290                         break;
291                 }
292         }
293
294         return false;
295 }
296
297 } // namespace sys
298
299 } // namespace PBD