forked from wwoods/gridfs-fuse
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ops_file.cpp
181 lines (137 loc) · 4.44 KB
/
ops_file.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
/*
* 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 <algorithm>
#include <pwd.h>
#include <grp.h>
#include <mongo/bson/bson.h>
#include <mongo/client/gridfs.h>
#include "operations.h"
#include "utils.h"
#include "options.h"
unsigned int FH = 1;
int gridfs_open(const char *path, struct fuse_file_info *fi) {
if ((fi->flags & O_ACCMODE) != O_RDONLY)
return -EACCES;
path = fuse_to_mongo_path(path);
if (open_files.find(path) != open_files.end())
return 0;
auto sdc = make_ScopedDbConnection();
mongo::GridFS gf = get_gridfs(sdc);
mongo::GridFile file = gf.findFile(path);
if (!file.exists())
return -ENOENT;
return 0;
}
int gridfs_release(const char* path, struct fuse_file_info* ffi) {
// 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
if (!ffi->fh)
return 0;
path = fuse_to_mongo_path(path);
open_files.erase(path);
return 0;
}
int gridfs_create(const char* path, mode_t mode, struct fuse_file_info* ffi) {
fuse_context *context = fuse_get_context();
path = fuse_to_mongo_path(path);
open_files[path] = std::make_shared<LocalGridFile>(context->uid, context->gid, mode);
ffi->fh = FH++;
return 0;
}
int gridfs_unlink(const char* path) {
auto sdc = make_ScopedDbConnection();
mongo::GridFS gf = get_gridfs(sdc);
path = fuse_to_mongo_path(path);
gf.removeFile(path);
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);
auto file_iter = open_files.find(path);
if (file_iter != open_files.end()) {
LocalGridFile::ptr lgf = file_iter->second;
return lgf->read(buf, size, offset);
}
auto sdc = make_ScopedDbConnection();
mongo::GridFS gf = get_gridfs(sdc);
mongo::GridFile file = gf.findFile(path);
if (!file.exists())
return -EBADF;
int chunk_size = file.getChunkSize();
int chunk_num = offset / chunk_size;
size_t len = 0;
while (len < size && chunk_num < file.getNumChunks()) {
mongo::GridFSChunk chunk = file.getChunk(chunk_num);
int to_read;
int cl = chunk.len();
const char *d = chunk.data(cl);
if (len) {
to_read = std::min((long unsigned)cl, (long unsigned)(size - len));
memcpy(buf + len, d, to_read);
} else {
to_read = std::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++;
}
return len;
}
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::ptr lgf = open_files[path];
return lgf->write(buf, nbyte, offset);
}
int gridfs_flush(const char* path, struct fuse_file_info *ffi) {
if (!ffi->fh)
return 0;
path = fuse_to_mongo_path(path);
auto file_iter = open_files.find(path);
if (file_iter == open_files.end())
return -ENOENT;
LocalGridFile::ptr lgf = file_iter->second;
if (lgf->is_clean())
return 0;
auto sdc = make_ScopedDbConnection();
mongo::GridFS gf = get_gridfs(sdc);
if (gf.findFile(path).exists())
gf.removeFile(path);
size_t len = lgf->Length();
char *buf = new char[len];
lgf->read(buf, len, 0);
gf.storeFile(buf, len, path);
mongo::BSONObjBuilder b;
{
passwd *pw = getpwuid(lgf->Uid());
if (pw)
b.append("owner", pw->pw_name);
}
{
group *gr = getgrgid(lgf->Gid());
if (gr)
b.append("group", gr->gr_name);
}
b.append("mode", lgf->Mode());
sdc->conn().update(db_name() + ".files",
BSON("filename" << path),
BSON("$set" << b.obj()));
lgf->set_flushed();
return 0;
}