forked from mikejs/gridfs-fuse
-
Notifications
You must be signed in to change notification settings - Fork 5
/
operations.cpp
466 lines (363 loc) · 11.1 KB
/
operations.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
* Copyright 2009 Michael Stephens
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "operations.h"
#include "options.h"
#include "utils.h"
#include "local_gridfile.h"
#include <algorithm>
#include <cstring>
#include <cerrno>
#include <fcntl.h>
#include <mongo/client/gridfs.h>
#include <mongo/client/connpool.h>
#include <mongo/client/dbclient.h>
#include <mongo/util/hostandport.h>
#ifdef __linux__
#include <sys/xattr.h>
#endif
using namespace std;
using namespace mongo;
std::map<string, LocalGridFile*> open_files;
unsigned int FH = 1;
void ScopedDbConnection_init(ScopedDbConnection& sdc) {
bool digest = true;
string err = "";
if (gridfs_options.username) {
sdc.conn().DBClientWithCommands::auth(gridfs_options.db, gridfs_options.username, gridfs_options.password, err, digest);
fprintf(stderr, "DEBUG: %s\n", err.c_str());
}
}
int gridfs_getattr(const char *path, struct stat *stbuf)
{
int res = 0;
memset(stbuf, 0, sizeof(struct stat));
if(strcmp(path, "/") == 0) {
stbuf->st_mode = S_IFDIR | 0777;
stbuf->st_nlink = 2;
return 0;
}
path = fuse_to_mongo_path(path);
map<string,LocalGridFile*>::const_iterator file_iter;
file_iter = open_files.find(path);
if(file_iter != open_files.end()) {
stbuf->st_mode = S_IFREG | 0555;
stbuf->st_nlink = 1;
stbuf->st_ctime = time(NULL);
stbuf->st_mtime = time(NULL);
stbuf->st_size = file_iter->second->getLength();
return 0;
}
// HACK: This is a protective measure to ensure we don't spin off into forever if a path without a period is requested.
if(path_depth(path) > 10) {
return -ENOENT;
}
// HACK: Assumes that if the last part of the path has a '.' in it, it's the leaf of the path, and if we haven't found a match by now,
// give up and go home. This works just dandy as long as you avoid putting periods in your 'directory' names.
/*if(!is_leaf(path)) {
stbuf->st_mode = S_IFDIR | 0777;
stbuf->st_nlink = 2;
return 0;
}*/
ScopedDbConnection sdc(*gridfs_options.conn_string);
ScopedDbConnection_init(sdc);
GridFS gf(sdc.conn(), gridfs_options.db, gridfs_options.prefix);
GridFile file = gf.findFile(path);
sdc.done();
if(!file.exists()) {
return -ENOENT;
}
stbuf->st_mode = S_IFREG | 0555;
stbuf->st_nlink = 1;
stbuf->st_size = file.getContentLength();
time_t upload_time = mongo_time_to_unix_time(file.getUploadDate());
stbuf->st_ctime = upload_time;
stbuf->st_mtime = upload_time;
return 0;
}
int gridfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi)
{
if(strcmp(path, "/") != 0)
return -ENOENT;
/* Create string to hold last filename seen */
string lastFN;
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
ScopedDbConnection sdc(*gridfs_options.conn_string);
ScopedDbConnection_init(sdc);
GridFS gf(sdc.conn(), gridfs_options.db, gridfs_options.prefix);
auto_ptr<DBClientCursor> cursor = gf.list();
while(cursor->more()) {
BSONObj f = cursor->next();
/* If this filename matches the last filename we've seen, *do not* add it to the buffer because it's a duplicate filename */
if(lastFN!=f.getStringField("filename")) {
filler(buf, f.getStringField("filename") , NULL , 0);
}
/* Update lastFN with our cursor's current filename */
lastFN = f.getStringField("filename");
fprintf(stderr, "DEBUG: %s\n", lastFN.c_str());
}
sdc.done();
for(map<string,LocalGridFile*>::const_iterator i = open_files.begin();
i != open_files.end(); i++)
{
filler(buf, i->first.c_str(), NULL, 0);
}
return 0;
}
int gridfs_open(const char *path, struct fuse_file_info *fi)
{
path = fuse_to_mongo_path(path);
if((fi->flags & O_ACCMODE) == O_RDONLY) {
if(open_files.find(path) != open_files.end()) {
return 0;
}
ScopedDbConnection sdc(*gridfs_options.conn_string);
ScopedDbConnection_init(sdc);
GridFS gf(sdc.conn(), gridfs_options.db, gridfs_options.prefix);
GridFile file = gf.findFile(path);
sdc.done();
if(file.exists()) {
return 0;
}
return -ENOENT;
} else {
return -EACCES;
}
}
int gridfs_create(const char* path, mode_t mode, struct fuse_file_info* ffi)
{
path = fuse_to_mongo_path(path);
open_files[path] = new LocalGridFile(DEFAULT_CHUNK_SIZE);
ffi->fh = FH++;
return 0;
}
int gridfs_release(const char* path, struct fuse_file_info* ffi)
{
path = fuse_to_mongo_path(path);
if(!ffi->fh) {
// fh is not set if file is opened read only
// Would check ffi->flags for O_RDONLY instead but MacFuse doesn't
// seem to properly pass flags into release
return 0;
}
delete open_files[path];
open_files.erase(path);
return 0;
}
int gridfs_unlink(const char* path) {
path = fuse_to_mongo_path(path);
ScopedDbConnection sdc(*gridfs_options.conn_string);
ScopedDbConnection_init(sdc);
GridFS gf(sdc.conn(), gridfs_options.db, gridfs_options.prefix);
gf.removeFile(path);
sdc.done();
return 0;
}
int gridfs_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
path = fuse_to_mongo_path(path);
size_t len = 0;
map<string,LocalGridFile*>::const_iterator file_iter;
file_iter = open_files.find(path);
if(file_iter != open_files.end()) {
LocalGridFile *lgf = file_iter->second;
return lgf->read(buf, size, offset);
}
ScopedDbConnection sdc(*gridfs_options.conn_string);
ScopedDbConnection_init(sdc);
GridFS gf(sdc.conn(), gridfs_options.db, gridfs_options.prefix);
GridFile file = gf.findFile(path);
if(!file.exists()) {
sdc.done();
return -EBADF;
}
int chunk_size = file.getChunkSize();
int chunk_num = offset / chunk_size;
while(len < size && chunk_num < file.getNumChunks()) {
GridFSChunk chunk = file.getChunk(chunk_num);
int to_read;
int cl = chunk.len();
const char *d = chunk.data(cl);
if(len) {
to_read = min((long unsigned)cl, (long unsigned)(size - len));
memcpy(buf + len, d, to_read);
} else {
to_read = min((long unsigned)(cl - (offset % chunk_size)), (long unsigned)(size - len));
memcpy(buf + len, d + (offset % chunk_size), to_read);
}
len += to_read;
chunk_num++;
}
sdc.done();
return len;
}
int gridfs_listxattr(const char* path, char* list, size_t size)
{
path = fuse_to_mongo_path(path);
if(open_files.find(path) != open_files.end()) {
return 0;
}
ScopedDbConnection sdc(*gridfs_options.conn_string);
ScopedDbConnection_init(sdc);
GridFS gf(sdc.conn(), gridfs_options.db, gridfs_options.prefix);
GridFile file = gf.findFile(path);
sdc.done();
if(!file.exists()) {
return -ENOENT;
}
int len = 0;
BSONObj metadata = file.getMetadata();
set<string> field_set;
metadata.getFieldNames(field_set);
for(set<string>::const_iterator s = field_set.begin(); s != field_set.end(); s++) {
string attr_name = namespace_xattr(*s);
int field_len = attr_name.size() + 1;
len += field_len;
if(size >= len) {
memcpy(list, attr_name.c_str(), field_len);
list += field_len;
}
}
if(size == 0) {
return len;
} else if(size < len) {
return -ERANGE;
}
return len;
}
int gridfs_getxattr(const char* path, const char* name, char* value, size_t size)
{
if(strcmp(path, "/") == 0) {
return -ENOATTR;
}
path = fuse_to_mongo_path(path);
const char* attr_name = unnamespace_xattr(name);
if(!attr_name) {
return -ENOATTR;
}
if(open_files.find(path) != open_files.end()) {
return -ENOATTR;
}
ScopedDbConnection sdc(*gridfs_options.conn_string);
ScopedDbConnection_init(sdc);
GridFS gf(sdc.conn(), gridfs_options.db, gridfs_options.prefix);
GridFile file = gf.findFile(path);
sdc.done();
if(!file.exists()) {
return -ENOENT;
}
BSONObj metadata = file.getMetadata();
if(metadata.isEmpty()) {
return -ENOATTR;
}
BSONElement field = metadata[attr_name];
if(field.eoo()) {
return -ENOATTR;
}
string field_str = field.toString();
int len = field_str.size() + 1;
if(size == 0) {
return len;
} else if(size < len) {
return -ERANGE;
}
memcpy(value, field_str.c_str(), len);
return len;
}
int gridfs_setxattr(const char* path, const char* name, const char* value,
size_t size, int flags)
{
return -ENOTSUP;
}
int gridfs_write(const char* path, const char* buf, size_t nbyte,
off_t offset, struct fuse_file_info* ffi)
{
path = fuse_to_mongo_path(path);
if(open_files.find(path) == open_files.end()) {
return -ENOENT;
}
LocalGridFile *lgf = open_files[path];
return lgf->write(buf, nbyte, offset);
}
int gridfs_flush(const char* path, struct fuse_file_info *ffi)
{
path = fuse_to_mongo_path(path);
if(!ffi->fh) {
return 0;
}
map<string,LocalGridFile*>::iterator file_iter;
file_iter = open_files.find(path);
if(file_iter == open_files.end()) {
return -ENOENT;
}
LocalGridFile *lgf = file_iter->second;
if(!lgf->dirty()) {
return 0;
}
ScopedDbConnection sdc(*gridfs_options.conn_string);
ScopedDbConnection_init(sdc);
GridFS gf(sdc.conn(), gridfs_options.db, gridfs_options.prefix);
if(gf.findFile(path).exists()) {
gf.removeFile(path);
}
size_t len = lgf->getLength();
char *buf = new char[len];
lgf->read(buf, len, 0);
gf.storeFile(buf, len, path);
sdc.done();
lgf->flushed();
return 0;
}
int gridfs_rename(const char* old_path, const char* new_path)
{
old_path = fuse_to_mongo_path(old_path);
new_path = fuse_to_mongo_path(new_path);
ScopedDbConnection sdc(*gridfs_options.conn_string);
bool digest = true;
string err = "";
sdc.conn().DBClientWithCommands::auth(gridfs_options.db, gridfs_options.username, gridfs_options.password, err, digest);
fprintf(stderr, "DEBUG: %s\n", err.c_str());
DBClientBase &client = sdc.conn();
string db_name = gridfs_options.db;
db_name += ".";
db_name += gridfs_options.prefix;
BSONObj file_obj = client.findOne(db_name + ".files",
BSON("filename" << old_path));
if(file_obj.isEmpty()) {
return -ENOENT;
}
BSONObjBuilder b;
set<string> field_names;
file_obj.getFieldNames(field_names);
for(set<string>::iterator name = field_names.begin();
name != field_names.end(); name++)
{
if(*name != "filename") {
b.append(file_obj.getField(*name));
}
}
b << "filename" << new_path;
client.update(db_name + ".files",
BSON("_id" << file_obj.getField("_id")), b.obj());
sdc.done();
return 0;
}
int gridfs_mknod(const char *path, mode_t mode, dev_t rdev)
{
fprintf(stderr, "MKNOD: %s\n", path);
}