-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_functions.py
102 lines (77 loc) · 2.02 KB
/
server_functions.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
import os.path
import errno
MOUNTED_DIRECTORY = "/tmp/moshfs"
FH_TABLE = {}
def fixpath(path):
if path.startswith("/"):
path = path[1:]
return os.path.join(MOUNTED_DIRECTORY, path)
def getattr(path):
try:
return os.lstat(fixpath(path))
except OSError:
return -errno.ENOENT
def readdir(path):
return os.listdir(fixpath(path))
def unlink(path):
os.unlink(fixpath(path))
def mkdir(path, mode):
os.mkdir(fixpath(path), mode)
def rmdir(path):
os.rmdir(fixpath(path))
def readlink(path):
return os.readlink(fixpath(path))
def symlink(path, path1):
os.symlink(fixpath(path), fixpath(path1))
def link(path, path1):
os.link(fixpath(path), fixpath(path1))
def rename(path, path1):
os.rename(fixpath(path), fixpath(path1))
def utime(path, times):
os.utime(fixpath(path), times)
def flag2mode(flags):
md = {os.O_RDONLY: 'r', os.O_WRONLY: 'w', os.O_RDWR: 'w+'}
m = md[flags & (os.O_RDONLY | os.O_WRONLY | os.O_RDWR)]
if flags & os.O_APPEND:
m = m.replace('w', 'a', 1)
return m
def open(path, flags, mode):
fh = os.fdopen(os.open(fixpath(path), flags, mode),
flag2mode(flags))
fd = fh.fileno()
FH_TABLE[fd] = fh
return fd
# these are relative a fileno
def read(fd, length, offset):
fh = FH_TABLE[fd]
fh.seek(offset)
return fh.read(length)
def write(fd, buf, offset):
fh = FH_TABLE[fd]
fh.seek(offset)
fh.write(buf)
return len(buf)
def release(fd):
fh = FH_TABLE[fd]
fh.close()
del FH_TABLE[fd]
def _fflush(fh):
if 'w' in fh.mode or 'a' in fh.mode:
fh.flush()
def fsync(fd, isfsyncfile):
fh = FH_TABLE[fd]
_fflush(fh)
if isfsyncfile and hasattr(os, 'fdatasync'):
os.fdatasync(fh.fileno())
else:
os.fsync(fh.fileno())
def flush(fd):
fh = FH_TABLE[fd]
_fflush(fh)
os.close(os.dup(fh.fileno()))
def fgetattr(fd):
fh = FH_TABLE[fd]
return os.fstat(fh.fileno())
def ftruncate(fd, len):
fh = FH_TABLE[fd]
fh.truncate(len)