zig/lib/std/os/freebsd.zig
Alex Rønne Petersen 36dbe66cf4
std: stop exposing anything having to do with ucontext_t
This type is useful for two things:

* Doing non-local control flow with ucontext.h functions.
* Inspecting machine state in a signal handler.

The first use case is not one we support; we no longer expose bindings to those
functions in the standard library. They're also deprecated in POSIX and, as a
result, not available in musl.

The second use case is valid, but is very poorly served by the standard library.
As evidenced by my changes to std.debug.cpu_context.signal_context_t, users will
be better served rolling their own ucontext_t and especially mcontext_t types
which fit their specific situation. Further, these types tend to evolve
frequently as architectures evolve, and the standard library has not done a good
job keeping up, or even providing them for all supported targets.
2025-10-10 04:43:18 +02:00

51 lines
2.1 KiB
Zig

const std = @import("../std.zig");
const fd_t = std.c.fd_t;
const off_t = std.c.off_t;
const unexpectedErrno = std.posix.unexpectedErrno;
const errno = std.posix.errno;
const builtin = @import("builtin");
pub const CopyFileRangeError = std.posix.UnexpectedError || error{
/// If infd is not open for reading or outfd is not open for writing, or
/// opened for writing with O_APPEND, or if infd and outfd refer to the
/// same file.
BadFileFlags,
/// If the copy exceeds the process's file size limit or the maximum
/// file size for the file system outfd re- sides on.
FileTooBig,
/// A signal interrupted the system call before it could be completed.
/// This may happen for files on some NFS mounts. When this happens,
/// the values pointed to by inoffp and outoffp are reset to the
/// initial values for the system call.
Interrupted,
/// One of:
/// * infd and outfd refer to the same file and the byte ranges overlap.
/// * The flags argument is not zero.
/// * Either infd or outfd refers to a file object that is not a regular file.
InvalidArguments,
/// An I/O error occurred while reading/writing the files.
InputOutput,
/// Corrupted data was detected while reading from a file system.
CorruptedData,
/// Either infd or outfd refers to a directory.
IsDir,
/// File system that stores outfd is full.
NoSpaceLeft,
};
pub fn copy_file_range(fd_in: fd_t, off_in: ?*i64, fd_out: fd_t, off_out: ?*i64, len: usize, flags: u32) CopyFileRangeError!usize {
const rc = std.c.copy_file_range(fd_in, off_in, fd_out, off_out, len, flags);
switch (errno(rc)) {
.SUCCESS => return @intCast(rc),
.BADF => return error.BadFileFlags,
.FBIG => return error.FileTooBig,
.INTR => return error.Interrupted,
.INVAL => return error.InvalidArguments,
.IO => return error.InputOutput,
.INTEGRITY => return error.CorruptedData,
.ISDIR => return error.IsDir,
.NOSPC => return error.NoSpaceLeft,
else => |err| return unexpectedErrno(err),
}
}