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>
53 #ifdef PLATFORM_WINDOWS
54 #define strtok_r strtok_s
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"
71 get_files_in_directory (const std::string& directory_path, vector<string>& result)
73 if (!Glib::file_test (directory_path, Glib::FILE_TEST_IS_DIR)) return;
77 Glib::Dir dir(directory_path);
78 std::copy(dir.begin(), dir.end(), std::back_inserter(result));
80 catch (Glib::FileError& err)
82 warning << err.what() << endmsg;
87 find_matching_files_in_directory (const std::string& directory,
88 const Glib::PatternSpec& pattern,
89 vector<std::string>& result)
91 vector<string> tmp_files;
93 get_files_in_directory (directory, tmp_files);
94 result.reserve(tmp_files.size());
96 for (vector<string>::iterator file_iter = tmp_files.begin();
97 file_iter != tmp_files.end();
100 if (!pattern.match(*file_iter)) continue;
102 std::string full_path(directory);
103 full_path = Glib::build_filename (full_path, *file_iter);
107 string_compose("Found file %1\n", full_path)
110 result.push_back(full_path);
115 find_matching_files_in_directories (const vector<std::string>& paths,
116 const Glib::PatternSpec& pattern,
117 vector<std::string>& result)
119 for (vector<std::string>::const_iterator path_iter = paths.begin();
120 path_iter != paths.end();
123 find_matching_files_in_directory (*path_iter, pattern, result);
128 find_matching_files_in_search_path (const Searchpath& search_path,
129 const Glib::PatternSpec& pattern,
130 vector<std::string>& result)
132 find_matching_files_in_directories (search_path, pattern, result);
136 find_file_in_search_path(const Searchpath& search_path,
137 const string& filename,
140 vector<std::string> tmp;
141 Glib::PatternSpec tmp_pattern(filename);
143 find_matching_files_in_search_path (search_path, tmp_pattern, tmp);
149 string_compose("No file matching %1 found in Path: %2\n", filename, search_path.to_string())
158 string_compose("Found more that one file matching %1 in Path: %2\n", filename, search_path.to_string())
162 result = tmp.front();
166 string_compose("Found file %1 in Path: %2\n", filename, search_path.to_string())
174 regexp_filter (const string& str, void *arg)
176 regex_t* pattern = (regex_t*)arg;
177 return regexec (pattern, str.c_str(), 0, 0, 0) == 0;
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,
190 regex_t compiled_pattern;
192 if ((err = regcomp (&compiled_pattern, regexp.c_str(),
193 REG_EXTENDED|REG_NOSUB))) {
195 regerror (err, &compiled_pattern,
198 error << "Cannot compile soundfile regexp for use ("
206 find_files_matching_filter (result, dirpath,
207 regexp_filter, &compiled_pattern,
208 match_fullpath, return_fullpath,
211 regfree (&compiled_pattern);
215 find_files_matching_filter (vector<string>& result,
216 const string &dirpath,
217 bool (*filter)(const string &, void *),
219 bool match_fullpath, bool return_fullpath,
224 struct dirent *finfo;
225 char *pathcopy = strdup (search_path_expand (dirpath).c_str());
232 if ((thisdir = strtok_r (pathcopy, G_SEARCHPATH_SEPARATOR_S, &saveptr)) == 0 ||
233 strlen (thisdir) == 0) {
240 if ((dir = opendir (thisdir)) == 0) {
244 while ((finfo = readdir (dir)) != 0) {
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')) {
251 fullpath = Glib::build_filename (thisdir, finfo->d_name);
254 if (stat (fullpath.c_str(), &statbuf) < 0) {
258 if (statbuf.st_mode & S_IFDIR && recurse) {
259 find_files_matching_filter (result, fullpath, filter, arg, match_fullpath, return_fullpath, limit, recurse);
262 if (match_fullpath) {
263 search_str = fullpath;
265 search_str = finfo->d_name;
268 if (!filter(search_str, arg)) {
272 if (return_fullpath) {
273 result.push_back(fullpath);
275 result.push_back(finfo->d_name);
283 } while ((limit < 0 || (nfound < limit)) && (thisdir = strtok_r (0, G_SEARCHPATH_SEPARATOR_S, &saveptr)));
290 copy_file(const std::string & from_path, const std::string & to_path)
292 if (!Glib::file_test (from_path, Glib::FILE_TEST_EXISTS)) return false;
296 char buf[4096]; // BUFSIZ ??
299 fd_from = ::open(from_path.c_str(), O_RDONLY);
304 fd_to = ::open(to_path.c_str(), O_WRONLY | O_CREAT, 0666);
309 while (nread = ::read(fd_from, buf, sizeof(buf)), nread > 0) {
312 ssize_t nwritten = ::write(fd_to, out_ptr, nread);
316 } else if (errno != EINTR) {
323 if (::close(fd_to)) {
332 int saved_errno = errno;
341 error << string_compose (_("Unable to Copy file %1 to %2 (%3)"),
342 from_path, to_path, strerror(saved_errno))
348 bool accept_all_files (string const &, void *)
354 copy_files(const std::string & from_path, const std::string & to_dir)
356 vector<string> files;
357 find_files_matching_filter (files, from_path, accept_all_files, 0, true, false);
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);
367 get_absolute_path (const std::string & p)
369 if (Glib::path_is_absolute(p)) return p;
370 return Glib::build_filename (Glib::get_current_dir(), p);
374 equivalent_paths (const std::string& a, const std::string& b)
377 int const rA = g_stat (a.c_str(), &bA);
379 int const rB = g_stat (b.c_str(), &bB);
381 return (rA == 0 && rB == 0 && bA.st_dev == bB.st_dev && bA.st_ino == bB.st_ino);
385 path_is_within (std::string const & haystack, std::string needle)
388 if (equivalent_paths (haystack, needle)) {
392 needle = Glib::path_get_dirname (needle);
393 if (needle == "." || needle == "/") {
402 exists_and_writable (const std::string & p)
404 /* writable() really reflects the whole folder, but if for any
405 reason the session state file can't be written to, still
411 if (g_stat (p.c_str(), &statbuf) != 0) {
412 /* doesn't exist - not writable */
415 if (!(statbuf.st_mode & S_IWUSR)) {
416 /* exists and is not writable */
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.
423 if (g_access (p.c_str(), W_OK) != 0) {