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"
45 path::operator/=(const path& rhs)
47 m_path = Glib::build_filename(m_path, rhs.m_path);
52 path::operator/=(const string& rhs)
54 m_path = Glib::build_filename(m_path, rhs);
59 path::operator/=(const char* rhs)
61 m_path = Glib::build_filename(m_path, rhs);
68 return Glib::path_get_basename(m_path);
72 path::branch_path () const
74 string dir = Glib::path_get_dirname (m_path);
77 * glib returns "." to signify that the path
78 * has no directory components(branch path)
79 * whereas boost::filesystem returns an empty
90 exists (const path & p)
92 return Glib::file_test (p.to_string(), Glib::FILE_TEST_EXISTS);
96 exists_and_writable (const path & p)
98 /* writable() really reflects the whole folder, but if for any
99 reason the session state file can't be written to, still
105 if (g_stat (p.to_string().c_str(), &statbuf) != 0) {
106 /* doesn't exist - not writable */
109 if (!(statbuf.st_mode & S_IWUSR)) {
110 /* exists and is not writable */
120 is_directory (const path & p)
122 return Glib::file_test (p.to_string(), Glib::FILE_TEST_IS_DIR);
126 create_directory(const path & p)
128 if(is_directory(p)) return false;
130 int error = g_mkdir (p.to_string().c_str(), S_IRWXU|S_IRWXG|S_IRWXO);
134 throw filesystem_error(g_strerror(errno), errno);
140 create_directories(const path & p)
142 if(is_directory(p)) return false;
144 int error = g_mkdir_with_parents (p.to_string().c_str(), S_IRWXU|S_IRWXG|S_IRWXO);
148 throw filesystem_error(g_strerror(errno), errno);
154 remove(const path & p)
156 if(!exists(p)) return false;
158 int error = g_unlink (p.to_string().c_str());
162 throw filesystem_error(g_strerror(errno), errno);
168 rename (const path & from_path, const path & to_path)
170 // g_rename is a macro that evaluates to ::rename on
171 // POSIX systems, without the global namespace qualifier
172 // it would evaluate to a recursive call(if it compiled)
173 if ( ::g_rename( from_path.to_string().c_str(),
174 to_path.to_string().c_str() ) == -1 )
176 throw filesystem_error(g_strerror(errno), errno);
180 // XXX character encoding.
182 copy_file(const path & from_path, const path & to_path)
184 std::ifstream in(from_path.to_string().c_str());
185 std::ofstream out(to_path.to_string().c_str());
188 throw filesystem_error(string_compose(_("Could not open files %1 and %2 for copying"),
189 from_path.to_string(), to_path.to_string()));
196 throw filesystem_error(string_compose(_("Could not copy existing file %1 to %2"),
197 from_path.to_string(), to_path.to_string()));
202 basename (const path & p)
204 string base(p.leaf());
206 string::size_type n = base.rfind ('.');
208 return base.substr (0, n);
212 extension (const path & p)
214 string base(p.leaf());
216 string::size_type n = base.rfind ('.');
218 if (n != string::npos)
220 return base.substr(n);
227 /** Take a (possibly) relative path and make it absolute */
229 get_absolute_path (const path & p)
231 Glib::RefPtr<Gio::File> f = Gio::File::create_for_path (p.to_string ());
232 return f->get_path ();