Remove parameters from PBD::find_files_matching_regex
[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 #define strtok_r strtok_s
30 #endif
31
32 #include <glibmm/fileutils.h>
33 #include <glibmm/miscutils.h>
34 #include <glibmm/pattern.h>
35
36 #include <errno.h>
37 #include <string.h> /* strerror */
38
39 /* open() */
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <fcntl.h>
43
44 /* close(), read(), write() */
45 #ifdef COMPILER_MSVC
46 #include <io.h> // Microsoft's nearest equivalent to <unistd.h>
47 #include <ardourext/misc.h>
48 #else
49 #include <dirent.h>
50 #include <unistd.h>
51 #include <regex.h>
52 #endif
53
54 #include "pbd/compose.h"
55 #include "pbd/file_utils.h"
56 #include "pbd/debug.h"
57 #include "pbd/error.h"
58 #include "pbd/pathexpand.h"
59 #include "pbd/stl_delete.h"
60
61 #include "i18n.h"
62
63 using namespace std;
64
65 namespace PBD {
66
67 void
68 get_directory_contents (const std::string& directory_path,
69                        vector<string>& result,
70                        bool files_only,
71                        bool recurse)
72 {
73         // perhaps we don't need this check assuming an exception is thrown
74         // as it would save checking that the path is a directory twice when
75         // recursing
76         if (!Glib::file_test (directory_path, Glib::FILE_TEST_IS_DIR)) return;
77
78         try
79         {
80                 Glib::Dir dir(directory_path);
81                 Glib::DirIterator i = dir.begin();
82
83                 while (i != dir.end()) {
84
85                         string fullpath = Glib::build_filename (directory_path, *i);
86
87                         bool is_dir = Glib::file_test (fullpath, Glib::FILE_TEST_IS_DIR);
88
89                         if (is_dir && recurse) {
90                                 get_directory_contents (fullpath, result, files_only, recurse);
91                         }
92
93                         i++;
94
95                         if (is_dir && files_only) {
96                                 continue;
97                         }
98                         result.push_back (fullpath);
99                 }
100         }
101         catch (Glib::FileError& err)
102         {
103                 warning << err.what() << endmsg;
104         }
105 }
106
107 void
108 get_files_in_directory (const std::string& directory_path, vector<string>& result)
109 {
110         return get_directory_contents (directory_path, result, true, false);
111 }
112
113 void
114 find_matching_files_in_directory (const std::string& directory,
115                                   const Glib::PatternSpec& pattern,
116                                   vector<std::string>& result)
117 {
118         vector<string> tmp_files;
119
120         get_files_in_directory (directory, tmp_files);
121         result.reserve(tmp_files.size());
122
123         for (vector<string>::iterator file_iter = tmp_files.begin();
124                         file_iter != tmp_files.end();
125                         ++file_iter)
126         {
127                 string filename = Glib::path_get_basename (*file_iter);
128                 if (!pattern.match(filename)) continue;
129
130                 DEBUG_TRACE (
131                         DEBUG::FileUtils,
132                         string_compose("Found file %1\n", *file_iter)
133                         );
134
135                 result.push_back(*file_iter);
136         }
137 }
138
139 void
140 find_matching_files_in_directories (const vector<std::string>& paths,
141                                     const Glib::PatternSpec& pattern,
142                                     vector<std::string>& result)
143 {
144         for (vector<std::string>::const_iterator path_iter = paths.begin();
145                         path_iter != paths.end();
146                         ++path_iter)
147         {
148                 find_matching_files_in_directory (*path_iter, pattern, result);
149         }               
150 }
151
152 void
153 find_matching_files_in_search_path (const Searchpath& search_path,
154                                     const Glib::PatternSpec& pattern,
155                                     vector<std::string>& result)
156 {
157         find_matching_files_in_directories (search_path, pattern, result);    
158 }
159
160 bool
161 find_file_in_search_path(const Searchpath& search_path,
162                          const string& filename,
163                          std::string& result)
164 {
165         vector<std::string> tmp;
166         Glib::PatternSpec tmp_pattern(filename);
167
168         find_matching_files_in_search_path (search_path, tmp_pattern, tmp);
169
170         if (tmp.size() == 0)
171         {
172                 DEBUG_TRACE (
173                         DEBUG::FileUtils,
174                         string_compose("No file matching %1 found in Path: %2\n", filename, search_path.to_string())
175                             );
176                 return false;
177         }
178
179         if (tmp.size() != 1)
180         {
181                 DEBUG_TRACE (
182                         DEBUG::FileUtils,
183                         string_compose("Found more that one file matching %1 in Path: %2\n", filename, search_path.to_string())
184                             );
185         }
186
187         result = tmp.front();
188
189         DEBUG_TRACE (
190                 DEBUG::FileUtils,
191                 string_compose("Found file %1 in Path: %2\n", filename, search_path.to_string())
192                     );
193
194         return true;
195 }
196
197 static
198 bool
199 regexp_filter (const string& str, void *arg)
200 {
201         regex_t* pattern = (regex_t*)arg;
202         return regexec (pattern, str.c_str(), 0, 0, 0) == 0;
203 }
204
205 void
206 find_files_matching_regex (vector<string>& result,
207                            const std::string& dirpath,
208                            const std::string& regexp)
209 {
210         int err;
211         char msg[256];
212         regex_t compiled_pattern;
213
214         if ((err = regcomp (&compiled_pattern, regexp.c_str(),
215                             REG_EXTENDED|REG_NOSUB))) {
216
217                 regerror (err, &compiled_pattern,
218                           msg, sizeof (msg));
219
220                 error << "Cannot compile soundfile regexp for use ("
221                       << msg
222                       << ")"
223                       << endmsg;
224
225                 return;
226         }
227
228         find_files_matching_filter (result, dirpath,
229                                     regexp_filter, &compiled_pattern,
230                                     true, true, -1, false);
231
232         regfree (&compiled_pattern);
233 }
234
235 void
236 find_files_matching_filter (vector<string>& result,
237                             const string &dirpath,
238                             bool (*filter)(const string &, void *),
239                             void *arg,
240                             bool match_fullpath, bool return_fullpath,
241                             long limit,
242                             bool recurse)
243 {
244         DIR *dir;
245         struct dirent *finfo;
246         char *pathcopy = strdup (search_path_expand (dirpath).c_str());
247         char *thisdir;
248         string fullpath;
249         string search_str;
250         long nfound = 0;
251         char *saveptr;
252
253         if ((thisdir = strtok_r (pathcopy, G_SEARCHPATH_SEPARATOR_S, &saveptr)) == 0 ||
254             strlen (thisdir) == 0) {
255                 free (pathcopy);
256                 return;
257         }
258
259         do {
260
261                 if ((dir = opendir (thisdir)) == 0) {
262                         continue;
263                 }
264
265                 while ((finfo = readdir (dir)) != 0) {
266
267                         if ((finfo->d_name[0] == '.' && finfo->d_name[1] == '\0') ||
268                             (finfo->d_name[0] == '.' && finfo->d_name[1] == '.' && finfo->d_name[2] == '\0')) {
269                                 continue;
270                         }
271
272                         fullpath = Glib::build_filename (thisdir, finfo->d_name);
273
274                         struct stat statbuf;
275                         if (stat (fullpath.c_str(), &statbuf) < 0) {
276                                 continue;
277                         }
278
279                         if (statbuf.st_mode & S_IFDIR && recurse) {
280                                 find_files_matching_filter (result, fullpath, filter, arg, match_fullpath, return_fullpath, limit, recurse);
281                         } else {
282
283                                 if (match_fullpath) {
284                                         search_str = fullpath;
285                                 } else {
286                                         search_str = finfo->d_name;
287                                 }
288
289                                 if (!filter(search_str, arg)) {
290                                         continue;
291                                 }
292
293                                 if (return_fullpath) {
294                                         result.push_back(fullpath);
295                                 } else {
296                                         result.push_back(finfo->d_name);
297                                 }
298
299                                 nfound++;
300                         }
301                 }
302                 closedir (dir);
303
304         } while ((limit < 0 || (nfound < limit)) && (thisdir = strtok_r (0, G_SEARCHPATH_SEPARATOR_S, &saveptr)));
305
306         free (pathcopy);
307         return;
308 }
309
310 bool
311 copy_file(const std::string & from_path, const std::string & to_path)
312 {
313         if (!Glib::file_test (from_path, Glib::FILE_TEST_EXISTS)) return false;
314
315         int fd_from = -1;
316         int fd_to = -1;
317         char buf[4096]; // BUFSIZ  ??
318         ssize_t nread;
319
320         fd_from = ::open(from_path.c_str(), O_RDONLY);
321         if (fd_from < 0) {
322                 goto copy_error;
323         }
324
325         fd_to = ::open(to_path.c_str(), O_WRONLY | O_CREAT, 0666);
326         if (fd_to < 0) {
327                 goto copy_error;
328         }
329
330         while (nread = ::read(fd_from, buf, sizeof(buf)), nread > 0) {
331                 char *out_ptr = buf;
332                 do {
333                         ssize_t nwritten = ::write(fd_to, out_ptr, nread);
334                         if (nwritten >= 0) {
335                                 nread -= nwritten;
336                                 out_ptr += nwritten;
337                         } else if (errno != EINTR) {
338                                 goto copy_error;
339                         }
340                 } while (nread > 0);
341         }
342
343         if (nread == 0) {
344                 if (::close(fd_to)) {
345                         fd_to = -1;
346                         goto copy_error;
347                 }
348                 ::close(fd_from);
349                 return true;
350         }
351
352 copy_error:
353         int saved_errno = errno;
354
355         if (fd_from >= 0) {
356                 ::close(fd_from);
357         }
358         if (fd_to >= 0) {
359                 ::close(fd_to);
360         }
361
362         error << string_compose (_("Unable to Copy file %1 to %2 (%3)"),
363                         from_path, to_path, strerror(saved_errno))
364                 << endmsg;
365         return false;
366 }
367
368 static
369 bool accept_all_files (string const &, void *)
370 {
371         return true;
372 }
373
374 void
375 copy_files(const std::string & from_path, const std::string & to_dir)
376 {
377         vector<string> files;
378         find_files_matching_filter (files, from_path, accept_all_files, 0, true, false);
379
380         for (vector<string>::iterator i = files.begin(); i != files.end(); ++i) {
381                 std::string from = Glib::build_filename (from_path, *i);
382                 std::string to = Glib::build_filename (to_dir, *i);
383                 copy_file (from, to);
384         }
385 }
386
387 std::string
388 get_absolute_path (const std::string & p)
389 {
390         if (Glib::path_is_absolute(p)) return p;
391         return Glib::build_filename (Glib::get_current_dir(), p);
392 }
393
394 bool
395 equivalent_paths (const std::string& a, const std::string& b)
396 {
397         GStatBuf bA;
398         int const rA = g_stat (a.c_str(), &bA);
399         GStatBuf bB;
400         int const rB = g_stat (b.c_str(), &bB);
401
402         return (rA == 0 && rB == 0 && bA.st_dev == bB.st_dev && bA.st_ino == bB.st_ino);
403 }
404
405 bool
406 path_is_within (std::string const & haystack, std::string needle)
407 {
408         while (1) {
409                 if (equivalent_paths (haystack, needle)) {
410                         return true;
411                 }
412
413                 needle = Glib::path_get_dirname (needle);
414                 if (needle == "." || needle == "/") {
415                         break;
416                 }
417         }
418
419         return false;
420 }
421
422 bool
423 exists_and_writable (const std::string & p)
424 {
425         /* writable() really reflects the whole folder, but if for any
426            reason the session state file can't be written to, still
427            make us unwritable.
428         */
429
430         GStatBuf statbuf;
431
432         if (g_stat (p.c_str(), &statbuf) != 0) {
433                 /* doesn't exist - not writable */
434                 return false;
435         } else {
436                 if (!(statbuf.st_mode & S_IWUSR)) {
437                         /* exists and is not writable */
438                         return false;
439                 }
440                 /* filesystem may be mounted read-only, so even though file
441                  * permissions permit access, the mount status does not.
442                  * access(2) seems like the best test for this.
443                  */
444                 if (g_access (p.c_str(), W_OK) != 0) {
445                         return false;
446                 }
447         }
448
449         return true;
450 }
451
452 } // namespace PBD