From d8c94a631953effd51659e7d4bbd910730f34a35 Mon Sep 17 00:00:00 2001 From: tompro Date: Sat, 24 Feb 2024 14:00:33 +0100 Subject: [PATCH] Makes errno fully derivable --- src/kernel/src/errno.rs | 3 +-- src/kernel/src/fs/dev/vnode.rs | 1 - src/kernel/src/fs/host/mod.rs | 2 ++ src/kernel/src/fs/mod.rs | 2 +- src/kernel/src/fs/tmp/mod.rs | 2 +- src/macros/src/errno.rs | 13 ++++++------- 6 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/kernel/src/errno.rs b/src/kernel/src/errno.rs index 33c51871c..26f266814 100644 --- a/src/kernel/src/errno.rs +++ b/src/kernel/src/errno.rs @@ -1,9 +1,8 @@ use std::convert::Infallible; use std::error::Error; use std::num::NonZeroI32; - -use macros::Errno; use thiserror::Error; + // This file contains errno used in a PS4 system. The value of each errno must be the same as the // PS4. diff --git a/src/kernel/src/fs/dev/vnode.rs b/src/kernel/src/fs/dev/vnode.rs index 1b7bd9e1c..bde4f1e81 100644 --- a/src/kernel/src/fs/dev/vnode.rs +++ b/src/kernel/src/fs/dev/vnode.rs @@ -7,7 +7,6 @@ use crate::fs::{ }; use crate::process::VThread; use macros::Errno; -use std::num::NonZeroI32; use std::sync::Arc; use thiserror::Error; diff --git a/src/kernel/src/fs/host/mod.rs b/src/kernel/src/fs/host/mod.rs index 6261e1690..603ec588e 100644 --- a/src/kernel/src/fs/host/mod.rs +++ b/src/kernel/src/fs/host/mod.rs @@ -180,8 +180,10 @@ enum MountError { #[derive(Debug, Error, Errno)] enum GetVnodeError { #[error("cannot open the specified file")] + #[errno(EIO)] OpenFileFailed(#[source] std::io::Error), #[error("cannot determine file type")] + #[errno(EIO)] GetFileTypeFailed(#[source] std::io::Error), } diff --git a/src/kernel/src/fs/mod.rs b/src/kernel/src/fs/mod.rs index fe9fe0b03..c785160a4 100644 --- a/src/kernel/src/fs/mod.rs +++ b/src/kernel/src/fs/mod.rs @@ -1131,7 +1131,7 @@ pub enum OpenError { #[derive(Debug, Error, Errno)] pub enum WriteError {} -#[derive(Debug, Error)] +#[derive(Debug, Error, Errno)] pub enum IoctlError { #[error("Couldn't get file")] FailedToGetFile(#[from] GetFileError), diff --git a/src/kernel/src/fs/tmp/mod.rs b/src/kernel/src/fs/tmp/mod.rs index fa8cd834c..b6d7e9462 100644 --- a/src/kernel/src/fs/tmp/mod.rs +++ b/src/kernel/src/fs/tmp/mod.rs @@ -2,7 +2,7 @@ use self::node::{AllocNodeError, Node, Nodes}; use super::{Filesystem, FsConfig, Mount, MountFlags, MountOpts, MountSource, VPathBuf, Vnode}; use crate::errno::{Errno, EINVAL}; use crate::ucred::{Ucred, Uid}; -use std::num::NonZeroI32; +use macros::Errno; use std::sync::atomic::AtomicI32; use std::sync::Arc; use thiserror::Error; diff --git a/src/macros/src/errno.rs b/src/macros/src/errno.rs index 3a2e7b0ce..f7430e899 100644 --- a/src/macros/src/errno.rs +++ b/src/macros/src/errno.rs @@ -1,6 +1,6 @@ use proc_macro2::{Ident, TokenStream}; use quote::quote; -use syn::{punctuated::Punctuated, Fields, ItemEnum, Meta, Token, Variant}; +use syn::{punctuated::Punctuated, Fields, ItemEnum, Meta, Token, Variant, Index}; pub fn transform(arg: ItemEnum) -> syn::Result { let enum_name = &arg.ident; @@ -12,7 +12,7 @@ pub fn transform(arg: ItemEnum) -> syn::Result { .collect::, _>>()?; if arms.is_empty() { - Ok(quote!( + Ok(quote!( impl Errno for #enum_name { fn errno(&self) -> std::num::NonZeroI32 { match *self {} @@ -105,14 +105,13 @@ fn process_variant(variant: &Variant, enum_name: &Ident) -> syn::Result { let variant_name = &variant.ident; - // The field at index `pos` is the one we are interested in - let inner = (0..fields.len()) - .map(|i| if i == pos { quote!(e) } else { quote!(_) }) - .fold(quote!(), |acc, c| quote!(#acc #c)); + // We have to use this. otherwise the macro would expand to something like + // `{ 0usize: e, .. }` which is accepted, but only temporarily + let index = Index::from(pos); - Ok(quote!(#enum_name::#variant_name ( #inner ) => e.errno(),)) + Ok(quote!(#enum_name::#variant_name { #index: e, .. } => e.errno(),)) } None => Err(syn::Error::new_spanned( variant,