From: Ian Jackson Date: Sun, 22 Aug 2021 23:33:25 +0000 (+0100) Subject: work around lack of io::ErrorKind::IsADirectory X-Git-Tag: hippotat/1.0.0~79 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=bda18e746c168e39256ea5bcb0d0f5682ea21607;p=hippotat.git work around lack of io::ErrorKind::IsADirectory Signed-off-by: Ian Jackson --- diff --git a/Cargo.lock b/Cargo.lock index db9a23d..79c9711 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -427,6 +427,7 @@ dependencies = [ "itertools", "lazy-regex", "lazy_static", + "libc", "log", "memchr", "mime", diff --git a/Cargo.toml b/Cargo.toml index 4774383..e6c0659 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,7 @@ hyper = { version = "0.14", features = ["full"] } hyper-tls = "0.5" ipnet = "2" itertools = "0.10" +libc = "0.2" # just for EISDIR due to IsADirectory mime = "0.3" parking_lot = "0.11" regex = "1.5" @@ -49,6 +50,10 @@ tokio = { version = "1", features = ["full"] } thiserror = "1" void = "1" +# for daemonic behaviours +# daemonize 0.4.1 in sid +# syslog 4.0 in sid, 5.0 in upstream, ideally want 5.0 (new API) + # Not in sid: extend = "1" # no deps not in sid eyre = "0.6" # deps not in sid: indenter (see below) diff --git a/src/config.rs b/src/config.rs index 7d0f911..2b63b2e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -163,13 +163,12 @@ struct Aggregate { sections: HashMap, } -type OkAnyway<'f,A> = &'f dyn Fn(ErrorKind) -> Option; +type OkAnyway<'f,A> = &'f dyn Fn(&io::Error) -> Option; #[ext] impl<'f,A> OkAnyway<'f,A> { fn ok(self, r: &Result) -> Option { let e = r.as_ref().err()?; - let k = e.kind(); - let a = self(k)?; + let a = self(e)?; Some(a) } } @@ -338,9 +337,9 @@ impl Aggregate { #[throws(AE)] // AE includes everything fn read_toplevel(&mut self, toplevel: &Path) { enum Anyway { None, Dir } - match self.read_file(toplevel, &|k| match k { - EK::NotFound => Some(Anyway::None), - EK::IsADirectory => Some(Anyway::Dir), + match self.read_file(toplevel, &|e| match e { + e if e.kind() == EK::NotFound => Some(Anyway::None), + e if e.is_is_a_directory() => Some(Anyway::Dir), _ => None, }) .dcontext(toplevel).context("top-level config directory (or file)")? @@ -349,8 +348,8 @@ impl Aggregate { Some(Anyway::Dir) => { struct AnywayNone; - let anyway_none = |k| match k { - EK::NotFound => Some(AnywayNone), + let anyway_none = |e: &io::Error| match e { + e if e.kind() == EK::NotFound => Some(AnywayNone), _ => None, }; @@ -391,8 +390,8 @@ impl Aggregate { fn read_extra(&mut self, extra: &Path) { struct AnywayDir; - match self.read_file(extra, &|k| match k { - EK::IsADirectory => Some(AnywayDir), + match self.read_file(extra, &|e| match e { + e if e.is_is_a_directory() => Some(AnywayDir), _ => None, }) .dcontext(extra)? diff --git a/src/lib.rs b/src/lib.rs index 443bd4b..89419bf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,8 +2,6 @@ // SPDX-License-Identifier: GPL-3.0-or-later // There is NO WARRANTY. -#![feature(io_error_more)] // EK::IsADirectory - pub mod prelude; pub mod config; diff --git a/src/utils.rs b/src/utils.rs index c210a7d..2df92b5 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -38,6 +38,20 @@ impl Result { } } +// Works around the lack of ErrorKind::IsADirectory +// #![feature(io_error_more)] +// https://github.com/rust-lang/rust/issues/86442 +#[ext(pub)] +impl io::Error { + fn is_is_a_directory(&self) -> bool { + self.raw_os_error() + .unwrap_or_else(|| panic!( + "trying to tell whether Kind is IsADirectory for non-OS error io::Error {}", + self)) + == libc::EISDIR + } +} + #[throws(ReadLimitedError)] pub async fn read_limited_bytes(limit: usize, initial: Box<[u8]>, capacity: usize,