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