-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayout.py
More file actions
265 lines (225 loc) · 8.89 KB
/
layout.py
File metadata and controls
265 lines (225 loc) · 8.89 KB
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
#!/usr/bin/env python3
import struct
from dataclasses import dataclass, field
from disk import Disk
from transaction import Transaction
from typing import Optional
from enum import IntFlag
import time
MAGIC = b"WAYNE_FS"
SB_FMT = "<8sIIIIIIIIIIIII"
SB_SIZE = struct.calcsize(SB_FMT)
INODE_SIZE = 128
class InodeMode(IntFlag):
# 檔案型別(高位 bits)
S_IFMT = 0xF000
S_IFSOCK = 0xC000
S_IFLNK = 0xA000
S_IFREG = 0x8000
S_IFBLK = 0x6000
S_IFDIR = 0x4000
S_IFCHR = 0x2000
S_IFIFO = 0x1000
S_INIT = 0x0000
# 權限位(低 9 bits,類似 0o755, 0o644)
S_IRUSR = 0o400
S_IWUSR = 0o200
S_IXUSR = 0o100
S_IRGRP = 0o040
S_IWGRP = 0o020
S_IXGRP = 0o010
S_IROTH = 0o004
S_IWOTH = 0o002
S_IXOTH = 0o001
@dataclass
class Inode:
mode: int = 0 # 4
nlink: int = 0 # 4
size: int = 0 # 8
ctime: int = 0 # 8
mtime: int = 0 # 8
atime: int = 0 # 8
direct: list = field(default_factory=lambda: [0]*12)
def pack(self) -> bytearray:
data = bytearray()
data += struct.pack("<I", self.mode)
data += struct.pack("<I", self.nlink)
data += struct.pack("<Q", self.size)
data += struct.pack("<Q", self.ctime)
data += struct.pack("<Q", self.mtime)
data += struct.pack("<Q", self.atime)
for idx in range(12):
data += struct.pack("<I", self.direct[idx])
return data
@classmethod
def empty(cls, mode: int):
now = int(time.time())
return cls(mode, 0, 0, now, now, now, [0]*12)
@classmethod
def unpack(cls, raw):
off = 0
mode = struct.unpack_from("<I", raw, off)[0]; off += 4
nlink = struct.unpack_from("<I", raw, off)[0]; off += 4
size = struct.unpack_from("<Q", raw, off)[0]; off += 8
ctime = struct.unpack_from("<Q", raw, off)[0]; off += 8
mtime = struct.unpack_from("<Q", raw, off)[0]; off += 8
atime = struct.unpack_from("<Q", raw, off)[0]; off += 8
direct = [0] * 12
for idx in range(12):
direct[idx] = struct.unpack_from("<I", raw, off)[0]; off += 4
return cls(mode, nlink, size, ctime, mtime, atime, direct)
@dataclass
class Superblock:
magic: int
block_size: int
total_blocks: int
inode_count: int
# --- Inode BMP ---
inode_bitmap_start: int
inode_bitmap_blocks: int
# --- Block BMP ---
block_bitmap_start: int
block_bitmap_blocks: int
# --- Inode Table ---
inode_table_start: int
inode_table_blocks: int
# --- Jouranl Area ---
journal_area_start: int
journal_area_total_blocks: int
# --- Data ---
data_start: int
@classmethod
def load(cls, disk: Disk):
raw = disk.read_block(0)
fields = struct.unpack(SB_FMT, raw[:SB_SIZE])
magic = fields[0]
if magic != MAGIC:
raise RuntimeError("Bad superblock magic; did you run mktoyfs.py?")
( magic,
block_size,
total_blocks,
inode_count,
inode_bitmap_start,
inode_bitmap_blocks,
block_bitmap_start,
block_bitmap_blocks,
inode_table_start,
inode_table_blocks,
journal_area_start,
journal_area_total_blocks,
data_start,
_reserved) = fields
disk.block_size = block_size # sync disk view
return cls(magic, block_size, total_blocks, inode_count,
inode_bitmap_start, inode_bitmap_blocks,
block_bitmap_start, block_bitmap_blocks,
inode_table_start, inode_table_blocks,
journal_area_start, journal_area_total_blocks,
data_start)
class DictEnDecoder:
def pack_dir(entries):
"""
entries: List[Tuple[int, str]] e.g. [(0, "."), (0, "..")]
return: bytes
"""
data = bytearray()
for ino, name in entries:
name_b = name.encode("utf-8")
# Packed:ino, name_length, name
entry_packed = struct.pack(f"<IH{len(name_b)}s", ino, len(name_b), name_b)
data.extend(entry_packed)
# Add all length
header = struct.pack("<I", len(data))
# DEBUG print
#print(f"[pack_dir] Total entries: {len(entries)}. Total data length: {len(data)}. Packed size: {len(header) + len(data)}")
return bytes(header + data)
@staticmethod
def unpack_dir(raw: bytes) -> list[tuple[int, str]]:
"""
raw: bytes of a directory file
return: List[Tuple[int, str]]
"""
#print("\n--- [unpack_dir] Starting Directory Unpack ---")
if not raw or len(raw) < 4:
#print("[unpack_dir] Error: Raw data is empty or too short for header.")
return []
try:
# 1. Read all data length
total_len, = struct.unpack_from("<I", raw, 0)
#print(f"[unpack_dir] Header reports total data length: {total_len} bytes.")
if total_len == 0:
#print("[unpack_dir] Header reports zero length. Directory is empty.")
return []
# 2. Data handle
effective_data_end = 4 + total_len
if effective_data_end > len(raw):
#print(f"[unpack_dir] Warning: Reported length ({total_len}) is greater than available raw data ({len(raw)-4}). Truncating.")
effective_data_end = len(raw)
data_slice = raw[4:effective_data_end]
#print(f"[unpack_dir] Sliced effective data of length: {len(data_slice)} bytes.")
except struct.error as e:
#print(f"[unpack_dir] Error reading header: {e}")
return []
out = []
offset = 0
entry_count = 1
while offset < len(data_slice):
#print(f"\n[unpack_dir] --- Parsing Entry #{entry_count} at offset {offset} ---")
try:
header_size = struct.calcsize("<IH") # 6 bytes
if offset + header_size > len(data_slice):
#print(f"[unpack_dir] Error: Not enough data left for entry header. Remaining: {len(data_slice)-offset} bytes.")
break
# 3. Read (inode, name length)
ino, nlen = struct.unpack_from("<IH", data_slice, offset)
#print(f"[unpack_dir] Entry Header: ino={ino}, name_length={nlen}")
# 4. Check valid
if offset + header_size + nlen > len(data_slice):
#print(f"[unpack_dir] Error: Name length ({nlen}) exceeds remaining data boundary.")
break
# 5. Read file name
name_bytes, = struct.unpack_from(f"<{nlen}s", data_slice, offset + header_size)
name = name_bytes.decode("utf-8")
#print(f"[unpack_dir] Entry Data: name='{name}'")
out.append((ino, name))
# 6. Move next
offset += header_size + nlen
entry_count += 1
except (struct.error, UnicodeDecodeError) as e:
#print(f"[unpack_dir] CRITICAL ERROR during entry parsing: {e}. Aborting.")
break
#print(f"--- [unpack_dir] Finished Unpack. Found {len(out)} entries: {out} ---\n")
return out
class InodeTable:
def __init__(self, disk: Disk, sb: Superblock, inode_size: int = 128):
self.disk = disk
self.sb = sb
self.inode_size = inode_size
self.inodes_per_block = self.sb.block_size // inode_size
def __inode_offset(self, idx: int) -> int:
return self.sb.inode_table_start * self.sb.block_size + idx * self.inode_size
def read(self, ino: int) -> Inode:
off = self.__inode_offset(ino)
raw = self.disk.read_at(off, self.inode_size)
return Inode.unpack(raw)
def write(self, ino: int, inode: Inode, tx: Optional[Transaction] = None):
block_addr = self.sb.inode_table_start + (ino // self.inodes_per_block)
offset_in_block = (ino % self.inodes_per_block) * self.inode_size
if tx and block_addr in tx.write_buffer:
_block_type, block_data = tx.write_buffer[block_addr]
full_block_data = bytearray(block_data)
else:
full_block_data = bytearray(self.disk.read_block(block_addr))
if tx:
full_block_data[offset_in_block : offset_in_block + self.inode_size] = inode.pack().ljust(self.inode_size, b'\x00')
print(f"[Debug] Inode Table block addr = {block_addr} off = {offset_in_block} ino = {ino}")
tx.write(block_addr, bytes(full_block_data), "Inode Table")
else:
off = self.__inode_offset(ino)
self.disk.write_at(off, inode.pack())
@dataclass
class OpenFileState:
ino: int
flags: int
offset: int
def ceil_div(a, b): return (a + b - 1) // b