forked from wwoods/gridfs-fuse
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathops_metadata.cpp
197 lines (160 loc) · 5.23 KB
/
ops_metadata.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
/*
* 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 <pwd.h>
#include <grp.h>
#include "operations.h"
#include "options.h"
#include "utils.h"
unsigned int subdir_count(mongo::DBClientBase &client, std::string path) {
mongo::BSONObj proj = BSON("mode" << 1);
std::string path_start = path;
if (path.length() > 0)
path_start += "/";
std::unique_ptr<mongo::DBClientCursor> cursor = client.query(db_name() + ".files",
BSON("filename" <<
BSON("$regex" << ("^" + path_start + "/[^/]*$"))
),
0, 0,
&proj);
std::string lastFN;
unsigned int count = 0;
while (cursor->more()) {
mongo::BSONObj file_obj = cursor->next();
if (S_ISDIR(file_obj["mode"].Int()))
count++;
}
return count;
}
int gridfs_getattr(const char *path, struct stat *stbuf) {
memset(stbuf, 0, sizeof(struct stat));
auto sdc = make_ScopedDbConnection();
mongo::DBClientBase &client = sdc->conn();
if (strcmp(path, "/") == 0) {
stbuf->st_mode = S_IFDIR | 0777;
stbuf->st_nlink = 2;
fuse_context *context = fuse_get_context();
stbuf->st_uid = context->uid;
stbuf->st_gid = context->gid;
return 0;
}
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;
stbuf->st_mode = S_IFREG | (lgf->Mode() & (0xffff ^ S_IFMT));
stbuf->st_nlink = 1;
stbuf->st_uid = lgf->Uid();
stbuf->st_gid = lgf->Gid();
stbuf->st_ctime = time(NULL);
stbuf->st_mtime = time(NULL);
stbuf->st_size = lgf->Length();
return 0;
}
mongo::BSONObj file_obj = client.findOne(db_name() + ".files",
BSON("filename" << path));
if (file_obj.isEmpty())
return -ENOENT;
if (file_obj.hasField("owner")) {
passwd *pw = getpwnam(file_obj["owner"].str().c_str());
if (pw)
stbuf->st_uid = pw->pw_uid;
}
if (file_obj.hasField("group")) {
group *gr = getgrnam(file_obj["group"].str().c_str());
if (gr)
stbuf->st_gid = gr->gr_gid;
}
stbuf->st_mode = file_obj["mode"].Int();
if (S_ISREG(stbuf->st_mode)) {
stbuf->st_nlink = 1;
stbuf->st_size = file_obj["length"].Int();
stbuf->st_blocks = stbuf->st_size >> 9;
}
if (S_ISDIR(stbuf->st_mode))
stbuf->st_nlink = 2 + subdir_count(client, path);
if (S_ISLNK(stbuf->st_mode))
stbuf->st_size = file_obj["target"].String().length();
time_t upload_time = mongo_time_to_unix_time(file_obj["uploadDate"].date());
stbuf->st_ctime = upload_time;
stbuf->st_mtime = upload_time;
return 0;
}
int gridfs_chmod(const char* path, mode_t mode) {
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;
lgf->setMode(mode);
}
auto sdc = make_ScopedDbConnection();
sdc->conn().update(db_name() + ".files",
BSON("filename" << path),
BSON("$set" << BSON("mode" << mode)));
return 0;
}
int gridfs_chown(const char* path, uid_t uid, gid_t gid) {
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;
lgf->setUid(uid);
lgf->setGid(gid);
}
mongo::BSONObjBuilder b;
{
passwd *pw = getpwuid(uid);
if (pw)
b.append("owner", pw->pw_name);
}
{
group *gr = getgrgid(gid);
if (gr)
b.append("group", gr->gr_name);
}
if (b.hasField("owner") || b.hasField("group")) {
auto sdc = make_ScopedDbConnection();
sdc->conn().update(db_name() + ".files",
BSON("filename" << path),
BSON("$set" << b.obj()));
}
return 0;
}
int gridfs_utimens(const char* path, const struct timespec tv[2]) {
path = fuse_to_mongo_path(path);
unsigned long long millis = ((unsigned long long)tv[1].tv_sec * 1000) + (tv[1].tv_nsec / 1e+6);
auto sdc = make_ScopedDbConnection();
sdc->conn().update(db_name() + ".files",
BSON("filename" << path),
BSON("$set" <<
BSON("uploadDate" << mongo::Date_t(millis))
));
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);
auto sdc = make_ScopedDbConnection();
mongo::DBClientBase &client = sdc->conn();
mongo::BSONObj file_obj = client.findOne(db_name() + ".files",
BSON("filename" << old_path));
if (file_obj.isEmpty())
return -ENOENT;
client.update(db_name() + ".files",
BSON("_id" << file_obj.getField("_id")),
BSON("$set" << BSON("filename" << new_path)));
return 0;
}