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 "pbd/gstdio_compat.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/scoped_file_descriptor.h"
58 #include "pbd/stl_delete.h"
67 run_functor_for_paths (vector<string>& result,
68 const Searchpath& paths,
69 bool (*functor)(const string &, void *),
72 bool pass_fullpath, bool return_fullpath,
75 for (vector<string>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
76 string expanded_path = path_expand (*i);
77 DEBUG_TRACE (DEBUG::FileUtils,
78 string_compose("Find files in expanded path: %1\n", expanded_path));
80 if (!Glib::file_test (expanded_path, Glib::FILE_TEST_IS_DIR)) continue;
84 Glib::Dir dir(expanded_path);
86 for (Glib::DirIterator di = dir.begin(); di != dir.end(); di++) {
88 string fullpath = Glib::build_filename (expanded_path, *di);
89 string basename = *di;
91 bool is_dir = Glib::file_test (fullpath, Glib::FILE_TEST_IS_DIR);
93 if (is_dir && recurse) {
94 DEBUG_TRACE (DEBUG::FileUtils,
95 string_compose("Descending into directory: %1\n",
97 run_functor_for_paths (result, fullpath, functor, arg, pass_files_only,
98 pass_fullpath, return_fullpath, recurse);
101 if (is_dir && pass_files_only) {
108 functor_str = fullpath;
110 functor_str = basename;
113 DEBUG_TRACE (DEBUG::FileUtils,
114 string_compose("Run Functor using string: %1\n", functor_str));
116 if (!functor(functor_str, arg)) {
120 DEBUG_TRACE (DEBUG::FileUtils,
121 string_compose("Found file %1 matching functor\n", functor_str));
123 if (return_fullpath) {
124 result.push_back(fullpath);
126 result.push_back(basename);
130 catch (Glib::FileError& err)
132 warning << err.what() << endmsg;
138 bool accept_all_files (string const &, void *)
144 get_paths (vector<string>& result,
145 const Searchpath& paths,
149 run_functor_for_paths (result, paths, accept_all_files, 0,
150 files_only, true, true, recurse);
154 get_files (vector<string>& result, const Searchpath& paths)
156 return get_paths (result, paths, true, false);
161 pattern_filter (const string& str, void *arg)
163 Glib::PatternSpec* pattern = (Glib::PatternSpec*)arg;
164 return pattern->match(str);
168 find_files_matching_pattern (vector<string>& result,
169 const Searchpath& paths,
170 const Glib::PatternSpec& pattern)
172 run_functor_for_paths (result, paths, pattern_filter,
173 const_cast<Glib::PatternSpec*>(&pattern),
174 true, false, true, false);
178 find_files_matching_pattern (vector<string>& result,
179 const Searchpath& paths,
180 const string& pattern)
182 Glib::PatternSpec tmp(pattern);
183 find_files_matching_pattern (result, paths, tmp);
187 find_file (const Searchpath& search_path,
188 const string& filename,
191 vector<std::string> tmp;
193 find_files_matching_pattern (tmp, search_path, filename);
195 if (tmp.size() == 0) {
196 DEBUG_TRACE (DEBUG::FileUtils,
197 string_compose("No file matching %1 found in Path: %2\n",
198 filename, search_path.to_string()));
202 if (tmp.size() != 1) {
203 DEBUG_TRACE (DEBUG::FileUtils,
204 string_compose("Found more that one file matching %1 in Path: %2\n",
205 filename, search_path.to_string()));
208 result = tmp.front();
210 DEBUG_TRACE (DEBUG::FileUtils,
211 string_compose("Found file %1 in Path: %2\n",
212 filename, search_path.to_string()));
219 regexp_filter (const string& str, void *arg)
221 regex_t* pattern = (regex_t*)arg;
222 return regexec (pattern, str.c_str(), 0, 0, 0) == 0;
226 find_files_matching_regex (vector<string>& result,
227 const Searchpath& paths,
228 const std::string& regexp,
233 regex_t compiled_pattern;
235 if ((err = regcomp (&compiled_pattern, regexp.c_str(),
236 REG_EXTENDED|REG_NOSUB))) {
238 regerror (err, &compiled_pattern,
241 error << "Cannot compile soundfile regexp for use ("
249 DEBUG_TRACE (DEBUG::FileUtils,
250 string_compose("Matching files using regexp: %1\n", regexp));
252 find_files_matching_filter (result, paths,
253 regexp_filter, &compiled_pattern,
254 true, true, recurse);
256 regfree (&compiled_pattern);
260 find_paths_matching_filter (vector<string>& result,
261 const Searchpath& paths,
262 bool (*filter)(const string &, void *),
264 bool pass_fullpath, bool return_fullpath,
267 run_functor_for_paths (result, paths, filter, arg, false, pass_fullpath, return_fullpath, recurse);
271 find_files_matching_filter (vector<string>& result,
272 const Searchpath& paths,
273 bool (*filter)(const string &, void *),
275 bool pass_fullpath, bool return_fullpath,
278 run_functor_for_paths (result, paths, filter, arg, true, pass_fullpath, return_fullpath, recurse);
282 copy_file(const std::string & from_path, const std::string & to_path)
284 if (!Glib::file_test (from_path, Glib::FILE_TEST_EXISTS)) return false;
286 PBD::ScopedFileDescriptor fd_from (g_open (from_path.c_str(), O_RDONLY, 0444));
287 PBD::ScopedFileDescriptor fd_to (g_open (to_path.c_str(), O_CREAT|O_TRUNC|O_RDWR, 0666));
289 char buf[4096]; // BUFSIZ ??
292 if ((fd_from < 0) || (fd_to < 0)) {
293 error << string_compose (_("Unable to Open files %1 to %2 for Copying(%3)"),
294 from_path, to_path, g_strerror(errno))
299 while (nread = ::read(fd_from, buf, sizeof(buf)), nread > 0) {
302 ssize_t nwritten = ::write(fd_to, out_ptr, nread);
306 } else if (errno != EINTR) {
307 error << string_compose (_("Unable to Copy files %1 to %2(%3)"),
308 from_path, to_path, g_strerror(errno))
319 copy_files(const std::string & from_path, const std::string & to_dir)
321 vector<string> files;
322 find_files_matching_filter (files, from_path, accept_all_files, 0, true, false);
324 for (vector<string>::iterator i = files.begin(); i != files.end(); ++i) {
325 std::string from = Glib::build_filename (from_path, *i);
326 std::string to = Glib::build_filename (to_dir, *i);
327 copy_file (from, to);
332 copy_recurse(const std::string & from_path, const std::string & to_dir)
334 vector<string> files;
335 find_files_matching_filter (files, from_path, accept_all_files, 0, false, true, true);
337 const size_t prefix_len = from_path.size();
338 for (vector<string>::iterator i = files.begin(); i != files.end(); ++i) {
339 std::string from = *i;
340 std::string to = Glib::build_filename (to_dir, (*i).substr(prefix_len));
341 g_mkdir_with_parents (Glib::path_get_dirname (to).c_str(), 0755);
342 copy_file (from, to);
347 touch_file (const std::string& path)
349 int fd = g_open (path.c_str(), O_RDWR|O_CREAT, 0660);
358 get_absolute_path (const std::string & p)
360 if (Glib::path_is_absolute(p)) return p;
361 return Glib::build_filename (Glib::get_current_dir(), p);
365 get_suffix (const std::string & p)
367 string::size_type period = p.find_last_of ('.');
368 if (period == string::npos || period == p.length() - 1) {
371 return p.substr (period+1);
375 equivalent_paths (const std::string& a, const std::string& b)
378 int const rA = g_stat (a.c_str(), &bA);
380 int const rB = g_stat (b.c_str(), &bB);
382 return (rA == 0 && rB == 0 && bA.st_dev == bB.st_dev && bA.st_ino == bB.st_ino);
386 path_is_within (std::string const & haystack, std::string needle)
389 if (equivalent_paths (haystack, needle)) {
393 needle = Glib::path_get_dirname (needle);
394 if (needle == "." || needle == "/" || Glib::path_skip_root(needle).empty()) {
403 exists_and_writable (const std::string & p)
405 /* writable() really reflects the whole folder, but if for any
406 reason the session state file can't be written to, still
412 if (g_stat (p.c_str(), &statbuf) != 0) {
413 /* doesn't exist - not writable */
416 if (!(statbuf.st_mode & S_IWUSR)) {
417 /* exists and is not writable */
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.
424 if (g_access (p.c_str(), W_OK) != 0) {
433 remove_directory_internal (const string& dir, size_t* size, vector<string>* paths,
434 bool just_remove_files)
436 vector<string> tmp_paths;
440 get_paths (tmp_paths, dir, just_remove_files, true);
442 for (vector<string>::const_iterator i = tmp_paths.begin();
443 i != tmp_paths.end(); ++i) {
445 if (g_stat (i->c_str(), &statbuf)) {
449 if (::g_remove (i->c_str())) {
450 error << string_compose (_("cannot remove path %1 (%2)"), *i, strerror (errno))
457 paths->push_back (Glib::path_get_basename(*i));
460 // statbuf.st_size is off_t
461 if (size && statbuf.st_size > 0) {
462 *size += statbuf.st_size;
471 clear_directory (const string& dir, size_t* size, vector<string>* paths)
473 return remove_directory_internal (dir, size, paths, true);
476 // rm -rf <dir> -- used to remove saved plugin state
478 remove_directory (const std::string& dir)
480 remove_directory_internal (dir, 0, 0, false);
481 g_rmdir (dir.c_str());
485 tmp_writable_directory (const char* domain, const string& prefix)
487 std::string tmp_dir = Glib::build_filename (g_get_tmp_dir(), domain);
488 std::string dir_name;
489 std::string new_test_dir;
493 oss << g_random_int ();
494 dir_name = oss.str();
495 new_test_dir = Glib::build_filename (tmp_dir, dir_name);
496 if (Glib::file_test (new_test_dir, Glib::FILE_TEST_EXISTS)) continue;
497 } while (g_mkdir_with_parents (new_test_dir.c_str(), 0755) != 0);
502 toggle_file_existence (string const & path)
504 if (Glib::file_test (path, Glib::FILE_TEST_IS_REGULAR)) {
505 return g_unlink (path.c_str());
508 PBD::ScopedFileDescriptor fd = g_open (path.c_str(), O_CREAT|O_TRUNC|O_RDWR, 0666);
509 return !((int) fd >= 0);