Rename PBD::get_files_in_directory to just PBD::get_files, change parameter order...
[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 void
137 get_paths (vector<string>& result,
138            const std::string& directory_path,
139            bool files_only,
140            bool recurse)
141 {
142         // perhaps we don't need this check assuming an exception is thrown
143         // as it would save checking that the path is a directory twice when
144         // recursing
145         if (!Glib::file_test (directory_path, Glib::FILE_TEST_IS_DIR)) return;
146
147         try
148         {
149                 Glib::Dir dir(directory_path);
150                 Glib::DirIterator i = dir.begin();
151
152                 while (i != dir.end()) {
153
154                         string fullpath = Glib::build_filename (directory_path, *i);
155
156                         bool is_dir = Glib::file_test (fullpath, Glib::FILE_TEST_IS_DIR);
157
158                         if (is_dir && recurse) {
159                                 DEBUG_TRACE (DEBUG::FileUtils,
160                                                 string_compose("Descending into directory:  %1\n",
161                                                 fullpath));
162                                 get_paths (result, fullpath, files_only, recurse);
163                         }
164
165                         i++;
166
167                         if (is_dir && files_only) {
168                                 continue;
169                         }
170                         result.push_back (fullpath);
171                 }
172         }
173         catch (Glib::FileError& err)
174         {
175                 warning << err.what() << endmsg;
176         }
177 }
178
179 void
180 get_files (vector<string>& result, const std::string& directory_path)
181 {
182         return get_paths (result, directory_path, true, false);
183 }
184
185 static
186 bool
187 pattern_filter (const string& str, void *arg)
188 {
189         Glib::PatternSpec* pattern = (Glib::PatternSpec*)arg;
190         return pattern->match(str);
191 }
192
193 void
194 find_files_matching_pattern (vector<string>& result,
195                              const Searchpath& paths,
196                              const Glib::PatternSpec& pattern)
197 {
198         run_functor_for_paths (result, paths, pattern_filter,
199                                const_cast<Glib::PatternSpec*>(&pattern),
200                                true, false, true, false);
201 }
202
203 void
204 find_files_matching_pattern (vector<string>& result,
205                              const Searchpath& paths,
206                              const string& pattern)
207 {
208         Glib::PatternSpec tmp(pattern);
209         find_files_matching_pattern (result, paths, tmp);
210 }
211
212 bool
213 find_file (const Searchpath& search_path,
214            const string& filename,
215            std::string& result)
216 {
217         vector<std::string> tmp;
218
219         find_files_matching_pattern (tmp, search_path, filename);
220
221         if (tmp.size() == 0) {
222                 DEBUG_TRACE (DEBUG::FileUtils,
223                              string_compose("No file matching %1 found in Path: %2\n",
224                              filename, search_path.to_string()));
225                 return false;
226         }
227
228         if (tmp.size() != 1) {
229                 DEBUG_TRACE (DEBUG::FileUtils,
230                              string_compose("Found more that one file matching %1 in Path: %2\n",
231                              filename, search_path.to_string()));
232         }
233
234         result = tmp.front();
235
236         DEBUG_TRACE (DEBUG::FileUtils,
237                      string_compose("Found file %1 in Path: %2\n",
238                      filename, search_path.to_string()));
239
240         return true;
241 }
242
243 static
244 bool
245 regexp_filter (const string& str, void *arg)
246 {
247         regex_t* pattern = (regex_t*)arg;
248         return regexec (pattern, str.c_str(), 0, 0, 0) == 0;
249 }
250
251 void
252 find_files_matching_regex (vector<string>& result,
253                            const Searchpath& paths,
254                            const std::string& regexp)
255 {
256         int err;
257         char msg[256];
258         regex_t compiled_pattern;
259
260         if ((err = regcomp (&compiled_pattern, regexp.c_str(),
261                             REG_EXTENDED|REG_NOSUB))) {
262
263                 regerror (err, &compiled_pattern,
264                           msg, sizeof (msg));
265
266                 error << "Cannot compile soundfile regexp for use ("
267                       << msg
268                       << ")"
269                       << endmsg;
270
271                 return;
272         }
273
274         DEBUG_TRACE (DEBUG::FileUtils,
275                         string_compose("Matching files using regexp: %1\n", regexp));
276
277         find_files_matching_filter (result, paths,
278                                     regexp_filter, &compiled_pattern,
279                                     true, true, false);
280
281         regfree (&compiled_pattern);
282 }
283
284 void
285 find_paths_matching_filter (vector<string>& result,
286                             const Searchpath& paths,
287                             bool (*filter)(const string &, void *),
288                             void *arg,
289                             bool match_fullpath, bool return_fullpath,
290                             bool recurse)
291 {
292         run_functor_for_paths (result, paths, filter, arg, false, match_fullpath, return_fullpath, recurse);
293 }
294
295 void
296 find_files_matching_filter (vector<string>& result,
297                             const Searchpath& paths,
298                             bool (*filter)(const string &, void *),
299                             void *arg,
300                             bool match_fullpath, bool return_fullpath,
301                             bool recurse)
302 {
303         run_functor_for_paths (result, paths, filter, arg, true, match_fullpath, return_fullpath, recurse);
304 }
305
306 bool
307 copy_file(const std::string & from_path, const std::string & to_path)
308 {
309         if (!Glib::file_test (from_path, Glib::FILE_TEST_EXISTS)) return false;
310
311         int fd_from = -1;
312         int fd_to = -1;
313         char buf[4096]; // BUFSIZ  ??
314         ssize_t nread;
315
316         fd_from = ::open(from_path.c_str(), O_RDONLY);
317         if (fd_from < 0) {
318                 goto copy_error;
319         }
320
321         fd_to = ::open(to_path.c_str(), O_WRONLY | O_CREAT, 0666);
322         if (fd_to < 0) {
323                 goto copy_error;
324         }
325
326         while (nread = ::read(fd_from, buf, sizeof(buf)), nread > 0) {
327                 char *out_ptr = buf;
328                 do {
329                         ssize_t nwritten = ::write(fd_to, out_ptr, nread);
330                         if (nwritten >= 0) {
331                                 nread -= nwritten;
332                                 out_ptr += nwritten;
333                         } else if (errno != EINTR) {
334                                 goto copy_error;
335                         }
336                 } while (nread > 0);
337         }
338
339         if (nread == 0) {
340                 if (::close(fd_to)) {
341                         fd_to = -1;
342                         goto copy_error;
343                 }
344                 ::close(fd_from);
345                 return true;
346         }
347
348 copy_error:
349         int saved_errno = errno;
350
351         if (fd_from >= 0) {
352                 ::close(fd_from);
353         }
354         if (fd_to >= 0) {
355                 ::close(fd_to);
356         }
357
358         error << string_compose (_("Unable to Copy file %1 to %2 (%3)"),
359                         from_path, to_path, strerror(saved_errno))
360                 << endmsg;
361         return false;
362 }
363
364 static
365 bool accept_all_files (string const &, void *)
366 {
367         return true;
368 }
369
370 void
371 copy_files(const std::string & from_path, const std::string & to_dir)
372 {
373         vector<string> files;
374         find_files_matching_filter (files, from_path, accept_all_files, 0, true, false);
375
376         for (vector<string>::iterator i = files.begin(); i != files.end(); ++i) {
377                 std::string from = Glib::build_filename (from_path, *i);
378                 std::string to = Glib::build_filename (to_dir, *i);
379                 copy_file (from, to);
380         }
381 }
382
383 std::string
384 get_absolute_path (const std::string & p)
385 {
386         if (Glib::path_is_absolute(p)) return p;
387         return Glib::build_filename (Glib::get_current_dir(), p);
388 }
389
390 bool
391 equivalent_paths (const std::string& a, const std::string& b)
392 {
393         GStatBuf bA;
394         int const rA = g_stat (a.c_str(), &bA);
395         GStatBuf bB;
396         int const rB = g_stat (b.c_str(), &bB);
397
398         return (rA == 0 && rB == 0 && bA.st_dev == bB.st_dev && bA.st_ino == bB.st_ino);
399 }
400
401 bool
402 path_is_within (std::string const & haystack, std::string needle)
403 {
404         while (1) {
405                 if (equivalent_paths (haystack, needle)) {
406                         return true;
407                 }
408
409                 needle = Glib::path_get_dirname (needle);
410                 if (needle == "." || needle == "/") {
411                         break;
412                 }
413         }
414
415         return false;
416 }
417
418 bool
419 exists_and_writable (const std::string & p)
420 {
421         /* writable() really reflects the whole folder, but if for any
422            reason the session state file can't be written to, still
423            make us unwritable.
424         */
425
426         GStatBuf statbuf;
427
428         if (g_stat (p.c_str(), &statbuf) != 0) {
429                 /* doesn't exist - not writable */
430                 return false;
431         } else {
432                 if (!(statbuf.st_mode & S_IWUSR)) {
433                         /* exists and is not writable */
434                         return false;
435                 }
436                 /* filesystem may be mounted read-only, so even though file
437                  * permissions permit access, the mount status does not.
438                  * access(2) seems like the best test for this.
439                  */
440                 if (g_access (p.c_str(), W_OK) != 0) {
441                         return false;
442                 }
443         }
444
445         return true;
446 }
447
448 int
449 remove_directory_internal (const string& dir, size_t* size, vector<string>* paths,
450                            bool just_remove_files)
451 {
452         vector<string> tmp_paths;
453         struct stat statbuf;
454         int ret = 0;
455
456         get_paths (tmp_paths, dir, just_remove_files, true);
457
458         for (vector<string>::const_iterator i = tmp_paths.begin();
459              i != tmp_paths.end(); ++i) {
460
461                 if (g_stat (i->c_str(), &statbuf)) {
462                         continue;
463                 }
464
465                 if (::g_remove (i->c_str())) {
466                         error << string_compose (_("cannot remove path %1 (%2)"), *i, strerror (errno))
467                               << endmsg;
468                         ret = 1;
469                 }
470
471                 if (paths) {
472                         paths->push_back (Glib::path_get_basename(*i));
473                 }
474
475                 if (size) {
476                         *size += statbuf.st_size;
477                 }
478
479         }
480
481         return ret;
482 }
483
484 int
485 clear_directory (const string& dir, size_t* size, vector<string>* paths)
486 {
487         return remove_directory_internal (dir, size, paths, true);
488 }
489
490 // rm -rf <dir> -- used to remove saved plugin state
491 void
492 remove_directory (const std::string& dir)
493 {
494         remove_directory_internal (dir, 0, 0, false);
495 }
496
497 } // namespace PBD