def __init__(self, fs, name): # Get the inode of the parent or raise ParentDirectoryMissing exception try: self.parent = send('Dir._Get_Inode', path=self.parent)[0][1] inode = send('Dir._Get_Inode', path=name, inode=self.parent)[0][1] except (FileNotFoundError, ParentDirectoryMissing): inode = None # If inode is a dir, raise error if inode and fs.db.Get_Mode(inode=inode) == S_IFDIR: raise IsADirectoryError(name) # raise IsADirectoryError(path) BaseFile.__init__(self, fs, inode) self.name = name
def remove(self): """Remove a file from the filesystem. :raises ParentDirectoryMissingError: if a containing directory is missing and recursive is False :raises ResourceInvalidError: if the path is a directory or a parent path is an file :raises ResourceNotFoundError: if the path is not found """ # # Get inode and name from path if self._inode == None: raise ResourceNotFoundError(self.path) # Unlink dir entry send('FS.unlink', parent_dir=self.parent, name=self.name) self._inode = None self._offset = 0
def __init__(self, fs, path): self.path = path self.parent, self.name = split(path) self.fs = fs # Get the inode of the parent or raise ParentDirectoryMissing exception try: self.parent = send('Dir._Get_Inode', path=self.parent)[0][1] inode = send('Dir._Get_Inode', path=self.name, inode=self.parent)[0][1] except (FileNotFoundError, ParentDirectoryMissing): inode = None Inode.__init__(self, inode) # If inode is not a dir, raise error if inode and fs.db.Get_Mode(inode=inode) != S_IFDIR: raise NotADirectoryError(path)
def _Path2InodeName(self, path): # OK ''' Get the parent dir inode and the name of a dir entry defined by path ''' path, name = split(path) try: inode = send('Dir._Get_Inode', path=path)[0][1] except FileNotFoundError: raise ParentDirectoryMissing(path) return inode, name
def _list(self): """Lists the files and directories under a given path. The directory contents are returned as a generator of unicode paths. @rtype: generator of paths @raise DirNotFoundError: directory doesn't exists """ if self._inode == None: raise FileNotFoundError(self.path) plugins.send("Dir.list begin") # yield unicode('.') # yield unicode('..') for direntry in self.db.read(parent_dir=self._inode, limit= -1): if direntry.name: yield unicode(direntry.name) plugins.send("Dir.list end")
def isfile(self, path): # OK """Check if a path references a file :param path: a path in the filessystem :rtype: bool """ try: inode = send('Dir._Get_Inode', path=path)[0][1] except (IsADirectoryError, NotADirectoryError, FileNotFoundError, ParentDirectoryMissing): return False return self.db.Get_Mode(inode=inode) != S_IFDIR
def __init__(self, db_file, drive, db_dirPath=None): BaseFS.__init__(self, db_file, db_dirPath) base.FS.__init__(self) for receiver, response in send("FS.__init__", self, db_file=db_file, drive_file=drive): recv_name = receiver.im_self.__class__.__name__ if recv_name == 'DirPlugin': self.dir_class = response elif recv_name == 'FilePlugin': self.file_class = response self._delegate_methods(self.dir_class, self._dir_class_map) self._delegate_methods(self.file_class, self._file_class_map)
def make(self, recursive=False, allow_recreate=False): """Make a directory on the filesystem. @param recursive: if True, any intermediate directories will also be created @type recursive: bool @param allow_recreate: if True, recreating a directory wont be an error @type allow_create: bool :raises DestinationExistsError: if the path is already a directory, and allow_recreate is False """ send("Dir.make.begin") # Check if direntry exist and we can recreate it if so happens if self._inode: if allow_recreate: return raise DestinationExistsError(self.path) # Get parent dir if isinstance(self.parent, basestring): if not recursive: raise ParentDirectoryMissingError(self.path) # Parents doesn't exist, they are the Three Wise men ;-) # but we want to create them anyway to get their inode d = Dir(self.fs, self.parent) d.make(True) self.parent = d._inode # Make directory self._inode = self.db._Make_Inode(type=S_IFDIR) self.db.link(parent_dir=self.parent, name=self.name, child_entry=self._inode) send("Dir.make.end")
def freespace(self): freespace = 0 for _, space in send("FS.freespace"): freespace += space return freespace
def remove(self, recursive=False, force=False): """Remove a directory from the filesystem @param recursive: if True, then empty parent directories will be removed @type recursive: bool @param force: if True, any directory contents will be removed @type force: bool @raise DirectoryNotEmptyError: if the directory is not empty and force is False @raise ResourceNotFoundError: if the directory not exists """ if self.path == '/': raise RemoveRootError() if not self._inode: raise ResourceNotFoundError(self.path) # Force dir deletion if force: for direntry in self.db.read(parent_dir=self._inode, limit= -1): path = join(self.path, direntry.name) try: inode = send('Dir._Get_Inode', path=path)[0][1] # Path doesn't exist, probably because it was removed by # another thead while we were getting the entries in this one. # Since in any case we are removing it, we can ignore the # exception except FileNotFoundError: pass else: # Entry is from a directory, delete it recursively if self.db.Get_Mode(inode=inode) == S_IFDIR: d = Dir(self.fs, path) d.remove(force=True) # Entry is from a file, ask to filesystem to delete it else: self.fs.remove(path) # If dir is not empty raise error if self.db.read(parent_dir=self._inode, limit= -1): raise DirectoryNotEmptyError(self.path) # Removed directory self.db.unlink(parent_dir=self.parent, name=self.name) self._inode = None # Delete parent dirs recursively if empty if recursive: path = self.path.rpartition(sep)[0] d = Dir(self.fs, path) try: d.remove(True) except (DirectoryNotEmptyError, ResourceNotFoundError): pass send("Dir.remove")
def _db_link(self): send("FS.link", parent_dir=self.parent, name=self.name, child_entry=self._inode)