229b22fcb54ed6e61f1bb6835d9d48f8ee3974fb
[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
26 #include <glibmm/fileutils.h>
27 #include <glibmm/miscutils.h>
28
29 #include <pbd/filesystem.h>
30 #include <pbd/error.h>
31
32 namespace PBD {
33
34 namespace sys {
35         
36 path&
37 path::operator/=(const path& rhs)
38 {
39         m_path = Glib::build_filename(m_path, rhs.m_path);
40         return *this;
41 }
42
43 path&
44 path::operator/=(const string& rhs)
45 {
46         m_path = Glib::build_filename(m_path, rhs);
47         return *this;
48 }
49
50 path&
51 path::operator/=(const char* rhs)
52 {
53         m_path = Glib::build_filename(m_path, rhs);
54         return *this;
55 }
56
57 bool
58 exists (const path & p)
59 {
60         return Glib::file_test (p.to_string(), Glib::FILE_TEST_EXISTS);
61 }
62
63 bool
64 is_directory (const path & p)
65 {
66         return Glib::file_test (p.to_string(), Glib::FILE_TEST_IS_DIR);
67 }
68
69 bool
70 create_directory(const path & p)
71 {
72         if(is_directory(p)) return false;
73
74         int error = g_mkdir (p.to_string().c_str(), S_IRWXU|S_IRWXG|S_IRWXO);
75
76         if(error == -1)
77         {
78                 throw filesystem_error(g_strerror(errno), errno);
79         }
80         return true;
81 }
82
83 bool
84 create_directories(const path & p)
85 {
86         if(is_directory(p)) return false;
87
88         int error = g_mkdir_with_parents (p.to_string().c_str(), S_IRWXU|S_IRWXG|S_IRWXO);
89
90         if(error == -1)
91         {
92                 throw filesystem_error(g_strerror(errno), errno);
93         }
94         return true;
95 }
96
97 string
98 basename (const path & p)
99 {
100         // I'm not sure if this works quite the same as boost::filesystem::basename
101         return Glib::path_get_basename (p.to_string ());
102 }
103
104 } // namespace sys
105
106 } // namespace PBD