-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.py
78 lines (66 loc) · 2.12 KB
/
util.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
import json
from Fs import Fs
import os
from inode import Inode
from block import Block
from super_block import SuperBlock
import enum
import constants
false = False
null = None
class FileType(enum.Enum):
regular = 1
dir = 2
def load(bytearr):
stringfied = str(bytearr)
null_index = find_null_index(stringfied)
if null_index != -1:
stringfied = stringfied[:null_index]
return eval(json.loads(json.dumps(stringfied)))
def save(bytearr, offset, filepath):
with open(filepath, "rw+") as disk:
disk.seek(offset, os.SEEK_SET)
disk.write(bytearr)
disk.close()
def read(filepath, offset, size):
with open(filepath, "rw+") as disk:
disk.seek(offset, os.SEEK_SET)
result = disk.read(size)
disk.close()
return result
def instance_from_obj(class_constructor, obj):
instance = class_constructor()
for key in obj:
setattr(instance, key, obj[key])
return instance
def get_sb():
sb_bytearr = read('disk', 0, constants.SUPER_BLOCK_SIZE)
sb_json = load(sb_bytearr)
sb_instance = instance_from_obj(SuperBlock, sb_json)
return sb_instance
def get_inode(number):
inode_bytearr = read('disk', constants.SUPER_BLOCK_SIZE + (number * constants.INODE_SIZE), constants.INODE_SIZE)
inode_json = load(inode_bytearr)
inode_instance = instance_from_obj(Inode, inode_json)
return inode_instance
def get_block(number):
offset = (constants.SUPER_BLOCK_SIZE + (constants.INODES_AMOUNT + 1) * constants.INODE_SIZE) + number * constants.BLOCK_SIZE
block_bytearr = read('disk', offset, constants.BLOCK_SIZE)
block_json = load(block_bytearr)
block_instance = instance_from_obj(Block, block_json)
return block_instance
def find_null_index(string):
for i in xrange(len(string)):
if string[i] == '\x00':
return i
return -1
def create_empty_inode(number):
inode = Inode()
inode.number = number
inode.size = 4096*12
save(inode.bytefy(), inode.get_offset(), 'disk')
def create_empty_block(number):
block = Block()
block.number = number
block.size = 4096
save(block.bytefy(), block.get_offset(), 'disk')