2 Copyright (C) 2007-2014 Tim Mayberry
3 Copyright (C) 1998-2014 Paul Davis
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.
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.
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.
25 #include <glib/gstdio.h>
28 #include <io.h> // For W_OK
31 #include <glibmm/fileutils.h>
32 #include <glibmm/miscutils.h>
33 #include <glibmm/pattern.h>
36 #include <string.h> /* strerror */
39 #include <sys/types.h>
43 /* close(), read(), write() */
45 #include <io.h> // Microsoft's nearest equivalent to <unistd.h>
46 #include <ardourext/misc.h>
52 #include "pbd/compose.h"
53 #include "pbd/file_utils.h"
54 #include "pbd/debug.h"
55 #include "pbd/error.h"
56 #include "pbd/pathexpand.h"
57 #include "pbd/stl_delete.h"
66 run_functor_for_paths (vector<string>& result,
67 const Searchpath& paths,
68 bool (*functor)(const string &, void *),
71 bool pass_fullpath, bool return_fullpath,
74 for (vector<string>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
75 string expanded_path = path_expand (*i);
76 DEBUG_TRACE (DEBUG::FileUtils,
77 string_compose("Find files in expanded path: %1\n", expanded_path));
79 if (!Glib::file_test (expanded_path, Glib::FILE_TEST_IS_DIR)) continue;
83 Glib::Dir dir(expanded_path);
85 for (Glib::DirIterator di = dir.begin(); di != dir.end(); di++) {
87 string fullpath = Glib::build_filename (expanded_path, *di);
88 string basename = *di;
90 bool is_dir = Glib::file_test (fullpath, Glib::FILE_TEST_IS_DIR);
92 if (is_dir && recurse) {
93 DEBUG_TRACE (DEBUG::FileUtils,
94 string_compose("Descending into directory: %1\n",
96 run_functor_for_paths (result, fullpath, functor, arg, pass_files_only,
97 pass_fullpath, return_fullpath, recurse);
100 if (is_dir && pass_files_only) {
107 functor_str = fullpath;
109 functor_str = basename;
112 DEBUG_TRACE (DEBUG::FileUtils,
113 string_compose("Run Functor using string: %1\n", functor_str));
115 if (!functor(functor_str, arg)) {
119 DEBUG_TRACE (DEBUG::FileUtils,
120 string_compose("Found file %1 matching functor\n", functor_str));
122 if (return_fullpath) {
123 result.push_back(fullpath);
125 result.push_back(basename);
129 catch (Glib::FileError& err)
131 warning << err.what() << endmsg;
137 get_directory_contents (const std::string& directory_path,
138 vector<string>& result,
142 // perhaps we don't need this check assuming an exception is thrown
143 // as it would save checking that the path is a directory twice when
145 if (!Glib::file_test (directory_path, Glib::FILE_TEST_IS_DIR)) return;
149 Glib::Dir dir(directory_path);
150 Glib::DirIterator i = dir.begin();
152 while (i != dir.end()) {
154 string fullpath = Glib::build_filename (directory_path, *i);
156 bool is_dir = Glib::file_test (fullpath, Glib::FILE_TEST_IS_DIR);
158 if (is_dir && recurse) {
159 DEBUG_TRACE (DEBUG::FileUtils,
160 string_compose("Descending into directory: %1\n",
162 get_directory_contents (fullpath, result, files_only, recurse);
167 if (is_dir && files_only) {
170 result.push_back (fullpath);
173 catch (Glib::FileError& err)
175 warning << err.what() << endmsg;
180 get_files_in_directory (const std::string& directory_path, vector<string>& result)
182 return get_directory_contents (directory_path, result, true, false);
186 find_files_matching_pattern (vector<string>& result,
187 const Searchpath& paths,
188 const Glib::PatternSpec& pattern)
190 vector<string> tmp_files;
192 for (vector<string>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
193 get_files_in_directory (*i, tmp_files);
196 for (vector<string>::iterator file_iter = tmp_files.begin();
197 file_iter != tmp_files.end();
200 string filename = Glib::path_get_basename (*file_iter);
201 if (!pattern.match(filename)) continue;
203 DEBUG_TRACE (DEBUG::FileUtils,
204 string_compose("Found file %1\n", *file_iter));
206 result.push_back(*file_iter);
212 find_files_matching_pattern (vector<string>& result,
213 const Searchpath& paths,
214 const string& pattern)
216 Glib::PatternSpec tmp(pattern);
217 find_files_matching_pattern (result, paths, tmp);
221 find_file (const Searchpath& search_path,
222 const string& filename,
225 vector<std::string> tmp;
227 find_files_matching_pattern (tmp, search_path, filename);
229 if (tmp.size() == 0) {
230 DEBUG_TRACE (DEBUG::FileUtils,
231 string_compose("No file matching %1 found in Path: %2\n",
232 filename, search_path.to_string()));
236 if (tmp.size() != 1) {
237 DEBUG_TRACE (DEBUG::FileUtils,
238 string_compose("Found more that one file matching %1 in Path: %2\n",
239 filename, search_path.to_string()));
242 result = tmp.front();
244 DEBUG_TRACE (DEBUG::FileUtils,
245 string_compose("Found file %1 in Path: %2\n",
246 filename, search_path.to_string()));
253 regexp_filter (const string& str, void *arg)
255 regex_t* pattern = (regex_t*)arg;
256 return regexec (pattern, str.c_str(), 0, 0, 0) == 0;
260 find_files_matching_regex (vector<string>& result,
261 const Searchpath& paths,
262 const std::string& regexp)
266 regex_t compiled_pattern;
268 if ((err = regcomp (&compiled_pattern, regexp.c_str(),
269 REG_EXTENDED|REG_NOSUB))) {
271 regerror (err, &compiled_pattern,
274 error << "Cannot compile soundfile regexp for use ("
282 DEBUG_TRACE (DEBUG::FileUtils,
283 string_compose("Matching files using regexp: %1\n", regexp));
285 find_files_matching_filter (result, paths,
286 regexp_filter, &compiled_pattern,
289 regfree (&compiled_pattern);
293 find_files_matching_filter (vector<string>& result,
294 const Searchpath& paths,
295 bool (*filter)(const string &, void *),
297 bool match_fullpath, bool return_fullpath,
300 run_functor_for_paths (result, paths, filter, arg, true, match_fullpath, return_fullpath, recurse);
304 copy_file(const std::string & from_path, const std::string & to_path)
306 if (!Glib::file_test (from_path, Glib::FILE_TEST_EXISTS)) return false;
310 char buf[4096]; // BUFSIZ ??
313 fd_from = ::open(from_path.c_str(), O_RDONLY);
318 fd_to = ::open(to_path.c_str(), O_WRONLY | O_CREAT, 0666);
323 while (nread = ::read(fd_from, buf, sizeof(buf)), nread > 0) {
326 ssize_t nwritten = ::write(fd_to, out_ptr, nread);
330 } else if (errno != EINTR) {
337 if (::close(fd_to)) {
346 int saved_errno = errno;
355 error << string_compose (_("Unable to Copy file %1 to %2 (%3)"),
356 from_path, to_path, strerror(saved_errno))
362 bool accept_all_files (string const &, void *)
368 copy_files(const std::string & from_path, const std::string & to_dir)
370 vector<string> files;
371 find_files_matching_filter (files, from_path, accept_all_files, 0, true, false);
373 for (vector<string>::iterator i = files.begin(); i != files.end(); ++i) {
374 std::string from = Glib::build_filename (from_path, *i);
375 std::string to = Glib::build_filename (to_dir, *i);
376 copy_file (from, to);
381 get_absolute_path (const std::string & p)
383 if (Glib::path_is_absolute(p)) return p;
384 return Glib::build_filename (Glib::get_current_dir(), p);
388 equivalent_paths (const std::string& a, const std::string& b)
391 int const rA = g_stat (a.c_str(), &bA);
393 int const rB = g_stat (b.c_str(), &bB);
395 return (rA == 0 && rB == 0 && bA.st_dev == bB.st_dev && bA.st_ino == bB.st_ino);
399 path_is_within (std::string const & haystack, std::string needle)
402 if (equivalent_paths (haystack, needle)) {
406 needle = Glib::path_get_dirname (needle);
407 if (needle == "." || needle == "/") {
416 exists_and_writable (const std::string & p)
418 /* writable() really reflects the whole folder, but if for any
419 reason the session state file can't be written to, still
425 if (g_stat (p.c_str(), &statbuf) != 0) {
426 /* doesn't exist - not writable */
429 if (!(statbuf.st_mode & S_IWUSR)) {
430 /* exists and is not writable */
433 /* filesystem may be mounted read-only, so even though file
434 * permissions permit access, the mount status does not.
435 * access(2) seems like the best test for this.
437 if (g_access (p.c_str(), W_OK) != 0) {
446 remove_directory_internal (const string& dir, size_t* size, vector<string>* paths,
447 bool just_remove_files)
449 vector<string> tmp_paths;
453 get_directory_contents (dir, tmp_paths, just_remove_files, true);
455 for (vector<string>::const_iterator i = tmp_paths.begin();
456 i != tmp_paths.end(); ++i) {
458 if (g_stat (i->c_str(), &statbuf)) {
462 if (::g_remove (i->c_str())) {
463 error << string_compose (_("cannot remove path %1 (%2)"), *i, strerror (errno))
469 paths->push_back (Glib::path_get_basename(*i));
473 *size += statbuf.st_size;
482 clear_directory (const string& dir, size_t* size, vector<string>* paths)
484 return remove_directory_internal (dir, size, paths, true);
487 // rm -rf <dir> -- used to remove saved plugin state
489 remove_directory (const std::string& dir)
491 remove_directory_internal (dir, 0, 0, false);