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" {...@@ -609,3 +609,27 @@ test "c out stream" {
609 const out_stream = &io.COutStream.init(out_file).stream;609 const out_stream = &io.COutStream.init(out_file).stream;
610 try out_stream.print("hi: {}\n", i32(123));610 try out_stream.print("hi: {}\n", i32(123));
611}611}
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{...@@ -2243,7 +2243,8 @@ pub const SeekError = error{
2243/// Repositions read/write file offset relative to the beginning.2243/// Repositions read/write file offset relative to the beginning.
2244pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void {2244pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void {
2245 if (linux.is_the_target and !builtin.link_libc and @sizeOf(usize) == 4) {2245 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))) {
2247 0 => return,2248 0 => return,
2248 EBADF => unreachable, // always a race condition2249 EBADF => unreachable, // always a race condition
2249 EINVAL => return error.Unseekable,2250 EINVAL => return error.Unseekable,
...@@ -2271,7 +2272,8 @@ pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void {...@@ -2271,7 +2272,8 @@ pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void {
2271/// Repositions read/write file offset relative to the current offset.2272/// Repositions read/write file offset relative to the current offset.
2272pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {2273pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {
2273 if (linux.is_the_target and !builtin.link_libc and @sizeOf(usize) == 4) {2274 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))) {
2275 0 => return,2277 0 => return,
2276 EBADF => unreachable, // always a race condition2278 EBADF => unreachable, // always a race condition
2277 EINVAL => return error.Unseekable,2279 EINVAL => return error.Unseekable,
...@@ -2298,7 +2300,9 @@ pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {...@@ -2298,7 +2300,9 @@ pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {
2298/// Repositions read/write file offset relative to the end.2300/// Repositions read/write file offset relative to the end.
2299pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void {2301pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void {
2300 if (linux.is_the_target and !builtin.link_libc and @sizeOf(usize) == 4) {2302 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,
2302 EBADF => unreachable, // always a race condition2306 EBADF => unreachable, // always a race condition
2303 EINVAL => return error.Unseekable,2307 EINVAL => return error.Unseekable,
2304 EOVERFLOW => return error.Unseekable,2308 EOVERFLOW => return error.Unseekable,