import disk_sys class file: def __init__(self): self.disk = disk_sys.disk() def initialize(self): self.dirblock = self.disk.getblockref(0) self.freebitmap = self.disk.getblockref(1) self.freebitmap.setbyte(0,0xc0) # allocate first 2 blocks for file system for i in range(0,disk_sys.block.BlockSize,16): # count in 16 byte increments self.dirblock.setbyte(i+2,0) # zero block address byte 1 self.dirblock.setbyte(i+3,0) # zero block address byte 2 def create(self,fname): self.dirblock = self.disk.getblockref(0) for free in range(0,disk_sys.block.BlockSize,16): pntr = self.dirblock.getword(free+2) if pntr == 0: break else: print "*** I/O error: no free directory entries" return -1 for i in range(len(fname)): b = ord(fname[i]) self.dirblock.setbyte(free+i+4,b) index = self.getfreeblock() indexblock = self.disk.getblockref(index) for i in range(disk_sys.block.BlockSize): indexblock.setbyte(i,0) self.dirblock.setword(free+2,index) return 0 def open(self,fname): f = self.searchdir(fname) if f < 0: print "*** I/O error: cannot open file %s" % fname return -1 return [f,0] def writebyte(self,fd,byte): byte = byte & 0xff # for safety index = fd[0]; posn = fd[1] bn = posn / disk_sys.block.BlockSize # block number from origin offset = posn % disk_sys.block.BlockSize # byte number within block indexblock = self.disk.getblockref(index) indexadr = 2+2*bn if indexadr >= disk_sys.block.BlockSize: print "*** I/O error: no space in index table" return -1 pntr = indexblock.getword(indexadr) if pntr == 0: pntr = self.getfreeblock() indexblock.setword(indexadr,pntr) datablock = self.disk.getblockref(pntr) datablock.setbyte(offset,byte) posn = posn + 1 eof = indexblock.getword(0) if posn > eof: if posn > 65535: print "*** I/O error: no space in file" return -1 else: indexblock.setword(0,posn) fd[1] = posn return 0 def writestr(self,fd,str): for i in range(len(str)): self.writebyte(fd,ord(str[i])) def close(self): pass def allocateblock(self,n): nbyte = n / 8; nbit = n % 8 fb = self.freebitmap byte = fb.getbyte(nbyte) mask = 1 << (7-nbit); clearmask = ~ mask byte = byte ^ mask # set the relevant bit byte = byte & 255 # for safety fb.setbyte(nbyte,byte) def freeblock(self,n): nbyte = n / 8; nbit = n % 8 fb = self.freebitmap byte = fb.getbyte(nbyte) mask = 1 << (7-nbit); clearmask = ~ mask byte = byte & clearmask # reset the relevant bit byte = byte & 255 # for safety fb.setbyte(nbyte,byte) def getfreeblock(self): fb = self.freebitmap for i in range(disk_sys.block.BlockSize): byte = fb.getbyte(i) if byte < 255: break else: print "*** I/O error: No free disk blocks" byteadr = i nbyte = (~ byte) & 255; mask = 128; i = i*8 # find first 1 bit in nbyte from msb while nbyte & mask == 0: mask = mask >> 1 i = i + 1 byte = byte ^ mask # turn bit on byte = byte & 255 # for safety fb.setbyte(byteadr,byte) return i def searchdir(self,fname): self.dirblock = self.disk.getblockref(0) for direntry in range(0,disk_sys.block.BlockSize,16): pntr = self.dirblock.getword(direntry+2) if pntr != 0: dirfn = "" for j in range(12): byte = self.dirblock.getbyte(direntry+4+j) if byte == 0: break dirfn = dirfn + chr(byte) if fname == dirfn: break else: print "*** I/O error: cannot find file %s" % fname return -1 return pntr