-
Notifications
You must be signed in to change notification settings - Fork 300
/
Copy pathTreeEntry.cpp
284 lines (252 loc) · 8.17 KB
/
TreeEntry.cpp
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
/*
* 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"
namespace facebook::eden {
using namespace folly;
using namespace folly::io;
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);
}
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;
}
XLOG(FATAL) << "illegal file type " << enumValue(ft);
}
TreeEntryType filteredEntryType(TreeEntryType ft, bool windowsSymlinksEnabled) {
if (folly::kIsWindows) {
if (ft != TreeEntryType::SYMLINK) {
return ft;
}
return windowsSymlinksEnabled ? ft : TreeEntryType::REGULAR_FILE;
}
return ft;
}
dtype_t filteredEntryDtype(dtype_t mode, bool windowsSymlinksEnabled) {
if (folly::kIsWindows) {
if (mode != dtype_t::Symlink) {
return mode;
}
return windowsSymlinksEnabled ? mode : dtype_t::Regular;
}
return mode;
}
std::optional<TreeEntryType> treeEntryTypeFromMode(mode_t mode) {
if (S_ISREG(mode)) {
#ifdef _WIN32
// On Windows, S_ISREG only means regular file and doesn't support
// TreeEntryType::EXECUTABLE_FILE
return TreeEntryType::REGULAR_FILE;
#else
return mode & S_IXUSR ? TreeEntryType::EXECUTABLE_FILE
: TreeEntryType::REGULAR_FILE;
#endif
} 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, hash_, fileTypeChar);
}
std::ostream& operator<<(std::ostream& os, TreeEntryType type) {
switch (type) {
case TreeEntryType::TREE:
return os << "TREE";
case TreeEntryType::REGULAR_FILE:
return os << "REGULAR_FILE";
case TreeEntryType::EXECUTABLE_FILE:
return os << "EXECUTABLE_FILE";
case TreeEntryType::SYMLINK:
return os << "SYMLINK";
}
return os << "TreeEntryType::" << int(type);
}
size_t TreeEntry::serializedSize(PathComponentPiece name) const {
return sizeof(uint8_t) + sizeof(uint16_t) + hash_.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 hash = hash_.getBytes();
XCHECK_LE(hash.size(), std::numeric_limits<uint16_t>::max());
appender.write<uint16_t>(folly::to_narrow(hash.size()));
appender.push(hash);
auto nameStringPiece = name.view();
XCHECK_LE(nameStringPiece.size(), std::numeric_limits<uint16_t>::max());
appender.write<uint16_t>(folly::to_narrow(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(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)) {
XLOG(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 hash_size;
if (data.size() < sizeof(uint16_t)) {
XLOG(ERR) << "Can not read tree entry hash size, bytes remaining "
<< data.size();
return std::nullopt;
}
memcpy(&hash_size, data.data(), sizeof(uint16_t));
data.advance(sizeof(uint16_t));
if (data.size() < hash_size) {
XLOG(ERR) << "Can not read tree entry hash, bytes remaining " << data.size()
<< " need " << hash_size;
return std::nullopt;
}
auto hash_bytes = ByteRange{StringPiece{data, 0, hash_size}};
auto hash = ObjectId{hash_bytes};
data.advance(hash_size);
uint16_t name_size;
if (data.size() < sizeof(uint16_t)) {
XLOG(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) {
XLOG(ERR) << "Can not read tree entry name, bytes remaining " << data.size()
<< " need " << 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)) {
XLOG(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) {
XLOG(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{hash, (TreeEntryType)type, size, sha1, blake3}};
}
} // namespace facebook::eden