backport 1d85ab27a7e and ba128eea from cairocanvas branch to remove GIO (possible...
[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 #include <glibmm/fileutils.h>
27 #include <glibmm/miscutils.h>
28 #include <glibmm/pattern.h>
29
30 #include <errno.h>
31 #include <string.h> /* strerror */
32
33 /* open() */
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37
38 /* close(), read(), write() */
39 #ifdef COMPILER_MSVC
40 #include <io.h> // Microsoft's nearest equivalent to <unistd.h>
41 #else
42 #include <unistd.h>
43 #endif
44
45 #include "pbd/compose.h"
46 #include "pbd/file_utils.h"
47 #include "pbd/error.h"
48 #include "pbd/pathscanner.h"
49 #include "pbd/stl_delete.h"
50
51 #include "i18n.h"
52
53 using namespace std;
54
55 namespace PBD {
56
57 void
58 get_files_in_directory (const std::string& directory_path, vector<string>& result)
59 {
60         if (!Glib::file_test (directory_path, Glib::FILE_TEST_IS_DIR)) return;
61
62         try
63         {
64                 Glib::Dir dir(directory_path);
65                 std::copy(dir.begin(), dir.end(), std::back_inserter(result));
66         }
67         catch (Glib::FileError& err)
68         {
69                 warning << err.what() << endmsg;
70         }
71 }
72
73 void
74 find_matching_files_in_directory (const std::string& directory,
75                                   const Glib::PatternSpec& pattern,
76                                   vector<std::string>& result)
77 {
78         vector<string> tmp_files;
79
80         get_files_in_directory (directory, tmp_files);
81         result.reserve(tmp_files.size());
82
83         for (vector<string>::iterator file_iter = tmp_files.begin();
84                         file_iter != tmp_files.end();
85                         ++file_iter)
86         {
87                 if (!pattern.match(*file_iter)) continue;
88
89                 std::string full_path(directory);
90                 full_path = Glib::build_filename (full_path, *file_iter);
91
92                 result.push_back(full_path);
93         }
94 }
95
96 void
97 find_matching_files_in_directories (const vector<std::string>& paths,
98                                     const Glib::PatternSpec& pattern,
99                                     vector<std::string>& result)
100 {
101         for (vector<std::string>::const_iterator path_iter = paths.begin();
102                         path_iter != paths.end();
103                         ++path_iter)
104         {
105                 find_matching_files_in_directory (*path_iter, pattern, result);
106         }               
107 }
108
109 void
110 find_matching_files_in_search_path (const SearchPath& search_path,
111                                     const Glib::PatternSpec& pattern,
112                                     vector<std::string>& result)
113 {
114         find_matching_files_in_directories (search_path, pattern, result);    
115 }
116
117 bool
118 find_file_in_search_path(const SearchPath& search_path,
119                          const string& filename,
120                          std::string& result)
121 {
122         vector<std::string> tmp;
123         Glib::PatternSpec tmp_pattern(filename);
124
125         find_matching_files_in_search_path (search_path, tmp_pattern, tmp);
126
127         if (tmp.size() == 0)
128         {
129                 return false;
130         }
131
132 #if 0
133         if (tmp.size() != 1)
134         {
135                 info << string_compose
136                         (
137                          "Found more than one file matching %1 in search path %2",
138                          filename,
139                          search_path ()
140                         )
141                         << endmsg;
142         }
143 #endif
144
145         result = tmp.front();
146
147         return true;
148 }
149
150 bool
151 copy_file(const std::string & from_path, const std::string & to_path)
152 {
153         if (!Glib::file_test (from_path, Glib::FILE_TEST_EXISTS)) return false;
154
155         int fd_from = -1;
156         int fd_to = -1;
157         char buf[4096]; // BUFSIZ  ??
158         ssize_t nread;
159
160         fd_from = ::open(from_path.c_str(), O_RDONLY);
161         if (fd_from < 0) {
162                 goto copy_error;
163         }
164
165         fd_to = ::open(to_path.c_str(), O_WRONLY | O_CREAT, 0666);
166         if (fd_to < 0) {
167                 goto copy_error;
168         }
169
170         while (nread = ::read(fd_from, buf, sizeof(buf)), nread > 0) {
171                 char *out_ptr = buf;
172                 do {
173                         ssize_t nwritten = ::write(fd_to, out_ptr, nread);
174                         if (nwritten >= 0) {
175                                 nread -= nwritten;
176                                 out_ptr += nwritten;
177                         } else if (errno != EINTR) {
178                                 goto copy_error;
179                         }
180                 } while (nread > 0);
181         }
182
183         if (nread == 0) {
184                 if (::close(fd_to)) {
185                         fd_to = -1;
186                         goto copy_error;
187                 }
188                 ::close(fd_from);
189                 return true;
190         }
191
192 copy_error:
193         int saved_errno = errno;
194
195         if (fd_from >= 0) {
196                 ::close(fd_from);
197         }
198         if (fd_to >= 0) {
199                 ::close(fd_to);
200         }
201
202         error << string_compose (_("Unable to Copy file %1 to %2 (%3)"),
203                         from_path, to_path, strerror(saved_errno))
204                 << endmsg;
205         return false;
206 }
207
208 static
209 bool accept_all_files (string const &, void *)
210 {
211         return true;
212 }
213
214 void
215 copy_files(const std::string & from_path, const std::string & to_dir)
216 {
217         PathScanner scanner;
218         vector<string*>* files = scanner (from_path, accept_all_files, 0, true, false);
219
220         if (files) {
221                 for (vector<string*>::iterator i = files->begin(); i != files->end(); ++i) {
222                         std::string from = Glib::build_filename (from_path, **i);
223                         std::string to = Glib::build_filename (to_dir, **i);
224                         copy_file (from, to);
225                 }
226                 vector_delete (files);
227         }
228 }
229
230 std::string
231 get_absolute_path (const std::string & p)
232 {
233         if (Glib::path_is_absolute(p)) return p;
234         return Glib::build_filename (Glib::get_current_dir(), p);
235 }
236
237 bool
238 equivalent_paths (const std::string& a, const std::string& b)
239 {
240         struct stat bA;
241         int const rA = g_stat (a.c_str(), &bA);
242         struct stat bB;
243         int const rB = g_stat (b.c_str(), &bB);
244
245         return (rA == 0 && rB == 0 && bA.st_dev == bB.st_dev && bA.st_ino == bB.st_ino);
246 }
247
248 bool
249 path_is_within (std::string const & haystack, std::string needle)
250 {
251         while (1) {
252                 if (equivalent_paths (haystack, needle)) {
253                         return true;
254                 }
255
256                 needle = Glib::path_get_dirname (needle);
257                 if (needle == "." || needle == "/") {
258                         break;
259                 }
260         }
261
262         return false;
263 }
264
265 bool
266 exists_and_writable (const std::string & p)
267 {
268         /* writable() really reflects the whole folder, but if for any
269            reason the session state file can't be written to, still
270            make us unwritable.
271         */
272
273         struct stat statbuf;
274
275         if (g_stat (p.c_str(), &statbuf) != 0) {
276                 /* doesn't exist - not writable */
277                 return false;
278         } else {
279                 if (!(statbuf.st_mode & S_IWUSR)) {
280                         /* exists and is not writable */
281                         return false;
282                 }
283                 /* filesystem may be mounted read-only, so even though file
284                  * permissions permit access, the mount status does not.
285                  * access(2) seems like the best test for this.
286                  */
287                 if (g_access (p.c_str(), W_OK) != 0) {
288                         return false;
289                 }
290         }
291
292         return true;
293 }
294
295 } // namespace PBD