forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.cc
61 lines (46 loc) · 1.64 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
// 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"
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() {
if (path_ != "") {
if (!UnlinkDirectory(path_.c_str())) {
FML_LOG(ERROR) << "Could not remove directory: " << path_;
}
}
}
} // namespace fml