-
Notifications
You must be signed in to change notification settings - Fork 39
/
Filesystem.cc
356 lines (307 loc) · 9.36 KB
/
Filesystem.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* Copyright (C) 2016 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <array>
#include <algorithm>
#include <sstream>
#include <filesystem>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <regex>
#include <ignition/common/config.hh>
#include <ignition/common/SystemPaths.hh>
#include <ignition/common/Util.hh>
#include <ignition/common/Uuid.hh>
#include <ignition/common/Console.hh>
#include "ignition/common/Filesystem.hh"
namespace fs = std::filesystem;
/////////////////////////////////////////////////
// Return true if success, false if error
inline bool fsWarn(const std::string &_fcn,
const std::error_code &_ec,
const ignition::common::FilesystemWarningOp &_warningOp =
ignition::common::FSWO_LOG_WARNINGS)
{
if (_ec)
{
if (ignition::common::FSWO_LOG_WARNINGS == _warningOp)
{
ignwarn << "Failed ignition::common::" << _fcn
<< " (ec: " << _ec << " " << _ec.message() << ")\n";
}
return false;
}
return true;
}
/////////////////////////////////////////////////
bool ignition::common::exists(const std::string &_path)
{
return fs::exists(_path);
}
/////////////////////////////////////////////////
bool ignition::common::isDirectory(const std::string &_path)
{
return fs::is_directory(_path);
}
/////////////////////////////////////////////////
bool ignition::common::isFile(const std::string &_path)
{
return fs::is_regular_file(_path);
}
/////////////////////////////////////////////////
bool ignition::common::isRelativePath(const std::string &_path)
{
return fs::path(_path).is_relative();
}
/////////////////////////////////////////////////
bool ignition::common::createDirectory(const std::string &_path)
{
std::error_code ec;
fs::create_directory(_path, ec);
return fsWarn("createDirectory", ec);
}
/////////////////////////////////////////////////
bool ignition::common::createDirectories(const std::string &_path)
{
std::error_code ec;
// Disregard return of create_directories, because it may return false if the
// directory is not actually created (already exists)
bool created = fs::create_directories(_path, ec);
(void) created;
return fsWarn("createDirectories", ec);
}
/////////////////////////////////////////////////
std::string const ignition::common::separator(std::string const &_s)
{
fs::path path(_s);
return (_s / fs::path("")).string();
}
/////////////////////////////////////////////////
void ignition::common::changeFromUnixPath(std::string &_path) {
std::replace(_path.begin(), _path.end(), '/',
static_cast<char>(fs::path::preferred_separator));
}
/////////////////////////////////////////////////
std::string ignition::common::copyFromUnixPath(const std::string &_path)
{
std::string copy = _path;
changeFromUnixPath(copy);
return copy;
}
/////////////////////////////////////////////////
void ignition::common::changeToUnixPath(std::string &_path) {
std::replace(_path.begin(), _path.end(),
static_cast<char>(fs::path::preferred_separator), '/');
}
/////////////////////////////////////////////////
std::string ignition::common::copyToUnixPath(const std::string &_path)
{
std::string copy = _path;
changeToUnixPath(copy);
return copy;;
}
/////////////////////////////////////////////////
std::string ignition::common::absPath(const std::string &_path)
{
return fs::absolute(_path).string();
}
/////////////////////////////////////////////////
std::string ignition::common::joinPaths(
const std::string &_path1, const std::string &_path2)
{
fs::path p1{_path1};
fs::path p2{_path2};
bool is_url = false;
if (_path1.find("://") == std::string::npos)
p1 = p1.lexically_normal();
else
is_url = true;
// TODO(mjcarroll) Address the case that path2 is also a URI.
// It's likely not a valid scenario, but not currently covered by our test
// suite and doesn't return an error.
if (_path2.find("://") == std::string::npos)
p2 = p2.lexically_normal();
else
is_url = true;
if (p2.string()[0] == fs::path::preferred_separator)
{
p2 = fs::path{p2.string().substr(1)};
}
auto ret = (p1 / p2);
if (is_url)
{
return copyToUnixPath(ret.string());
}
else
{
return ret.lexically_normal().string();
}
}
/////////////////////////////////////////////////
std::string ignition::common::cwd()
{
std::error_code ec;
auto curdir = fs::current_path(ec);
if (!fsWarn("cwd", ec))
{
curdir = "";
}
return curdir.string();
}
/////////////////////////////////////////////////
bool ignition::common::chdir(const std::string &_dir)
{
std::error_code ec;
fs::current_path(_dir, ec);
return fsWarn("chdir", ec);
}
/////////////////////////////////////////////////
std::string ignition::common::basename(const std::string &_path)
{
fs::path p(_path);
// Maintain compatibility with ign-common
if (*_path.rbegin() == fs::path::preferred_separator)
p = fs::path(_path.substr(0, _path.size()-1));
return p.filename().string();
}
/////////////////////////////////////////////////
std::string ignition::common::parentPath(const std::string &_path)
{
fs::path p(_path);
// Maintain compatibility with ign-common
if (*_path.rbegin() == fs::path::preferred_separator)
p = fs::path(_path.substr(0, _path.size()-1));
if (!p.has_parent_path())
return _path;
return p.parent_path().string();
}
/////////////////////////////////////////////////
bool ignition::common::copyFile(
const std::string &_existingFilename,
const std::string &_newFilename,
const FilesystemWarningOp _warningOp)
{
const auto copyOptions = fs::copy_options::overwrite_existing;
std::error_code ec;
auto ret = fs::copy_file(_existingFilename, _newFilename, copyOptions, ec);
return ret && fsWarn("copyFile", ec, _warningOp);
}
/////////////////////////////////////////////////
bool ignition::common::copyDirectory(
const std::string &_existingDirname,
const std::string &_newDirname,
const FilesystemWarningOp _warningOp)
{
const auto copyOptions = fs::copy_options::recursive
| fs::copy_options::overwrite_existing;
// std::filesystem won't create intermediate directories
// before copying, this maintains compatibility with ignition behavior.
if (!ignition::common::createDirectories(_newDirname))
{
return false;
}
std::error_code ec;
fs::copy(_existingDirname, _newDirname, copyOptions, ec);
return fsWarn("copyDirectory", ec, _warningOp);
}
/////////////////////////////////////////////////
bool ignition::common::moveFile(
const std::string &_existingFilename,
const std::string &_newFilename,
const FilesystemWarningOp _warningOp)
{
std::error_code ec;
fs::rename(_existingFilename, _newFilename, ec);
return fsWarn("moveFile", ec, _warningOp);
}
/////////////////////////////////////////////////
bool ignition::common::removeDirectory(
const std::string &_path,
const FilesystemWarningOp _warningOp)
{
if (!isDirectory(_path))
{
if (FSWO_LOG_WARNINGS == _warningOp)
{
ignwarn << "Cannot remove, not a directory [" << _path << "]\n";
}
return false;
}
return removeDirectoryOrFile(_path, _warningOp);
}
/////////////////////////////////////////////////
bool ignition::common::removeFile(
const std::string &_existingFilename,
const FilesystemWarningOp _warningOp)
{
if (!isFile(_existingFilename))
{
if (FSWO_LOG_WARNINGS == _warningOp)
{
ignwarn << "Cannot remove, not a file [" << _existingFilename << "]\n";
}
return false;
}
return removeDirectoryOrFile(_existingFilename, _warningOp);
}
/////////////////////////////////////////////////
bool ignition::common::removeDirectoryOrFile(
const std::string &_path,
const FilesystemWarningOp _warningOp)
{
std::error_code ec;
auto ret = fs::remove(_path, ec);
fsWarn("removeDirectoryOrFile", ec, _warningOp);
return ret;
}
/////////////////////////////////////////////////
bool ignition::common::removeAll(
const std::string &_path,
const FilesystemWarningOp _warningOp)
{
std::error_code ec;
fs::remove_all(_path, ec);
return fsWarn("removeAll", ec, _warningOp);
}
/////////////////////////////////////////////////
std::string ignition::common::uniqueFilePath(
const std::string &_pathAndName,
const std::string &_extension)
{
std::string result = _pathAndName + "." + _extension;
int count = 1;
// Check if file exists and change name accordingly
while (exists(result.c_str()))
{
result = _pathAndName + "(" + std::to_string(count++) + ").";
result += _extension;
}
return result;
}
/////////////////////////////////////////////////
std::string ignition::common::uniqueDirectoryPath(const std::string &_dir)
{
std::string result = _dir;
int count = 1;
// Check if file exists and change name accordingly
while (exists(result.c_str()))
{
result = _dir + "(" + std::to_string(count++) + ")";
}
return result;
}