std: fix inappropriate use of unreachable in fanotify_init

This commit is contained in:
Andrew Kelley 2024-09-25 09:54:41 -07:00
parent 4ceefca14b
commit 4442288656
2 changed files with 9 additions and 3 deletions

View File

@ -516,7 +516,7 @@ const Os = switch (builtin.os.tag) {
pub fn init() !Watch {
switch (builtin.os.tag) {
.linux => {
const fan_fd = try std.posix.fanotify_init(.{
const fan_fd = std.posix.fanotify_init(.{
.CLASS = .NOTIF,
.CLOEXEC = true,
.NONBLOCK = true,
@ -524,7 +524,10 @@ pub fn init() !Watch {
.REPORT_DIR_FID = true,
.REPORT_FID = true,
.REPORT_TARGET_FID = true,
}, 0);
}, 0) catch |err| switch (err) {
error.UnsupportedFlags => fatal("fanotify_init failed due to old kernel; requires 5.17+", .{}),
else => |e| return e,
};
return .{
.dir_table = .{},
.os = switch (builtin.os.tag) {

View File

@ -4544,13 +4544,16 @@ pub const FanotifyInitError = error{
SystemFdQuotaExceeded,
SystemResources,
PermissionDenied,
/// The kernel does not recognize the flags passed, likely because it is an
/// older version.
UnsupportedFlags,
} || UnexpectedError;
pub fn fanotify_init(flags: std.os.linux.fanotify.InitFlags, event_f_flags: u32) FanotifyInitError!i32 {
const rc = system.fanotify_init(flags, event_f_flags);
switch (errno(rc)) {
.SUCCESS => return @intCast(rc),
.INVAL => unreachable,
.INVAL => return error.UnsupportedFlags,
.MFILE => return error.ProcessFdQuotaExceeded,
.NFILE => return error.SystemFdQuotaExceeded,
.NOMEM => return error.SystemResources,