-
Notifications
You must be signed in to change notification settings - Fork 357
Expand file tree
/
Copy pathTreeEntry.cpp
More file actions
279 lines (248 loc) · 8.13 KB
/
TreeEntry.cpp
File metadata and controls
279 lines (248 loc) · 8.13 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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This software may be used and distributed according to the terms of the
* GNU General Public License version 2.
*/
#include "eden/fs/model/TreeEntry.h"
#include <sys/stat.h>
#include <cstdint>
#include <ostream>
#include <folly/Range.h>
#include <folly/logging/xlog.h>
#include "eden/common/utils/EnumValue.h"
#include "eden/common/utils/PathFuncs.h"
#include "eden/common/utils/TimeUtil.h"
namespace facebook::eden {
using namespace folly;
using namespace folly::io;
namespace {
// Platform-independent execute bit mask for tree entry type detection.
// On POSIX platforms, this matches S_IXUSR. On Windows, we use the same
// value to enable consistent executable file detection across platforms.
constexpr mode_t EXECUTE_BIT_MASK = 0000100;
} // namespace
template <typename T>
bool checkValueEqual(
const std::optional<folly::Try<T>>& lhs,
const std::optional<folly::Try<T>>& rhs) {
if (!lhs.has_value() || !rhs.has_value()) {
return lhs.has_value() == rhs.has_value();
}
if (lhs.value().hasException() || rhs.value().hasException()) {
return lhs.value().hasException() == rhs.value().hasException();
}
return lhs.value().value() == rhs.value().value();
}
bool operator==(const EntryAttributes& lhs, const EntryAttributes& rhs) {
return checkValueEqual(lhs.sha1, rhs.sha1) &&
checkValueEqual(lhs.size, rhs.size) &&
checkValueEqual(lhs.type, rhs.type) &&
checkValueEqual(lhs.objectId, rhs.objectId) &&
checkValueEqual(lhs.digestSize, rhs.digestSize) &&
checkValueEqual(lhs.digestHash, rhs.digestHash) &&
checkValueEqual(lhs.mtime, rhs.mtime) &&
checkValueEqual(lhs.mode, rhs.mode);
}
bool operator!=(const EntryAttributes& lhs, const EntryAttributes& rhs) {
return !(lhs == rhs);
}
bool operator==(
const folly::Try<EntryAttributes>& lhs,
const folly::Try<EntryAttributes>& rhs) {
if (lhs.hasException()) {
return rhs.hasException();
}
if (rhs.hasException()) {
return lhs.hasException();
}
return rhs.value() == lhs.value();
}
mode_t modeFromTreeEntryType(TreeEntryType ft) {
switch (ft) {
case TreeEntryType::TREE:
return S_IFDIR | 0755;
case TreeEntryType::REGULAR_FILE:
return S_IFREG | 0644;
case TreeEntryType::EXECUTABLE_FILE:
return S_IFREG | 0755;
case TreeEntryType::SYMLINK:
return S_IFLNK | 0755;
}
XLOGF(FATAL, "illegal file type {}", enumValue(ft));
}
bool compareTreeEntryType(
std::optional<TreeEntryType> lhs,
std::optional<TreeEntryType> rhs) {
auto ignoreWindowsExecutableTypeForComparison =
[](TreeEntryType ft) -> TreeEntryType {
if (folly::kIsWindows) {
return ft == TreeEntryType::EXECUTABLE_FILE ? TreeEntryType::REGULAR_FILE
: ft;
}
return ft;
};
if (!lhs.has_value() || !rhs.has_value()) {
return lhs.has_value() == rhs.has_value();
}
return ignoreWindowsExecutableTypeForComparison(lhs.value()) ==
ignoreWindowsExecutableTypeForComparison(rhs.value());
}
std::optional<TreeEntryType> treeEntryTypeFromMode(mode_t mode) {
if (S_ISREG(mode)) {
return mode & EXECUTE_BIT_MASK ? TreeEntryType::EXECUTABLE_FILE
: TreeEntryType::REGULAR_FILE;
} else if (S_ISLNK(mode)) {
return TreeEntryType::SYMLINK;
} else if (S_ISDIR(mode)) {
return TreeEntryType::TREE;
} else {
return std::nullopt;
}
}
std::string TreeEntry::toLogString(PathComponentPiece name) const {
char fileTypeChar = '?';
switch (type_) {
case TreeEntryType::TREE:
fileTypeChar = 'd';
break;
case TreeEntryType::REGULAR_FILE:
fileTypeChar = 'f';
break;
case TreeEntryType::EXECUTABLE_FILE:
fileTypeChar = 'x';
break;
case TreeEntryType::SYMLINK:
fileTypeChar = 'l';
break;
}
return fmt::format("({}, {}, {})", name, id_, fileTypeChar);
}
size_t TreeEntry::serializedSize(PathComponentPiece name) const {
return sizeof(uint8_t) + sizeof(uint16_t) + id_.size() + sizeof(uint16_t) +
name.view().size() + sizeof(uint64_t) + Hash20::RAW_SIZE +
sizeof(uint8_t) + Hash32::RAW_SIZE;
}
void TreeEntry::serialize(PathComponentPiece name, Appender& appender) const {
appender.write<uint8_t>(static_cast<uint8_t>(type_));
auto id = id_.getBytes();
XCHECK_LE(id.size(), std::numeric_limits<uint16_t>::max());
appender.write<uint16_t>(static_cast<uint16_t>(id.size()));
appender.push(id);
auto nameStringPiece = name.view();
XCHECK_LE(nameStringPiece.size(), std::numeric_limits<uint16_t>::max());
appender.write<uint16_t>(static_cast<uint16_t>(nameStringPiece.size()));
appender.push(folly::StringPiece{nameStringPiece});
if (size_) {
appender.write<uint64_t>(*size_);
} else {
appender.write<uint64_t>(NO_SIZE);
}
if (contentSha1_) {
appender.push(contentSha1_->getBytes());
} else {
appender.push(kZeroHash.getBytes());
}
// we need to be backward compatible with the old serialization format
// so adding a byte (with flipped bits) to distinguish between a possible
// blake3 hash and the next entry type because we have access to the entire
// serialized tree
appender.write<int>(0xff, sizeof(uint8_t));
appender.push(contentBlake3_.value_or(kZeroHash32).getBytes());
}
std::optional<std::pair<PathComponent, TreeEntry>> TreeEntry::deserialize(
folly::StringPiece& data) {
uint8_t type;
if (data.size() < sizeof(uint8_t)) {
XLOGF(ERR, "Can not read tree entry type, bytes remaining {}", data.size());
return std::nullopt;
}
memcpy(&type, data.data(), sizeof(uint8_t));
data.advance(sizeof(uint8_t));
uint16_t id_size;
if (data.size() < sizeof(uint16_t)) {
XLOGF(
ERR,
"Can not read tree entry id size, bytes remaining {}",
data.size());
return std::nullopt;
}
memcpy(&id_size, data.data(), sizeof(uint16_t));
data.advance(sizeof(uint16_t));
if (data.size() < id_size) {
XLOGF(
ERR,
"Can not read tree entry id, bytes remaining {} need {}",
data.size(),
id_size);
return std::nullopt;
}
auto id_bytes = ByteRange{StringPiece{data, 0, id_size}};
auto id = ObjectId{id_bytes};
data.advance(id_size);
uint16_t name_size;
if (data.size() < sizeof(uint16_t)) {
XLOGF(
ERR,
"Can not read tree entry name size, bytes remaining {}",
data.size());
return std::nullopt;
}
memcpy(&name_size, data.data(), sizeof(uint16_t));
data.advance(sizeof(uint16_t));
if (data.size() < name_size) {
XLOGF(
ERR,
"Can not read tree entry name, bytes remaining {} need {}",
data.size(),
name_size);
return std::nullopt;
}
auto name_bytes = StringPiece{data, 0, name_size};
auto name = PathComponent{name_bytes};
data.advance(name_size);
if (data.size() < sizeof(uint64_t)) {
XLOGF(ERR, "Can not read tree entry size, bytes remaining {}", data.size());
return std::nullopt;
}
uint64_t size_bytes;
memcpy(&size_bytes, data.data(), sizeof(uint64_t));
data.advance(sizeof(uint64_t));
std::optional<uint64_t> size;
if (size_bytes == NO_SIZE) {
size = std::nullopt;
} else {
size = size_bytes;
}
if (data.size() < Hash20::RAW_SIZE) {
XLOGF(ERR, "Can not read tree entry sha1, bytes remaining {}", data.size());
return std::nullopt;
}
Hash20::Storage sha1_bytes;
memcpy(&sha1_bytes, data.data(), Hash20::RAW_SIZE);
data.advance(Hash20::RAW_SIZE);
Hash20 sha1_raw = Hash20{sha1_bytes};
std::optional<Hash20> sha1;
if (sha1_raw == kZeroHash) {
sha1 = std::nullopt;
} else {
sha1 = sha1_raw;
}
std::optional<Hash32> blake3;
if (!data.empty() && static_cast<uint8_t>(data.data()[0]) == 0xff) {
data.advance(1);
if (data.size() >= Hash32::RAW_SIZE) {
blake3.emplace();
auto blake3Bytes = blake3->mutableBytes();
memcpy(blake3Bytes.data(), data.data(), Hash32::RAW_SIZE);
data.advance(Hash32::RAW_SIZE);
if (*blake3 == kZeroHash32) {
blake3.reset();
}
}
}
return std::pair{
std::move(name),
TreeEntry{std::move(id), (TreeEntryType)type, size, sha1, blake3}};
}
} // namespace facebook::eden