ea45b1da44f34b38ecbd03604f95f50c0b01f06d
[ardour.git] / libs / pbd / filesystem.cc
1 /*
2         Copyright (C) 2007 Tim Mayberry 
3
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.
8
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.
13
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.
17 */
18
19 #include <sys/stat.h>
20
21 #include <glib.h>
22 #include <glib/gstdio.h>
23
24 #include <cerrno>
25 #include <fstream>
26
27 #include <glibmm/fileutils.h>
28 #include <glibmm/miscutils.h>
29
30 #include <pbd/filesystem.h>
31 #include <pbd/error.h>
32 #include <pbd/compose.h>
33
34 #include "i18n.h"
35
36 namespace PBD {
37
38 namespace sys {
39         
40 path&
41 path::operator/=(const path& rhs)
42 {
43         m_path = Glib::build_filename(m_path, rhs.m_path);
44         return *this;
45 }
46
47 path&
48 path::operator/=(const string& rhs)
49 {
50         m_path = Glib::build_filename(m_path, rhs);
51         return *this;
52 }
53
54 path&
55 path::operator/=(const char* rhs)
56 {
57         m_path = Glib::build_filename(m_path, rhs);
58         return *this;
59 }
60
61 path
62 path::branch_path () const
63 {
64         string dir = Glib::path_get_dirname (m_path);
65
66         /*
67          * glib returns "." to signify that the path
68          * has no directory components(branch path)
69          * whereas boost::filesystem returns an empty
70          * string
71          */
72         if(dir == ".")
73         {
74                 return "";
75         }
76         return dir;
77 }
78
79 bool
80 exists (const path & p)
81 {
82         return Glib::file_test (p.to_string(), Glib::FILE_TEST_EXISTS);
83 }
84
85 bool
86 is_directory (const path & p)
87 {
88         return Glib::file_test (p.to_string(), Glib::FILE_TEST_IS_DIR);
89 }
90
91 bool
92 create_directory(const path & p)
93 {
94         if(is_directory(p)) return false;
95
96         int error = g_mkdir (p.to_string().c_str(), S_IRWXU|S_IRWXG|S_IRWXO);
97
98         if(error == -1)
99         {
100                 throw filesystem_error(g_strerror(errno), errno);
101         }
102         return true;
103 }
104
105 bool
106 create_directories(const path & p)
107 {
108         if(is_directory(p)) return false;
109
110         int error = g_mkdir_with_parents (p.to_string().c_str(), S_IRWXU|S_IRWXG|S_IRWXO);
111
112         if(error == -1)
113         {
114                 throw filesystem_error(g_strerror(errno), errno);
115         }
116         return true;
117 }
118
119 bool
120 remove(const path & p)
121 {
122         if(!exists(p)) return false;
123
124         int error = g_unlink (p.to_string().c_str());
125
126         if(error == -1)
127         {
128                 throw filesystem_error(g_strerror(errno), errno);
129         }
130         return true;
131 }
132
133 void
134 copy_file(const path & from_path, const path & to_path)
135 {
136         // this implementation could use mucho memory
137         // for big files.
138         std::ifstream in(from_path.to_string().c_str());
139         std::ofstream out(to_path.to_string().c_str());
140         
141         if (!in || !out) {
142                 throw filesystem_error(string_compose(_("Could not open files %1 and %2 for copying"),
143                                         from_path.to_string(), to_path.to_string()));
144         }
145         
146         out << in.rdbuf();
147         
148         if (!in || !out) {
149                 throw filesystem_error(string_compose(_("Could not copy existing file %1 to %2"),
150                                         from_path.to_string(), to_path.to_string()));
151                 remove (to_path);
152         }
153 }
154
155 string
156 basename (const path & p)
157 {
158         string base = Glib::path_get_basename (p.to_string());
159
160         string::size_type n = base.rfind ('.');
161
162         return base.substr (0, n);
163 }
164
165 } // namespace sys
166
167 } // namespace PBD