authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-09-25 16:38:25+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-25 12:10:58-04:00
log3907e3b675daaa9869732aca6851bd2e0000000a
tree61ce558aa094d7ff47811f495c0659e2b3f799ad
parent48c9e17aa1566ca7140d2460f2ed56daa8db7eeb

Fix llseek behavior


2 files changed, 31 insertions(+), 3 deletions(-)

std/io/test.zig+24
......@@ -609,3 +609,27 @@ test "c out stream" {
609609 const out_stream = &io.COutStream.init(out_file).stream;
610610 try out_stream.print("hi: {}\n", i32(123));
611611}
612
613test "File seek ops" {
614 const tmp_file_name = "temp_test_file.txt";
615 var file = try File.openWrite(tmp_file_name);
616 defer {
617 file.close();
618 fs.deleteFile(tmp_file_name) catch {};
619 }
620
621 try file.write([_]u8{0x55} ** 8192);
622
623 // Seek to the end
624 try file.seekFromEnd(0);
625 std.testing.expect((try file.getPos()) == try file.getEndPos());
626 // Negative delta
627 try file.seekBy(-4096);
628 std.testing.expect((try file.getPos()) == 4096);
629 // Positive delta
630 try file.seekBy(10);
631 std.testing.expect((try file.getPos()) == 4106);
632 // Absolute position
633 try file.seekTo(1234);
634 std.testing.expect((try file.getPos()) == 1234);
635}
std/os.zig+7-3
......@@ -2243,7 +2243,8 @@ pub const SeekError = error{
22432243/// Repositions read/write file offset relative to the beginning.
22442244pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void {
22452245 if (linux.is_the_target and !builtin.link_libc and @sizeOf(usize) == 4) {
2246 switch (errno(system.llseek(fd, offset, null, SEEK_SET))) {
2246 var result: u64 = undefined;
2247 switch (errno(system.llseek(fd, offset, &result, SEEK_SET))) {
22472248 0 => return,
22482249 EBADF => unreachable, // always a race condition
22492250 EINVAL => return error.Unseekable,
......@@ -2271,7 +2272,8 @@ pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void {
22712272/// Repositions read/write file offset relative to the current offset.
22722273pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {
22732274 if (linux.is_the_target and !builtin.link_libc and @sizeOf(usize) == 4) {
2274 switch (errno(system.llseek(fd, @bitCast(u64, offset), null, SEEK_CUR))) {
2275 var result: u64 = undefined;
2276 switch (errno(system.llseek(fd, @bitCast(u64, offset), &result, SEEK_CUR))) {
22752277 0 => return,
22762278 EBADF => unreachable, // always a race condition
22772279 EINVAL => return error.Unseekable,
......@@ -2298,7 +2300,9 @@ pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {
22982300/// Repositions read/write file offset relative to the end.
22992301pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void {
23002302 if (linux.is_the_target and !builtin.link_libc and @sizeOf(usize) == 4) {
2301 switch (errno(system.llseek(fd, @bitCast(u64, offset), null, SEEK_END))) {
2303 var result: u64 = undefined;
2304 switch (errno(system.llseek(fd, @bitCast(u64, offset), &result, SEEK_END))) {
2305 0 => return,
23022306 EBADF => unreachable, // always a race condition
23032307 EINVAL => return error.Unseekable,
23042308 EOVERFLOW => return error.Unseekable,