-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilesystem_tools.cpp
More file actions
410 lines (365 loc) · 9.46 KB
/
Copy pathfilesystem_tools.cpp
File metadata and controls
410 lines (365 loc) · 9.46 KB
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include "filesystem_tools.hpp"
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <sstream>
#include <vector>
namespace droidcli::cli {
namespace {
namespace fs = std::filesystem;
#ifdef _WIN32
constexpr char kPathListSeparator = ';';
#else
constexpr char kPathListSeparator = ':';
#endif
std::vector<core::String> split_path_env(const core::String& value)
{
std::vector<core::String> parts;
size_t start = 0;
for (size_t index = 0; index <= value.size(); ++index)
{
if (index == value.size() || value[index] == kPathListSeparator)
{
if (index > start)
{
parts.push_back(value.substr(start, index - start));
}
start = index + 1;
}
}
return parts;
}
} // namespace
FileReadResult read_file(const core::String& path, const int64_t max_bytes)
{
FileReadResult result;
if (path.empty())
{
result.error_message = "path is empty";
return result;
}
std::error_code error;
const fs::path target(path);
if (!fs::exists(target, error) || error)
{
result.error_message = "path does not exist: " + path;
return result;
}
if (fs::is_directory(target, error) || error)
{
result.error_message = "path is a directory, not a file: " + path;
return result;
}
std::ifstream file(target, std::ios::binary);
if (!file)
{
result.error_message = "could not open file: " + path;
return result;
}
const int64_t file_size = static_cast<int64_t>(fs::file_size(target, error));
if (error)
{
result.error_message = "could not determine file size: " + path;
return result;
}
result.size_bytes = file_size;
const int64_t read_limit = max_bytes > 0 ? max_bytes : file_size;
const size_t to_read = static_cast<size_t>(file_size > read_limit ? read_limit : file_size);
result.content.resize(to_read);
if (to_read > 0)
{
file.read(&result.content[0], static_cast<std::streamsize>(to_read));
}
result.truncated = file_size > read_limit;
result.ok = true;
return result;
}
FileWriteResult write_file(const core::String& path, const core::String& content, const bool append_mode)
{
FileWriteResult result;
if (path.empty())
{
result.error_message = "path is empty";
return result;
}
std::error_code error;
const fs::path target(path);
const fs::path parent = target.parent_path();
if (!parent.empty() && !fs::exists(parent, error))
{
fs::create_directories(parent, error);
if (error)
{
result.error_message = "could not create parent directories: " + error.message();
return result;
}
}
const std::ios::openmode mode = std::ios::binary | (append_mode ? std::ios::app : std::ios::trunc);
std::ofstream file(target, mode);
if (!file)
{
result.error_message = "could not open file for writing: " + path;
return result;
}
file.write(content.data(), static_cast<std::streamsize>(content.size()));
if (!file)
{
result.error_message = "write failed: " + path;
return result;
}
result.ok = true;
result.bytes_written = static_cast<int64_t>(content.size());
return result;
}
ListDirResult list_dir(const core::String& path)
{
ListDirResult result;
const fs::path target = path.empty() ? fs::current_path() : fs::path(path);
std::error_code error;
if (!fs::exists(target, error) || error)
{
result.error_message = "path does not exist: " + path;
return result;
}
if (!fs::is_directory(target, error) || error)
{
result.error_message = "path is not a directory: " + path;
return result;
}
for (const fs::directory_entry& entry : fs::directory_iterator(target, error))
{
if (error)
{
break;
}
DirEntryInfo info;
info.name = entry.path().filename().string();
std::error_code entry_error;
info.is_dir = entry.is_directory(entry_error);
if (!info.is_dir)
{
info.size_bytes = static_cast<int64_t>(entry.file_size(entry_error));
}
result.entries.push_back(info);
}
if (error)
{
result.error_message = "error while listing directory: " + error.message();
return result;
}
result.ok = true;
return result;
}
StatResult stat_path(const core::String& path)
{
StatResult result;
if (path.empty())
{
result.error_message = "path is empty";
return result;
}
std::error_code error;
const fs::path target(path);
result.exists = fs::exists(target, error);
if (!result.exists)
{
result.ok = true;
return result;
}
result.is_dir = fs::is_directory(target, error);
result.is_file = fs::is_regular_file(target, error);
if (result.is_file)
{
result.size_bytes = static_cast<int64_t>(fs::file_size(target, error));
}
result.ok = true;
return result;
}
core::String get_current_working_directory()
{
std::error_code error;
const fs::path current = fs::current_path(error);
if (error)
{
return {};
}
return current.string();
}
WhichResult which_executable(const core::String& name)
{
WhichResult result;
if (name.empty())
{
result.error_message = "name is empty";
return result;
}
// A path separator in the name means the caller already gave a path -
// just check it exists rather than searching PATH.
const fs::path given(name);
if (given.has_parent_path())
{
std::error_code error;
if (fs::exists(given, error) && fs::is_regular_file(given, error))
{
result.ok = true;
result.resolved_path = fs::absolute(given, error).string();
return result;
}
result.error_message = "not found: " + name;
return result;
}
#ifdef _WIN32
const std::vector<core::String> extensions = {"", ".exe", ".bat", ".cmd"};
#else
const std::vector<core::String> extensions = {""};
#endif
const char* path_env = std::getenv("PATH");
const std::vector<core::String> directories = split_path_env(path_env != nullptr ? path_env : "");
for (const core::String& directory : directories)
{
for (const core::String& extension : extensions)
{
const fs::path candidate = fs::path(directory) / (name + extension);
std::error_code error;
if (fs::exists(candidate, error) && fs::is_regular_file(candidate, error))
{
result.ok = true;
result.resolved_path = candidate.string();
return result;
}
}
}
result.error_message = "not found on PATH: " + name;
return result;
}
FileOpResult copy_file(const core::String& source_path, const core::String& destination_path)
{
FileOpResult result;
if (source_path.empty() || destination_path.empty())
{
result.error_message = "source_path and destination_path are both required";
return result;
}
std::error_code error;
const fs::path source(source_path);
if (fs::is_directory(source, error))
{
result.error_message = "source is a directory - copy_file only copies a single file";
return result;
}
const fs::path destination(destination_path);
const fs::path parent = destination.parent_path();
if (!parent.empty() && !fs::exists(parent, error))
{
fs::create_directories(parent, error);
if (error)
{
result.error_message = "could not create destination parent directories: " + error.message();
return result;
}
}
fs::copy_file(source, destination, fs::copy_options::overwrite_existing, error);
if (error)
{
result.error_message = "copy failed: " + error.message();
return result;
}
result.ok = true;
return result;
}
FileOpResult move_path(const core::String& source_path, const core::String& destination_path)
{
FileOpResult result;
if (source_path.empty() || destination_path.empty())
{
result.error_message = "source_path and destination_path are both required";
return result;
}
std::error_code error;
const fs::path source(source_path);
if (!fs::exists(source, error))
{
result.error_message = "source does not exist: " + source_path;
return result;
}
const fs::path destination(destination_path);
const fs::path parent = destination.parent_path();
if (!parent.empty() && !fs::exists(parent, error))
{
fs::create_directories(parent, error);
if (error)
{
result.error_message = "could not create destination parent directories: " + error.message();
return result;
}
}
fs::rename(source, destination, error);
if (error)
{
result.error_message = "move failed: " + error.message();
return result;
}
result.ok = true;
return result;
}
FileOpResult delete_file(const core::String& path)
{
FileOpResult result;
if (path.empty())
{
result.error_message = "path is empty";
return result;
}
std::error_code error;
const fs::path target(path);
if (!fs::exists(target, error))
{
result.error_message = "path does not exist: " + path;
return result;
}
if (fs::is_directory(target, error))
{
result.error_message = "path is a directory - delete_file only deletes a single file, not a directory tree";
return result;
}
const bool removed = fs::remove(target, error);
if (error || !removed)
{
result.error_message = "delete failed: " + (error ? error.message() : core::String("unknown error"));
return result;
}
result.ok = true;
return result;
}
FileOpResult create_directory(const core::String& path)
{
FileOpResult result;
if (path.empty())
{
result.error_message = "path is empty";
return result;
}
std::error_code error;
const fs::path target(path);
if (fs::exists(target, error))
{
if (fs::is_directory(target, error))
{
// Idempotent - "create a folder" that's already there isn't a
// failure, same as `mkdir -p`.
result.ok = true;
return result;
}
result.error_message = "path already exists as a file, not a directory: " + path;
return result;
}
const bool created = fs::create_directories(target, error);
if (error || !created)
{
result.error_message = "create_directory failed: " + (error ? error.message() : core::String("unknown error"));
return result;
}
result.ok = true;
return result;
}
} // namespace droidcli::cli