libzfs_types/
lib.rs

1// Copyright (c) 2018 DDN. All rights reserved.
2// Use of this source code is governed by a MIT-style
3// license that can be found in the LICENSE file.
4
5//! libzfs-types — Shared types for libzfs
6//!
7extern crate serde_derive;
8
9use serde_derive::{Deserialize, Serialize};
10
11use std::{error, ffi::IntoStringError, fmt, io::Error, path::PathBuf, result};
12
13#[derive(Debug)]
14pub enum LibZfsError {
15    Io(::std::io::Error),
16    IntoString(IntoStringError),
17    PoolNotFound(Option<String>, Option<u64>),
18    ZfsNotFound(String),
19}
20
21impl fmt::Display for LibZfsError {
22    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
23        match *self {
24            LibZfsError::Io(ref err) => write!(f, "{}", err),
25            LibZfsError::IntoString(ref err) => write!(f, "{}", err),
26            LibZfsError::PoolNotFound(ref pool, guid) => match (pool, guid) {
27                (Some(pool), Some(guid)) => write!(
28                    f,
29                    "The pool: {} with guid: {} could not be found.",
30                    pool, guid
31                ),
32                (Some(pool), None) => write!(f, "The pool: {} could not be found.", pool),
33                (None, Some(guid)) => write!(f, "The pool with guid: {} could not be found.", guid),
34                (None, None) => write!(f, "The pool could not be found."),
35            },
36            LibZfsError::ZfsNotFound(ref err) => {
37                write!(f, "The zfs object {} could not be found", err)
38            }
39        }
40    }
41}
42
43impl error::Error for LibZfsError {
44    fn cause(&self) -> Option<&error::Error> {
45        match *self {
46            LibZfsError::Io(ref err) => Some(err),
47            LibZfsError::IntoString(ref err) => Some(err),
48            LibZfsError::PoolNotFound(_, _) => None,
49            LibZfsError::ZfsNotFound(_) => None,
50        }
51    }
52}
53
54impl From<Error> for LibZfsError {
55    fn from(err: Error) -> Self {
56        LibZfsError::Io(err)
57    }
58}
59
60impl From<IntoStringError> for LibZfsError {
61    fn from(err: IntoStringError) -> Self {
62        LibZfsError::IntoString(err)
63    }
64}
65
66pub type Result<T> = result::Result<T, LibZfsError>;
67
68#[derive(Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Clone, PartialOrd, Ord)]
69pub enum VDev {
70    Mirror {
71        children: Vec<VDev>,
72        is_log: Option<bool>,
73    },
74    RaidZ {
75        children: Vec<VDev>,
76    },
77    Replacing {
78        children: Vec<VDev>,
79    },
80    Root {
81        children: Vec<VDev>,
82        spares: Vec<VDev>,
83        cache: Vec<VDev>,
84    },
85    Disk {
86        guid: Option<u64>,
87        state: String,
88        path: PathBuf,
89        dev_id: Option<String>,
90        phys_path: Option<String>,
91        whole_disk: Option<bool>,
92        is_log: Option<bool>,
93    },
94    File {
95        guid: Option<u64>,
96        state: String,
97        path: PathBuf,
98        is_log: Option<bool>,
99    },
100}
101
102#[derive(Debug, PartialEq, Eq, Hash, Deserialize, Serialize, Clone, PartialOrd, Ord)]
103pub struct ZProp {
104    pub name: String,
105    pub value: String,
106}
107
108/// A Pool at a point in time
109#[derive(Debug, Serialize, PartialEq, Deserialize, Clone)]
110pub struct Pool {
111    pub name: String,
112    pub guid: u64,
113    pub health: String,
114    pub hostname: String,
115    pub hostid: Option<u64>,
116    pub state: String,
117    pub readonly: bool,
118    pub size: String,
119    pub vdev: VDev,
120    pub props: Vec<ZProp>,
121    pub datasets: Vec<Dataset>,
122}
123
124/// A Dataset at a point in time
125#[derive(Debug, Serialize, PartialEq, Deserialize, Clone)]
126pub struct Dataset {
127    pub name: String,
128    pub guid: String,
129    pub kind: String,
130    pub props: Vec<ZProp>,
131}