def test_do_rmdir(self):
     tmpdir = mkdtemp()
     try:
         subdir = mkdtemp(dir=tmpdir)
         fd, tmpfile = mkstemp(dir=tmpdir)
         try:
             fs.do_rmdir(tmpfile)
         except SwiftOnFileSystemOSError:
             pass
         else:
             self.fail("Expected SwiftOnFileSystemOSError")
         assert os.path.exists(subdir)
         try:
             fs.do_rmdir(tmpdir)
         except SwiftOnFileSystemOSError:
             pass
         else:
             self.fail("Expected SwiftOnFileSystemOSError")
         assert os.path.exists(subdir)
         fs.do_rmdir(subdir)
         assert not os.path.exists(subdir)
     finally:
         os.close(fd)
         shutil.rmtree(tmpdir)
Example #2
0
def rmobjdir(dir_path):
    """
    Removes the directory as long as there are no objects stored in it. This
    works for containers also.
    """
    try:
        do_rmdir(dir_path)
    except OSError as err:
        if err.errno == errno.ENOENT:
            # No such directory exists
            return False
        if err.errno != errno.ENOTEMPTY:
            raise
        # Handle this non-empty directories below.
    else:
        return True

    # We have a directory that is not empty, walk it to see if it is filled
    # with empty sub-directories that are not user created objects
    # (gratuitously created as a result of other object creations).
    for (path, dirs, files) in do_walk(dir_path, topdown=False):
        for directory in dirs:
            fullpath = os.path.join(path, directory)

            try:
                metadata = read_metadata(fullpath)
            except IOError as err:
                if err.errno == errno.ENOENT:
                    # Ignore removal from another entity.
                    continue
                raise
            else:
                if dir_is_object(metadata):
                    # Wait, this is an object created by the caller
                    # We cannot delete
                    return False

            # Directory is not an object created by the caller
            # so we can go ahead and delete it.
            try:
                do_rmdir(fullpath)
            except OSError as err:
                if err.errno == errno.ENOTEMPTY:
                    # Directory is not empty, it might have objects in it
                    return False
                if err.errno == errno.ENOENT:
                    # No such directory exists, already removed, ignore
                    continue
                raise

    try:
        do_rmdir(dir_path)
    except OSError as err:
        if err.errno == errno.ENOTEMPTY:
            # Directory is not empty, race with object creation
            return False
        if err.errno == errno.ENOENT:
            # No such directory exists, already removed, ignore
            return True
        raise
    else:
        return True