def open(self, path, isdir=False): inode_no = self.search_dir(path) if inode_no is None: raise Exception("path does not exist") if isdir: return Directory(inode_no) else: return File(inode_no)
def unlink(self, path): filename = find_filename(path) parent_inode_no = self.search_dir(find_parent(path)) parent_inode_block = inodemap.inodemap.lookup(parent_inode_no) parent_inode = Inode(data=segment.segman.block_read(parent_inode_block)) parent_old_size = parent_inode.filesize parent_dir = Directory(parent_inode_no) found_entry = False entries = [] for (name, inode) in parent_dir.enumerate(): if found_entry: entries.append((name, inode)) if name == filename: fount = True position = parent_dir.position - (FILENAMELEN + 4) for (name, inode) in entries: parent_inode.write(position, struct.pack("%dsI" % FILENAMELEN, name, inode)) position += FILENAMELEN + 4 parent_inode.filesize = parent_old_size - (FILENAMELEN + 4) inodemap.inodemap.update_inode(parent_inode_no, parent_inode.serialize())
def search_dir(self, path): # print map(ord, path) path = path.strip() if path == "/": return 1 current_dir = Directory(1) stack = path.split("/")[1:] stack.reverse() while True: name = stack.pop() found = False for (n, inode) in current_dir.enumerate(): if n == name: found = True break if found: if len(stack) == 0: return inode else: current_dir = Directory(inode) else: return None
def search_dir(self, path): #print map(ord, path) path = path.strip() if path == "/": return 1 current_dir = Directory(1) stack = path.split("/")[1:] stack.reverse() while True: name = stack.pop() found = False for (n, inode) in current_dir.enumerate(): if n == name: found = True break if found: if len(stack) == 0: return inode else: current_dir = Directory(inode) else: return None
def unlink(self, path): filename = find_filename(path) parent_inode_no = self.search_dir(find_parent(path)) parent_inode_block = inodemap.inodemap.lookup(parent_inode_no) parent_inode = Inode( data=segment.segman.block_read(parent_inode_block)) parent_old_size = parent_inode.filesize parent_dir = Directory(parent_inode_no) found_entry = False entries = [] for (name, inode) in parent_dir.enumerate(): if found_entry: entries.append((name, inode)) if name == filename: fount = True position = parent_dir.position - (FILENAMELEN + 4) for (name, inode) in entries: parent_inode.write(position, struct.pack("%dsI" % FILENAMELEN, name, inode)) position += (FILENAMELEN + 4) parent_inode.filesize = parent_old_size - (FILENAMELEN + 4) inodemap.inodemap.update_inode(parent_inode_no, parent_inode.serialize())
def create(self, filename, isdir=False): inode_no = self.search_dir(filename) if inode_no is not None: raise Exception("file already exists") inode = Inode(isdir=isdir) parent_dir = find_parent(filename) parent_inode_no = self.search_dir(parent_dir) if parent_inode_no is None: raise Exception("parent direntory does not exist") parent_inode_block = inodemap.inodemap.lookup(parent_inode_no) parent_inode = Inode( data=segment.segman.block_read(parent_inode_block)) self.append_entry(parent_inode, find_filename(filename), inode) if isdir: return Directory(inode.id) else: return File(inode.id)