forked from facebook/watchman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalFileResult.cpp
141 lines (120 loc) · 3.72 KB
/
LocalFileResult.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
#include "LocalFileResult.h"
#include "ContentHash.h"
#include "watchman_error_category.h"
using folly::Optional;
namespace watchman {
LocalFileResult::LocalFileResult(
const std::shared_ptr<w_root_t>& root,
w_string fullPath,
w_clock_t clock)
: root_(root), fullPath_(fullPath), clock_(clock) {}
void LocalFileResult::getInfo() {
if (info_.has_value()) {
return;
}
try {
info_ = getFileInformation(fullPath_.c_str(), root_->case_sensitive);
exists_ = true;
} catch (const std::exception&) {
// Treat any error as effectively deleted
exists_ = false;
info_ = FileInformation::makeDeletedFileInformation();
}
}
Optional<FileInformation> LocalFileResult::stat() {
if (!info_.has_value()) {
accessorNeedsProperties(FileResult::Property::FullFileInformation);
return folly::none;
}
return info_;
}
Optional<size_t> LocalFileResult::size() {
if (!info_.has_value()) {
accessorNeedsProperties(FileResult::Property::Size);
return folly::none;
}
return info_->size;
}
Optional<struct timespec> LocalFileResult::accessedTime() {
if (!info_.has_value()) {
accessorNeedsProperties(FileResult::Property::StatTimeStamps);
return folly::none;
}
return info_->atime;
}
Optional<struct timespec> LocalFileResult::modifiedTime() {
if (!info_.has_value()) {
accessorNeedsProperties(FileResult::Property::StatTimeStamps);
return folly::none;
}
return info_->mtime;
}
Optional<struct timespec> LocalFileResult::changedTime() {
if (!info_.has_value()) {
accessorNeedsProperties(FileResult::Property::StatTimeStamps);
return folly::none;
}
return info_->ctime;
}
w_string_piece LocalFileResult::baseName() {
return w_string_piece(fullPath_).baseName();
}
w_string_piece LocalFileResult::dirName() {
return w_string_piece(fullPath_).dirName();
}
Optional<bool> LocalFileResult::exists() {
if (!info_.has_value()) {
accessorNeedsProperties(FileResult::Property::Exists);
return folly::none;
}
return exists_;
}
Optional<w_string> LocalFileResult::readLink() {
if (symlinkTarget_.has_value()) {
return symlinkTarget_;
}
accessorNeedsProperties(FileResult::Property::SymlinkTarget);
return folly::none;
}
Optional<w_clock_t> LocalFileResult::ctime() {
return clock_;
}
Optional<w_clock_t> LocalFileResult::otime() {
return clock_;
}
Optional<FileResult::ContentHash> LocalFileResult::getContentSha1() {
if (contentSha1_.empty()) {
accessorNeedsProperties(FileResult::Property::ContentSha1);
return folly::none;
}
return contentSha1_.value();
}
void LocalFileResult::batchFetchProperties(
const std::vector<std::unique_ptr<FileResult>>& files) {
for (auto& f : files) {
auto localFile = dynamic_cast<LocalFileResult*>(f.get());
localFile->getInfo();
if (localFile->neededProperties() & FileResult::Property::SymlinkTarget) {
if (!localFile->info_->isSymlink()) {
// If this file is not a symlink then we immediately yield
// a nullptr w_string instance rather than propagating an error.
// This behavior is relied upon by the field rendering code and
// checked in test_symlink.py.
localFile->symlinkTarget_ = w_string();
} else {
localFile->symlinkTarget_ =
readSymbolicLink(localFile->fullPath_.c_str());
}
}
if (localFile->neededProperties() & FileResult::Property::ContentSha1) {
// TODO: find a way to reference a ContentHashCache instance
// that will work with !InMemoryView based views.
localFile->contentSha1_ = makeResultWith([&] {
return ContentHashCache::computeHashImmediate(
localFile->fullPath_.c_str());
});
}
localFile->clearNeededProperties();
}
}
} // namespace watchman