25 #include <glib/gstdio.h>
31 #include <glibmm/fileutils.h>
32 #include <glibmm/miscutils.h>
33 #include <glibmm/pattern.h>
39 #include <sys/types.h>
46 #include <ardourext/misc.h>
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) {
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) {
98 pass_fullpath, return_fullpath, recurse);
101 if (is_dir && pass_files_only) {
108 functor_str = fullpath;
110 functor_str = basename;
116 if (!functor(functor_str, arg)) {
123 if (return_fullpath) {
124 result.push_back(fullpath);
126 result.push_back(basename);
130 catch (Glib::FileError& err)
150 files_only,
true,
true, recurse);
156 return get_paths (result, paths,
true,
false);
163 Glib::PatternSpec* pattern = (Glib::PatternSpec*)arg;
164 return pattern->match(str);
170 const Glib::PatternSpec& pattern)
173 const_cast<Glib::PatternSpec*>(&pattern),
174 true,
false,
true,
false);
180 const string& pattern)
182 Glib::PatternSpec tmp(pattern);
188 const string& filename,
191 vector<std::string> tmp;
195 if (tmp.size() == 0) {
202 if (tmp.size() != 1) {
204 string_compose(
"Found more that one file matching %1 in Path: %2\n",
208 result = tmp.front();
221 regex_t* pattern = (regex_t*)arg;
222 return regexec (pattern, str.c_str(), 0, 0, 0) == 0;
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 ("
254 true,
true, recurse);
256 regfree (&compiled_pattern);
262 bool (*filter)(
const string &,
void *),
264 bool pass_fullpath,
bool return_fullpath,
273 bool (*filter)(
const string &,
void *),
275 bool pass_fullpath,
bool return_fullpath,
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;
292 if ((fd_from < 0) || (fd_to < 0)) {
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) {
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;
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);
332 copy_recurse(
const std::string & from_path,
const std::string & to_dir)
334 vector<string> files;
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);
349 if (Glib::path_is_absolute(p))
return p;
350 return Glib::build_filename (Glib::get_current_dir(), p);
356 string::size_type period = p.find_last_of (
'.');
357 if (period == string::npos || period == p.length() - 1) {
360 return p.substr (period+1);
367 int const rA = g_stat (a.c_str(), &bA);
369 int const rB = g_stat (b.c_str(), &bB);
371 return (rA == 0 && rB == 0 && bA.st_dev == bB.st_dev && bA.st_ino == bB.st_ino);
382 needle = Glib::path_get_dirname (needle);
383 if (needle ==
"." || needle ==
"/" || Glib::path_skip_root(needle).empty()) {
401 if (g_stat (p.c_str(), &statbuf) != 0) {
405 if (!(statbuf.st_mode & S_IWUSR)) {
413 if (g_access (p.c_str(), W_OK) != 0) {
423 bool just_remove_files)
425 vector<string> tmp_paths;
429 get_paths (tmp_paths, dir, just_remove_files,
true);
431 for (vector<string>::const_iterator i = tmp_paths.begin();
432 i != tmp_paths.end(); ++i) {
434 if (g_stat (i->c_str(), &statbuf)) {
438 if (::g_remove (i->c_str())) {
445 paths->push_back (Glib::path_get_basename(*i));
449 *size += statbuf.st_size;
473 std::string tmp_dir = Glib::build_filename (g_get_tmp_dir(), domain);
474 std::string dir_name;
475 std::string new_test_dir;
479 oss << g_random_int ();
480 dir_name = oss.str();
481 new_test_dir = Glib::build_filename (tmp_dir, dir_name);
482 if (Glib::file_test (new_test_dir, Glib::FILE_TEST_EXISTS))
continue;
483 }
while (g_mkdir_with_parents (new_test_dir.c_str(), 0755) != 0);
490 if (Glib::file_test (path, Glib::FILE_TEST_IS_REGULAR)) {
491 return g_unlink (path.c_str());
495 return !((int) fd >= 0);
LIBPBD_API std::string path_expand(std::string path)
void find_paths_matching_filter(vector< string > &result, const Searchpath &paths, bool(*filter)(const string &, void *), void *arg, bool pass_fullpath, bool return_fullpath, bool recurse)
void remove_directory(const std::string &dir)
bool path_is_within(std::string const &haystack, std::string needle)
void find_files_matching_regex(vector< string > &result, const Searchpath &paths, const std::string ®exp, bool recurse)
std::string get_suffix(const std::string &p)
LIBPBD_API Transmitter error
LIBPBD_API Transmitter warning
std::ostream & endmsg(std::ostream &ostr)
bool find_file(const Searchpath &search_path, const string &filename, std::string &result)
int toggle_file_existence(string const &path)
int clear_directory(const string &dir, size_t *size, vector< string > *paths)
static bool regexp_filter(const string &str, void *arg)
void copy_recurse(const std::string &from_path, const std::string &to_dir)
void run_functor_for_paths(vector< string > &result, const Searchpath &paths, bool(*functor)(const string &, void *), void *arg, bool pass_files_only, bool pass_fullpath, bool return_fullpath, bool recurse)
bool copy_file(const std::string &from_path, const std::string &to_path)
void find_files_matching_pattern(vector< string > &result, const Searchpath &paths, const string &pattern)
string tmp_writable_directory(const char *domain, const string &prefix)
bool exists_and_writable(const std::string &p)
std::string get_absolute_path(const std::string &p)
#define DEBUG_TRACE(bits, str)
void copy_files(const std::string &from_path, const std::string &to_dir)
void get_paths(vector< string > &result, const Searchpath &paths, bool files_only, bool recurse)
static bool accept_all_files(string const &, void *)
LIBPBD_TEMPLATE_MEMBER_API const std::string to_string() const
static bool pattern_filter(const string &str, void *arg)
void get_files(vector< string > &result, const Searchpath &paths)
int remove_directory_internal(const string &dir, size_t *size, vector< string > *paths, bool just_remove_files)
bool equivalent_paths(const std::string &a, const std::string &b)
void find_files_matching_filter(vector< string > &result, const Searchpath &paths, bool(*filter)(const string &, void *), void *arg, bool pass_fullpath, bool return_fullpath, bool recurse)
std::string string_compose(const std::string &fmt, const T1 &o1)