89f6818704c9d8c05d0ad2dc7c668cadc8e2d05d
[ardour.git] / libs / pbd / file_utils.cc
1 /*
2     Copyright (C) 2007-2014 Tim Mayberry
3     Copyright (C) 1998-2014 Paul Davis
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include <algorithm>
22 #include <vector>
23
24 #include <glib.h>
25 #include <glib/gstdio.h>
26
27 #ifdef COMPILER_MINGW
28 #include <io.h> // For W_OK
29 #endif
30
31 #include <glibmm/fileutils.h>
32 #include <glibmm/miscutils.h>
33 #include <glibmm/pattern.h>
34
35 #include <errno.h>
36 #include <string.h> /* strerror */
37
38 /* open() */
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <fcntl.h>
42
43 /* close(), read(), write() */
44 #ifdef COMPILER_MSVC
45 #include <io.h> // Microsoft's nearest equivalent to <unistd.h>
46 #include <ardourext/misc.h>
47 #else
48 #include <unistd.h>
49 #include <regex.h>
50 #endif
51
52 #include "pbd/compose.h"
53 #include "pbd/file_utils.h"
54 #include "pbd/debug.h"
55 #include "pbd/error.h"
56 #include "pbd/pathexpand.h"
57 #include "pbd/stl_delete.h"
58
59 #include "i18n.h"
60
61 using namespace std;
62
63 namespace PBD {
64
65 void
66 run_functor_for_paths (vector<string>& result,
67                        const Searchpath& paths,
68                        bool (*functor)(const string &, void *),
69                        void *arg,
70                        bool pass_files_only,
71                        bool pass_fullpath, bool return_fullpath,
72                        bool recurse)
73 {
74         for (vector<string>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
75                 string expanded_path = path_expand (*i);
76                 DEBUG_TRACE (DEBUG::FileUtils,
77                                 string_compose("Find files in expanded path: %1\n", expanded_path));
78
79                 if (!Glib::file_test (expanded_path, Glib::FILE_TEST_IS_DIR)) continue;
80
81                 try
82                 {
83                         Glib::Dir dir(expanded_path);
84
85                         for (Glib::DirIterator di = dir.begin(); di != dir.end(); di++) {
86
87                                 string fullpath = Glib::build_filename (expanded_path, *di);
88                                 string basename = *di;
89
90                                 bool is_dir = Glib::file_test (fullpath, Glib::FILE_TEST_IS_DIR);
91
92                                 if (is_dir && recurse) {
93                                         DEBUG_TRACE (DEBUG::FileUtils,
94                                                         string_compose("Descending into directory:  %1\n",
95                                                                 fullpath));
96                                         run_functor_for_paths (result, fullpath, functor, arg, pass_files_only,
97                                                                pass_fullpath, return_fullpath, recurse);
98                                 }
99
100                                 if (is_dir && pass_files_only) {
101                                         continue;
102                                 }
103
104                                 string functor_str;
105
106                                 if (pass_fullpath) {
107                                         functor_str = fullpath;
108                                 } else {
109                                         functor_str = basename;
110                                 }
111
112                                 DEBUG_TRACE (DEBUG::FileUtils,
113                                                 string_compose("Run Functor using string: %1\n", functor_str));
114
115                                 if (!functor(functor_str, arg)) {
116                                         continue;
117                                 }
118
119                                 DEBUG_TRACE (DEBUG::FileUtils,
120                                                 string_compose("Found file %1 matching functor\n", functor_str));
121
122                                 if (return_fullpath) {
123                                         result.push_back(fullpath);
124                                 } else {
125                                         result.push_back(basename);
126                                 }
127                         }
128                 }
129                 catch (Glib::FileError& err)
130                 {
131                         warning << err.what() << endmsg;
132                 }
133         }
134 }
135
136 static
137 bool accept_all_files (string const &, void *)
138 {
139         return true;
140 }
141
142 void
143 get_paths (vector<string>& result,
144            const Searchpath& paths,
145            bool files_only,
146            bool recurse)
147 {
148         run_functor_for_paths (result, paths, accept_all_files, 0,
149                                files_only, true, true, recurse);
150 }
151
152 void
153 get_files (vector<string>& result, const Searchpath& paths)
154 {
155         return get_paths (result, paths, true, false);
156 }
157
158 static
159 bool
160 pattern_filter (const string& str, void *arg)
161 {
162         Glib::PatternSpec* pattern = (Glib::PatternSpec*)arg;
163         return pattern->match(str);
164 }
165
166 void
167 find_files_matching_pattern (vector<string>& result,
168                              const Searchpath& paths,
169                              const Glib::PatternSpec& pattern)
170 {
171         run_functor_for_paths (result, paths, pattern_filter,
172                                const_cast<Glib::PatternSpec*>(&pattern),
173                                true, false, true, false);
174 }
175
176 void
177 find_files_matching_pattern (vector<string>& result,
178                              const Searchpath& paths,
179                              const string& pattern)
180 {
181         Glib::PatternSpec tmp(pattern);
182         find_files_matching_pattern (result, paths, tmp);
183 }
184
185 bool
186 find_file (const Searchpath& search_path,
187            const string& filename,
188            std::string& result)
189 {
190         vector<std::string> tmp;
191
192         find_files_matching_pattern (tmp, search_path, filename);
193
194         if (tmp.size() == 0) {
195                 DEBUG_TRACE (DEBUG::FileUtils,
196                              string_compose("No file matching %1 found in Path: %2\n",
197                              filename, search_path.to_string()));
198                 return false;
199         }
200
201         if (tmp.size() != 1) {
202                 DEBUG_TRACE (DEBUG::FileUtils,
203                              string_compose("Found more that one file matching %1 in Path: %2\n",
204                              filename, search_path.to_string()));
205         }
206
207         result = tmp.front();
208
209         DEBUG_TRACE (DEBUG::FileUtils,
210                      string_compose("Found file %1 in Path: %2\n",
211                      filename, search_path.to_string()));
212
213         return true;
214 }
215
216 static
217 bool
218 regexp_filter (const string& str, void *arg)
219 {
220         regex_t* pattern = (regex_t*)arg;
221         return regexec (pattern, str.c_str(), 0, 0, 0) == 0;
222 }
223
224 void
225 find_files_matching_regex (vector<string>& result,
226                            const Searchpath& paths,
227                            const std::string& regexp)
228 {
229         int err;
230         char msg[256];
231         regex_t compiled_pattern;
232
233         if ((err = regcomp (&compiled_pattern, regexp.c_str(),
234                             REG_EXTENDED|REG_NOSUB))) {
235
236                 regerror (err, &compiled_pattern,
237                           msg, sizeof (msg));
238
239                 error << "Cannot compile soundfile regexp for use ("
240                       << msg
241                       << ")"
242                       << endmsg;
243
244                 return;
245         }
246
247         DEBUG_TRACE (DEBUG::FileUtils,
248                         string_compose("Matching files using regexp: %1\n", regexp));
249
250         find_files_matching_filter (result, paths,
251                                     regexp_filter, &compiled_pattern,
252                                     true, true, false);
253
254         regfree (&compiled_pattern);
255 }
256
257 void
258 find_paths_matching_filter (vector<string>& result,
259                             const Searchpath& paths,
260                             bool (*filter)(const string &, void *),
261                             void *arg,
262                             bool pass_fullpath, bool return_fullpath,
263                             bool recurse)
264 {
265         run_functor_for_paths (result, paths, filter, arg, false, pass_fullpath, return_fullpath, recurse);
266 }
267
268 void
269 find_files_matching_filter (vector<string>& result,
270                             const Searchpath& paths,
271                             bool (*filter)(const string &, void *),
272                             void *arg,
273                             bool pass_fullpath, bool return_fullpath,
274                             bool recurse)
275 {
276         run_functor_for_paths (result, paths, filter, arg, true, pass_fullpath, return_fullpath, recurse);
277 }
278
279 bool
280 copy_file(const std::string & from_path, const std::string & to_path)
281 {
282         if (!Glib::file_test (from_path, Glib::FILE_TEST_EXISTS)) return false;
283
284         int fd_from (::open (from_path.c_str(), O_RDONLY));
285         int fd_to (::open (to_path.c_str(), O_CREAT|O_TRUNC|O_RDWR, 0666));
286
287         char buf[4096]; // BUFSIZ  ??
288         ssize_t nread;
289
290         if ((fd_from < 0) || (fd_to < 0)) {
291                 error << string_compose (_("Unable to Open files %1 to %2 for Copying(%3)"),
292                                 from_path, to_path, g_strerror(errno))
293                       << endmsg;
294                 return false;
295         }
296
297         while (nread = ::read(fd_from, buf, sizeof(buf)), nread > 0) {
298                 char *out_ptr = buf;
299                 do {
300                         ssize_t nwritten = ::write(fd_to, out_ptr, nread);
301                         if (nwritten >= 0) {
302                                 nread -= nwritten;
303                                 out_ptr += nwritten;
304                         } else if (errno != EINTR) {
305                                 error << string_compose (_("Unable to Copy files %1 to %2(%3)"),
306                                                 from_path, to_path, g_strerror(errno))
307                                         << endmsg;
308                                 return false;
309                         }
310                 } while (nread > 0);
311         }
312
313         return true;
314 }
315
316 void
317 copy_files(const std::string & from_path, const std::string & to_dir)
318 {
319         vector<string> files;
320         find_files_matching_filter (files, from_path, accept_all_files, 0, true, false);
321
322         for (vector<string>::iterator i = files.begin(); i != files.end(); ++i) {
323                 std::string from = Glib::build_filename (from_path, *i);
324                 std::string to = Glib::build_filename (to_dir, *i);
325                 copy_file (from, to);
326         }
327 }
328
329 void
330 copy_recurse(const std::string & from_path, const std::string & to_dir)
331 {
332         vector<string> files;
333         find_files_matching_filter (files, from_path, accept_all_files, 0, false, true, true);
334
335         const size_t prefix_len = from_path.size();
336         for (vector<string>::iterator i = files.begin(); i != files.end(); ++i) {
337                 std::string from = *i;
338                 std::string to = Glib::build_filename (to_dir, (*i).substr(prefix_len));
339                 g_mkdir_with_parents (Glib::path_get_dirname (to).c_str(), 0755);
340                 copy_file (from, to);
341         }
342 }
343
344 std::string
345 get_absolute_path (const std::string & p)
346 {
347         if (Glib::path_is_absolute(p)) return p;
348         return Glib::build_filename (Glib::get_current_dir(), p);
349 }
350
351 std::string
352 get_suffix (const std::string & p)
353 {
354         string::size_type period = p.find_last_of ('.');
355         if (period == string::npos || period == p.length() - 1) {
356                 return string();
357         }
358         return p.substr (period+1);
359 }
360
361 bool
362 equivalent_paths (const std::string& a, const std::string& b)
363 {
364         GStatBuf bA;
365         int const rA = g_stat (a.c_str(), &bA);
366         GStatBuf bB;
367         int const rB = g_stat (b.c_str(), &bB);
368
369         return (rA == 0 && rB == 0 && bA.st_dev == bB.st_dev && bA.st_ino == bB.st_ino);
370 }
371
372 bool
373 path_is_within (std::string const & haystack, std::string needle)
374 {
375         while (1) {
376                 if (equivalent_paths (haystack, needle)) {
377                         return true;
378                 }
379
380                 needle = Glib::path_get_dirname (needle);
381                 if (needle == "." || needle == "/") {
382                         break;
383                 }
384         }
385
386         return false;
387 }
388
389 bool
390 exists_and_writable (const std::string & p)
391 {
392         /* writable() really reflects the whole folder, but if for any
393            reason the session state file can't be written to, still
394            make us unwritable.
395         */
396
397         GStatBuf statbuf;
398
399         if (g_stat (p.c_str(), &statbuf) != 0) {
400                 /* doesn't exist - not writable */
401                 return false;
402         } else {
403                 if (!(statbuf.st_mode & S_IWUSR)) {
404                         /* exists and is not writable */
405                         return false;
406                 }
407                 /* filesystem may be mounted read-only, so even though file
408                  * permissions permit access, the mount status does not.
409                  * access(2) seems like the best test for this.
410                  */
411                 if (g_access (p.c_str(), W_OK) != 0) {
412                         return false;
413                 }
414         }
415
416         return true;
417 }
418
419 int
420 remove_directory_internal (const string& dir, size_t* size, vector<string>* paths,
421                            bool just_remove_files)
422 {
423         vector<string> tmp_paths;
424         GStatBuf statbuf;
425         int ret = 0;
426
427         get_paths (tmp_paths, dir, just_remove_files, true);
428
429         for (vector<string>::const_iterator i = tmp_paths.begin();
430              i != tmp_paths.end(); ++i) {
431
432                 if (g_stat (i->c_str(), &statbuf)) {
433                         continue;
434                 }
435
436                 if (::g_remove (i->c_str())) {
437                         error << string_compose (_("cannot remove path %1 (%2)"), *i, strerror (errno))
438                               << endmsg;
439                         ret = 1;
440                 }
441
442                 if (paths) {
443                         paths->push_back (Glib::path_get_basename(*i));
444                 }
445
446                 if (size) {
447                         *size += statbuf.st_size;
448                 }
449
450         }
451
452         return ret;
453 }
454
455 int
456 clear_directory (const string& dir, size_t* size, vector<string>* paths)
457 {
458         return remove_directory_internal (dir, size, paths, true);
459 }
460
461 // rm -rf <dir> -- used to remove saved plugin state
462 void
463 remove_directory (const std::string& dir)
464 {
465         remove_directory_internal (dir, 0, 0, false);
466 }
467
468 string
469 tmp_writable_directory (const char* domain, const string& prefix)
470 {
471         std::string tmp_dir = Glib::build_filename (g_get_tmp_dir(), domain);
472         std::string dir_name;
473         std::string new_test_dir;
474         do {
475                 ostringstream oss;
476                 oss << prefix;
477                 oss << g_random_int ();
478                 dir_name = oss.str();
479                 new_test_dir = Glib::build_filename (tmp_dir, dir_name);
480                 if (Glib::file_test (new_test_dir, Glib::FILE_TEST_EXISTS)) continue;
481         } while (g_mkdir_with_parents (new_test_dir.c_str(), 0755) != 0);
482         return new_test_dir;
483 }
484
485 } // namespace PBD