forked from wwoods/gridfs-fuse
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathops_dir.cpp
116 lines (95 loc) · 3.13 KB
/
ops_dir.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
/*
* 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 <mongo/bson/bson.h>
#include <mongo/client/gridfs.h>
#include "operations.h"
#include "options.h"
#include "utils.h"
int gridfs_mkdir(const char* path, mode_t mode) {
path = fuse_to_mongo_path(path);
auto sdc = make_ScopedDbConnection();
mongo::DBClientBase &client = sdc->conn();
mongo::OID id;
id.init();
fuse_context *context = fuse_get_context();
mongo::BSONObjBuilder file;
file << "_id" << id
<< "filename" << path
<< "chunkSize" << 0
<< "uploadDate" << mongo::DATENOW
<< "md5" << 0
<< "mode" << (mode | S_IFDIR);
{
passwd *pw = getpwuid(context->uid);
if (pw)
file << "owner" << pw->pw_name;
}
{
group *gr = getgrgid(context->gid);
if (gr)
file << "group" << gr->gr_name;
}
client.insert(db_name() + ".files",
file.obj());
return 0;
}
int gridfs_rmdir(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_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi) {
path = fuse_to_mongo_path(path);
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
auto sdc = make_ScopedDbConnection();
mongo::BSONObj proj = BSON("filename" << 1);
std::string path_start = path;
if (strlen(path) > 0)
path_start += "/";
std::unique_ptr<mongo::DBClientCursor> cursor = sdc->conn().query(db_name() + ".files",
BSON("filename" <<
BSON("$regex" << "^" + path_start)
),
0, 0,
&proj);
std::string lastFN;
while (cursor->more()) {
std::string filename = cursor->next()["filename"].String();
std::string rel = filename.substr(path_start.length());
if (rel.find("/") != std::string::npos)
continue;
/* 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 != filename)
filler(buf, rel.c_str(), NULL, 0);
/* Update lastFN with our cursor's current filename */
lastFN = filename;
fprintf(stderr, "DEBUG: %s\n", lastFN.c_str());
}
for (auto i : open_files)
if (i.first.find(path_start) == 0) {
std::string rel = i.first.substr(path_start.length());
if (rel.find("/") != std::string::npos)
continue;
filler(buf, i.first.c_str(), NULL, 0);
}
return 0;
}