Skip to content

std.fs.File: fix streaming seekBy ignoring buffer #25589

@rhizoome

Description

@rhizoome

Zig Version

6669885 Oct 15

Steps to Reproduce and Observed Behavior

test "seekBy streaming" {
    var tmp_dir = testing.tmpDir(.{});
    defer tmp_dir.cleanup();

    try tmp_dir.dir.writeFile(.{ .sub_path = "blah.txt", .data = "let's test seekBy" });
    const f = try tmp_dir.dir.openFile("blah.txt", .{ .mode = .read_only });
    defer f.close();

    var read_buf: [10]u8 = undefined;
    var buffer: [10]u8 = undefined;

    var reader = f.readerStreaming(&read_buf);
    const n1 = try reader.interface.readSliceShort(buffer[0..2]);
    try testing.expectEqual(2, n1);
    try testing.expectEqualStrings("le", buffer[0..2]);

    // seek within bufferedLen
    try reader.seekBy(2);
    const n2 = try reader.interface.readSliceShort(buffer[0..2]);
    try testing.expectEqual(2, n2);
    try testing.expectEqualStrings("s ", buffer[0..2]);

    // seek past bufferedLen: causing discard
    try reader.seekBy(8);
    const n3 = try reader.interface.readSliceShort(&buffer);
    try testing.expectEqual(3, n3);
    try testing.expectEqualStrings("kBy", buffer[0..3]);
}

Result

error: 'fs.test.test.seekBy streaming' failed: ====== expected this output: =========
s ␃

======== instead found this: =========
kB␃

seekBy just starts discarding even though there is "enough" data in the buffer.

Expected Behavior

There is a PR #25095 fixing this. It lost its issue, because I fixed more than was in the original issue and that got fixed by 529aa9f.

The new logic checks if the seek offset is smaller than the buffer and adds the offset to r.interface.seek, otherwise the old logic is invoked.

I also rebased just now.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions