Disk Abstraction Layer 3 provides a more useful framework for secondary memory and provides an abstract interface for implementing a generic file system. It renumbers the blocks on the hard drive to start at block 1 so that block 0 can be considered a NULL reference. At a lower level, block 0 would be seen to be where the OS can keep a seed for its random number generator (interface provided).
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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | import dal_2
from math import ceil
################################################################################
class DAL2(dal_2.DAL2):
# Get Configuration Information
def info(self):
return self.__blocks, self.__disk.SIZE
# Change Status Information
def edit(self, block, status):
assert type(block) is int and 0 <= block < self.__blocks
assert type(status) is int and 1 <= status <= 255
assert self.__BIT[block] != 0
self.__BIT[block] = status
################################################################################
class Seed:
# Random Seed Generator
def __init__(self, data):
if type(data) is int:
assert 1 <= data <= 65536
self.__data = [0 for index in range(data)]
elif type(data) is str:
assert 1 <= len(data) <= 65536
self.__data = [ord(c) for c in data]
else:
raise TypeError
self.__active = True
self.__index = 0
# Seed Control Interface
def __call__(self, data=None):
if data is None:
return ''.join([chr(i) for i in self.__data])
elif type(data) is bool:
self.__active = data
elif type(data) is str:
if self.__active:
for c in data:
self.__data[self.__index] ^= ord(c)
self.__index = (self.__index + 1) % len(self.__data)
else:
raise TypeError
################################################################################
# Create Enumeration Objects
def enum(*names):
class enum:
def __setattr__(self, name, value):
raise AttributeError
def __delattr__(self, name):
raise AttributeError
temp = enum()
for value, name in enumerate(names):
temp.__dict__[name] = value
return temp
################################################################################
class DAL3:
# CONSTANTS
MAX_BLOCKS = (2 ** 16) - 2
MAX_SIZE = (2 ** 16)
STATUS = enum('closed', 'd1', 'dA', 'dB', 'dZ', \
'f1', 'fA', 'fB', 'fZ', 'b1', 's1')
# Disk Abstraction Layer
def __init__(self, blocks, size):
assert type(blocks) is int and 0 < blocks <= self.MAX_BLOCKS
assert type(size) is int and 4 < size <= self.MAX_SIZE
blocks += 1
assert int(ceil(float(blocks * (size + 1)) / size)) <= self.MAX_SIZE
self.__disk = DAL2(blocks, size)
self.__blocks, self.__size = self.__disk.info()
self.__blocks -= 1
self.__seed = Seed(self.__size)
self.__disk.open(self.STATUS.s1)
# Open A Directory
def open_directory(self):
return self.__disk.open(self.STATUS.d1)
# Read A Directory
def read_directory(self, block):
assert type(block) is int and 1 <= block <= self.__blocks
status = self.__disk.status(block)
assert self.STATUS.d1 <= status <= self.STATUS.dZ
if status == self.STATUS.d1:
temp = self.__disk.read(block)
size = (ord(temp[0]) << 8) + ord(temp[1])
return temp[2:size+2]
else:
while self.__disk.status(block) != self.STATUS.dA:
temp = self.__disk.read(block)
block = (ord(temp[0]) << 8) + ord(temp[1])
temp = self.__disk.read(block)
size = (ord(temp[0]) << 8) + ord(temp[1])
data = temp[2:-2]
block = (ord(temp[-2]) << 8) + ord(temp[-1])
while self.__disk.status(block) == self.STATUS.dB:
temp = self.__disk.read(block)
data += temp[2:-2]
block = (ord(temp[-2]) << 8) + ord(temp[-1])
temp = self.__disk.read(block)
return data + temp[2:size-len(data)+2]
# Write A Directory
def write_directory(self, block, data):
assert type(block) is int and 1 <= block <= self.__blocks
assert type(data) is str and len(data)
status = self.__disk.status(block)
assert self.STATUS.d1 <= status <= self.STATUS.dZ
self.__seed(data)
dir_map = [block]
if status == self.STATUS.dA:
temp = self.__disk.read(block)
block = (ord(temp[-2]) << 8) + ord(temp[-1])
dir_map = dir_map + [block]
while self.__disk.status(block) == self.STATUS.dB:
temp = self.__disk.read(block)
block = (ord(temp[-2]) << 8) + ord(temp[-1])
dir_map = dir_map + [block]
elif status == self.STATUS.dB:
temp = self.__disk.read(block)
dirA = (ord(temp[0]) << 8) + ord(temp[1])
dir_map = [dirA] + dir_map
dirZ = (ord(temp[-2]) << 8) + ord(temp[-1])
dir_map = dir_map + [dirZ]
while self.__disk.status(dirA) == self.STATUS.dB:
temp = self.__disk.read(dirA)
dirA = (ord(temp[0]) << 8) + ord(temp[1])
dir_map = [dirA] + dir_map
while self.__disk.status(dirZ) == self.STATUS.dB:
temp = self.__disk.read(dirZ)
dirZ = (ord(temp[-2]) << 8) + ord(temp[-1])
dir_map = dir_map + [dirZ]
elif status == self.STATUS.dZ:
temp = self.__disk.read(block)
block = (ord(temp[0]) << 8) + ord(temp[1])
dir_map = [block] + dir_map
while self.__disk.status(block) == self.STATUS.dB:
temp = self.__disk.read(block)
block = (ord(temp[0]) << 8) + ord(temp[1])
dir_map = [block] + dir_map
size = self.__size - 4
if len(data) % size:
blocks = len(data) / size + 1
else:
blocks = len(data) / size
blocks = [data[index*size:index*size+size] for index in range(blocks)]
while len(dir_map) > len(blocks):
self.__disk.close(dir_map[-1])
del dir_map[-1]
while len(dir_map) < len(blocks):
dir_map.append(self.__disk.open(255))
if len(blocks) == 1:
temp = len(data)
size = chr(temp >> 8) + chr(temp & 0xFF)
blocks[0] = size + blocks[0] + size
else:
temp = len(data)
size = chr(temp >> 8) + chr(temp & 0xFF)
temp = dir_map[1]
next = chr(temp >> 8) + chr(temp & 0xFF)
blocks[0] = size + blocks[0] + next
index = 1
while blocks[index] is not blocks[-1]:
temp = dir_map[index - 1]
last = chr(temp >> 8) + chr(temp & 0xFF)
temp = dir_map[index + 1]
next = chr(temp >> 8) + chr(temp & 0xFF)
blocks[index] = last + blocks[index] + next
index += 1
temp = dir_map[index - 1]
last = chr(temp >> 8) + chr(temp & 0xFF)
blocks[index] = last + blocks[index] + size
if len(blocks) == 1:
self.__disk.write(dir_map[0], blocks[0] + chr(0) * \
(self.__size - len(blocks[0])))
self.__disk.edit(dir_map[0], self.STATUS.d1)
else:
self.__disk.write(dir_map[0], blocks[0])
self.__disk.edit(dir_map[0], self.STATUS.dA)
index = 1
while blocks[index] is not blocks[-1]:
self.__disk.write(dir_map[index], blocks[index])
self.__disk.edit(dir_map[index], self.STATUS.dB)
index += 1
self.__disk.write(dir_map[index], blocks[index] + chr(0) * \
(self.__size - len(blocks[index])))
self.__disk.edit(dir_map[index], self.STATUS.dZ)
# Close A Directory
def close_directory(self, block):
assert type(block) is int and 1 <= block <= self.__blocks
status = self.__disk.status(block)
assert self.STATUS.d1 <= status <= self.STATUS.dZ
if status == self.STATUS.dA:
temp = self.__disk.read(block)
self.__disk.close(block)
block = (ord(temp[-2]) << 8) + ord(temp[-1])
while self.__disk.status(block) == self.STATUS.dB:
temp = self.__disk.read(block)
self.__disk.close(block)
block = (ord(temp[-2]) << 8) + ord(temp[-1])
self.__disk.close(block)
elif status == self.STATUS.dB:
temp = self.__disk.read(block)
self.__disk.close(block)
dirA = (ord(temp[0]) << 8) + ord(temp[1])
dirZ = (ord(temp[-2]) << 8) + ord(temp[-1])
while self.__disk.status(dirA) == self.STATUS.dB:
temp = self.__disk.read(dirA)
self.__disk.close(dirA)
dirA = (ord(temp[0]) << 8) + ord(temp[1])
self.__disk.close(dirA)
while self.__disk.status(dirZ) == self.STATUS.dB:
temp = self.__disk.read(dirZ)
self.__disk.close(dirZ)
dirZ = (ord(temp[-2]) << 8) + ord(temp[-1])
self.__disk.close(dirZ)
elif status == self.STATUS.dZ:
temp = self.__disk.read(block)
self.__disk.blose(block)
block = (ord(temp[0]) << 8) + ord(temp[1])
while self.__disk.status(block) == self.STATUS.dB:
temp = self.__disk.read(block)
self.__disk.close(block)
block = (ord(temp[0]) << 8) + ord(temp[1])
self.__disk.close(block)
else:
self.__disk.close(block)
# Open A File
def open_file(self):
return self.__disk.open(self.STATUS.f1)
# Read A File
def read_file(self, block):
assert type(block) is int and 1 <= block <= self.__blocks
status = self.__disk.status(block)
assert self.STATUS.f1 <= status <= self.STATUS.fZ
if status == self.STATUS.f1:
temp = self.__disk.read(block)
size = (ord(temp[0]) << 8) + ord(temp[1])
return temp[2:size+2]
else:
while self.__disk.status(block) != self.STATUS.fA:
temp = self.__disk.read(block)
block = (ord(temp[0]) << 8) + ord(temp[1])
temp = self.__disk.read(block)
size = (ord(temp[0]) << 8) + ord(temp[1])
data = temp[2:-2]
block = (ord(temp[-2]) << 8) + ord(temp[-1])
while self.__disk.status(block) == self.STATUS.fB:
temp = self.__disk.read(block)
data += temp[2:-2]
block = (ord(temp[-2]) << 8) + ord(temp[-1])
temp = self.__disk.read(block)
return data + temp[2:size-len(data)+2]
# Write A File
def write_file(self, block, data):
assert type(block) is int and 1 <= block <= self.__blocks
assert type(data) is str and len(data)
status = self.__disk.status(block)
assert self.STATUS.f1 <= status <= self.STATUS.fZ
self.__seed(data)
dir_map = [block]
if status == self.STATUS.fA:
temp = self.__disk.read(block)
block = (ord(temp[-2]) << 8) + ord(temp[-1])
dir_map = dir_map + [block]
while self.__disk.status(block) == self.STATUS.fB:
temp = self.__disk.read(block)
block = (ord(temp[-2]) << 8) + ord(temp[-1])
dir_map = dir_map + [block]
elif status == self.STATUS.fB:
temp = self.__disk.read(block)
dirA = (ord(temp[0]) << 8) + ord(temp[1])
dir_map = [dirA] + dir_map
dirZ = (ord(temp[-2]) << 8) + ord(temp[-1])
dir_map = dir_map + [dirZ]
while self.__disk.status(dirA) == self.STATUS.fB:
temp = self.__disk.read(dirA)
dirA = (ord(temp[0]) << 8) + ord(temp[1])
dir_map = [dirA] + dir_map
while self.__disk.status(dirZ) == self.STATUS.fB:
temp = self.__disk.read(dirZ)
dirZ = (ord(temp[-2]) << 8) + ord(temp[-1])
dir_map = dir_map + [dirZ]
elif status == self.STATUS.fZ:
temp = self.__disk.read(block)
block = (ord(temp[0]) << 8) + ord(temp[1])
dir_map = [block] + dir_map
while self.__disk.status(block) == self.STATUS.fB:
temp = self.__disk.read(block)
block = (ord(temp[0]) << 8) + ord(temp[1])
dir_map = [block] + dir_map
size = self.__size - 4
if len(data) % size:
blocks = len(data) / size + 1
else:
blocks = len(data) / size
blocks = [data[index*size:index*size+size] for index in range(blocks)]
while len(dir_map) > len(blocks):
self.__disk.close(dir_map[-1])
del dir_map[-1]
while len(dir_map) < len(blocks):
dir_map.append(self.__disk.open(255))
if len(blocks) == 1:
temp = len(data)
size = chr(temp >> 8) + chr(temp & 0xFF)
blocks[0] = size + blocks[0] + size
else:
temp = len(data)
size = chr(temp >> 8) + chr(temp & 0xFF)
temp = dir_map[1]
next = chr(temp >> 8) + chr(temp & 0xFF)
blocks[0] = size + blocks[0] + next
index = 1
while blocks[index] is not blocks[-1]:
temp = dir_map[index - 1]
last = chr(temp >> 8) + chr(temp & 0xFF)
temp = dir_map[index + 1]
next = chr(temp >> 8) + chr(temp & 0xFF)
blocks[index] = last + blocks[index] + next
index += 1
temp = dir_map[index - 1]
last = chr(temp >> 8) + chr(temp & 0xFF)
blocks[index] = last + blocks[index] + size
if len(blocks) == 1:
self.__disk.write(dir_map[0], blocks[0] + chr(0) * \
(self.__size - len(blocks[0])))
self.__disk.edit(dir_map[0], self.STATUS.f1)
else:
self.__disk.write(dir_map[0], blocks[0])
self.__disk.edit(dir_map[0], self.STATUS.fA)
index = 1
while blocks[index] is not blocks[-1]:
self.__disk.write(dir_map[index], blocks[index])
self.__disk.edit(dir_map[index], self.STATUS.fB)
index += 1
self.__disk.write(dir_map[index], blocks[index] + chr(0) * \
(self.__size - len(blocks[index])))
self.__disk.edit(dir_map[index], self.STATUS.fZ)
# Close A File
def close_file(self, block):
assert type(block) is int and 1 <= block <= self.__blocks
status = self.__disk.status(block)
assert self.STATUS.f1 <= status <= self.STATUS.fZ
if status == self.STATUS.fA:
temp = self.__disk.read(block)
self.__disk.close(block)
block = (ord(temp[-2]) << 8) + ord(temp[-1])
while self.__disk.status(block) == self.STATUS.fB:
temp = self.__disk.read(block)
self.__disk.close(block)
block = (ord(temp[-2]) << 8) + ord(temp[-1])
self.__disk.close(block)
elif status == self.STATUS.fB:
temp = self.__disk.read(block)
self.__disk.close(block)
dirA = (ord(temp[0]) << 8) + ord(temp[1])
dirZ = (ord(temp[-2]) << 8) + ord(temp[-1])
while self.__disk.status(dirA) == self.STATUS.fB:
temp = self.__disk.read(dirA)
self.__disk.close(dirA)
dirA = (ord(temp[0]) << 8) + ord(temp[1])
self.__disk.close(dirA)
while self.__disk.status(dirZ) == self.STATUS.fB:
temp = self.__disk.read(dirZ)
self.__disk.close(dirZ)
dirZ = (ord(temp[-2]) << 8) + ord(temp[-1])
self.__disk.close(dirZ)
elif status == self.STATUS.fZ:
temp = self.__disk.read(block)
self.__disk.blose(block)
block = (ord(temp[0]) << 8) + ord(temp[1])
while self.__disk.status(block) == self.STATUS.fB:
temp = self.__disk.read(block)
self.__disk.close(block)
block = (ord(temp[0]) << 8) + ord(temp[1])
self.__disk.close(block)
else:
self.__disk.close(block)
# Open A Block
def open_block(self):
return self.__disk.open(self.STATUS.b1)
# Read A Block
def read_block(self, block):
assert type(block) is int and 1 <= block <= self.__blocks
assert self.__disk.status(block) == self.STATUS.b1
return self.__disk.read(block)
# Write A Block
def write_block(self, block, data):
assert type(block) is int and 1 <= block <= self.__blocks
assert type(data) is str and len(data) == self.__size
assert self.__disk.status(block) == self.STATUS.b1
self.__seed(data)
self.__disk.write(block, data)
# Close A Block
def close_block(self, block):
assert type(block) is int and 1 <= block <= self.__blocks
assert self.__disk.status(block) == self.STATUS.b1
self.__disk.close(block)
# Get Status Information
def status(self, block):
assert type(block) is int and 1 <= block <= self.__blocks
return self.__disk.status(block)
# Seed Control Interface
def seed(self, data=None):
return self.__seed(data)
# Probability Of Failure
def fail(self, probability):
self.__disk.fail(probability)
# Dump To File
def dump(self, name):
self.__disk.write(0, self.__seed())
self.__disk.dump(name)
# Load From File
def load(self, name, abstract):
assert type(abstract) is bool
self.__disk.load(name, abstract)
self.__blocks, self.__size = self.__disk.info()
self.__blocks -= 1
assert 0 < self.__blocks <= self.MAX_BLOCKS
assert 4 < self.__size <= self.MAX_SIZE
self.__seed = Seed(self.__disk.read(0))
if abstract:
self.__soft()
else:
self.__hard()
# Fix All Errors
def __soft(self):
# Not Yet Implemented
pass
# Find Any Error
def __hard(self):
# Not Yet Implemented
pass
################################################################################
def test():
# Not Yet Implemented
pass
################################################################################
if __name__ == '__main__':
test()
|
This recipe is presented from a collection of recipes that outline the creation of a custom file system. dal_2.py can be found at http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/492210