| author | |
| committer | |
| log | f85d7199521290121ade4506ea53acdaf6284982 |
| tree | c4d4c4f312b8950a2004ed105f1a5c9479ab78a4 |
| parent | e7b60b219be3885718425d8ddf25d38d3808e90e |
| parent | 8193f558200da27531304b77c1e80c16bb728420 |
| signature |
linux: add fallocate()4 files changed, 66 insertions(+), 0 deletions(-)
lib/std/c/linux.zig+2| ... | @@ -87,6 +87,8 @@ pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int; | ... | @@ -87,6 +87,8 @@ pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int; |
| 87 | 87 | ||
| 88 | pub extern "c" fn memfd_create(name: [*:0]const u8, flags: c_uint) c_int; | 88 | pub extern "c" fn memfd_create(name: [*:0]const u8, flags: c_uint) c_int; |
| 89 | 89 | ||
| 90 | pub extern "c" fn fallocate(fd: fd_t, mode: c_int, offset: off_t, len: off_t) c_int; | ||
| 91 | |||
| 90 | pub extern "c" fn ftruncate64(fd: c_int, length: off_t) c_int; | 92 | pub extern "c" fn ftruncate64(fd: c_int, length: off_t) c_int; |
| 91 | 93 | ||
| 92 | pub extern "c" fn sendfile( | 94 | pub extern "c" fn sendfile( |
lib/std/os/bits/linux.zig+21| ... | @@ -84,6 +84,27 @@ pub const AT_STATX_DONT_SYNC = 0x4000; | ... | @@ -84,6 +84,27 @@ pub const AT_STATX_DONT_SYNC = 0x4000; |
| 84 | /// Apply to the entire subtree | 84 | /// Apply to the entire subtree |
| 85 | pub const AT_RECURSIVE = 0x8000; | 85 | pub const AT_RECURSIVE = 0x8000; |
| 86 | 86 | ||
| 87 | /// Default is extend size | ||
| 88 | pub const FALLOC_FL_KEEP_SIZE = 0x01; | ||
| 89 | |||
| 90 | /// De-allocates range | ||
| 91 | pub const FALLOC_FL_PUNCH_HOLE = 0x02; | ||
| 92 | |||
| 93 | /// Reserved codepoint | ||
| 94 | pub const FALLOC_FL_NO_HIDE_STALE = 0x04; | ||
| 95 | |||
| 96 | /// Removes a range of a file without leaving a hole in the file | ||
| 97 | pub const FALLOC_FL_COLLAPSE_RANGE = 0x08; | ||
| 98 | |||
| 99 | /// Converts a range of file to zeros preferably without issuing data IO | ||
| 100 | pub const FALLOC_FL_ZERO_RANGE = 0x10; | ||
| 101 | |||
| 102 | /// Inserts space within the file size without overwriting any existing data | ||
| 103 | pub const FALLOC_FL_INSERT_RANGE = 0x20; | ||
| 104 | |||
| 105 | /// Unshares shared blocks within the file size without overwriting any existing data | ||
| 106 | pub const FALLOC_FL_UNSHARE_RANGE = 0x40; | ||
| 107 | |||
| 87 | pub const FUTEX_WAIT = 0; | 108 | pub const FUTEX_WAIT = 0; |
| 88 | pub const FUTEX_WAKE = 1; | 109 | pub const FUTEX_WAKE = 1; |
| 89 | pub const FUTEX_FD = 2; | 110 | pub const FUTEX_FD = 2; |
lib/std/os/linux.zig+24| ... | @@ -136,6 +136,30 @@ pub fn utimensat(dirfd: i32, path: ?[*:0]const u8, times: *const [2]timespec, fl | ... | @@ -136,6 +136,30 @@ pub fn utimensat(dirfd: i32, path: ?[*:0]const u8, times: *const [2]timespec, fl |
| 136 | return syscall4(.utimensat, @bitCast(usize, @as(isize, dirfd)), @ptrToInt(path), @ptrToInt(times), flags); | 136 | return syscall4(.utimensat, @bitCast(usize, @as(isize, dirfd)), @ptrToInt(path), @ptrToInt(times), flags); |
| 137 | } | 137 | } |
| 138 | 138 | ||
| 139 | pub fn fallocate(fd: i32, mode: i32, offset: u64, length: u64) usize { | ||
| 140 | if (@sizeOf(usize) == 4) { | ||
| 141 | const offset_halves = splitValue64(offset); | ||
| 142 | const length_halves = splitValue64(length); | ||
| 143 | return syscall6( | ||
| 144 | .fallocate, | ||
| 145 | @bitCast(usize, @as(isize, fd)), | ||
| 146 | @bitCast(usize, @as(isize, mode)), | ||
| 147 | offset_halves[0], | ||
| 148 | offset_halves[1], | ||
| 149 | length_halves[0], | ||
| 150 | length_halves[1], | ||
| 151 | ); | ||
| 152 | } else { | ||
| 153 | return syscall4( | ||
| 154 | .fallocate, | ||
| 155 | @bitCast(usize, @as(isize, fd)), | ||
| 156 | @bitCast(usize, @as(isize, mode)), | ||
| 157 | offset, | ||
| 158 | length, | ||
| 159 | ); | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 139 | pub fn futex_wait(uaddr: *const i32, futex_op: u32, val: i32, timeout: ?*timespec) usize { | 163 | pub fn futex_wait(uaddr: *const i32, futex_op: u32, val: i32, timeout: ?*timespec) usize { |
| 140 | return syscall4(.futex, @ptrToInt(uaddr), futex_op, @bitCast(u32, val), @ptrToInt(timeout)); | 164 | return syscall4(.futex, @ptrToInt(uaddr), futex_op, @bitCast(u32, val), @ptrToInt(timeout)); |
| 141 | } | 165 | } |
lib/std/os/linux/test.zig+19| ... | @@ -11,6 +11,25 @@ const elf = std.elf; | ... | @@ -11,6 +11,25 @@ const elf = std.elf; |
| 11 | const expect = std.testing.expect; | 11 | const expect = std.testing.expect; |
| 12 | const fs = std.fs; | 12 | const fs = std.fs; |
| 13 | 13 | ||
| 14 | test "fallocate" { | ||
| 15 | const path = "test_fallocate"; | ||
| 16 | const file = try fs.cwd().createFile(path, .{ .truncate = true, .mode = 0o666 }); | ||
| 17 | defer file.close(); | ||
| 18 | defer fs.cwd().deleteFile(path) catch {}; | ||
| 19 | |||
| 20 | expect((try file.stat()).size == 0); | ||
| 21 | |||
| 22 | const len: u64 = 65536; | ||
| 23 | switch (linux.getErrno(linux.fallocate(file.handle, 0, 0, len))) { | ||
| 24 | 0 => {}, | ||
| 25 | linux.ENOSYS => return error.SkipZigTest, | ||
| 26 | linux.EOPNOTSUPP => return error.SkipZigTest, | ||
| 27 | else => |errno| std.debug.panic("unhandled errno: {}", .{ errno }), | ||
| 28 | } | ||
| 29 | |||
| 30 | expect((try file.stat()).size == len); | ||
| 31 | } | ||
| 32 | |||
| 14 | test "getpid" { | 33 | test "getpid" { |
| 15 | expect(linux.getpid() != 0); | 34 | expect(linux.getpid() != 0); |
| 16 | } | 35 | } |