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