forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.cc
95 lines (77 loc) · 2.91 KB
/
file.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
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/fml/file.h"
#include "flutter/fml/logging.h"
#include "flutter/fml/unique_fd.h"
namespace fml {
static fml::UniqueFD CreateDirectory(const fml::UniqueFD& base_directory,
const std::vector<std::string>& components,
FilePermission permission,
size_t index) {
FML_DCHECK(index <= components.size());
const char* file_path = components[index].c_str();
auto directory = OpenDirectory(base_directory, file_path, true, permission);
if (!directory.is_valid()) {
return {};
}
if (index == components.size() - 1) {
return directory;
}
return CreateDirectory(directory, components, permission, index + 1);
}
fml::UniqueFD CreateDirectory(const fml::UniqueFD& base_directory,
const std::vector<std::string>& components,
FilePermission permission) {
if (!IsDirectory(base_directory)) {
return {};
}
if (components.size() == 0) {
return {};
}
return CreateDirectory(base_directory, components, permission, 0);
}
ScopedTemporaryDirectory::ScopedTemporaryDirectory() {
path_ = CreateTemporaryDirectory();
if (path_ != "") {
dir_fd_ = OpenDirectory(path_.c_str(), false, FilePermission::kRead);
}
}
ScopedTemporaryDirectory::~ScopedTemporaryDirectory() {
// Windows has to close UniqueFD first before UnlinkDirectory
dir_fd_.reset();
if (path_ != "") {
if (!UnlinkDirectory(path_.c_str())) {
FML_LOG(ERROR) << "Could not remove directory: " << path_;
}
}
}
bool VisitFilesRecursively(const fml::UniqueFD& directory,
const FileVisitor& visitor) {
FileVisitor recursive_visitor = [&recursive_visitor, &visitor](
const UniqueFD& directory,
const std::string& filename) {
if (!visitor(directory, filename)) {
return false;
}
if (IsDirectory(directory, filename.c_str())) {
UniqueFD sub_dir = OpenDirectoryReadOnly(directory, filename.c_str());
if (!sub_dir.is_valid()) {
FML_LOG(ERROR) << "Can't open sub-directory: " << filename;
return true;
}
return VisitFiles(sub_dir, recursive_visitor);
}
return true;
};
return VisitFiles(directory, recursive_visitor);
}
fml::UniqueFD OpenFileReadOnly(const fml::UniqueFD& base_directory,
const char* path) {
return OpenFile(base_directory, path, false, FilePermission::kRead);
}
fml::UniqueFD OpenDirectoryReadOnly(const fml::UniqueFD& base_directory,
const char* path) {
return OpenDirectory(base_directory, path, false, FilePermission::kRead);
}
} // namespace fml