Example #1
0
 def test_rmdirs(self):
     try:
         tmpdir = mkdtemp()
         subdir = mkdtemp(dir=tmpdir)
         fd, tmpfile = mkstemp(dir=tmpdir)
         assert not fs.rmdirs(tmpfile)
         assert not fs.rmdirs(tmpdir)
         assert fs.rmdirs(subdir)
         assert not os.path.exists(subdir)
     finally:
         os.close(fd)
         shutil.rmtree(tmpdir)
Example #2
0
    def delete_db(self, timestamp):
        """
        Delete the container (directory) if empty.

        :param timestamp: delete timestamp
        """
        try:
            if not dir_empty(self.datadir):
                # FIXME: This is a failure condition here, isn't it?
                return
        except FileOrDirNotFoundError:
            return
        rmdirs(self.datadir)
Example #3
0
    def unlinkold(self, timestamp):
        """
        Remove any older versions of the object file.  Any file that has an
        older timestamp than timestamp will be deleted.

        :param timestamp: timestamp to compare with each file
        """
        if not self.metadata or self.metadata[X_TIMESTAMP] >= timestamp:
            return

        assert self.data_file, \
            "Have metadata, %r, but no data_file" % self.metadata

        if self._is_dir:
            # Marker directory object
            if not rmdirs(self.data_file):
                logging.error('Unable to delete dir object: %s',
                              self.data_file)
                return
        else:
            # File object
            do_unlink(self.data_file)

        self.metadata = {}
        self.data_file = None
Example #4
0
def rmobjdir(dir_path):
    """
    Removes the directory as long as there are
    no objects stored in it.  This works for containers also.
    """
    try:
        if dir_empty(dir_path):
            rmdirs(dir_path)
            return True
    except FileOrDirNotFoundError:
        # No such directory exists
        return False

    for (path, dirs, files) in do_walk(dir_path, topdown=False):
        for directory in dirs:
            fullpath = os.path.join(path, directory)
            metadata = read_metadata(fullpath)

            if not dir_is_object(metadata):
                # Directory is not an object created by the caller
                # so we can go ahead and delete it.
                try:
                    if dir_empty(fullpath):
                        rmdirs(fullpath)
                    else:
                        # Non-object dir is not empty!
                        return False
                except FileOrDirNotFoundError:
                    # No such dir!
                    return False
            else:
                # Wait, this is an object created by the caller
                # We cannot delete
                return False
    rmdirs(dir_path)
    return True