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