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.
22 #include <glib/gstdio.h>
24 #include <giomm/file.h>
29 #include <glibmm/fileutils.h>
30 #include <glibmm/miscutils.h>
32 #include "pbd/filesystem.h"
33 #include "pbd/error.h"
34 #include "pbd/compose.h"
35 #include "pbd/pathscanner.h"
46 path::operator/=(const path& rhs)
48 m_path = Glib::build_filename(m_path, rhs.m_path);
53 path::operator/=(const string& rhs)
55 m_path = Glib::build_filename(m_path, rhs);
60 path::operator/=(const char* rhs)
62 m_path = Glib::build_filename(m_path, rhs);
69 return Glib::path_get_basename(m_path);
73 path::branch_path () const
75 string dir = Glib::path_get_dirname (m_path);
78 * glib returns "." to signify that the path
79 * has no directory components(branch path)
80 * whereas boost::filesystem returns an empty
91 exists (const path & p)
93 return Glib::file_test (p.to_string(), Glib::FILE_TEST_EXISTS);
97 exists_and_writable (const path & p)
99 /* writable() really reflects the whole folder, but if for any
100 reason the session state file can't be written to, still
106 if (g_stat (p.to_string().c_str(), &statbuf) != 0) {
107 /* doesn't exist - not writable */
110 if (!(statbuf.st_mode & S_IWUSR)) {
111 /* exists and is not writable */
121 is_directory (const path & p)
123 return Glib::file_test (p.to_string(), Glib::FILE_TEST_IS_DIR);
127 create_directory(const path & p)
129 if(is_directory(p)) return false;
131 int error = g_mkdir (p.to_string().c_str(), S_IRWXU|S_IRWXG|S_IRWXO);
135 throw filesystem_error(g_strerror(errno), errno);
141 create_directories(const path & p)
143 if(is_directory(p)) return false;
145 int error = g_mkdir_with_parents (p.to_string().c_str(), S_IRWXU|S_IRWXG|S_IRWXO);
149 throw filesystem_error(g_strerror(errno), errno);
155 remove(const path & p)
157 if(!exists(p)) return false;
159 int error = g_unlink (p.to_string().c_str());
163 throw filesystem_error(g_strerror(errno), errno);
169 rename (const path & from_path, const path & to_path)
171 // g_rename is a macro that evaluates to ::rename on
172 // POSIX systems, without the global namespace qualifier
173 // it would evaluate to a recursive call(if it compiled)
174 if ( ::g_rename( from_path.to_string().c_str(),
175 to_path.to_string().c_str() ) == -1 )
177 throw filesystem_error(g_strerror(errno), errno);
181 // XXX character encoding.
183 copy_file(const path & from_path, const path & to_path)
185 std::ifstream in(from_path.to_string().c_str());
186 std::ofstream out(to_path.to_string().c_str());
189 throw filesystem_error(string_compose(_("Could not open files %1 and %2 for copying"),
190 from_path.to_string(), to_path.to_string()));
197 throw filesystem_error(string_compose(_("Could not copy existing file %1 to %2"),
198 from_path.to_string(), to_path.to_string()));
203 bool accept_all_files (string const &, void *)
209 copy_files(const path & from_path, const path & to_dir)
212 vector<string*>* files = scanner (from_path.to_string(), accept_all_files, 0, true, false);
213 for (vector<string*>::iterator i = files->begin(); i != files->end(); ++i) {
214 sys::path from = from_path;
216 sys::path to = to_dir;
219 copy_file (from, to);
224 basename (const path & p)
226 string base(p.leaf());
228 string::size_type n = base.rfind ('.');
230 return base.substr (0, n);
234 extension (const path & p)
236 string base(p.leaf());
238 string::size_type n = base.rfind ('.');
240 if (n != string::npos)
242 return base.substr(n);
249 /** Take a (possibly) relative path and make it absolute */
251 get_absolute_path (const path & p)
253 Glib::RefPtr<Gio::File> f = Gio::File::create_for_path (p.to_string ());
254 return f->get_path ();
257 /** @return true if a and b have the same inode */
259 inodes_same (const path& a, const path& b)
262 int const rA = stat (a.to_string().c_str(), &bA);
264 int const rB = stat (b.to_string().c_str(), &bB);
266 return (rA == 0 && rB == 0 && bA.st_ino == bB.st_ino);
269 /** Find out if `needle' is a file or directory within the
270 * directory `haystack'.
271 * @return true if it is.
274 path_is_within (path const & haystack, path needle)
277 if (inodes_same (haystack, needle)) {
281 needle = needle.branch_path ();
282 if (needle.to_string().empty() || needle.to_string() == "/") {