forked from mikejs/gridfs-fuse
-
Notifications
You must be signed in to change notification settings - Fork 5
/
local_gridfile.cpp
67 lines (53 loc) · 1.74 KB
/
local_gridfile.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
#include "local_gridfile.h"
#include <algorithm>
using namespace std;
int LocalGridFile::write(const char *buf, size_t nbyte, off_t offset)
{
int last_chunk = (offset + nbyte) / _chunkSize;
int written = 0;
while(last_chunk > _chunks.size() - 1) {
char *new_buf = new char[_chunkSize];
memset(new_buf, 0, _chunkSize);
_chunks.push_back(new_buf);
}
int chunk_num = offset / _chunkSize;
char* dest_buf = _chunks[chunk_num];
int buf_offset = offset % _chunkSize;
if(buf_offset) {
dest_buf += offset % _chunkSize;
int to_write = min<size_t>(nbyte - written,
(long unsigned int)(_chunkSize - buf_offset));
memcpy(dest_buf, buf, to_write);
written += to_write;
chunk_num++;
}
while(written < nbyte) {
dest_buf = _chunks[chunk_num];
int to_write = min<size_t>(nbyte - written,
(long unsigned int)_chunkSize);
memcpy(dest_buf, buf, to_write);
written += to_write;
chunk_num++;
}
_length = max(_length, (int)offset + written);
_dirty = true;
return written;
}
int LocalGridFile::read(char* buf, size_t size, off_t offset)
{
size_t len = 0;
int chunk_num = offset / _chunkSize;
while(len < size && chunk_num < _chunks.size()) {
const char* chunk = _chunks[chunk_num];
size_t to_read = min<size_t>((size_t)_chunkSize, size - len);
if(!len && offset) {
chunk += offset % _chunkSize;
to_read = min<size_t>(to_read,
(size_t)(_chunkSize - (offset % _chunkSize)));
}
memcpy(buf + len, chunk, to_read);
len += to_read;
chunk_num++;
}
return len;
}