-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
246 lines (216 loc) · 7.99 KB
/
Copy pathserver.cpp
File metadata and controls
246 lines (216 loc) · 7.99 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
#include <dirent.h>
#include <fcntl.h>
#include <grpcpp/ext/proto_server_reflection_plugin.h>
#include <grpcpp/grpcpp.h>
#include <sys/stat.h>
#include <utime.h>
#include <filesystem>
#include "includes/hello.grpc.pb.h"
using aafs::gRPCService;
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::ServerReader;
using grpc::ServerReaderWriter;
using grpc::ServerWriter;
using grpc::Status;
constexpr char kServerTempFolder[] = "./aafs_server_temp/";
constexpr char kServerTransferTemplate[] =
"./aafs_server_temp/aafs_serverXXXXXX";
class gRPCServiceImpl final : public gRPCService::Service {
public:
explicit gRPCServiceImpl(const std::string server_folder)
: kServerFolder(std::move(server_folder)) {}
private:
const std::string kServerFolder;
std::string to_server_path(const std::string &path) {
return kServerFolder + path;
}
static std::pair<int, std::string> get_tmp_file() {
char transfer_template[sizeof(kServerTransferTemplate)];
memcpy(transfer_template, kServerTransferTemplate,
sizeof(transfer_template));
int ret = mkstemp(transfer_template);
chmod(transfer_template, 0777);
return std::make_pair(ret, transfer_template);
}
Status s_getattr(ServerContext *context, const aafs::PathRequest *req,
aafs::GetAttrResponse *reply) override {
struct stat st {};
int ret = lstat(to_server_path(req->path()).c_str(), &st);
if (ret == -1) {
reply->set_ret(-errno);
return Status::OK;
}
reply->set_ret(ret);
auto stat = std::make_unique<aafs::Stat>();
stat->set_ino(st.st_ino);
stat->set_mode(st.st_mode);
stat->set_nlink(st.st_nlink);
stat->set_uid(st.st_uid);
stat->set_gid(st.st_gid);
stat->set_rdev(st.st_rdev);
stat->set_size(st.st_size);
stat->set_blocks(st.st_blocks);
stat->set_atime(st.st_atime);
stat->set_mtime(st.st_mtime);
reply->set_allocated_stat(stat.release());
return Status::OK;
}
Status s_readdir(ServerContext *context, const aafs::PathRequest *req,
aafs::ReadDirResponse *reply) override {
DIR *dp;
struct dirent *de;
dp = opendir(to_server_path(req->path()).c_str());
if (dp == nullptr) {
reply->set_ret(-errno);
return Status::OK;
}
while ((de = readdir(dp)) != nullptr) {
reply->add_entries(de->d_name);
}
closedir(dp);
return Status::OK;
}
Status s_download(ServerContext *context, const aafs::PathRequest *req,
ServerWriter<aafs::DownloadResponse> *writer) override {
int fd = open(to_server_path(req->path()).c_str(), O_RDONLY);
if (fd == -1) {
return Status::CANCELLED;
}
struct stat st {};
fstat(fd, &st);
aafs::DownloadResponse reply;
auto matime = std::make_unique<aafs::MATime>();
matime->set_atime(st.st_atime);
matime->set_mtime(st.st_mtime);
reply.set_allocated_time(matime.release());
writer->Write(reply);
constexpr int buf_size = 409600;
auto buf = std::make_unique<std::string>(buf_size, '\0');
ssize_t n;
while ((n = read(fd, buf->data(), buf_size)) > 0) {
buf->resize(n);
reply.set_allocated_data(buf.release());
if (!writer->Write(reply)) {
return Status::CANCELLED;
}
buf = std::make_unique<std::string>(buf_size, '\0');
}
fsync(fd);
close(fd);
return Status::OK;
}
Status s_upload(ServerContext *context,
ServerReader<aafs::UploadRequest> *reader,
aafs::StatusResponse *reply) override {
aafs::UploadRequest req;
if (!reader->Read(&req)) return Status::CANCELLED;
if (!req.has_meta()) return Status::CANCELLED;
std::string path = req.meta().path();
int atime = req.meta().atime();
int mtime = req.meta().mtime();
auto [tmp_fd, tmp_name] = get_tmp_file();
if (tmp_fd < 0) {
reply->set_ret(-errno);
return Status::OK;
}
while (reader->Read(&req)) {
if (!req.has_data()) {
close(tmp_fd);
return Status::CANCELLED;
}
int ret = write(tmp_fd, req.data().c_str(), req.data().size());
if (ret < 0) {
reply->set_ret(-errno);
close(tmp_fd);
return Status::OK;
}
}
fsync(tmp_fd);
close(tmp_fd);
struct utimbuf times {};
times.actime = atime;
times.modtime = mtime;
utime(tmp_name.c_str(), ×);
rename(tmp_name.c_str(), to_server_path(path).c_str());
reply->set_ret(0);
return Status::OK;
}
Status s_unlink(ServerContext *context, const aafs::PathRequest *req,
aafs::StatusResponse *reply) override {
int ret = unlink(to_server_path(req->path()).c_str());
reply->set_ret(ret);
return Status::OK;
}
Status s_creat(ServerContext *context, const aafs::PathRequest *req,
aafs::StatusResponse *reply) override {
int fd = creat(to_server_path(req->path()).c_str(), 0777);
if (fd == -1) {
reply->set_ret(-errno);
return Status::OK;
}
close(fd);
return Status::OK;
}
Status s_mkdir(ServerContext *context, const aafs::PathRequest *req,
aafs::StatusResponse *reply) override {
int ret = mkdir(to_server_path(req->path()).c_str(), 0777);
if (ret == -1) {
reply->set_ret(-errno);
return Status::OK;
}
reply->set_ret(ret);
return Status::OK;
}
Status s_rmdir(ServerContext *context, const aafs::PathRequest *req,
aafs::StatusResponse *reply) override {
int ret = rmdir(to_server_path(req->path()).c_str());
if (ret == -1) {
reply->set_ret(-errno);
return Status::OK;
}
reply->set_ret(ret);
return Status::OK;
}
Status s_rename(ServerContext *context, const aafs::RenameRequest *req,
aafs::StatusResponse *reply) override {
int ret = rename(to_server_path(req->oldpath()).c_str(),
to_server_path(req->newpath()).c_str());
reply->set_ret(ret);
return Status::OK;
}
};
int main(int argc, char *argv[]) {
if (argc < 3) {
printf("%s [listen address:port] [server folder]\n", argv[0]);
return 1;
}
int ret = mkdir(kServerTempFolder, 0755);
if (ret != 0) {
if (errno == EEXIST) { // delete all dirty files
for (const auto &entry :
std::filesystem::directory_iterator(kServerTempFolder))
std::filesystem::remove_all(entry.path());
} else {
assert_perror(errno);
}
}
std::string server_address(argv[1]);
gRPCServiceImpl service(argv[2]);
ServerBuilder builder;
// Listen on the given address without any authentication mechanism.
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
builder.SetMaxSendMessageSize(INT_MAX);
builder.SetMaxReceiveMessageSize(INT_MAX);
builder.SetMaxMessageSize(INT_MAX);
// Register "service" as the instance through which we'll communicate with
// clients. In this case it corresponds to an *synchronous* service.
builder.RegisterService(&service);
// Finally assemble the server.
std::unique_ptr<Server> server(builder.BuildAndStart());
std::cout << "Server listening on " << server_address << std::endl;
// Wait for the server to shutdown. Note that some other thread must be
// responsible for shutting down the server for this call to ever return.
server->Wait();
}