2 Copyright (C) 2007 Tim Mayberry
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <glib/gstdio.h>
27 #include <io.h> // For W_OK
30 #include <glibmm/fileutils.h>
31 #include <glibmm/miscutils.h>
32 #include <glibmm/pattern.h>
34 #include <giomm/file.h>
36 #include "pbd/compose.h"
37 #include "pbd/file_utils.h"
38 #include "pbd/error.h"
39 #include "pbd/pathscanner.h"
40 #include "pbd/stl_delete.h"
49 get_files_in_directory (const std::string& directory_path, vector<string>& result)
51 if (!Glib::file_test (directory_path, Glib::FILE_TEST_IS_DIR)) return;
55 Glib::Dir dir(directory_path);
56 std::copy(dir.begin(), dir.end(), std::back_inserter(result));
58 catch (Glib::FileError& err)
60 warning << err.what() << endmsg;
65 find_matching_files_in_directory (const std::string& directory,
66 const Glib::PatternSpec& pattern,
67 vector<std::string>& result)
69 vector<string> tmp_files;
71 get_files_in_directory (directory, tmp_files);
72 result.reserve(tmp_files.size());
74 for (vector<string>::iterator file_iter = tmp_files.begin();
75 file_iter != tmp_files.end();
78 if (!pattern.match(*file_iter)) continue;
80 std::string full_path(directory);
81 full_path = Glib::build_filename (full_path, *file_iter);
83 result.push_back(full_path);
88 find_matching_files_in_directories (const vector<std::string>& paths,
89 const Glib::PatternSpec& pattern,
90 vector<std::string>& result)
92 for (vector<std::string>::const_iterator path_iter = paths.begin();
93 path_iter != paths.end();
96 find_matching_files_in_directory (*path_iter, pattern, result);
101 find_matching_files_in_search_path (const SearchPath& search_path,
102 const Glib::PatternSpec& pattern,
103 vector<std::string>& result)
105 find_matching_files_in_directories (search_path, pattern, result);
109 find_file_in_search_path(const SearchPath& search_path,
110 const string& filename,
113 vector<std::string> tmp;
114 Glib::PatternSpec tmp_pattern(filename);
116 find_matching_files_in_search_path (search_path, tmp_pattern, tmp);
126 info << string_compose
128 "Found more than one file matching %1 in search path %2",
136 result = tmp.front();
142 copy_file(const std::string & from_path, const std::string & to_path)
144 if (!Glib::file_test (from_path, Glib::FILE_TEST_EXISTS)) return false;
146 Glib::RefPtr<Gio::File> from_file = Gio::File::create_for_path(from_path);
147 Glib::RefPtr<Gio::File> to_file = Gio::File::create_for_path(to_path);
151 from_file->copy (to_file, Gio::FILE_COPY_OVERWRITE);
153 catch(const Glib::Exception& ex)
155 error << string_compose (_("Unable to Copy file %1 to %2 (%3)"),
156 from_path, to_path, ex.what())
164 bool accept_all_files (string const &, void *)
170 copy_files(const std::string & from_path, const std::string & to_dir)
173 vector<string*>* files = scanner (from_path, accept_all_files, 0, true, false);
176 for (vector<string*>::iterator i = files->begin(); i != files->end(); ++i) {
177 std::string from = Glib::build_filename (from_path, **i);
178 std::string to = Glib::build_filename (to_dir, **i);
179 copy_file (from, to);
181 vector_delete (files);
186 get_absolute_path (const std::string & p)
188 Glib::RefPtr<Gio::File> f = Gio::File::create_for_path (p);
189 return f->get_path ();
193 equivalent_paths (const std::string& a, const std::string& b)
196 int const rA = g_stat (a.c_str(), &bA);
198 int const rB = g_stat (b.c_str(), &bB);
200 return (rA == 0 && rB == 0 && bA.st_dev == bB.st_dev && bA.st_ino == bB.st_ino);
204 path_is_within (std::string const & haystack, std::string needle)
207 if (equivalent_paths (haystack, needle)) {
211 needle = Glib::path_get_dirname (needle);
212 if (needle == "." || needle == "/") {
221 exists_and_writable (const std::string & p)
223 /* writable() really reflects the whole folder, but if for any
224 reason the session state file can't be written to, still
230 if (g_stat (p.c_str(), &statbuf) != 0) {
231 /* doesn't exist - not writable */
234 if (!(statbuf.st_mode & S_IWUSR)) {
235 /* exists and is not writable */
238 /* filesystem may be mounted read-only, so even though file
239 * permissions permit access, the mount status does not.
240 * access(2) seems like the best test for this.
242 if (g_access (p.c_str(), W_OK) != 0) {