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
|
#include "repo.h"
#include "blob.h"
#include "dir.h"
#include <Ice/Current.h>
#include <Ice/ObjectAdapter.h>
#include <Ice/Proxy.h>
#include <cerrno>
#include <directory.h>
#include <exceptions.h>
#include <file.h>
#include <git2.h>
#include <memory>
#include <sys/stat.h>
#include <types.h>
#include <unistd.h>
#include <utility>
namespace {
std::string
operator/(const std::string & value, const std::string & fallback)
{
return value.empty() ? fallback : value;
}
}
GitFS::Repo::Repo(const PropertyReader & properties) :
repo(Git::repositoryOpenBare(properties("gitdir"))), commitish(properties("commitish") / "main"), isBranch(false),
resolvedAt(0), gid(properties("gid") / "root"), uid(properties("uid") / "root")
{
if (commitish.length() == GIT_OID_HEXSZ) {
commit = Git::commitLookup(repo, Git::oidParse(commitish));
}
else {
ref = Git::commitish(repo, commitish);
isBranch = git_reference_is_branch(ref.get());
}
update();
}
void
GitFS::Repo::update()
{
constexpr time_t MIN_CHECK_TIME = 30;
if (!commit || (isBranch && ref && resolvedAt < std::time(nullptr) - MIN_CHECK_TIME)) {
commit = Git::commitLookup(repo, *git_reference_target(Git::resolve(ref).get()));
resolvedAt = std::time(nullptr);
}
tree = Git::treeLookup(repo, *git_commit_tree_id(commit.get()));
}
void
GitFS::Repo::disconnect(const ::Ice::Current & ice)
{
ice.adapter->remove(ice.id);
}
NetFS::DirectoryPrxPtr
GitFS::Repo::opendir(ReqEnv, ::std::string path, const ::Ice::Current & ice)
{
if (path.empty()) {
throw NetFS::SystemError(EINVAL);
}
return Ice::uncheckedCast<NetFS::DirectoryPrx>(
ice.adapter->addWithUUID(std::make_shared<Directory>(this, std::move(path))));
}
NetFS::VFS
GitFS::Repo::statfs(ReqEnv, ::std::string path, const ::Ice::Current &)
{
if (path.empty()) {
throw NetFS::SystemError(EINVAL);
}
return {};
}
int
GitFS::Repo::access(ReqEnv, const ::std::string path, const int mode, const ::Ice::Current &)
{
if (mode & W_OK) {
return EACCES;
}
if (path.empty()) {
return EINVAL;
}
if (path == "/") {
return 0;
}
try {
update();
auto entry = Git::treeEntryByPath(tree, path);
const auto emode = git_tree_entry_filemode(entry.get());
if (S_ISDIR(emode)) {
return 0;
}
if (S_ISLNK(emode)) {
return 0;
}
if (mode & R_OK && !(S_IRUSR & emode)) {
return EACCES;
}
if (mode & X_OK && !(S_IXUSR & emode)) {
return EACCES;
}
return 0;
}
catch (const NetFS::SystemError & e) {
return e.syserrno;
}
}
NetFS::Attr
GitFS::Repo::getattr(ReqEnv, const ::std::string path, const ::Ice::Current &)
{
if (path.empty()) {
throw NetFS::SystemError(EINVAL);
}
NetFS::Attr attr {};
if (path == "/") {
attr.mode = S_IFDIR | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
}
else {
update();
auto entry = Git::treeEntryByPath(tree, path);
attr << *entry;
if (S_ISREG(git_tree_entry_filemode(entry.get()))) {
auto blob = Git::blobLookup(repo, *git_tree_entry_id(entry.get()));
attr << *blob;
}
}
attr << *commit;
attr.gid = gid;
attr.uid = uid;
return attr;
}
::std::string
GitFS::Repo::readlink(ReqEnv, const ::std::string path, const ::Ice::Current &)
{
if (path.empty() || path == "/") {
throw NetFS::SystemError(EINVAL);
}
update();
auto entry = Git::treeEntryByPath(tree, path);
if (!S_ISLNK(git_tree_entry_filemode(entry.get()))) {
throw NetFS::SystemError(EINVAL);
}
auto blob = Git::blobLookup(repo, *git_tree_entry_id(entry.get()));
auto content = static_cast<const ::std::string::value_type *>(git_blob_rawcontent(blob.get()));
return {content, git_blob_rawsize(blob.get())};
}
NetFS::FilePrxPtr
GitFS::Repo::open(ReqEnv, ::std::string path, int, const ::Ice::Current & ice)
{
if (path.empty()) {
throw NetFS::SystemError(EINVAL);
}
if (path == "/") {
throw NetFS::SystemError(EISDIR);
}
update();
return Ice::uncheckedCast<NetFS::FilePrx>(ice.adapter->addWithUUID(std::make_shared<Blob>(this, std::move(path))));
}
NetFS::FilePrxPtr
GitFS::Repo::create(ReqEnv, ::std::string, int, int, const ::Ice::Current &)
{
throw NetFS::SystemError(EROFS);
}
void
GitFS::Repo::truncate(ReqEnv, ::std::string, long long int, const ::Ice::Current &)
{
throw NetFS::SystemError(EROFS);
}
void
GitFS::Repo::unlink(ReqEnv, ::std::string, const ::Ice::Current &)
{
throw NetFS::SystemError(EROFS);
}
void
GitFS::Repo::mkdir(ReqEnv, ::std::string, int, const ::Ice::Current &)
{
throw NetFS::SystemError(EROFS);
}
void
GitFS::Repo::rmdir(ReqEnv, ::std::string, const ::Ice::Current &)
{
throw NetFS::SystemError(EROFS);
}
void
GitFS::Repo::mknod(ReqEnv, ::std::string, int, int, const ::Ice::Current &)
{
throw NetFS::SystemError(EROFS);
}
void
GitFS::Repo::symlink(ReqEnv, ::std::string, ::std::string, const ::Ice::Current &)
{
throw NetFS::SystemError(EROFS);
}
void
GitFS::Repo::link(ReqEnv, ::std::string, ::std::string, const ::Ice::Current &)
{
throw NetFS::SystemError(EROFS);
}
void
GitFS::Repo::rename(ReqEnv, ::std::string, ::std::string, const Ice::optional<Ice::Int>, const ::Ice::Current &)
{
throw NetFS::SystemError(EROFS);
}
void
GitFS::Repo::chmod(ReqEnv, ::std::string, int, const ::Ice::Current &)
{
throw NetFS::SystemError(EROFS);
}
void
GitFS::Repo::chown(ReqEnv, ::std::string, int, int, const ::Ice::Current &)
{
throw NetFS::SystemError(EROFS);
}
void
GitFS::Repo::utimens(
ReqEnv, ::std::string, long long int, long long int, long long int, long long int, const ::Ice::Current &)
{
throw NetFS::SystemError(EROFS);
}
|