master
 1const std = @import("../std.zig");
 2const fd_t = std.c.fd_t;
 3const off_t = std.c.off_t;
 4const unexpectedErrno = std.posix.unexpectedErrno;
 5const errno = std.posix.errno;
 6const builtin = @import("builtin");
 7
 8pub const CopyFileRangeError = std.posix.UnexpectedError || error{
 9    /// If infd is not open for reading or outfd is not open for writing, or
10    /// opened for writing with O_APPEND, or if infd and outfd refer to the
11    /// same file.
12    BadFileFlags,
13    /// If the copy exceeds the process's file size limit or the maximum
14    /// file size for the file system outfd  re- sides on.
15    FileTooBig,
16    /// A signal interrupted the system call before it could be completed.
17    /// This may happen for files on some NFS mounts.  When this happens,
18    /// the values pointed to by inoffp  and  outoffp are reset to the
19    /// initial values for the system call.
20    Interrupted,
21    /// One of:
22    /// * infd and outfd refer to the same file and  the  byte ranges overlap.
23    /// * The flags argument is not zero.
24    /// * Either infd or outfd refers to a file object that is not a regular file.
25    InvalidArguments,
26    /// An  I/O  error  occurred  while  reading/writing the files.
27    InputOutput,
28    /// Corrupted data was detected  while  reading  from  a file system.
29    CorruptedData,
30    /// Either infd or outfd refers to a directory.
31    IsDir,
32    /// File system that stores outfd is full.
33    NoSpaceLeft,
34};
35
36pub fn copy_file_range(fd_in: fd_t, off_in: ?*i64, fd_out: fd_t, off_out: ?*i64, len: usize, flags: u32) CopyFileRangeError!usize {
37    const rc = std.c.copy_file_range(fd_in, off_in, fd_out, off_out, len, flags);
38    switch (errno(rc)) {
39        .SUCCESS => return @intCast(rc),
40        .BADF => return error.BadFileFlags,
41        .FBIG => return error.FileTooBig,
42        .INTR => return error.Interrupted,
43        .INVAL => return error.InvalidArguments,
44        .IO => return error.InputOutput,
45        .INTEGRITY => return error.CorruptedData,
46        .ISDIR => return error.IsDir,
47        .NOSPC => return error.NoSpaceLeft,
48        else => |err| return unexpectedErrno(err),
49    }
50}