-
Notifications
You must be signed in to change notification settings - Fork 0
/
hgfs.py
executable file
·402 lines (290 loc) · 12.1 KB
/
hgfs.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/usr/bin/env python
import argparse
import errno
import grp
import json
import logging
import os
import pwd
import shutil
import tempfile
from sys import argv, exit
from time import time
from fuse import FUSE, FuseOSError, EACCES, Operations, LoggingMixIn, fuse_get_context
from mercurial.dispatch import dispatch
try:
from mercurial.dispatch import request
except:
request = list
class HgFS(LoggingMixIn, Operations):
'''
A Mercurial filesystem.
'''
ATTRS = ('st_uid', 'st_gid', 'st_mode', 'st_atime', 'st_mtime', 'st_size')
STATV = ('f_bavail', 'f_bfree', 'f_blocks', 'f_bsize', 'f_favail', 'f_ffree', 'f_files', 'f_flag', 'f_frsize', 'f_namemax')
def __init__(self, repo, mountpoint='.', args={}):
self.log.setLevel(args.log)
self.log.debug("repo: %s mountpoint: %s args: %s", repo, mountpoint, repr(args))
self.repo = repo
self.mountpoint = os.path.abspath(mountpoint)
self.args = args
if not self.args.clone:
self.repo = os.path.abspath(repo)
self.log.debug("SELF: repo: %s mountpoint: %s args: %s", self.repo, self.mountpoint, repr(self.args))
if self.args.clone:
self.tmp = os.path.abspath(tempfile.mkdtemp(prefix='hgfs-'))
dispatch(request(['clone', self.repo, self.tmp]))
else:
self.tmp = self.repo
self.log.debug("Tmp: %s", self.tmp)
self.__load_attributes()
def __load_attributes(self):
ahgfs = os.path.join(self.tmp, '.hgfs')
try:
os.mkdir(ahgfs)
except Exception, e:
self.log.debug("Failed to create .hgfs.")
pass
attributes = os.walk(ahgfs)
for (dirpath, dirnames, filenames) in attributes:
for fname in filenames:
apath = os.path.join(ahgfs, os.path.join(dirpath, fname))
with open(apath, 'rb') as f:
attrs = json.load(f)
fname_no_ext = os.path.splitext(fname)[0]
atarget = os.path.join(self.tmp, os.path.join(dirpath[len(ahgfs) + 1:], fname_no_ext))
os.chmod(atarget, attrs['st_mode'])
os.chown(atarget, attrs['st_uid'], attrs['st_gid'])
def __save_attributes(self, path, msg):
_path = path[1:]
try:
ahgfs_path = os.path.join(self.tmp, '.hgfs', os.path.dirname(_path))
os.makedirs(ahgfs_path)
except OSError, e:
if e.errno == errno.EEXIST:
pass
else:
raise
apath = os.path.join(self.tmp, _path)
st = os.stat(apath)
attributes = dict((key, getattr(st, key)) for key in self.ATTRS)
uid, gid, pid = fuse_get_context()
username = pwd.getpwuid(uid)[0]
attributes['.hgfs'] = {
'pw_uid': uid,
'pw_name': username,
'gr_gid': gid,
'gr_name': grp.getgrgid(gid)[0],
}
modpath = os.path.join(self.tmp, '.hgfs', _path + '.attr')
with open(modpath, 'wb+') as f:
f.write(json.dumps(attributes, sort_keys=True, indent=2))
ahgfs = os.path.join(self.tmp, '.hgfs')
dispatch(request(['--cwd', self.tmp, 'commit', '-A', '-u', username, '-m', msg, '.hgfs', str(_path)]))
if self.args.clone: dispatch(request(['--cwd', self.tmp, 'push']))
def access(self, path, mode):
_path = path[1:]
apath = os.path.join(self.tmp, _path)
if not os.access(apath, mode):
raise FuseOSError(EACCES)
def chmod(self, path, mode):
_path = path[1:]
apath = os.path.join(self.tmp, _path)
status = os.chmod(apath, mode)
self.__save_attributes(path, "hgfs[chmod]: %s %o" % (_path, mode))
return status
def chown(self, path, uid, gid):
_path = path[1:]
apath = os.path.join(self.tmp, _path)
status = os.chown(apath, uid, gid)
self.__save_attributes(path, "hgfs[chown]: %s %d %d" % (_path, uid, gid))
return status
def create(self, path, mode, fi=None):
_path = path[1:]
apath = os.path.join(self.tmp, _path)
fh = -1
if fi == None:
fh = os.open(apath, os.O_CREAT | os.O_RDWR, mode)
#f = open(apath, 'w+', mode)
#fh = f.fileno()
else:
fi.fh = os.open(apath, os.O_CREAT | os.O_RDWR, mode)
#f = open(apath, 'w+', mode)
#fi.fh = f.fileno()
fh = 0
self.__save_attributes(path, "hgfs[create]: %s %o" % (_path, mode))
return fh
def destroy(self, path):
try:
dispatch(request(['--cwd', self.tmp, 'commit', '-A', '-u', 'hgfs', '-m', 'hgfs[cruft]']))
if self.args.clone: dispatch(request(['--cwd', self.tmp, 'push']))
finally:
if self.args.clone: shutil.rmtree(self.tmp)
def flush(self, path, fh):
return os.fsync(fh)
def fsync(self, path, datasync, fh):
return os.fsync(fh)
fsyncdir = None
# def fsyncdir(self, path, datasync, fh):
# pass
def getattr(self, path, fh=None):
if self.args.clone: dispatch(request(['--cwd', self.tmp, 'pull', '-u']))
apath = os.path.join(self.tmp, path[1:])
st = os.lstat(apath)
return dict((key, getattr(st, key)) for key in self.ATTRS)
getxattr = None
# def getxattr(self, path, name, position=0):
# pass
init = None
# def init(self, path):
# pass
link = None
# def link(self, target, source):
# pass
listxattr = None
# def listxattr(self, path):
# pass
def mkdir(self, path, mode):
_path = path[1:]
apath = os.path.join(self.tmp, _path)
status = os.mkdir(apath, mode)
self.__save_attributes(path, "hgfs[mkdir]: %s %o" % (_path, mode))
return status
mknod = None
def open(self, path, flags):
if self.args.clone:
dispatch(request(['--cwd', self.tmp, 'pull', '-u']))
self.__load_attributes()
_path = path[1:]
apath = os.path.join(self.tmp, _path)
return os.open(apath, flags)
# opendir = None
def read(self, path, size, offset, fh):
if self.args.clone: dispatch(request(['--cwd', self.tmp, 'pull', '-u']))
os.lseek(fh, offset, os.SEEK_SET)
data = os.read(fh, size)
return data
def readdir(self, path, fh):
if self.args.clone: dispatch(request(['--cwd', self.tmp, 'pull', '-u']))
apath = os.path.join(self.tmp, path[1:])
paths = os.listdir(apath)
clean = []
for path in paths:
if path != '.hg' and path != '.hgfs':
clean.append(path)
return ['.', '..'] + clean
def readlink(self, path):
if self.args.clone: dispatch(request(['--cwd', self.tmp, 'pull', '-u']))
apath = os.path.join(self.tmp, path[1:])
return os.readlink(apath)
def release(self, path, fh):
return os.close(fh)
# def releasedir(self, path, fh):
# return os.close(fh)
removexattr = None
# def removexattr(self, path, name):
# pass
def rename(self, old, new):
_old = old[1:]
_new = new[1:]
uid, gid, pid = fuse_get_context()
username = pwd.getpwuid(uid)[0]
dispatch(request(['--cwd', self.tmp, 'mv', _old, _new]))
dispatch(request(['--cwd', self.tmp, 'mv', os.path.join('.hgfs', _old + '.attr'), os.path.join('.hgfs', _new + '.attr')]))
dispatch(request(['--cwd', self.tmp, 'commit', '-u', username, '-m', "hgfs[rename]: %s -> %s" % (_old, _new), str(_old), str(_new)]))
if self.args.clone: dispatch(request(['--cwd', self.tmp, 'push']))
return 0
def rmdir(self, path):
_path = path[1:]
apath = os.path.join(self.tmp, _path)
dpath = os.path.join(self.tmp, '.hgfs', _path)
hpath = os.path.join(self.tmp, '.hgfs', _path + '.attr')
try:
status = os.rmdir(dpath)
except:
pass
status = os.rmdir(apath)
try:
status = os.unlink(hpath)
except:
pass
uid, gid, pid = fuse_get_context()
username = pwd.getpwuid(uid)[0]
dispatch(request(['--cwd', self.tmp, 'commit', '-A', '-u', username, '-m', "hgfs[rmdir]: %s" % (_path), str(_path)]))
if self.args.clone: dispatch(request(['--cwd', self.tmp, 'push']))
return status
setxattr = None
# def setxattr(self, path, name, value, options, position=0):
# pass
def statfs(self, path):
_path = path[1:]
apath = os.path.join(self.tmp, _path)
stv = os.statvfs(apath)
return dict((key, getattr(stv, key)) for key in self.STATV)
def symlink(self, target, source):
_source = source[1:]
_target = target[1:]
asource = os.path.join(self.tmp, _source)
atarget = os.path.join(self.tmp, _target)
status = os.symlink(source, atarget)
uid, gid, pid = fuse_get_context()
username = pwd.getpwuid(uid)[0]
dispatch(request(['--cwd', self.tmp, 'commit', '-A', '-u', username, '-m', "hgfs[symlink]: %s -> %s" % (_target, source), str(source), str(_target)]))
if self.args.clone: dispatch(request(['--cwd', self.tmp, 'push']))
return status
# FIXME: You have to have this here, otherwise, fusepy won't call your
# truncate with the fh set. This causes certain cases to fail where a file
# was opened with write, but mode set to 0000. In that case you should NOT
# be able to reopen the file, but you SHOULD be able to truncate the
# existing handle. See iozone sanity check.
def ftruncate(self, path, length, fh):
pass
def truncate(self, path, length, fh=None):
_path = path[1:]
apath = os.path.join(self.tmp, _path)
if fh == None:
with open(apath, 'wb') as f:
pass
else:
os.ftruncate(fh, length)
uid, gid, pid = fuse_get_context()
username = pwd.getpwuid(uid)[0]
dispatch(request(['--cwd', self.tmp, 'commit', '-A', '-u', username, '-m', "hgfs[truncate]: %s" % (_path), str(_path)]))
if self.args.clone: dispatch(request(['--cwd', self.tmp, 'push']))
def unlink(self, path):
_path = path[1:]
apath = os.path.join(self.tmp, _path)
hpath = os.path.join(self.tmp, '.hgfs', _path + '.attr')
status = os.unlink(apath)
try:
status = os.unlink(hpath)
except:
pass
uid, gid, pid = fuse_get_context()
username = pwd.getpwuid(uid)[0]
dispatch(request(['--cwd', self.tmp, 'commit', '-A', '-u', username, '-m', "hgfs[unlink]: %s" % (_path), str(_path)]))
if self.args.clone: dispatch(request(['--cwd', self.tmp, 'push']))
return status
def utimens(self, path, times=None):
apath = os.path.join(self.tmp, path[1:])
return os.utime(apath, times)
def write(self, path, data, offset, fh):
_path = path[1:]
apath = os.path.join(self.tmp, _path)
os.lseek(fh, offset, os.SEEK_SET)
os.write(fh, data)
uid, gid, pid = fuse_get_context()
username = pwd.getpwuid(uid)[0]
dispatch(request(['--cwd', self.tmp, 'commit', '-A', '-u', username, '-m', "hgfs[write]: %s" % (_path), str(_path)]))
if self.args.clone: dispatch(request(['--cwd', self.tmp, 'push']))
return len(data)
if __name__ == '__main__':
logging.basicConfig()
parser = argparse.ArgumentParser(description='HgFS')
parser.add_argument('repository', help='Mercurial repository specifier.')
parser.add_argument('mountpoint', help='Location of mount point.')
parser.add_argument('-c', '--clone', help='Clone repository. If set to false, then repository MUST be local.', action='store', default='True')
parser.add_argument('-l', '--log', help='Set the log level.', action='store', choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], default='ERROR')
args = parser.parse_args()
args.clone = args.clone in ('True', 'true', '1')
fuse = FUSE(HgFS(args.repository, args.mountpoint, args), args.mountpoint, foreground=True, nothreads=True)