From 241471ed8d54b769831fc383075233df967a897a Mon Sep 17 00:00:00 2001
From: chenjiayi <chenjiayi22@huawei.com>
Date: Wed, 8 Nov 2023 02:09:09 +0800
Subject: [PATCH 045/103] fix(basic): complete feature dependencies

uuid depends on random.

random depends on io.

Also add get_errno method for basic Error.
---
 libs/basic/Cargo.toml   |  4 ++--
 libs/basic/src/error.rs | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/libs/basic/Cargo.toml b/libs/basic/Cargo.toml
index f7c84157..654c1a3b 100644
--- a/libs/basic/Cargo.toml
+++ b/libs/basic/Cargo.toml
@@ -111,9 +111,9 @@ string = []
 sysfs = ["nix/dir"]
 unistd = ["nix/user"]
 unit_name = []
-uuid = ["bitflags"]
+uuid = ["bitflags", "random"]
 murmurhash2 = []
 strbuf = []
 argv = []
 exec_util = []
-random = []
+random = ["io"]
diff --git a/libs/basic/src/error.rs b/libs/basic/src/error.rs
index aead8ce7..d138f477 100644
--- a/libs/basic/src/error.rs
+++ b/libs/basic/src/error.rs
@@ -70,6 +70,42 @@ pub enum Error {
     Other { msg: String },
 }
 
+impl Error {
+    /// Translate the basic error to error number.
+    pub fn get_errno(&self) -> i32 {
+        match self {
+            Self::Syscall {
+                syscall: _,
+                ret: _,
+                errno,
+            } => *errno,
+            Error::Io { source } => source.raw_os_error().unwrap_or_default(),
+            Error::Caps { what: _ } => nix::errno::Errno::EINVAL as i32,
+            Error::Nix { source } => *source as i32,
+            Error::Var { source } => {
+                (match source {
+                    std::env::VarError::NotPresent => nix::errno::Errno::ENOENT,
+                    std::env::VarError::NotUnicode(_) => nix::errno::Errno::EINVAL,
+                }) as i32
+            }
+            Error::Proc { source } => match source {
+                procfs::ProcError::Incomplete(_) => nix::errno::Errno::EINVAL as i32,
+                procfs::ProcError::PermissionDenied(_) => nix::errno::Errno::EPERM as i32,
+                procfs::ProcError::NotFound(_) => nix::errno::Errno::ENOENT as i32,
+                procfs::ProcError::Io(_, _) => nix::errno::Errno::EIO as i32,
+                procfs::ProcError::Other(_) => nix::errno::Errno::EINVAL as i32,
+                procfs::ProcError::InternalError(_) => nix::errno::Errno::EINVAL as i32,
+            },
+            Error::NulError { source: _ } => nix::errno::Errno::EINVAL as i32,
+            Error::Parse { source: _ } => nix::errno::Errno::EINVAL as i32,
+            Error::ParseNamingScheme { what: _ } => nix::errno::Errno::EINVAL as i32,
+            Error::NotExisted { what: _ } => nix::errno::Errno::ENOENT as i32,
+            Error::Invalid { what: _ } => nix::errno::Errno::EINVAL as i32,
+            Error::Other { msg: _ } => nix::errno::Errno::EINVAL as i32,
+        }
+    }
+}
+
 #[allow(unused_macros)]
 macro_rules! errfrom {
     ($($st:ty),* => $variant:ident) => (
-- 
2.33.0

