Merge branch 'patches' of https://github.com/jdekozak/ardour
[ardour.git] / libs / pbd / file_utils.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
20 #include <algorithm>
21
22 #include <glib.h>
23 #include <glib/gstdio.h>
24
25 #include <glibmm/fileutils.h>
26 #include <glibmm/miscutils.h>
27 #include <glibmm/pattern.h>
28
29 #include <giomm/file.h>
30
31 #include "pbd/compose.h"
32 #include "pbd/file_utils.h"
33 #include "pbd/error.h"
34 #include "pbd/pathscanner.h"
35
36 #include "i18n.h"
37
38 using namespace std;
39
40 namespace PBD {
41
42 void
43 get_files_in_directory (const std::string& directory_path, vector<string>& result)
44 {
45         if (!Glib::file_test (directory_path, Glib::FILE_TEST_IS_DIR)) return;
46
47         try
48         {
49                 Glib::Dir dir(directory_path);
50                 std::copy(dir.begin(), dir.end(), std::back_inserter(result));
51         }
52         catch (Glib::FileError& err)
53         {
54                 warning << err.what() << endmsg;
55         }
56 }
57
58 void
59 find_matching_files_in_directory (const std::string& directory,
60                                   const Glib::PatternSpec& pattern,
61                                   vector<std::string>& result)
62 {
63         vector<string> tmp_files;
64
65         get_files_in_directory (directory, tmp_files);
66         result.reserve(tmp_files.size());
67
68         for (vector<string>::iterator file_iter = tmp_files.begin();
69                         file_iter != tmp_files.end();
70                         ++file_iter)
71         {
72                 if (!pattern.match(*file_iter)) continue;
73
74                 std::string full_path(directory);
75                 full_path = Glib::build_filename (full_path, *file_iter);
76
77                 result.push_back(full_path);
78         }
79 }
80
81 void
82 find_matching_files_in_directories (const vector<std::string>& paths,
83                                     const Glib::PatternSpec& pattern,
84                                     vector<std::string>& result)
85 {
86         for (vector<std::string>::const_iterator path_iter = paths.begin();
87                         path_iter != paths.end();
88                         ++path_iter)
89         {
90                 find_matching_files_in_directory (*path_iter, pattern, result);
91         }               
92 }
93
94 void
95 find_matching_files_in_search_path (const SearchPath& search_path,
96                                     const Glib::PatternSpec& pattern,
97                                     vector<std::string>& result)
98 {
99         find_matching_files_in_directories (search_path, pattern, result);    
100 }
101
102 bool
103 find_file_in_search_path(const SearchPath& search_path,
104                          const string& filename,
105                          std::string& result)
106 {
107         vector<std::string> tmp;
108         Glib::PatternSpec tmp_pattern(filename);
109
110         find_matching_files_in_search_path (search_path, tmp_pattern, tmp);
111
112         if (tmp.size() == 0)
113         {
114                 return false;
115         }
116
117 #if 0
118         if (tmp.size() != 1)
119         {
120                 info << string_compose
121                         (
122                          "Found more than one file matching %1 in search path %2",
123                          filename,
124                          search_path ()
125                         )
126                         << endmsg;
127         }
128 #endif
129
130         result = tmp.front();
131
132         return true;
133 }
134
135 bool
136 copy_file(const std::string & from_path, const std::string & to_path)
137 {
138         if (!Glib::file_test (from_path, Glib::FILE_TEST_EXISTS)) return false;
139
140         Glib::RefPtr<Gio::File> from_file = Gio::File::create_for_path(from_path);
141         Glib::RefPtr<Gio::File> to_file = Gio::File::create_for_path(to_path);
142
143         try
144         {
145                 from_file->copy (to_file, Gio::FILE_COPY_OVERWRITE);
146         }
147         catch(const Glib::Exception& ex)
148         {
149                 error << string_compose (_("Unable to Copy file %1 to %2 (%3)"),
150                                 from_path, to_path, ex.what())
151                         << endmsg;
152                 return false;
153         }
154         return true;
155 }
156
157 static
158 bool accept_all_files (string const &, void *)
159 {
160         return true;
161 }
162
163 void
164 copy_files(const std::string & from_path, const std::string & to_dir)
165 {
166         PathScanner scanner;
167         vector<string*>* files = scanner (from_path, accept_all_files, 0, true, false);
168         for (vector<string*>::iterator i = files->begin(); i != files->end(); ++i) {
169                 std::string from = Glib::build_filename (from_path, **i);
170                 std::string to = Glib::build_filename (to_dir, **i);
171                 copy_file (from, to);
172         }
173 }
174
175 std::string
176 get_absolute_path (const std::string & p)
177 {
178         Glib::RefPtr<Gio::File> f = Gio::File::create_for_path (p);
179         return f->get_path ();
180 }
181
182 bool
183 equivalent_paths (const std::string& a, const std::string& b)
184 {
185         struct stat bA;
186         int const rA = g_stat (a.c_str(), &bA);
187         struct stat bB;
188         int const rB = g_stat (b.c_str(), &bB);
189
190         return (rA == 0 && rB == 0 && bA.st_dev == bB.st_dev && bA.st_ino == bB.st_ino);
191 }
192
193 bool
194 path_is_within (std::string const & haystack, std::string needle)
195 {
196         while (1) {
197                 if (equivalent_paths (haystack, needle)) {
198                         return true;
199                 }
200
201                 needle = Glib::path_get_dirname (needle);
202                 if (needle == "." || needle == "/") {
203                         break;
204                 }
205         }
206
207         return false;
208 }
209
210 bool
211 exists_and_writable (const std::string & p)
212 {
213         /* writable() really reflects the whole folder, but if for any
214            reason the session state file can't be written to, still
215            make us unwritable.
216         */
217
218         struct stat statbuf;
219
220         if (g_stat (p.c_str(), &statbuf) != 0) {
221                 /* doesn't exist - not writable */
222                 return false;
223         } else {
224                 if (!(statbuf.st_mode & S_IWUSR)) {
225                         /* exists and is not writable */
226                         return false;
227                 }
228                 /* filesystem may be mounted read-only, so even though file
229                  * permissions permit access, the mount status does not.
230                  * access(2) seems like the best test for this.
231                  */
232                 if (g_access (p.c_str(), W_OK) != 0) {
233                         return false;
234                 }
235         }
236
237         return true;
238 }
239
240 } // namespace PBD