-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcouchmount.py
370 lines (332 loc) · 12.6 KB
/
couchmount.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2008 Jason Davies
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.
from couchdb.client import Database, Document, Server, Row, ViewResults
from couchdb.http import ResourceNotFound
import errno
import fuse
import os
import stat
import sys
from time import time
try:
import simplejson as json
except ImportError:
import json # Python 2.6
from urllib import quote, unquote
fuse.fuse_python_api = (0, 2)
COUCHFS_DIRECTORY_PLACEHOLDER = u'.couchfs-directory-placeholder'
class CouchStat(fuse.Stat):
def __init__(self):
self.st_mode = 0
self.st_ino = 0
self.st_dev = 0
self.st_nlink = 0
self.st_uid = os.getuid()
self.st_gid = os.getgid()
self.st_size = 4096
self.st_atime = 0
self.st_mtime = 0
self.st_ctime = 0
def _normalize_path(path):
return u'/'.join([part for part in path.split(u'/') if part != u''])
class CouchFSDocument(fuse.Fuse):
def __init__(self, mountpoint, uri=None, *args, **kwargs):
fuse.Fuse.__init__(self, *args, **kwargs)
db_uri, doc_id = uri.rsplit('/', 1)
self.doc_id = unquote(doc_id)
self.db = Database(db_uri)
def get_dirs(self):
dirs = {}
attachments = self.db[self.doc_id].get('_attachments', {}).keys()
for att in attachments:
parents = [u'']
for name in att.split('/'):
filenames = dirs.setdefault(u'/'.join(parents[1:]), set())
if name != COUCHFS_DIRECTORY_PLACEHOLDER:
filenames.add(name)
parents.append(name)
return dirs
def readdir(self, path, offset):
path = _normalize_path(path)
for r in '.', '..':
yield fuse.Direntry(r)
for name in self.get_dirs().get(path, []):
yield fuse.Direntry(name.encode('utf-8'))
def getattr(self, path):
path = _normalize_path(path)
try:
st = CouchStat()
if path == '' or path in self.get_dirs().keys():
st.st_mode = stat.S_IFDIR | 0775
st.st_nlink = 2
else:
att = self.db[self.doc_id].get('_attachments', {})
data = att[path]
st.st_mode = stat.S_IFREG | 0664
st.st_nlink = 1
st.st_size = data['length']
return st
except (KeyError, ResourceNotFound):
return -errno.ENOENT
def open(self, path, flags):
path = _normalize_path(path)
try:
#data = self.db.get_attachment(self.db[self.doc_id], path.split('/')[-1])
#att = self.db[self.doc_id].get('_attachments', {})
#data = att[path.split('/')[-1]]
parts = path.rsplit(u'/', 1)
if len(parts) == 1:
dirname, filename = u'', parts[0]
else:
dirname, filename = parts
if filename in self.get_dirs()[dirname]:
return 0
return -errno.ENOENT
except (KeyError, ResourceNotFound):
return -errno.ENOENT
#accmode = os.O_RDONLY | os.O_WRONLY | os.O_RDWR
#if (flags & accmode) != os.O_RDONLY:
# return -errno.EACCES
def read(self, path, size, offset):
path = _normalize_path(path)
try:
data = self.db.get_attachment(self.db[self.doc_id], path)
slen = len(data)
if offset < slen:
if offset + size > slen:
size = slen - offset
buf = data[offset:offset+size]
else:
buf = ''
return buf
except (KeyError, ResourceNotFound):
pass
return -errno.ENOENT
def write(self, path, buf, offset):
path = _normalize_path(path)
try:
data = self.db.get_attachment(self.db[self.doc_id], path)
data = data[0:offset] + buf + data[offset+len(buf):]
self.db.put_attachment(self.db[self.doc_id], data, filename=path)
return len(buf)
except (KeyError, ResourceNotFound):
pass
return -errno.ENOENT
def mknod(self, path, mode, dev):
path = _normalize_path(path)
self.db.put_attachment(self.db[self.doc_id], u'', filename=path)
def unlink(self, path):
path = _normalize_path(path)
parts = path.rsplit(u'/', 1)
if len(parts) == 1:
dirname, filename = u'', parts[0]
else:
dirname, filename = parts
self.db.delete_attachment(self.db[self.doc_id], path)
if filename != COUCHFS_DIRECTORY_PLACEHOLDER and len(self.get_dirs().get(dirname, [])) == 0:
print "putting to:", u'%s/%s' % (dirname, COUCHFS_DIRECTORY_PLACEHOLDER)
self.db.put_attachment(self.db[self.doc_id], u'', filename=u'%s/%s' % (dirname, COUCHFS_DIRECTORY_PLACEHOLDER))
def truncate(self, path, size):
path = _normalize_path(path)
self.db.put_attachment(self.db[self.doc_id], u'', filename=path)
return 0
def utime(self, path, times):
return 0
def mkdir(self, path, mode):
path = _normalize_path(path)
self.db.put_attachment(self.db[self.doc_id], u'', filename=u'%s/%s' % (path, COUCHFS_DIRECTORY_PLACEHOLDER))
return 0
def rmdir(self, path):
path = _normalize_path(path)
self.db.delete_attachment(self.db[self.doc_id], u'%s/%s' % (path, COUCHFS_DIRECTORY_PLACEHOLDER))
return 0
def rename(self, pathfrom, pathto):
pathfrom, pathto = _normalize_path(pathfrom), _normalize_path(pathto)
data = self.db.get_attachment(self.db[self.doc_id], pathfrom)
self.db.put_attachment(self.db[self.doc_id], data, filename=pathto)
self.db.delete_attachment(self.db[self.doc_id], pathfrom)
return 0
def fsync(self, path, isfsyncfile):
return 0
def statfs(self):
"""
Should return a tuple with the following 6 elements:
- blocksize - size of file blocks, in bytes
- totalblocks - total number of blocks in the filesystem
- freeblocks - number of free blocks
- availblocks - number of blocks available to non-superuser
- totalfiles - total number of file inodes
- freefiles - nunber of free file inodes
Feel free to set any of the above values to 0, which tells
the kernel that the info is not available.
"""
st = fuse.StatVfs()
block_size = 1024
blocks = 1024 * 1024
blocks_free = blocks
blocks_avail = blocks_free
files = 0
files_free = 0
st.f_bsize = block_size
st.f_frsize = block_size
st.f_blocks = blocks
st.f_bfree = blocks_free
st.f_bavail = blocks_avail
st.f_files = files
st.f_ffree = files_free
return st
class CouchFS(fuse.Fuse):
"""FUSE interface to a CouchDB database."""
def __init__(self, mountpoint, uri=None, *args, **kw):
fuse.Fuse.__init__(self, *args, **kw)
self.fuse_args.mountpoint = mountpoint
if uri is not None:
self.server = Server(uri)
else:
self.server = Server()
def getcouchattrs(self, path):
attr = self.server
attrs = [attr]
parts = [x for x in path[1:].split('/') if x != '']
i = 0
for part in parts:
if isinstance(attr, Database):
if part == '_view':
attr = attr.view('_all_docs')['_design/':'_design/ZZZ']
attr.is_view = True
elif part == '_all_docs':
attr = attr.view('_all_docs')
elif isinstance(attr, ViewResults):
if getattr(attr, 'is_view', False):
attr = list(attr['_design/'+part])[0]
attr.is_view = True
else:
if attr.view.name != '_all_docs':
part = json.loads(unquote(part))
results = list(attr.view()[part])
if len(results) == 1:
attr = results[0]
else:
attr = attr.view()[part+'/':part+'/ZZZ']
if i + 1 < len(parts):
parts[i+1] = '%s/%s' % (part, parts[i+1])
elif isinstance(attr, Row):
if getattr(attr, 'is_view', False):
db = self.server[parts[0]]
attr = db.view('_view/' + '/'.join(parts[i-1:i+1]), group=True)
elif part == 'value':
attr = attr.value
else:
attr = attr[part]
attrs.append(attr)
i += 1
return attrs
def getattr(self, path):
try:
st = CouchStat()
attr = self.getcouchattrs(path)[-1]
if (isinstance(attr, Server) or isinstance(attr, Database) or
isinstance(attr, Document) or isinstance(attr, ViewResults)
or isinstance(attr, Row)):
st.st_mode = stat.S_IFDIR | 0755
st.st_nlink = 2
else:
data = json.dumps(attr)
st.st_mode = stat.S_IFREG | 0444
st.st_nlink = 1
st.st_size = len(data)
return st
except (KeyError, ResourceNotFound):
return -errno.ENOENT
def readdir(self, path, offset):
attr = self.getcouchattrs(path)[-1]
for r in '.', '..':
yield fuse.Direntry(r)
if isinstance(attr, Server):
for db_name in self.server:
yield fuse.Direntry(db_name.encode('utf-8'))
return
if isinstance(attr, Database):
yield fuse.Direntry('_all_docs')
yield fuse.Direntry('_view')
return
if isinstance(attr, ViewResults):
is_view = getattr(attr, 'is_view', False)
for row in list(attr):
dirname = row.key
if attr.view.name != '_all_docs':
dirname = quote(json.dumps(row.key))
else:
dirname = dirname.split('/')[attr.options.get('startkey', '').count('/')]
if is_view:
dirname = dirname.split('/')[-1]
yield fuse.Direntry(dirname.encode('utf-8'))
return
if isinstance(attr, Row):
is_view = getattr(attr, 'is_view', False)
if is_view:
db = self.getcouchattrs(path)[1]
for r in db[attr.id]['views']:
yield fuse.Direntry(r.encode('utf-8'))
else:
yield fuse.Direntry('value')
return
for r in attr.keys():
yield fuse.Direntry(r.encode('utf-8'))
def open(self, path, flags):
try:
attr = self.getcouchattrs(path)[-1]
except (KeyError, ResourceNotFound):
return -errno.ENOENT
accmode = os.O_RDONLY | os.O_WRONLY | os.O_RDWR
if (flags & accmode) != os.O_RDONLY:
return -errno.EACCES
def read(self, path, size, offset):
try:
attr = self.getcouchattrs(path)[-1]
data = json.dumps(attr)
slen = len(data)
if offset < slen:
if offset + size > slen:
size = slen - offset
buf = data[offset:offset+size]
else:
buf = ''
return buf
except (KeyError, ResourceNotFound):
pass
return -errno.ENOENT
def write(self, path, buf, offset):
pass
def unlink(self, path):
attr = self.getcouchattrs(path)[-1]
def mkdir(self, path, mode):
server = self.getcouchattrs(path)[0]
if isinstance(server, Server):
server.create(path.split('/')[-1])
def main():
args = sys.argv[1:]
if len(args) not in (2, 3):
print "CouchDB FUSE Connector: Allows you to browse the _attachments of"
print " any CouchDB document on your own filesystem!"
print
print "Remember to URL-encode your <doc_id> appropriately."
print
print "Usage: python couchmount.py [-d] <http://hostname:port/db/doc_id> <mount-point>"
sys.exit(-1)
if len(args) == 1:
fs = CouchFS(args[0])
elif len(args) == 2:
fs = CouchFSDocument(args[1], args[0])
elif len(args) == 3:
fs = CouchFSDocument(args[2], args[1])
fs.parse(errex=1)
fs.main()
if __name__ == '__main__':
main()