def test_zeroout_end(loop_device): with util.open(loop_device, "r+") as f: ioutil.blkzeroout(f.fileno(), BLOCKSIZE * 2, BLOCKSIZE) with util.open(loop_device, "r") as f: buf = util.aligned_buffer(BLOCKSIZE * 3) with closing(buf): f.readinto(buf) assert buf[:-BLOCKSIZE] == b"x" * BLOCKSIZE * 2 assert buf[-BLOCKSIZE:] == b"\0" * BLOCKSIZE
def test_fallocate_zero_end(tmpdir, mode): path = str(tmpdir.join("file")) with open(path, "wb") as f: f.write(b"x" * BLOCKSIZE * 3) buf = util.aligned_buffer(BLOCKSIZE * 3) with closing(buf), util.open(path, "r+") as f: try_fallocate(f.fileno(), mode, BLOCKSIZE * 2, BLOCKSIZE) n = f.readinto(buf) assert n == BLOCKSIZE * 3 assert buf[:-BLOCKSIZE] == b"x" * BLOCKSIZE * 2 assert buf[-BLOCKSIZE:] == b"\0" * BLOCKSIZE assert f.readinto(buf) == 0
def loop_device(tmpdir): backing_file = str(tmpdir.join("backing_file")) with util.open(backing_file, "w") as f: buf = util.aligned_buffer(BLOCKSIZE * 3) with closing(buf): buf[:] = b"x" * BLOCKSIZE * 3 f.write(buf) out = subprocess.check_output( ["losetup", "--find", backing_file, "--show"]) try: loop = out.strip().decode("ascii") yield loop finally: subprocess.check_call(["losetup", "--detach", loop])
def test_fallocate_punch_hole_after_end(tmpdir): path = str(tmpdir.join("file")) with open(path, "wb") as f: f.write(b"x" * BLOCKSIZE * 3) buf = util.aligned_buffer(BLOCKSIZE * 3) with closing(buf), util.open(path, "r+") as f: # This does not change file contents or size. mode = ioutil.FALLOC_FL_PUNCH_HOLE | ioutil.FALLOC_FL_KEEP_SIZE try_fallocate(f.fileno(), mode, BLOCKSIZE * 3, BLOCKSIZE) n = f.readinto(buf) assert n == BLOCKSIZE * 3 assert buf[:] == b"x" * BLOCKSIZE * 3 assert f.readinto(buf) == 0
def test_fallocate_zero_after_end(tmpdir): path = str(tmpdir.join("file")) with open(path, "wb") as f: f.write(b"x" * BLOCKSIZE * 3) buf = util.aligned_buffer(BLOCKSIZE * 4) with closing(buf), util.open(path, "r+") as f: # Will allocate more space that will return zeros when read. mode = ioutil.FALLOC_FL_ZERO_RANGE try_fallocate(f.fileno(), mode, BLOCKSIZE * 3, BLOCKSIZE) n = f.readinto(buf) assert n == BLOCKSIZE * 4 assert buf[:-BLOCKSIZE] == b"x" * BLOCKSIZE * 3 assert buf[-BLOCKSIZE:] == b"\0" * BLOCKSIZE assert f.readinto(buf) == 0
def test_blksszget_512(loop_device): with util.open(loop_device, "r+") as f: assert ioutil.blksszget(f.fileno()) == 512