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