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