authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-11-05 17:23:00-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-11-05 17:23:00-05:00
logf85d7199521290121ade4506ea53acdaf6284982
treec4d4c4f312b8950a2004ed105f1a5c9479ab78a4
parente7b60b219be3885718425d8ddf25d38d3808e90e
parent8193f558200da27531304b77c1e80c16bb728420
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #6895 from jorangreef/fallocate

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;
8787
8888pub extern "c" fn memfd_create(name: [*:0]const u8, flags: c_uint) c_int;
8989
90pub extern "c" fn fallocate(fd: fd_t, mode: c_int, offset: off_t, len: off_t) c_int;
91
9092pub extern "c" fn ftruncate64(fd: c_int, length: off_t) c_int;
9193
9294pub extern "c" fn sendfile(
lib/std/os/bits/linux.zig+21
......@@ -84,6 +84,27 @@ pub const AT_STATX_DONT_SYNC = 0x4000;
8484/// Apply to the entire subtree
8585pub const AT_RECURSIVE = 0x8000;
8686
87/// Default is extend size
88pub const FALLOC_FL_KEEP_SIZE = 0x01;
89
90/// De-allocates range
91pub const FALLOC_FL_PUNCH_HOLE = 0x02;
92
93/// Reserved codepoint
94pub const FALLOC_FL_NO_HIDE_STALE = 0x04;
95
96/// Removes a range of a file without leaving a hole in the file
97pub const FALLOC_FL_COLLAPSE_RANGE = 0x08;
98
99/// Converts a range of file to zeros preferably without issuing data IO
100pub const FALLOC_FL_ZERO_RANGE = 0x10;
101
102/// Inserts space within the file size without overwriting any existing data
103pub const FALLOC_FL_INSERT_RANGE = 0x20;
104
105/// Unshares shared blocks within the file size without overwriting any existing data
106pub const FALLOC_FL_UNSHARE_RANGE = 0x40;
107
87108pub const FUTEX_WAIT = 0;
88109pub const FUTEX_WAKE = 1;
89110pub 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
136136 return syscall4(.utimensat, @bitCast(usize, @as(isize, dirfd)), @ptrToInt(path), @ptrToInt(times), flags);
137137}
138138
139pub 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
139163pub fn futex_wait(uaddr: *const i32, futex_op: u32, val: i32, timeout: ?*timespec) usize {
140164 return syscall4(.futex, @ptrToInt(uaddr), futex_op, @bitCast(u32, val), @ptrToInt(timeout));
141165}
lib/std/os/linux/test.zig+19
......@@ -11,6 +11,25 @@ const elf = std.elf;
1111const expect = std.testing.expect;
1212const fs = std.fs;
1313
14test "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
1433test "getpid" {
1534 expect(linux.getpid() != 0);
1635}