forked from wwoods/gridfs-fuse
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathops_xattr.cpp
158 lines (123 loc) · 3.9 KB
/
ops_xattr.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
/*
* 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 "utils.h"
#include "options.h"
#ifdef __linux__
#include <sys/xattr.h>
#endif
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;
auto sdc = make_ScopedDbConnection();
mongo::GridFS gf = get_gridfs(sdc);
mongo::GridFile file = gf.findFile(path);
if (!file.exists())
return -ENOENT;
size_t len = 0;
mongo::BSONObj metadata = file.getMetadata();
std::set<std::string> field_set;
metadata.getFieldNames(field_set);
for (auto s : field_set) {
std::string attr_name = namespace_xattr(s);
int field_len = attr_name.size() + 1;
len += field_len;
if (len < size) {
memcpy(list, attr_name.c_str(), field_len);
list += field_len;
}
}
if (size == 0)
return len;
if (len >= size)
return -ERANGE;
return len;
}
int gridfs_getxattr(const char* path, const char* name, char* value, size_t size) {
if (strcmp(path, "/") == 0)
return -ENODATA;
const char* attr_name = unnamespace_xattr(name);
if (!attr_name)
return -ENODATA;
path = fuse_to_mongo_path(path);
if (open_files.find(path) != open_files.end())
return -ENOATTR;
auto sdc = make_ScopedDbConnection();
mongo::GridFS gf = get_gridfs(sdc);
mongo::GridFile file = gf.findFile(path);
if (!file.exists())
return -ENOENT;
mongo::BSONObj metadata = file.getMetadata();
if (metadata.isEmpty())
return -ENOATTR;
mongo::BSONElement field = metadata[attr_name];
if (field.eoo())
return -ENOATTR;
std::string field_str = field.toString();
size_t len = field_str.size() + 1;
if (size == 0)
return len;
if (len >= size)
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) {
if (strcmp(path, "/") == 0)
return -ENODATA;
const char* attr_name = unnamespace_xattr(name);
if (!attr_name)
return -ENODATA;
path = fuse_to_mongo_path(path);
if (open_files.find(path) != open_files.end())
return -ENOATTR;
auto sdc = make_ScopedDbConnection();
mongo::DBClientBase &client = sdc->conn();
mongo::BSONObj file_obj = client.findOne(db_name() + ".files",
BSON("filename" << path));
if (file_obj.isEmpty())
return -ENOENT;
client.update(db_name() + ".files",
BSON("filename" << path),
BSON("$set" <<
BSON((std::string("metadata.") + attr_name) << value)
));
return 0;
}
int gridfs_removexattr(const char* path, const char* name) {
if (strcmp(path, "/") == 0)
return -ENODATA;
const char* attr_name = unnamespace_xattr(name);
if (!attr_name)
return -ENODATA;
path = fuse_to_mongo_path(path);
if (open_files.find(path) != open_files.end())
return -ENOATTR;
auto sdc = make_ScopedDbConnection();
mongo::DBClientBase &client = sdc->conn();
mongo::BSONObj file_obj = client.findOne(db_name() + ".files",
BSON("filename" << path));
if (file_obj.isEmpty())
return -ENOENT;
client.update(db_name() + ".files",
BSON("filename" << path),
BSON("$unset" <<
BSON((std::string("metadata.") + attr_name) << "")
));
return 0;
}