~ubuntu-branches/ubuntu/utopic/ardour3/utopic

« back to all changes in this revision

Viewing changes to libs/pbd/file_utils.cc

  • Committer: Package Import Robot
  • Author(s): Felipe Sateler
  • Date: 2013-09-21 19:05:02 UTC
  • Revision ID: package-import@ubuntu.com-20130921190502-8gsftrku6jnzhd7v
Tags: upstream-3.4~dfsg
ImportĀ upstreamĀ versionĀ 3.4~dfsg

Show diffs side-by-side

added added

removed removed

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