-
Notifications
You must be signed in to change notification settings - Fork 6
/
extent_server.cc
72 lines (64 loc) · 1.8 KB
/
extent_server.cc
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
// the extent server implementation
#include "extent_server.h"
#include <sstream>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
extent_server::extent_server() {
pthread_mutex_init(&lock, NULL);
disk.clear();
int i; put(1, "", i); // create the root
}
#ifdef _DEBUG
std::string output(std::string x) {
for (size_t i=0; i<x.length(); i++) if (x[i] == 0) x[i] = ' ';
return x;
}
#endif
int extent_server::put(extent_protocol::extentid_t id, std::string buf, int &)
{
ScopedLock m(&lock);
#ifdef _DEBUG
printf("put %llu |%s|\n", id, output(buf).c_str());
#endif
file &ex = disk[id];
ex.str = buf;
unsigned int t = time(NULL);
ex.attr.ctime = ex.attr.mtime = t;
ex.attr.size = buf.size();
return extent_protocol::OK;
}
int extent_server::get(extent_protocol::extentid_t id, std::string &buf)
{
ScopedLock m(&lock);
#ifdef _DEBUG
printf("get %llu\n", id);
#endif
std::map<extent_protocol::extentid_t, file>::iterator it = disk.find(id);
if (it == disk.end()) return extent_protocol::NOENT;
buf = it->second.str;
#ifdef _DEBUG
printf("get %llu |%s|\n", id, output(buf).c_str());
#endif
unsigned int t = time(NULL);
it->second.attr.atime = t;
return extent_protocol::OK;
}
int extent_server::getattr(extent_protocol::extentid_t id, extent_protocol::attr &a)
{
ScopedLock m(&lock);
std::map<extent_protocol::extentid_t, file>::iterator it = disk.find(id);
if (it == disk.end()) return extent_protocol::NOENT;
a = it->second.attr;
return extent_protocol::OK;
}
int extent_server::remove(extent_protocol::extentid_t id, int &)
{
ScopedLock m(&lock);
std::map<extent_protocol::extentid_t, file>::iterator it = disk.find(id);
if (it == disk.end()) return extent_protocol::NOENT;
disk.erase(it);
return extent_protocol::OK;
}