2412b7676119db816139107a6185c45da3befff7
[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 bool
62 exists (const path & p)
63 {
64         return Glib::file_test (p.to_string(), Glib::FILE_TEST_EXISTS);
65 }
66
67 bool
68 is_directory (const path & p)
69 {
70         return Glib::file_test (p.to_string(), Glib::FILE_TEST_IS_DIR);
71 }
72
73 bool
74 create_directory(const path & p)
75 {
76         if(is_directory(p)) return false;
77
78         int error = g_mkdir (p.to_string().c_str(), S_IRWXU|S_IRWXG|S_IRWXO);
79
80         if(error == -1)
81         {
82                 throw filesystem_error(g_strerror(errno), errno);
83         }
84         return true;
85 }
86
87 bool
88 create_directories(const path & p)
89 {
90         if(is_directory(p)) return false;
91
92         int error = g_mkdir_with_parents (p.to_string().c_str(), S_IRWXU|S_IRWXG|S_IRWXO);
93
94         if(error == -1)
95         {
96                 throw filesystem_error(g_strerror(errno), errno);
97         }
98         return true;
99 }
100
101 bool
102 remove(const path & p)
103 {
104         if(!exists(p)) return false;
105
106         int error = g_unlink (p.to_string().c_str());
107
108         if(error == -1)
109         {
110                 throw filesystem_error(g_strerror(errno), errno);
111         }
112         return true;
113 }
114
115 void
116 copy_file(const path & from_path, const path & to_path)
117 {
118         // this implementation could use mucho memory
119         // for big files.
120         std::ifstream in(from_path.to_string().c_str());
121         std::ofstream out(to_path.to_string().c_str());
122         
123         if (!in || !out) {
124                 throw filesystem_error(string_compose(_("Could not open files %1 and %2 for copying"),
125                                         from_path.to_string(), to_path.to_string()));
126         }
127         
128         out << in.rdbuf();
129         
130         if (!in || !out) {
131                 throw filesystem_error(string_compose(_("Could not copy existing file %1 to %2"),
132                                         from_path.to_string(), to_path.to_string()));
133                 remove (to_path);
134         }
135 }
136
137 string
138 basename (const path & p)
139 {
140         // I'm not sure if this works quite the same as boost::filesystem::basename
141         return Glib::path_get_basename (p.to_string ());
142 }
143
144 } // namespace sys
145
146 } // namespace PBD