-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisk.py
More file actions
31 lines (24 loc) · 897 Bytes
/
disk.py
File metadata and controls
31 lines (24 loc) · 897 Bytes
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
#!/usr/bin/env python3
import os
class Disk:
def __init__(self, path, block_size=None):
self.path = path
self.fd = os.open(path, os.O_RDWR)
self.block_size = block_size or 4096 # may be updated by layout.Superblock.load()
def close(self):
if self.fd is not None:
os.close(self.fd)
self.fd = None
def read_at(self, offset, length):
return os.pread(self.fd, length, offset)
def write_at(self, offset, data):
return os.pwrite(self.fd, data, offset)
def read_block(self, blkno):
off = blkno * self.block_size
return self.read_at(off, self.block_size)
def write_block(self, blkno, data):
assert len(data) == self.block_size, "must write full block"
off = blkno * self.block_size
return self.write_at(off, data)
def fsync(self):
os.fsync(self.fd)