Overwrite target file in PBD::copy_file
[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 <glibmm/fileutils.h>
23 #include <glibmm/miscutils.h>
24 #include <glibmm/pattern.h>
25
26 #include <giomm/file.h>
27
28 #include "pbd/compose.h"
29 #include "pbd/file_utils.h"
30 #include "pbd/error.h"
31 #include "pbd/pathscanner.h"
32
33 #include "i18n.h"
34
35 using namespace std;
36
37 namespace PBD {
38
39 void
40 get_files_in_directory (const std::string& directory_path, vector<string>& result)
41 {
42         if (!Glib::file_test (directory_path, Glib::FILE_TEST_IS_DIR)) return;
43
44         try
45         {
46                 Glib::Dir dir(directory_path);
47                 std::copy(dir.begin(), dir.end(), std::back_inserter(result));
48         }
49         catch (Glib::FileError& err)
50         {
51                 warning << err.what() << endmsg;
52         }
53 }
54
55 void
56 find_matching_files_in_directory (const std::string& directory,
57                                   const Glib::PatternSpec& pattern,
58                                   vector<std::string>& result)
59 {
60         vector<string> tmp_files;
61
62         get_files_in_directory (directory, tmp_files);
63         result.reserve(tmp_files.size());
64
65         for (vector<string>::iterator file_iter = tmp_files.begin();
66                         file_iter != tmp_files.end();
67                         ++file_iter)
68         {
69                 if (!pattern.match(*file_iter)) continue;
70
71                 std::string full_path(directory);
72                 full_path = Glib::build_filename (full_path, *file_iter);
73
74                 result.push_back(full_path);
75         }
76 }
77
78 void
79 find_matching_files_in_directories (const vector<std::string>& paths,
80                                     const Glib::PatternSpec& pattern,
81                                     vector<std::string>& result)
82 {
83         for (vector<std::string>::const_iterator path_iter = paths.begin();
84                         path_iter != paths.end();
85                         ++path_iter)
86         {
87                 find_matching_files_in_directory (*path_iter, pattern, result);
88         }               
89 }
90
91 void
92 find_matching_files_in_search_path (const SearchPath& search_path,
93                                     const Glib::PatternSpec& pattern,
94                                     vector<std::string>& result)
95 {
96         find_matching_files_in_directories (search_path, pattern, result);    
97 }
98
99 bool
100 find_file_in_search_path(const SearchPath& search_path,
101                          const string& filename,
102                          std::string& result)
103 {
104         vector<std::string> tmp;
105         Glib::PatternSpec tmp_pattern(filename);
106
107         find_matching_files_in_search_path (search_path, tmp_pattern, tmp);
108
109         if (tmp.size() == 0)
110         {
111                 return false;
112         }
113
114 #if 0
115         if (tmp.size() != 1)
116         {
117                 info << string_compose
118                         (
119                          "Found more than one file matching %1 in search path %2",
120                          filename,
121                          search_path ()
122                         )
123                         << endmsg;
124         }
125 #endif
126
127         result = tmp.front();
128
129         return true;
130 }
131
132 bool
133 copy_file(const std::string & from_path, const std::string & to_path)
134 {
135         if (!Glib::file_test (from_path, Glib::FILE_TEST_EXISTS)) return false;
136
137         Glib::RefPtr<Gio::File> from_file = Gio::File::create_for_path(from_path);
138         Glib::RefPtr<Gio::File> to_file = Gio::File::create_for_path(to_path);
139
140         try
141         {
142                 from_file->copy (to_file, Gio::FILE_COPY_OVERWRITE);
143         }
144         catch(const Glib::Exception& ex)
145         {
146                 error << string_compose (_("Unable to Copy file %1 to %2 (%3)"),
147                                 from_path, to_path, ex.what())
148                         << endmsg;
149                 return false;
150         }
151         return true;
152 }
153
154 static
155 bool accept_all_files (string const &, void *)
156 {
157         return true;
158 }
159
160 void
161 copy_files(const std::string & from_path, const std::string & to_dir)
162 {
163         PathScanner scanner;
164         vector<string*>* files = scanner (from_path, accept_all_files, 0, true, false);
165         for (vector<string*>::iterator i = files->begin(); i != files->end(); ++i) {
166                 std::string from = Glib::build_filename (from_path, **i);
167                 std::string to = Glib::build_filename (to_dir, **i);
168                 copy_file (from, to);
169         }
170 }
171
172 } // namespace PBD