This repository was archived by the owner on Apr 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilesystem.py
135 lines (108 loc) · 3.54 KB
/
filesystem.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
from uio import FileIO
import errno
import ocpath
from component import Component
forced_cd = False
class FileSystem:
def __init__(self, address, root=False):
self.fs = Component(address)
self.address = address
self.readonly = self.fs.isReadOnly()
self.cwd = "/" if root else ''
self.root = root
# noinspection PyUnusedLocal
def mount(self, readonly, mkfs):
self.readonly = self.fs.isReadOnly() or readonly
def umount(self):
pass
def ilistdir(self, dir):
for name in self.fs.list(dir):
if self.fs.isDirectory(dir + "/" + name):
yield (name, 0x4000, 0, -1)
else:
size = self.fs.size(name)
yield (name, 0x8000, 0, size)
def guard_readonly(self):
if self.readonly:
raise OSError(errno.EPERM)
def chdir(self, dir):
path = ocpath.join(self.cwd, dir)
path = ocpath.normpath(path)
path = path.rstrip(ocpath.sep)
if not path:
path = "/" if self.root else "'"
elif '..' == path or ('..' + ocpath.sep) in path:
path = "/" if self.root else ""
if not forced_cd and not self.fs.isDirectory(path):
raise OSError(errno.ENOTDIR)
self.cwd = path
def getcwd(self):
return self.cwd
def mkdir(self, path):
self.guard_readonly()
result = self.fs.makeDirectory(path)
if not result:
exists = self.fs.exists(path)
if self.fs.isDirectory(path):
raise OSError(errno.EISDIR)
elif exists: # file
raise OSError(errno.EEXIST)
raise OSError(errno.ENOENT)
def remove(self, path):
self.guard_readonly()
self.fs.remove(path)
def rename(self, old_path, new_path):
self.guard_readonly()
result = self.fs.rename(old_path, new_path)
if not result:
raise OSError(errno.ENOENT)
def rmdir(self, path):
self.guard_readonly()
if not self.fs.isDirectory(path):
if self.fs.exists(path):
# is file
raise OSError(errno.EEXIST)
raise OSError(errno.ENOENT)
result = self.fs.remove(path)
if not result:
raise OSError(errno.ENOENT)
def stat(self, path):
if not self.fs.exists(path):
raise OSError(errno.ENOENT)
is_dir = self.fs.isDirectory(path)
size = self.fs.size(path) if not is_dir else 0
mtime = self.fs.lastModified(path)
return (
0x4000 if is_dir else 0x8000, # st_mode
hash(path), # st_ino
hash(self.address), # dev
1, # nlink
0, # uid: root
0, # gid: root
size, # size
mtime, # atime
mtime, # mtime
mtime, # ctime
)
# noinspection PyUnusedLocal
def statvfs(self, path):
used = self.fs.spaceUsed()
total = self.fs.spaceTotal()
free = total - used
return (
1, # f_bsize
1, # f_frsize
used, # f_blocks
free, # f_bfree
free, # f_bavail
used, # f_files
free, # f_ffree
free, # f_favail
0, # f_flag
256, # f_namemax
)
def open(self, file, mode):
# TODO: nomalize mode
return FileIO(self.fs.address, file, mode)
def __repr__(self):
return "<FileSystem: {!r}>".format(self.address)