-
Notifications
You must be signed in to change notification settings - Fork 0
/
inode.py
134 lines (108 loc) · 3.3 KB
/
inode.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
from stat import *
from data import *
class Inode():
def __init__(self, create_inode, name, mode=None,
ctime=None, atime=None, mtime=None,
uid=None, gid=None):
self.name = name
self.inodes = [] # list of inodes in that directory
if create_inode:
self.inode = _Inode(
mode,
ctime,
atime,
mtime,
uid,
gid
)
def set_inode(self, new_inode):
self.inode = new_inode
def get_inode(self):
return self.inode
def set_name(self, new_name):
self.name = new_name
def set_ctime(self, ctime):
if self.inode:
self.inode.ctime = ctime
def set_atime(self, atime):
if self.inode:
self.inode.atime = atime
def set_mtime(self, mtime):
if self.inode:
self.inode.mtime = mtime
# used to set permission bits
def set_mode(self, mode):
if self.inode:
self.inode.mode &= 0o770000
self.inode.mode |= mode
def set_uid(self, uid):
if self.inode:
self.inode.uid = uid
def set_gid(self, gid):
if self.inode:
self.inode.gid = gid
def get_inode_info(self):
if self.inode:
return self.inode.get_inode_info()
def get_inode_type(self):
if self.inode:
return S_IFMT(self.inode.mode)
def get_data(self):
if self.inode:
return self.inode.get_data()
def set_data(self, new_data):
if self.inode:
return self.inode.set_data(new_data)
def get_nlink(self):
if self.inode:
return self.inode.nlink
def dec_nlink(self):
if self.inode:
self.inode.nlink -= 1
def inc_nlink(self):
if self.inode:
self.inode.nlink += 1
class _Inode():
def __init__(self, mode, ctime, atime,
mtime, uid, gid):
self.mode = mode
self.ctime = ctime
self.atime = atime
self.mtime = mtime
self.uid = uid
self.gid = gid
self.data_blocks = self.get_data_class()
if S_IFMT(self.mode) in [S_IFREG, S_IFLNK]:
self.nlink = 1
elif S_IFMT(self.mode) == S_IFDIR:
self.nlink = 2
def get_data_class(self):
# no data stored for directory.
# the inode list is enough data for the directory
if S_IFMT(self.mode) == S_IFREG:
return FileData()
elif S_IFMT(self.mode) == S_IFLNK:
return SymlinkData()
elif S_IFMT(self.mode) == S_IFDIR:
return DirData()
def get_data(self):
return self.data_blocks.get_data()
def set_data(self, new_data):
self.data_blocks.set_data(new_data)
def get_size(self):
return self.data_blocks.get_size()
'''
This is for the getattrs
'''
def get_inode_info(self):
return dict(
st_mode = self.mode,
st_ctime = self.ctime,
st_atime = self.atime,
st_mtime = self.mtime,
st_nlink = self.nlink,
st_size = self.get_size(),
st_uid = self.uid,
st_gid = self.gid,
st_blocks = int(self.get_size() / 512) + 1
)