Ejemplo n.º 1
0
def test_zeroout_end(loop_device):
    with directio.open(loop_device, "r+") as f:
        ioutil.blkzeroout(f.fileno(), BLOCKSIZE * 2, BLOCKSIZE)

    with directio.open(loop_device, "r") as f:
        buf = directio.aligned_buffer(BLOCKSIZE * 3)
        with closing(buf):
            f.readinto(buf)
            assert buf[:-BLOCKSIZE] == b"x" * BLOCKSIZE * 2
            assert buf[-BLOCKSIZE:] == b"\0" * BLOCKSIZE
Ejemplo n.º 2
0
def test_open_read_only(tmpdir):
    path = str(tmpdir.join("path"))
    with io.open(path, "wb") as f:
        f.write(b"x" * 512)
    with directio.open(path, "r") as f, \
            closing(directio.aligned_buffer(512)) as buf:
        f.readinto(buf)
        assert buf[:] == b"x" * 512
Ejemplo n.º 3
0
def test_open_write_only_truncate(tmpdir):
    path = str(tmpdir.join("path"))
    with io.open(path, "wb") as f:
        f.write(b"x" * 512)
    with directio.open(path, "w") as f:
        pass
    with io.open(path, "rb") as f:
        assert f.read() == b""
Ejemplo n.º 4
0
def test_open_write_only(tmpdir):
    path = str(tmpdir.join("path"))
    with directio.open(path, "w") as f, \
            closing(directio.aligned_buffer(512)) as buf:
        buf.write(b"x" * 512)
        f.write(buf)
    with io.open(path, "rb") as f:
        assert f.read() == b"x" * 512
Ejemplo n.º 5
0
def test_open_no_direct_read_write(tmpdir):
    path = str(tmpdir.join("path"))
    with io.open(path, "wb") as f:
        f.write(b"a" * 512)
    with directio.open(path, "r+", direct=False) as f:
        f.write(b"b" * 512)
        f.seek(0)
        assert f.read() == b"b" * 512
Ejemplo n.º 6
0
def test_open_no_direct_read_only(tmpdir):
    path = str(tmpdir.join("path"))
    with io.open(path, "wb") as f:
        f.write(b"x" * 512)
    with directio.open(path, "r", direct=False) as f:
        buf = bytearray(512)
        n = f.readinto(buf)
        assert n == 512
        assert buf == "x" * n
Ejemplo n.º 7
0
def test_open_no_direct_read_write(tmpdir):
    path = str(tmpdir.join("path"))
    with io.open(path, "wb") as f:
        f.write(b"a" * 512)
    with directio.open(path, "r+", direct=False) as f:
        f.write(b"b" * 512)
        f.seek(0)
        buf = bytearray(512)
        n = f.readinto(buf)
        assert n == 512
        assert buf == "b" * n
Ejemplo n.º 8
0
def test_open_read_write(tmpdir):
    path = str(tmpdir.join("path"))
    with io.open(path, "wb") as f:
        f.write(b"a" * 512)
    with directio.open(path, "r+") as f, \
            closing(directio.aligned_buffer(512)) as buf:
        f.readinto(buf)
        buf[:] = b"b" * 512
        f.seek(0)
        f.write(buf)
    with io.open(path, "rb") as f:
        assert f.read() == b"b" * 512
Ejemplo n.º 9
0
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 = directio.aligned_buffer(BLOCKSIZE * 3)
    with closing(buf), directio.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
Ejemplo n.º 10
0
def loop_device(tmpdir):
    backing_file = str(tmpdir.join("backing_file"))
    with directio.open(backing_file, "w") as f:
        buf = directio.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])
Ejemplo n.º 11
0
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 = directio.aligned_buffer(BLOCKSIZE * 3)
    with closing(buf), directio.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
Ejemplo n.º 12
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 = directio.aligned_buffer(BLOCKSIZE * 4)
    with closing(buf), directio.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
Ejemplo n.º 13
0
    help=("The number of bytes to zero within one iteration. The default "
          "is to discard all by one ioctl call"))

parser.add_argument("-s",
                    "--sparse",
                    dest="sparse",
                    action="store_true",
                    help="Deallocate zeroed space (punch holes)")

parser.add_argument("filename", help="file or block device to fill with zeros")

args = parser.parse_args()

start_time = time.time()

with directio.open(args.filename, "r+") as f:
    if args.length is None:
        st = os.stat(args.filename)
        if stat.S_ISBLK(st.st_mode):
            f.seek(0, os.SEEK_END)
            size = f.tell()
        else:
            size = st.st_size
        args.length = size - args.offset

    if args.step is None:
        args.step = args.length

    f.seek(args.offset)

    count = args.length
Ejemplo n.º 14
0
def test_open_no_direct_write_only(tmpdir):
    path = str(tmpdir.join("path"))
    with directio.open(path, "w", direct=False) as f:
        f.write(b"x" * 512)
    with io.open(path, "rb") as f:
        assert f.read() == b"x" * 512
Ejemplo n.º 15
0
def test_open_no_create(tmpdir, mode):
    path = str(tmpdir.join("path"))
    with pytest.raises(OSError) as e:
        with directio.open(path, mode):
            pass
    assert e.value.errno == errno.ENOENT
Ejemplo n.º 16
0
                    help="use direct I/O for output file (default False)")
parser.add_argument("input", help="input filename")
parser.add_argument("output", help="output filename")

args = parser.parse_args()

if args.size is None:
    args.size = os.path.getsize(args.input)
    if args.size == 0:
        parser.error("Cannot determine file size, please specify --size")

start = time.time()

buf = mmap.mmap(-1, args.blocksize)
with closing(buf), \
        directio.open(args.input, "r", direct=args.direct_input) as src, \
        directio.open(args.output, "w", direct=args.direct_output) as dst:
    try:
        dst.truncate(args.size)
    except EnvironmentError as e:
        if e.errno != errno.EINVAL:
            raise
    pos = 0
    while pos < args.size:
        n = src.readinto(buf)
        n = min(n, args.size - pos)
        if ioutil.is_zero(buffer(buf, 0, n)):
            dst.seek(n, os.SEEK_CUR)
        else:
            written = 0
            while written < n: