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>
35 #include <string.h> /* strerror */
38 #include <sys/types.h>
42 /* close(), read(), write() */
44 #include <io.h> // Microsoft's nearest equivalent to <unistd.h>
49 #include "pbd/compose.h"
50 #include "pbd/file_utils.h"
51 #include "pbd/debug.h"
52 #include "pbd/error.h"
53 #include "pbd/pathscanner.h"
54 #include "pbd/stl_delete.h"
63 get_files_in_directory (const std::string& directory_path, vector<string>& result)
65 if (!Glib::file_test (directory_path, Glib::FILE_TEST_IS_DIR)) return;
69 Glib::Dir dir(directory_path);
70 std::copy(dir.begin(), dir.end(), std::back_inserter(result));
72 catch (Glib::FileError& err)
74 warning << err.what() << endmsg;
79 find_matching_files_in_directory (const std::string& directory,
80 const Glib::PatternSpec& pattern,
81 vector<std::string>& result)
83 vector<string> tmp_files;
85 get_files_in_directory (directory, tmp_files);
86 result.reserve(tmp_files.size());
88 for (vector<string>::iterator file_iter = tmp_files.begin();
89 file_iter != tmp_files.end();
92 if (!pattern.match(*file_iter)) continue;
94 std::string full_path(directory);
95 full_path = Glib::build_filename (full_path, *file_iter);
99 string_compose("Found file %1\n", full_path)
102 result.push_back(full_path);
107 find_matching_files_in_directories (const vector<std::string>& paths,
108 const Glib::PatternSpec& pattern,
109 vector<std::string>& result)
111 for (vector<std::string>::const_iterator path_iter = paths.begin();
112 path_iter != paths.end();
115 find_matching_files_in_directory (*path_iter, pattern, result);
120 find_matching_files_in_search_path (const Searchpath& search_path,
121 const Glib::PatternSpec& pattern,
122 vector<std::string>& result)
124 find_matching_files_in_directories (search_path, pattern, result);
128 find_file_in_search_path(const Searchpath& search_path,
129 const string& filename,
132 vector<std::string> tmp;
133 Glib::PatternSpec tmp_pattern(filename);
135 find_matching_files_in_search_path (search_path, tmp_pattern, tmp);
141 string_compose("No file matching %1 found in Path: %2\n", filename, search_path.to_string())
150 string_compose("Found more that one file matching %1 in Path: %2\n", filename, search_path.to_string())
154 result = tmp.front();
158 string_compose("Found file %1 in Path: %2\n", filename, search_path.to_string())
165 copy_file(const std::string & from_path, const std::string & to_path)
167 if (!Glib::file_test (from_path, Glib::FILE_TEST_EXISTS)) return false;
171 char buf[4096]; // BUFSIZ ??
174 fd_from = ::open(from_path.c_str(), O_RDONLY);
179 fd_to = ::open(to_path.c_str(), O_WRONLY | O_CREAT, 0666);
184 while (nread = ::read(fd_from, buf, sizeof(buf)), nread > 0) {
187 ssize_t nwritten = ::write(fd_to, out_ptr, nread);
191 } else if (errno != EINTR) {
198 if (::close(fd_to)) {
207 int saved_errno = errno;
216 error << string_compose (_("Unable to Copy file %1 to %2 (%3)"),
217 from_path, to_path, strerror(saved_errno))
223 bool accept_all_files (string const &, void *)
229 copy_files(const std::string & from_path, const std::string & to_dir)
232 vector<string*>* files = scanner (from_path, accept_all_files, 0, true, false);
235 for (vector<string*>::iterator i = files->begin(); i != files->end(); ++i) {
236 std::string from = Glib::build_filename (from_path, **i);
237 std::string to = Glib::build_filename (to_dir, **i);
238 copy_file (from, to);
240 vector_delete (files);
245 get_absolute_path (const std::string & p)
247 if (Glib::path_is_absolute(p)) return p;
248 return Glib::build_filename (Glib::get_current_dir(), p);
252 equivalent_paths (const std::string& a, const std::string& b)
255 int const rA = g_stat (a.c_str(), &bA);
257 int const rB = g_stat (b.c_str(), &bB);
259 return (rA == 0 && rB == 0 && bA.st_dev == bB.st_dev && bA.st_ino == bB.st_ino);
263 path_is_within (std::string const & haystack, std::string needle)
266 if (equivalent_paths (haystack, needle)) {
270 needle = Glib::path_get_dirname (needle);
271 if (needle == "." || needle == "/") {
280 exists_and_writable (const std::string & p)
282 /* writable() really reflects the whole folder, but if for any
283 reason the session state file can't be written to, still
289 if (g_stat (p.c_str(), &statbuf) != 0) {
290 /* doesn't exist - not writable */
293 if (!(statbuf.st_mode & S_IWUSR)) {
294 /* exists and is not writable */
297 /* filesystem may be mounted read-only, so even though file
298 * permissions permit access, the mount status does not.
299 * access(2) seems like the best test for this.
301 if (g_access (p.c_str(), W_OK) != 0) {