mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-13 16:46:55 +00:00
refactor: Restructure project layout and clean up dependencies (#30)
This commit introduces a significant reorganization of the project structure to improve maintainability and clarity. Key changes include: - Adjusted the directory layout for a more logical module organization. - Removed unused crate dependencies, reducing the overall project size and potentially speeding up build times. - Updated import paths and configuration files to reflect the structural changes.
This commit is contained in:
@@ -0,0 +1,539 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use super::error::{Error, Result};
|
||||
use path_absolutize::Absolutize;
|
||||
use rustfs_utils::{is_local_host, is_socket_addr};
|
||||
use std::{fmt::Display, path::Path};
|
||||
use tracing::debug;
|
||||
use url::{ParseError, Url};
|
||||
|
||||
/// enum for endpoint type.
|
||||
#[derive(PartialEq, Eq, Debug)]
|
||||
pub enum EndpointType {
|
||||
/// path style endpoint type enum.
|
||||
Path,
|
||||
|
||||
/// URL style endpoint type enum.
|
||||
Url,
|
||||
}
|
||||
|
||||
/// any type of endpoint.
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
|
||||
pub struct Endpoint {
|
||||
pub url: Url,
|
||||
pub is_local: bool,
|
||||
|
||||
pub pool_idx: i32,
|
||||
pub set_idx: i32,
|
||||
pub disk_idx: i32,
|
||||
}
|
||||
|
||||
impl Display for Endpoint {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
if self.url.scheme() == "file" {
|
||||
write!(f, "{}", self.get_file_path())
|
||||
} else {
|
||||
write!(f, "{}", self.url)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&str> for Endpoint {
|
||||
/// The type returned in the event of a conversion error.
|
||||
type Error = Error;
|
||||
|
||||
/// Performs the conversion.
|
||||
fn try_from(value: &str) -> std::result::Result<Self, Self::Error> {
|
||||
// check whether given path is not empty.
|
||||
if ["", "/", "\\"].iter().any(|&v| v.eq(value)) {
|
||||
return Err(Error::other("empty or root endpoint is not supported"));
|
||||
}
|
||||
|
||||
let mut is_local = false;
|
||||
let url = match Url::parse(value) {
|
||||
#[allow(unused_mut)]
|
||||
Ok(mut url) if url.has_host() => {
|
||||
// URL style of endpoint.
|
||||
// Valid URL style endpoint is
|
||||
// - Scheme field must contain "http" or "https"
|
||||
// - All field should be empty except Host and Path.
|
||||
if !((url.scheme() == "http" || url.scheme() == "https")
|
||||
&& url.username().is_empty()
|
||||
&& url.fragment().is_none()
|
||||
&& url.query().is_none())
|
||||
{
|
||||
return Err(Error::other("invalid URL endpoint format"));
|
||||
}
|
||||
|
||||
let path = url.path().to_string();
|
||||
|
||||
#[cfg(not(windows))]
|
||||
let path = Path::new(&path).absolutize()?;
|
||||
|
||||
// On windows having a preceding SlashSeparator will cause problems, if the
|
||||
// command line already has C:/<export-folder/ in it. Final resulting
|
||||
// path on windows might become C:/C:/ this will cause problems
|
||||
// of starting rustfs server properly in distributed mode on windows.
|
||||
// As a special case make sure to trim the separator.
|
||||
#[cfg(windows)]
|
||||
let path = Path::new(&path[1..]).absolutize()?;
|
||||
|
||||
debug!("endpoint try_from: path={}", path.display());
|
||||
|
||||
if path.parent().is_none() || Path::new("").eq(&path) {
|
||||
return Err(Error::other("empty or root path is not supported in URL endpoint"));
|
||||
}
|
||||
|
||||
match path.to_str() {
|
||||
Some(v) => url.set_path(v),
|
||||
None => return Err(Error::other("invalid path")),
|
||||
}
|
||||
|
||||
url
|
||||
}
|
||||
Ok(_) => {
|
||||
// like d:/foo
|
||||
is_local = true;
|
||||
url_parse_from_file_path(value)?
|
||||
}
|
||||
Err(e) => match e {
|
||||
ParseError::InvalidPort => {
|
||||
return Err(Error::other("invalid URL endpoint format: port number must be between 1 to 65535"));
|
||||
}
|
||||
ParseError::EmptyHost => return Err(Error::other("invalid URL endpoint format: empty host name")),
|
||||
ParseError::RelativeUrlWithoutBase => {
|
||||
// like /foo
|
||||
is_local = true;
|
||||
url_parse_from_file_path(value)?
|
||||
}
|
||||
_ => return Err(Error::other(format!("invalid URL endpoint format: {e}"))),
|
||||
},
|
||||
};
|
||||
|
||||
Ok(Endpoint {
|
||||
url,
|
||||
is_local,
|
||||
pool_idx: -1,
|
||||
set_idx: -1,
|
||||
disk_idx: -1,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Endpoint {
|
||||
/// returns type of endpoint.
|
||||
pub fn get_type(&self) -> EndpointType {
|
||||
if self.url.scheme() == "file" {
|
||||
EndpointType::Path
|
||||
} else {
|
||||
EndpointType::Url
|
||||
}
|
||||
}
|
||||
|
||||
/// sets a specific pool number to this node
|
||||
pub fn set_pool_index(&mut self, idx: usize) {
|
||||
self.pool_idx = idx as i32
|
||||
}
|
||||
|
||||
/// sets a specific set number to this node
|
||||
pub fn set_set_index(&mut self, idx: usize) {
|
||||
self.set_idx = idx as i32
|
||||
}
|
||||
|
||||
/// sets a specific disk number to this node
|
||||
pub fn set_disk_index(&mut self, idx: usize) {
|
||||
self.disk_idx = idx as i32
|
||||
}
|
||||
|
||||
/// resolves the host and updates if it is local or not.
|
||||
pub fn update_is_local(&mut self, local_port: u16) -> Result<()> {
|
||||
match (self.url.scheme(), self.url.host()) {
|
||||
(v, Some(host)) if v != "file" => {
|
||||
self.is_local = is_local_host(host, self.url.port().unwrap_or_default(), local_port)?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// returns the host to be used for grid connections.
|
||||
pub fn grid_host(&self) -> String {
|
||||
match (self.url.host(), self.url.port()) {
|
||||
(Some(host), Some(port)) => {
|
||||
debug!("grid_host scheme={}: host={}, port={}", self.url.scheme(), host, port);
|
||||
format!("{}://{}:{}", self.url.scheme(), host, port)
|
||||
}
|
||||
(Some(host), None) => {
|
||||
debug!("grid_host scheme={}: host={}", self.url.scheme(), host);
|
||||
format!("{}://{}", self.url.scheme(), host)
|
||||
}
|
||||
_ => String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn host_port(&self) -> String {
|
||||
match (self.url.host(), self.url.port()) {
|
||||
(Some(host), Some(port)) => {
|
||||
debug!("host_port host={}, port={}", host, port);
|
||||
format!("{host}:{port}")
|
||||
}
|
||||
(Some(host), None) => {
|
||||
debug!("host_port host={}, port={}", host, self.url.port().unwrap_or(0));
|
||||
format!("{host}")
|
||||
}
|
||||
_ => String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_file_path(&self) -> &str {
|
||||
let path = self.url.path();
|
||||
#[cfg(windows)]
|
||||
if self.url.scheme() == "file" {
|
||||
let stripped = path.strip_prefix('/').unwrap_or(path);
|
||||
debug!("get_file_path windows: path={}", stripped);
|
||||
return stripped;
|
||||
}
|
||||
path
|
||||
}
|
||||
}
|
||||
|
||||
/// parse a file path into a URL.
|
||||
fn url_parse_from_file_path(value: &str) -> Result<Url> {
|
||||
// Only check if the arg is an ip address and ask for scheme since its absent.
|
||||
// localhost, example.com, any FQDN cannot be disambiguated from a regular file path such as
|
||||
// /mnt/export1. So we go ahead and start the rustfs server in FS modes in these cases.
|
||||
let addr: Vec<&str> = value.splitn(2, '/').collect();
|
||||
if is_socket_addr(addr[0]) {
|
||||
return Err(Error::other("invalid URL endpoint format: missing scheme http or https"));
|
||||
}
|
||||
|
||||
let file_path = match Path::new(value).absolutize() {
|
||||
Ok(path) => path,
|
||||
Err(err) => return Err(Error::other(format!("absolute path failed: {err}"))),
|
||||
};
|
||||
|
||||
match Url::from_file_path(file_path) {
|
||||
Ok(url) => Ok(url),
|
||||
Err(_) => Err(Error::other("Convert a file path into an URL failed")),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_new_endpoint() {
|
||||
#[derive(Default)]
|
||||
struct TestCase<'a> {
|
||||
arg: &'a str,
|
||||
expected_endpoint: Option<Endpoint>,
|
||||
expected_type: Option<EndpointType>,
|
||||
expected_err: Option<Error>,
|
||||
}
|
||||
|
||||
let u2 = Url::parse("https://example.org/path").unwrap();
|
||||
let u4 = Url::parse("http://192.168.253.200/path").unwrap();
|
||||
let u6 = Url::parse("http://server:/path").unwrap();
|
||||
let root_slash_foo = Url::from_file_path("/foo").unwrap();
|
||||
|
||||
let test_cases = [
|
||||
TestCase {
|
||||
arg: "/foo",
|
||||
expected_endpoint: Some(Endpoint {
|
||||
url: root_slash_foo,
|
||||
is_local: true,
|
||||
pool_idx: -1,
|
||||
set_idx: -1,
|
||||
disk_idx: -1,
|
||||
}),
|
||||
expected_type: Some(EndpointType::Path),
|
||||
expected_err: None,
|
||||
},
|
||||
TestCase {
|
||||
arg: "https://example.org/path",
|
||||
expected_endpoint: Some(Endpoint {
|
||||
url: u2,
|
||||
is_local: false,
|
||||
pool_idx: -1,
|
||||
set_idx: -1,
|
||||
disk_idx: -1,
|
||||
}),
|
||||
expected_type: Some(EndpointType::Url),
|
||||
expected_err: None,
|
||||
},
|
||||
TestCase {
|
||||
arg: "http://192.168.253.200/path",
|
||||
expected_endpoint: Some(Endpoint {
|
||||
url: u4,
|
||||
is_local: false,
|
||||
pool_idx: -1,
|
||||
set_idx: -1,
|
||||
disk_idx: -1,
|
||||
}),
|
||||
expected_type: Some(EndpointType::Url),
|
||||
expected_err: None,
|
||||
},
|
||||
TestCase {
|
||||
arg: "",
|
||||
expected_endpoint: None,
|
||||
expected_type: None,
|
||||
expected_err: Some(Error::other("empty or root endpoint is not supported")),
|
||||
},
|
||||
TestCase {
|
||||
arg: "/",
|
||||
expected_endpoint: None,
|
||||
expected_type: None,
|
||||
expected_err: Some(Error::other("empty or root endpoint is not supported")),
|
||||
},
|
||||
TestCase {
|
||||
arg: "\\",
|
||||
expected_endpoint: None,
|
||||
expected_type: None,
|
||||
expected_err: Some(Error::other("empty or root endpoint is not supported")),
|
||||
},
|
||||
TestCase {
|
||||
arg: "c://foo",
|
||||
expected_endpoint: None,
|
||||
expected_type: None,
|
||||
expected_err: Some(Error::other("invalid URL endpoint format")),
|
||||
},
|
||||
TestCase {
|
||||
arg: "ftp://foo",
|
||||
expected_endpoint: None,
|
||||
expected_type: None,
|
||||
expected_err: Some(Error::other("invalid URL endpoint format")),
|
||||
},
|
||||
TestCase {
|
||||
arg: "http://server/path?location",
|
||||
expected_endpoint: None,
|
||||
expected_type: None,
|
||||
expected_err: Some(Error::other("invalid URL endpoint format")),
|
||||
},
|
||||
TestCase {
|
||||
arg: "http://:/path",
|
||||
expected_endpoint: None,
|
||||
expected_type: None,
|
||||
expected_err: Some(Error::other("invalid URL endpoint format: empty host name")),
|
||||
},
|
||||
TestCase {
|
||||
arg: "http://:8080/path",
|
||||
expected_endpoint: None,
|
||||
expected_type: None,
|
||||
expected_err: Some(Error::other("invalid URL endpoint format: empty host name")),
|
||||
},
|
||||
TestCase {
|
||||
arg: "http://server:/path",
|
||||
expected_endpoint: Some(Endpoint {
|
||||
url: u6,
|
||||
is_local: false,
|
||||
pool_idx: -1,
|
||||
set_idx: -1,
|
||||
disk_idx: -1,
|
||||
}),
|
||||
expected_type: Some(EndpointType::Url),
|
||||
expected_err: None,
|
||||
},
|
||||
TestCase {
|
||||
arg: "https://93.184.216.34:808080/path",
|
||||
expected_endpoint: None,
|
||||
expected_type: None,
|
||||
expected_err: Some(Error::other("invalid URL endpoint format: port number must be between 1 to 65535")),
|
||||
},
|
||||
TestCase {
|
||||
arg: "http://server:8080//",
|
||||
expected_endpoint: None,
|
||||
expected_type: None,
|
||||
expected_err: Some(Error::other("empty or root path is not supported in URL endpoint")),
|
||||
},
|
||||
TestCase {
|
||||
arg: "http://server:8080/",
|
||||
expected_endpoint: None,
|
||||
expected_type: None,
|
||||
expected_err: Some(Error::other("empty or root path is not supported in URL endpoint")),
|
||||
},
|
||||
TestCase {
|
||||
arg: "192.168.1.210:9000",
|
||||
expected_endpoint: None,
|
||||
expected_type: None,
|
||||
expected_err: Some(Error::other("invalid URL endpoint format: missing scheme http or https")),
|
||||
},
|
||||
];
|
||||
|
||||
for test_case in test_cases {
|
||||
let ret = Endpoint::try_from(test_case.arg);
|
||||
if test_case.expected_err.is_none() && ret.is_err() {
|
||||
panic!("{}: error: expected = <nil>, got = {:?}", test_case.arg, ret);
|
||||
}
|
||||
if test_case.expected_err.is_some() && ret.is_ok() {
|
||||
panic!("{}: error: expected = {:?}, got = <nil>", test_case.arg, test_case.expected_err);
|
||||
}
|
||||
match (test_case.expected_err, ret) {
|
||||
(None, Err(e)) => panic!("{}: error: expected = <nil>, got = {}", test_case.arg, e),
|
||||
(None, Ok(mut ep)) => {
|
||||
let _ = ep.update_is_local(9000);
|
||||
if test_case.expected_type != Some(ep.get_type()) {
|
||||
panic!(
|
||||
"{}: type: expected = {:?}, got = {:?}",
|
||||
test_case.arg,
|
||||
test_case.expected_type,
|
||||
ep.get_type()
|
||||
);
|
||||
}
|
||||
|
||||
assert_eq!(test_case.expected_endpoint, Some(ep), "{}: endpoint", test_case.arg);
|
||||
}
|
||||
(Some(e), Ok(_)) => panic!("{}: error: expected = {}, got = <nil>", test_case.arg, e),
|
||||
(Some(e), Err(e2)) => {
|
||||
assert_eq!(e.to_string(), e2.to_string(), "{}: error: expected = {}, got = {}", test_case.arg, e, e2)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_endpoint_display() {
|
||||
// Test file path display
|
||||
let file_endpoint = Endpoint::try_from("/tmp/data").unwrap();
|
||||
let display_str = format!("{file_endpoint}");
|
||||
assert_eq!(display_str, "/tmp/data");
|
||||
|
||||
// Test URL display
|
||||
let url_endpoint = Endpoint::try_from("http://example.com:9000/path").unwrap();
|
||||
let display_str = format!("{url_endpoint}");
|
||||
assert_eq!(display_str, "http://example.com:9000/path");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_endpoint_type() {
|
||||
let file_endpoint = Endpoint::try_from("/tmp/data").unwrap();
|
||||
assert_eq!(file_endpoint.get_type(), EndpointType::Path);
|
||||
|
||||
let url_endpoint = Endpoint::try_from("http://example.com:9000/path").unwrap();
|
||||
assert_eq!(url_endpoint.get_type(), EndpointType::Url);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_endpoint_indexes() {
|
||||
let mut endpoint = Endpoint::try_from("/tmp/data").unwrap();
|
||||
|
||||
// Test initial values
|
||||
assert_eq!(endpoint.pool_idx, -1);
|
||||
assert_eq!(endpoint.set_idx, -1);
|
||||
assert_eq!(endpoint.disk_idx, -1);
|
||||
|
||||
// Test setting indexes
|
||||
endpoint.set_pool_index(2);
|
||||
endpoint.set_set_index(3);
|
||||
endpoint.set_disk_index(4);
|
||||
|
||||
assert_eq!(endpoint.pool_idx, 2);
|
||||
assert_eq!(endpoint.set_idx, 3);
|
||||
assert_eq!(endpoint.disk_idx, 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_endpoint_grid_host() {
|
||||
let endpoint = Endpoint::try_from("http://example.com:9000/path").unwrap();
|
||||
assert_eq!(endpoint.grid_host(), "http://example.com:9000");
|
||||
|
||||
let endpoint_no_port = Endpoint::try_from("https://example.com/path").unwrap();
|
||||
assert_eq!(endpoint_no_port.grid_host(), "https://example.com");
|
||||
|
||||
let file_endpoint = Endpoint::try_from("/tmp/data").unwrap();
|
||||
assert_eq!(file_endpoint.grid_host(), "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_endpoint_host_port() {
|
||||
let endpoint = Endpoint::try_from("http://example.com:9000/path").unwrap();
|
||||
assert_eq!(endpoint.host_port(), "example.com:9000");
|
||||
|
||||
let endpoint_no_port = Endpoint::try_from("https://example.com/path").unwrap();
|
||||
assert_eq!(endpoint_no_port.host_port(), "example.com");
|
||||
|
||||
let file_endpoint = Endpoint::try_from("/tmp/data").unwrap();
|
||||
assert_eq!(file_endpoint.host_port(), "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_endpoint_get_file_path() {
|
||||
let file_endpoint = Endpoint::try_from("/tmp/data").unwrap();
|
||||
assert_eq!(file_endpoint.get_file_path(), "/tmp/data");
|
||||
|
||||
let url_endpoint = Endpoint::try_from("http://example.com:9000/path/to/data").unwrap();
|
||||
assert_eq!(url_endpoint.get_file_path(), "/path/to/data");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_endpoint_clone_and_equality() {
|
||||
let endpoint1 = Endpoint::try_from("/tmp/data").unwrap();
|
||||
let endpoint2 = endpoint1.clone();
|
||||
|
||||
assert_eq!(endpoint1, endpoint2);
|
||||
assert_eq!(endpoint1.url, endpoint2.url);
|
||||
assert_eq!(endpoint1.is_local, endpoint2.is_local);
|
||||
assert_eq!(endpoint1.pool_idx, endpoint2.pool_idx);
|
||||
assert_eq!(endpoint1.set_idx, endpoint2.set_idx);
|
||||
assert_eq!(endpoint1.disk_idx, endpoint2.disk_idx);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_endpoint_with_special_paths() {
|
||||
// Test with complex paths
|
||||
let complex_path = "/var/lib/rustfs/data/bucket1";
|
||||
let endpoint = Endpoint::try_from(complex_path).unwrap();
|
||||
assert_eq!(endpoint.get_file_path(), complex_path);
|
||||
assert!(endpoint.is_local);
|
||||
assert_eq!(endpoint.get_type(), EndpointType::Path);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_endpoint_update_is_local() {
|
||||
let mut endpoint = Endpoint::try_from("http://localhost:9000/path").unwrap();
|
||||
let result = endpoint.update_is_local(9000);
|
||||
assert!(result.is_ok());
|
||||
|
||||
let mut file_endpoint = Endpoint::try_from("/tmp/data").unwrap();
|
||||
let result = file_endpoint.update_is_local(9000);
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_url_parse_from_file_path() {
|
||||
let result = url_parse_from_file_path("/tmp/test");
|
||||
assert!(result.is_ok());
|
||||
|
||||
let url = result.unwrap();
|
||||
assert_eq!(url.scheme(), "file");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_endpoint_hash() {
|
||||
use std::collections::HashSet;
|
||||
|
||||
let endpoint1 = Endpoint::try_from("/tmp/data1").unwrap();
|
||||
let endpoint2 = Endpoint::try_from("/tmp/data2").unwrap();
|
||||
let endpoint3 = endpoint1.clone();
|
||||
|
||||
let mut set = HashSet::new();
|
||||
set.insert(endpoint1);
|
||||
set.insert(endpoint2);
|
||||
set.insert(endpoint3); // Should not be added as it's equal to endpoint1
|
||||
|
||||
assert_eq!(set.len(), 2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,878 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// use crate::quorum::CheckErrorFn;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::io::{self};
|
||||
use std::path::PathBuf;
|
||||
use tracing::error;
|
||||
|
||||
pub type Error = DiskError;
|
||||
pub type Result<T> = core::result::Result<T, Error>;
|
||||
|
||||
// DiskError == StorageErr
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum DiskError {
|
||||
#[error("maximum versions exceeded, please delete few versions to proceed")]
|
||||
MaxVersionsExceeded,
|
||||
|
||||
#[error("unexpected error")]
|
||||
Unexpected,
|
||||
|
||||
#[error("corrupted format")]
|
||||
CorruptedFormat,
|
||||
|
||||
#[error("corrupted backend")]
|
||||
CorruptedBackend,
|
||||
|
||||
#[error("unformatted disk error")]
|
||||
UnformattedDisk,
|
||||
|
||||
#[error("inconsistent drive found")]
|
||||
InconsistentDisk,
|
||||
|
||||
#[error("drive does not support O_DIRECT")]
|
||||
UnsupportedDisk,
|
||||
|
||||
#[error("drive path full")]
|
||||
DiskFull,
|
||||
|
||||
#[error("disk not a dir")]
|
||||
DiskNotDir,
|
||||
|
||||
#[error("disk not found")]
|
||||
DiskNotFound,
|
||||
|
||||
#[error("drive still did not complete the request")]
|
||||
DiskOngoingReq,
|
||||
|
||||
#[error("drive is part of root drive, will not be used")]
|
||||
DriveIsRoot,
|
||||
|
||||
#[error("remote drive is faulty")]
|
||||
FaultyRemoteDisk,
|
||||
|
||||
#[error("drive is faulty")]
|
||||
FaultyDisk,
|
||||
|
||||
#[error("drive access denied")]
|
||||
DiskAccessDenied,
|
||||
|
||||
#[error("file not found")]
|
||||
FileNotFound,
|
||||
|
||||
#[error("file version not found")]
|
||||
FileVersionNotFound,
|
||||
|
||||
#[error("too many open files, please increase 'ulimit -n'")]
|
||||
TooManyOpenFiles,
|
||||
|
||||
#[error("file name too long")]
|
||||
FileNameTooLong,
|
||||
|
||||
#[error("volume already exists")]
|
||||
VolumeExists,
|
||||
|
||||
#[error("not of regular file type")]
|
||||
IsNotRegular,
|
||||
|
||||
#[error("path not found")]
|
||||
PathNotFound,
|
||||
|
||||
#[error("volume not found")]
|
||||
VolumeNotFound,
|
||||
|
||||
#[error("volume is not empty")]
|
||||
VolumeNotEmpty,
|
||||
|
||||
#[error("volume access denied")]
|
||||
VolumeAccessDenied,
|
||||
|
||||
#[error("disk access denied")]
|
||||
FileAccessDenied,
|
||||
|
||||
#[error("file is corrupted")]
|
||||
FileCorrupt,
|
||||
|
||||
#[error("short write")]
|
||||
ShortWrite,
|
||||
|
||||
#[error("bit-rot hash algorithm is invalid")]
|
||||
BitrotHashAlgoInvalid,
|
||||
|
||||
#[error("Rename across devices not allowed, please fix your backend configuration")]
|
||||
CrossDeviceLink,
|
||||
|
||||
#[error("less data available than what was requested")]
|
||||
LessData,
|
||||
|
||||
#[error("more data was sent than what was advertised")]
|
||||
MoreData,
|
||||
|
||||
#[error("outdated XL meta")]
|
||||
OutdatedXLMeta,
|
||||
|
||||
#[error("part missing or corrupt")]
|
||||
PartMissingOrCorrupt,
|
||||
|
||||
#[error("No healing is required")]
|
||||
NoHealRequired,
|
||||
|
||||
#[error("method not allowed")]
|
||||
MethodNotAllowed,
|
||||
|
||||
#[error("erasure write quorum")]
|
||||
ErasureWriteQuorum,
|
||||
|
||||
#[error("erasure read quorum")]
|
||||
ErasureReadQuorum,
|
||||
|
||||
#[error("io error {0}")]
|
||||
Io(io::Error),
|
||||
}
|
||||
|
||||
impl DiskError {
|
||||
pub fn other<E>(error: E) -> Self
|
||||
where
|
||||
E: Into<Box<dyn std::error::Error + Send + Sync>>,
|
||||
{
|
||||
DiskError::Io(std::io::Error::other(error))
|
||||
}
|
||||
|
||||
pub fn is_all_not_found(errs: &[Option<DiskError>]) -> bool {
|
||||
for err in errs.iter() {
|
||||
if let Some(err) = err {
|
||||
if err == &DiskError::FileNotFound || err == &DiskError::FileVersionNotFound {
|
||||
continue;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
!errs.is_empty()
|
||||
}
|
||||
|
||||
pub fn is_err_object_not_found(err: &DiskError) -> bool {
|
||||
matches!(err, &DiskError::FileNotFound) || matches!(err, &DiskError::VolumeNotFound)
|
||||
}
|
||||
|
||||
pub fn is_err_version_not_found(err: &DiskError) -> bool {
|
||||
matches!(err, &DiskError::FileVersionNotFound)
|
||||
}
|
||||
|
||||
// /// If all errors are of the same fatal disk error type, returns the corresponding error.
|
||||
// /// Otherwise, returns Ok.
|
||||
// pub fn check_disk_fatal_errs(errs: &[Option<Error>]) -> Result<()> {
|
||||
// if DiskError::UnsupportedDisk.count_errs(errs) == errs.len() {
|
||||
// return Err(DiskError::UnsupportedDisk.into());
|
||||
// }
|
||||
|
||||
// if DiskError::FileAccessDenied.count_errs(errs) == errs.len() {
|
||||
// return Err(DiskError::FileAccessDenied.into());
|
||||
// }
|
||||
|
||||
// if DiskError::DiskNotDir.count_errs(errs) == errs.len() {
|
||||
// return Err(DiskError::DiskNotDir.into());
|
||||
// }
|
||||
|
||||
// Ok(())
|
||||
// }
|
||||
|
||||
// pub fn count_errs(&self, errs: &[Option<Error>]) -> usize {
|
||||
// errs.iter()
|
||||
// .filter(|&err| match err {
|
||||
// None => false,
|
||||
// Some(e) => self.is(e),
|
||||
// })
|
||||
// .count()
|
||||
// }
|
||||
|
||||
// pub fn quorum_unformatted_disks(errs: &[Option<Error>]) -> bool {
|
||||
// DiskError::UnformattedDisk.count_errs(errs) > (errs.len() / 2)
|
||||
// }
|
||||
|
||||
// pub fn should_init_erasure_disks(errs: &[Option<Error>]) -> bool {
|
||||
// DiskError::UnformattedDisk.count_errs(errs) == errs.len()
|
||||
// }
|
||||
|
||||
// // Check if the error is a disk error
|
||||
// pub fn is(&self, err: &DiskError) -> bool {
|
||||
// if let Some(e) = err.downcast_ref::<DiskError>() {
|
||||
// e == self
|
||||
// } else {
|
||||
// false
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
impl From<rustfs_filemeta::Error> for DiskError {
|
||||
fn from(e: rustfs_filemeta::Error) -> Self {
|
||||
match e {
|
||||
rustfs_filemeta::Error::Io(e) => DiskError::other(e),
|
||||
rustfs_filemeta::Error::FileNotFound => DiskError::FileNotFound,
|
||||
rustfs_filemeta::Error::FileVersionNotFound => DiskError::FileVersionNotFound,
|
||||
rustfs_filemeta::Error::FileCorrupt => DiskError::FileCorrupt,
|
||||
rustfs_filemeta::Error::MethodNotAllowed => DiskError::MethodNotAllowed,
|
||||
e => DiskError::other(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for DiskError {
|
||||
fn from(e: std::io::Error) -> Self {
|
||||
e.downcast::<DiskError>().unwrap_or_else(DiskError::Io)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DiskError> for std::io::Error {
|
||||
fn from(e: DiskError) -> Self {
|
||||
match e {
|
||||
DiskError::Io(io_error) => io_error,
|
||||
e => std::io::Error::other(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<tonic::Status> for DiskError {
|
||||
fn from(e: tonic::Status) -> Self {
|
||||
DiskError::other(e.message().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<rustfs_protos::proto_gen::node_service::Error> for DiskError {
|
||||
fn from(e: rustfs_protos::proto_gen::node_service::Error) -> Self {
|
||||
if let Some(err) = DiskError::from_u32(e.code) {
|
||||
if matches!(err, DiskError::Io(_)) {
|
||||
DiskError::other(e.error_info)
|
||||
} else {
|
||||
err
|
||||
}
|
||||
} else {
|
||||
DiskError::other(e.error_info)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DiskError> for rustfs_protos::proto_gen::node_service::Error {
|
||||
fn from(e: DiskError) -> Self {
|
||||
rustfs_protos::proto_gen::node_service::Error {
|
||||
code: e.to_u32(),
|
||||
error_info: e.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<serde_json::Error> for DiskError {
|
||||
fn from(e: serde_json::Error) -> Self {
|
||||
DiskError::other(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<rmp_serde::encode::Error> for DiskError {
|
||||
fn from(e: rmp_serde::encode::Error) -> Self {
|
||||
DiskError::other(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<rmp::encode::ValueWriteError> for DiskError {
|
||||
fn from(e: rmp::encode::ValueWriteError) -> Self {
|
||||
DiskError::other(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<rmp::decode::ValueReadError> for DiskError {
|
||||
fn from(e: rmp::decode::ValueReadError) -> Self {
|
||||
DiskError::other(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::string::FromUtf8Error> for DiskError {
|
||||
fn from(e: std::string::FromUtf8Error) -> Self {
|
||||
DiskError::other(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<rmp::decode::NumValueReadError> for DiskError {
|
||||
fn from(e: rmp::decode::NumValueReadError) -> Self {
|
||||
DiskError::other(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<tokio::task::JoinError> for DiskError {
|
||||
fn from(e: tokio::task::JoinError) -> Self {
|
||||
DiskError::other(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for DiskError {
|
||||
fn clone(&self) -> Self {
|
||||
match self {
|
||||
DiskError::Io(io_error) => DiskError::Io(std::io::Error::new(io_error.kind(), io_error.to_string())),
|
||||
DiskError::MaxVersionsExceeded => DiskError::MaxVersionsExceeded,
|
||||
DiskError::Unexpected => DiskError::Unexpected,
|
||||
DiskError::CorruptedFormat => DiskError::CorruptedFormat,
|
||||
DiskError::CorruptedBackend => DiskError::CorruptedBackend,
|
||||
DiskError::UnformattedDisk => DiskError::UnformattedDisk,
|
||||
DiskError::InconsistentDisk => DiskError::InconsistentDisk,
|
||||
DiskError::UnsupportedDisk => DiskError::UnsupportedDisk,
|
||||
DiskError::DiskFull => DiskError::DiskFull,
|
||||
DiskError::DiskNotDir => DiskError::DiskNotDir,
|
||||
DiskError::DiskNotFound => DiskError::DiskNotFound,
|
||||
DiskError::DiskOngoingReq => DiskError::DiskOngoingReq,
|
||||
DiskError::DriveIsRoot => DiskError::DriveIsRoot,
|
||||
DiskError::FaultyRemoteDisk => DiskError::FaultyRemoteDisk,
|
||||
DiskError::FaultyDisk => DiskError::FaultyDisk,
|
||||
DiskError::DiskAccessDenied => DiskError::DiskAccessDenied,
|
||||
DiskError::FileNotFound => DiskError::FileNotFound,
|
||||
DiskError::FileVersionNotFound => DiskError::FileVersionNotFound,
|
||||
DiskError::TooManyOpenFiles => DiskError::TooManyOpenFiles,
|
||||
DiskError::FileNameTooLong => DiskError::FileNameTooLong,
|
||||
DiskError::VolumeExists => DiskError::VolumeExists,
|
||||
DiskError::IsNotRegular => DiskError::IsNotRegular,
|
||||
DiskError::PathNotFound => DiskError::PathNotFound,
|
||||
DiskError::VolumeNotFound => DiskError::VolumeNotFound,
|
||||
DiskError::VolumeNotEmpty => DiskError::VolumeNotEmpty,
|
||||
DiskError::VolumeAccessDenied => DiskError::VolumeAccessDenied,
|
||||
DiskError::FileAccessDenied => DiskError::FileAccessDenied,
|
||||
DiskError::FileCorrupt => DiskError::FileCorrupt,
|
||||
DiskError::BitrotHashAlgoInvalid => DiskError::BitrotHashAlgoInvalid,
|
||||
DiskError::CrossDeviceLink => DiskError::CrossDeviceLink,
|
||||
DiskError::LessData => DiskError::LessData,
|
||||
DiskError::MoreData => DiskError::MoreData,
|
||||
DiskError::OutdatedXLMeta => DiskError::OutdatedXLMeta,
|
||||
DiskError::PartMissingOrCorrupt => DiskError::PartMissingOrCorrupt,
|
||||
DiskError::NoHealRequired => DiskError::NoHealRequired,
|
||||
DiskError::MethodNotAllowed => DiskError::MethodNotAllowed,
|
||||
DiskError::ErasureWriteQuorum => DiskError::ErasureWriteQuorum,
|
||||
DiskError::ErasureReadQuorum => DiskError::ErasureReadQuorum,
|
||||
DiskError::ShortWrite => DiskError::ShortWrite,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DiskError {
|
||||
pub fn to_u32(&self) -> u32 {
|
||||
match self {
|
||||
DiskError::MaxVersionsExceeded => 0x01,
|
||||
DiskError::Unexpected => 0x02,
|
||||
DiskError::CorruptedFormat => 0x03,
|
||||
DiskError::CorruptedBackend => 0x04,
|
||||
DiskError::UnformattedDisk => 0x05,
|
||||
DiskError::InconsistentDisk => 0x06,
|
||||
DiskError::UnsupportedDisk => 0x07,
|
||||
DiskError::DiskFull => 0x08,
|
||||
DiskError::DiskNotDir => 0x09,
|
||||
DiskError::DiskNotFound => 0x0A,
|
||||
DiskError::DiskOngoingReq => 0x0B,
|
||||
DiskError::DriveIsRoot => 0x0C,
|
||||
DiskError::FaultyRemoteDisk => 0x0D,
|
||||
DiskError::FaultyDisk => 0x0E,
|
||||
DiskError::DiskAccessDenied => 0x0F,
|
||||
DiskError::FileNotFound => 0x10,
|
||||
DiskError::FileVersionNotFound => 0x11,
|
||||
DiskError::TooManyOpenFiles => 0x12,
|
||||
DiskError::FileNameTooLong => 0x13,
|
||||
DiskError::VolumeExists => 0x14,
|
||||
DiskError::IsNotRegular => 0x15,
|
||||
DiskError::PathNotFound => 0x16,
|
||||
DiskError::VolumeNotFound => 0x17,
|
||||
DiskError::VolumeNotEmpty => 0x18,
|
||||
DiskError::VolumeAccessDenied => 0x19,
|
||||
DiskError::FileAccessDenied => 0x1A,
|
||||
DiskError::FileCorrupt => 0x1B,
|
||||
DiskError::BitrotHashAlgoInvalid => 0x1C,
|
||||
DiskError::CrossDeviceLink => 0x1D,
|
||||
DiskError::LessData => 0x1E,
|
||||
DiskError::MoreData => 0x1F,
|
||||
DiskError::OutdatedXLMeta => 0x20,
|
||||
DiskError::PartMissingOrCorrupt => 0x21,
|
||||
DiskError::NoHealRequired => 0x22,
|
||||
DiskError::MethodNotAllowed => 0x23,
|
||||
DiskError::Io(_) => 0x24,
|
||||
DiskError::ErasureWriteQuorum => 0x25,
|
||||
DiskError::ErasureReadQuorum => 0x26,
|
||||
DiskError::ShortWrite => 0x27,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_u32(error: u32) -> Option<Self> {
|
||||
match error {
|
||||
0x01 => Some(DiskError::MaxVersionsExceeded),
|
||||
0x02 => Some(DiskError::Unexpected),
|
||||
0x03 => Some(DiskError::CorruptedFormat),
|
||||
0x04 => Some(DiskError::CorruptedBackend),
|
||||
0x05 => Some(DiskError::UnformattedDisk),
|
||||
0x06 => Some(DiskError::InconsistentDisk),
|
||||
0x07 => Some(DiskError::UnsupportedDisk),
|
||||
0x08 => Some(DiskError::DiskFull),
|
||||
0x09 => Some(DiskError::DiskNotDir),
|
||||
0x0A => Some(DiskError::DiskNotFound),
|
||||
0x0B => Some(DiskError::DiskOngoingReq),
|
||||
0x0C => Some(DiskError::DriveIsRoot),
|
||||
0x0D => Some(DiskError::FaultyRemoteDisk),
|
||||
0x0E => Some(DiskError::FaultyDisk),
|
||||
0x0F => Some(DiskError::DiskAccessDenied),
|
||||
0x10 => Some(DiskError::FileNotFound),
|
||||
0x11 => Some(DiskError::FileVersionNotFound),
|
||||
0x12 => Some(DiskError::TooManyOpenFiles),
|
||||
0x13 => Some(DiskError::FileNameTooLong),
|
||||
0x14 => Some(DiskError::VolumeExists),
|
||||
0x15 => Some(DiskError::IsNotRegular),
|
||||
0x16 => Some(DiskError::PathNotFound),
|
||||
0x17 => Some(DiskError::VolumeNotFound),
|
||||
0x18 => Some(DiskError::VolumeNotEmpty),
|
||||
0x19 => Some(DiskError::VolumeAccessDenied),
|
||||
0x1A => Some(DiskError::FileAccessDenied),
|
||||
0x1B => Some(DiskError::FileCorrupt),
|
||||
0x1C => Some(DiskError::BitrotHashAlgoInvalid),
|
||||
0x1D => Some(DiskError::CrossDeviceLink),
|
||||
0x1E => Some(DiskError::LessData),
|
||||
0x1F => Some(DiskError::MoreData),
|
||||
0x20 => Some(DiskError::OutdatedXLMeta),
|
||||
0x21 => Some(DiskError::PartMissingOrCorrupt),
|
||||
0x22 => Some(DiskError::NoHealRequired),
|
||||
0x23 => Some(DiskError::MethodNotAllowed),
|
||||
0x24 => Some(DiskError::Io(std::io::Error::other(String::new()))),
|
||||
0x25 => Some(DiskError::ErasureWriteQuorum),
|
||||
0x26 => Some(DiskError::ErasureReadQuorum),
|
||||
0x27 => Some(DiskError::ShortWrite),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for DiskError {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
match (self, other) {
|
||||
(DiskError::Io(e1), DiskError::Io(e2)) => e1.kind() == e2.kind() && e1.to_string() == e2.to_string(),
|
||||
_ => self.to_u32() == other.to_u32(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for DiskError {}
|
||||
|
||||
impl Hash for DiskError {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
self.to_u32().hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
// NOTE: Remove commented out code later if not needed
|
||||
// Some error-related helper functions and complex error handling logic
|
||||
// is currently commented out to avoid complexity. These can be re-enabled
|
||||
// when needed for specific disk quorum checking and error aggregation logic.
|
||||
|
||||
/// Bitrot errors
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum BitrotErrorType {
|
||||
#[error("bitrot checksum verification failed")]
|
||||
BitrotChecksumMismatch { expected: String, got: String },
|
||||
}
|
||||
|
||||
impl From<BitrotErrorType> for DiskError {
|
||||
fn from(e: BitrotErrorType) -> Self {
|
||||
DiskError::other(e)
|
||||
}
|
||||
}
|
||||
|
||||
/// Context wrapper for file access errors
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub struct FileAccessDeniedWithContext {
|
||||
pub path: PathBuf,
|
||||
#[source]
|
||||
pub source: io::Error,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for FileAccessDeniedWithContext {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "file access denied for path: {}", self.path.display())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[test]
|
||||
fn test_disk_error_variants() {
|
||||
let errors = vec![
|
||||
DiskError::MaxVersionsExceeded,
|
||||
DiskError::Unexpected,
|
||||
DiskError::CorruptedFormat,
|
||||
DiskError::CorruptedBackend,
|
||||
DiskError::UnformattedDisk,
|
||||
DiskError::InconsistentDisk,
|
||||
DiskError::UnsupportedDisk,
|
||||
DiskError::DiskFull,
|
||||
DiskError::DiskNotDir,
|
||||
DiskError::DiskNotFound,
|
||||
DiskError::DiskOngoingReq,
|
||||
DiskError::DriveIsRoot,
|
||||
DiskError::FaultyRemoteDisk,
|
||||
DiskError::FaultyDisk,
|
||||
DiskError::DiskAccessDenied,
|
||||
DiskError::FileNotFound,
|
||||
DiskError::FileVersionNotFound,
|
||||
DiskError::TooManyOpenFiles,
|
||||
DiskError::FileNameTooLong,
|
||||
DiskError::VolumeExists,
|
||||
DiskError::IsNotRegular,
|
||||
DiskError::PathNotFound,
|
||||
DiskError::VolumeNotFound,
|
||||
DiskError::VolumeNotEmpty,
|
||||
DiskError::VolumeAccessDenied,
|
||||
DiskError::FileAccessDenied,
|
||||
DiskError::FileCorrupt,
|
||||
DiskError::ShortWrite,
|
||||
DiskError::BitrotHashAlgoInvalid,
|
||||
DiskError::CrossDeviceLink,
|
||||
DiskError::LessData,
|
||||
DiskError::MoreData,
|
||||
DiskError::OutdatedXLMeta,
|
||||
DiskError::PartMissingOrCorrupt,
|
||||
DiskError::NoHealRequired,
|
||||
DiskError::MethodNotAllowed,
|
||||
DiskError::ErasureWriteQuorum,
|
||||
DiskError::ErasureReadQuorum,
|
||||
];
|
||||
|
||||
for error in errors {
|
||||
// Test error display
|
||||
assert!(!error.to_string().is_empty());
|
||||
|
||||
// Test error conversion to u32 and back
|
||||
let code = error.to_u32();
|
||||
let converted_back = DiskError::from_u32(code);
|
||||
assert!(converted_back.is_some());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_disk_error_other() {
|
||||
let custom_error = DiskError::other("custom error message");
|
||||
assert!(matches!(custom_error, DiskError::Io(_)));
|
||||
// The error message format might vary, so just check it's not empty
|
||||
assert!(!custom_error.to_string().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_disk_error_from_io_error() {
|
||||
let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
|
||||
let disk_error = DiskError::from(io_error);
|
||||
assert!(matches!(disk_error, DiskError::Io(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_all_not_found() {
|
||||
// Empty slice
|
||||
assert!(!DiskError::is_all_not_found(&[]));
|
||||
|
||||
// All file not found
|
||||
let all_not_found = vec![
|
||||
Some(DiskError::FileNotFound),
|
||||
Some(DiskError::FileVersionNotFound),
|
||||
Some(DiskError::FileNotFound),
|
||||
];
|
||||
assert!(DiskError::is_all_not_found(&all_not_found));
|
||||
|
||||
// Mixed errors
|
||||
let mixed_errors = vec![
|
||||
Some(DiskError::FileNotFound),
|
||||
Some(DiskError::DiskNotFound),
|
||||
Some(DiskError::FileNotFound),
|
||||
];
|
||||
assert!(!DiskError::is_all_not_found(&mixed_errors));
|
||||
|
||||
// Contains None
|
||||
let with_none = vec![Some(DiskError::FileNotFound), None, Some(DiskError::FileNotFound)];
|
||||
assert!(!DiskError::is_all_not_found(&with_none));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_err_object_not_found() {
|
||||
assert!(DiskError::is_err_object_not_found(&DiskError::FileNotFound));
|
||||
assert!(DiskError::is_err_object_not_found(&DiskError::VolumeNotFound));
|
||||
assert!(!DiskError::is_err_object_not_found(&DiskError::DiskNotFound));
|
||||
assert!(!DiskError::is_err_object_not_found(&DiskError::FileCorrupt));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_err_version_not_found() {
|
||||
assert!(DiskError::is_err_version_not_found(&DiskError::FileVersionNotFound));
|
||||
assert!(!DiskError::is_err_version_not_found(&DiskError::FileNotFound));
|
||||
assert!(!DiskError::is_err_version_not_found(&DiskError::VolumeNotFound));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_disk_error_to_u32_from_u32() {
|
||||
let test_cases = vec![
|
||||
(DiskError::MaxVersionsExceeded, 1),
|
||||
(DiskError::Unexpected, 2),
|
||||
(DiskError::CorruptedFormat, 3),
|
||||
(DiskError::UnformattedDisk, 5),
|
||||
(DiskError::DiskNotFound, 10),
|
||||
(DiskError::FileNotFound, 16),
|
||||
(DiskError::VolumeNotFound, 23),
|
||||
];
|
||||
|
||||
for (error, expected_code) in test_cases {
|
||||
assert_eq!(error.to_u32(), expected_code);
|
||||
assert_eq!(DiskError::from_u32(expected_code), Some(error));
|
||||
}
|
||||
|
||||
// Test unknown error code
|
||||
assert_eq!(DiskError::from_u32(999), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_disk_error_equality() {
|
||||
assert_eq!(DiskError::FileNotFound, DiskError::FileNotFound);
|
||||
assert_ne!(DiskError::FileNotFound, DiskError::VolumeNotFound);
|
||||
|
||||
let error1 = DiskError::other("test");
|
||||
let error2 = DiskError::other("test");
|
||||
// IO errors with the same message should be equal
|
||||
assert_eq!(error1, error2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_disk_error_clone() {
|
||||
let original = DiskError::FileNotFound;
|
||||
let cloned = original.clone();
|
||||
assert_eq!(original, cloned);
|
||||
|
||||
let io_error = DiskError::other("test error");
|
||||
let cloned_io = io_error.clone();
|
||||
assert_eq!(io_error, cloned_io);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_disk_error_hash() {
|
||||
let mut map = HashMap::new();
|
||||
map.insert(DiskError::FileNotFound, "file not found");
|
||||
map.insert(DiskError::VolumeNotFound, "volume not found");
|
||||
|
||||
assert_eq!(map.get(&DiskError::FileNotFound), Some(&"file not found"));
|
||||
assert_eq!(map.get(&DiskError::VolumeNotFound), Some(&"volume not found"));
|
||||
assert_eq!(map.get(&DiskError::DiskNotFound), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_error_conversions() {
|
||||
// Test From implementations
|
||||
let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "test");
|
||||
let _disk_error: DiskError = io_error.into();
|
||||
|
||||
let json_str = r#"{"invalid": json}"#; // Invalid JSON
|
||||
let json_error = serde_json::from_str::<serde_json::Value>(json_str).unwrap_err();
|
||||
let _disk_error: DiskError = json_error.into();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bitrot_error_type() {
|
||||
let bitrot_error = BitrotErrorType::BitrotChecksumMismatch {
|
||||
expected: "abc123".to_string(),
|
||||
got: "def456".to_string(),
|
||||
};
|
||||
|
||||
assert!(bitrot_error.to_string().contains("bitrot checksum verification failed"));
|
||||
|
||||
let disk_error: DiskError = bitrot_error.into();
|
||||
assert!(matches!(disk_error, DiskError::Io(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_file_access_denied_with_context() {
|
||||
let path = PathBuf::from("/test/path");
|
||||
let io_error = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "permission denied");
|
||||
|
||||
let context_error = FileAccessDeniedWithContext {
|
||||
path: path.clone(),
|
||||
source: io_error,
|
||||
};
|
||||
|
||||
let display_str = format!("{context_error}");
|
||||
assert!(display_str.contains("/test/path"));
|
||||
assert!(display_str.contains("file access denied"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_error_debug_format() {
|
||||
let error = DiskError::FileNotFound;
|
||||
let debug_str = format!("{error:?}");
|
||||
assert_eq!(debug_str, "FileNotFound");
|
||||
|
||||
let io_error = DiskError::other("test error");
|
||||
let debug_str = format!("{io_error:?}");
|
||||
assert!(debug_str.contains("Io"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_error_source() {
|
||||
use std::error::Error;
|
||||
|
||||
let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "test");
|
||||
let disk_error = DiskError::Io(io_error);
|
||||
|
||||
// DiskError should have a source
|
||||
if let DiskError::Io(ref inner) = disk_error {
|
||||
assert!(inner.source().is_none()); // std::io::Error typically doesn't have a source
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_io_error_roundtrip_conversion() {
|
||||
// Test DiskError -> std::io::Error -> DiskError roundtrip
|
||||
let original_disk_errors = vec![
|
||||
DiskError::FileNotFound,
|
||||
DiskError::VolumeNotFound,
|
||||
DiskError::DiskFull,
|
||||
DiskError::FileCorrupt,
|
||||
DiskError::MethodNotAllowed,
|
||||
];
|
||||
|
||||
for original_error in original_disk_errors {
|
||||
// Convert to io::Error and back
|
||||
let io_error: std::io::Error = original_error.clone().into();
|
||||
let recovered_error: DiskError = io_error.into();
|
||||
|
||||
// For non-Io variants, they become Io(ErrorKind::Other) and then back to the original
|
||||
match &original_error {
|
||||
DiskError::Io(_) => {
|
||||
// Io errors should maintain their kind
|
||||
assert!(matches!(recovered_error, DiskError::Io(_)));
|
||||
}
|
||||
_ => {
|
||||
// Other errors become Io(Other) and then are recovered via downcast
|
||||
// The recovered error should be functionally equivalent
|
||||
assert_eq!(original_error.to_u32(), recovered_error.to_u32());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_io_error_with_disk_error_inside() {
|
||||
// Test that io::Error containing DiskError can be properly converted back
|
||||
let original_disk_error = DiskError::FileNotFound;
|
||||
let io_with_disk_error = std::io::Error::other(original_disk_error.clone());
|
||||
|
||||
// Convert io::Error back to DiskError
|
||||
let recovered_disk_error: DiskError = io_with_disk_error.into();
|
||||
assert_eq!(original_disk_error, recovered_disk_error);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_io_error_different_kinds() {
|
||||
use std::io::ErrorKind;
|
||||
|
||||
let test_cases = vec![
|
||||
(ErrorKind::NotFound, "file not found"),
|
||||
(ErrorKind::PermissionDenied, "permission denied"),
|
||||
(ErrorKind::ConnectionRefused, "connection refused"),
|
||||
(ErrorKind::TimedOut, "timed out"),
|
||||
(ErrorKind::InvalidInput, "invalid input"),
|
||||
];
|
||||
|
||||
for (kind, message) in test_cases {
|
||||
let io_error = std::io::Error::new(kind, message);
|
||||
let disk_error: DiskError = io_error.into();
|
||||
|
||||
// Should become DiskError::Io with the same kind and message
|
||||
match disk_error {
|
||||
DiskError::Io(inner_io) => {
|
||||
assert_eq!(inner_io.kind(), kind);
|
||||
assert!(inner_io.to_string().contains(message));
|
||||
}
|
||||
_ => panic!("Expected DiskError::Io variant"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_disk_error_to_io_error_preserves_information() {
|
||||
let test_cases = vec![
|
||||
DiskError::FileNotFound,
|
||||
DiskError::VolumeNotFound,
|
||||
DiskError::DiskFull,
|
||||
DiskError::FileCorrupt,
|
||||
DiskError::MethodNotAllowed,
|
||||
DiskError::ErasureReadQuorum,
|
||||
DiskError::ErasureWriteQuorum,
|
||||
];
|
||||
|
||||
for disk_error in test_cases {
|
||||
let io_error: std::io::Error = disk_error.clone().into();
|
||||
|
||||
// Error message should be preserved
|
||||
assert!(io_error.to_string().contains(&disk_error.to_string()));
|
||||
|
||||
// Should be able to downcast back to DiskError
|
||||
let recovered_error = io_error.downcast::<DiskError>();
|
||||
assert!(recovered_error.is_ok());
|
||||
assert_eq!(recovered_error.unwrap(), disk_error);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_io_error_downcast_chain() {
|
||||
// Test nested error downcasting chain
|
||||
let original_disk_error = DiskError::FileNotFound;
|
||||
|
||||
// Create a chain: DiskError -> io::Error -> DiskError -> io::Error
|
||||
let io_error1: std::io::Error = original_disk_error.clone().into();
|
||||
let disk_error2: DiskError = io_error1.into();
|
||||
let io_error2: std::io::Error = disk_error2.into();
|
||||
|
||||
// Final io::Error should still contain the original DiskError
|
||||
let final_disk_error = io_error2.downcast::<DiskError>();
|
||||
assert!(final_disk_error.is_ok());
|
||||
assert_eq!(final_disk_error.unwrap(), original_disk_error);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_io_error_with_original_io_content() {
|
||||
// Test DiskError::Io variant preserves original io::Error
|
||||
let original_io = std::io::Error::new(std::io::ErrorKind::BrokenPipe, "broken pipe");
|
||||
let disk_error = DiskError::Io(original_io);
|
||||
|
||||
let converted_io: std::io::Error = disk_error.into();
|
||||
assert_eq!(converted_io.kind(), std::io::ErrorKind::BrokenPipe);
|
||||
assert!(converted_io.to_string().contains("broken pipe"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_error_display_preservation() {
|
||||
let disk_errors = vec![
|
||||
DiskError::MaxVersionsExceeded,
|
||||
DiskError::CorruptedFormat,
|
||||
DiskError::UnformattedDisk,
|
||||
DiskError::DiskNotFound,
|
||||
DiskError::FileAccessDenied,
|
||||
];
|
||||
|
||||
for disk_error in disk_errors {
|
||||
let original_message = disk_error.to_string();
|
||||
let io_error: std::io::Error = disk_error.clone().into();
|
||||
|
||||
// The io::Error should contain the original error message
|
||||
assert!(io_error.to_string().contains(&original_message));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,449 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use super::error::DiskError;
|
||||
|
||||
pub fn to_file_error(io_err: std::io::Error) -> std::io::Error {
|
||||
match io_err.kind() {
|
||||
std::io::ErrorKind::NotFound => DiskError::FileNotFound.into(),
|
||||
std::io::ErrorKind::PermissionDenied => DiskError::FileAccessDenied.into(),
|
||||
std::io::ErrorKind::IsADirectory => DiskError::IsNotRegular.into(),
|
||||
std::io::ErrorKind::NotADirectory => DiskError::FileAccessDenied.into(),
|
||||
std::io::ErrorKind::DirectoryNotEmpty => DiskError::FileAccessDenied.into(),
|
||||
std::io::ErrorKind::UnexpectedEof => DiskError::FaultyDisk.into(),
|
||||
std::io::ErrorKind::TooManyLinks => DiskError::TooManyOpenFiles.into(),
|
||||
std::io::ErrorKind::InvalidInput => DiskError::FileNotFound.into(),
|
||||
std::io::ErrorKind::InvalidData => DiskError::FileCorrupt.into(),
|
||||
std::io::ErrorKind::StorageFull => DiskError::DiskFull.into(),
|
||||
_ => io_err,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_volume_error(io_err: std::io::Error) -> std::io::Error {
|
||||
match io_err.kind() {
|
||||
std::io::ErrorKind::NotFound => DiskError::VolumeNotFound.into(),
|
||||
std::io::ErrorKind::PermissionDenied => DiskError::DiskAccessDenied.into(),
|
||||
std::io::ErrorKind::DirectoryNotEmpty => DiskError::VolumeNotEmpty.into(),
|
||||
std::io::ErrorKind::NotADirectory => DiskError::IsNotRegular.into(),
|
||||
std::io::ErrorKind::Other => match io_err.downcast::<DiskError>() {
|
||||
Ok(err) => match err {
|
||||
DiskError::FileNotFound => DiskError::VolumeNotFound.into(),
|
||||
DiskError::FileAccessDenied => DiskError::DiskAccessDenied.into(),
|
||||
err => err.into(),
|
||||
},
|
||||
Err(err) => to_file_error(err),
|
||||
},
|
||||
_ => to_file_error(io_err),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_disk_error(io_err: std::io::Error) -> std::io::Error {
|
||||
match io_err.kind() {
|
||||
std::io::ErrorKind::NotFound => DiskError::DiskNotFound.into(),
|
||||
std::io::ErrorKind::PermissionDenied => DiskError::DiskAccessDenied.into(),
|
||||
std::io::ErrorKind::Other => match io_err.downcast::<DiskError>() {
|
||||
Ok(err) => match err {
|
||||
DiskError::FileNotFound => DiskError::DiskNotFound.into(),
|
||||
DiskError::VolumeNotFound => DiskError::DiskNotFound.into(),
|
||||
DiskError::FileAccessDenied => DiskError::DiskAccessDenied.into(),
|
||||
DiskError::VolumeAccessDenied => DiskError::DiskAccessDenied.into(),
|
||||
err => err.into(),
|
||||
},
|
||||
Err(err) => to_volume_error(err),
|
||||
},
|
||||
_ => to_volume_error(io_err),
|
||||
}
|
||||
}
|
||||
|
||||
// only errors from FileSystem operations
|
||||
pub fn to_access_error(io_err: std::io::Error, per_err: DiskError) -> std::io::Error {
|
||||
match io_err.kind() {
|
||||
std::io::ErrorKind::PermissionDenied => per_err.into(),
|
||||
std::io::ErrorKind::NotADirectory => per_err.into(),
|
||||
std::io::ErrorKind::NotFound => DiskError::VolumeNotFound.into(),
|
||||
std::io::ErrorKind::UnexpectedEof => DiskError::FaultyDisk.into(),
|
||||
std::io::ErrorKind::Other => match io_err.downcast::<DiskError>() {
|
||||
Ok(err) => match err {
|
||||
DiskError::DiskAccessDenied => per_err.into(),
|
||||
DiskError::FileAccessDenied => per_err.into(),
|
||||
DiskError::FileNotFound => DiskError::VolumeNotFound.into(),
|
||||
err => err.into(),
|
||||
},
|
||||
Err(err) => to_volume_error(err),
|
||||
},
|
||||
_ => to_volume_error(io_err),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_unformatted_disk_error(io_err: std::io::Error) -> std::io::Error {
|
||||
match io_err.kind() {
|
||||
std::io::ErrorKind::NotFound => DiskError::UnformattedDisk.into(),
|
||||
std::io::ErrorKind::PermissionDenied => DiskError::DiskAccessDenied.into(),
|
||||
std::io::ErrorKind::Other => match io_err.downcast::<DiskError>() {
|
||||
Ok(err) => match err {
|
||||
DiskError::FileNotFound => DiskError::UnformattedDisk.into(),
|
||||
DiskError::DiskNotFound => DiskError::UnformattedDisk.into(),
|
||||
DiskError::VolumeNotFound => DiskError::UnformattedDisk.into(),
|
||||
DiskError::FileAccessDenied => DiskError::DiskAccessDenied.into(),
|
||||
DiskError::DiskAccessDenied => DiskError::DiskAccessDenied.into(),
|
||||
_ => DiskError::CorruptedBackend.into(),
|
||||
},
|
||||
Err(_err) => DiskError::CorruptedBackend.into(),
|
||||
},
|
||||
_ => DiskError::CorruptedBackend.into(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::io::{Error as IoError, ErrorKind};
|
||||
|
||||
// Helper function to create IO errors with specific kinds
|
||||
fn create_io_error(kind: ErrorKind) -> IoError {
|
||||
IoError::new(kind, "test error")
|
||||
}
|
||||
|
||||
// Helper function to create IO errors with DiskError as the source
|
||||
fn create_io_error_with_disk_error(disk_error: DiskError) -> IoError {
|
||||
IoError::other(disk_error)
|
||||
}
|
||||
|
||||
// Helper function to check if an IoError contains a specific DiskError
|
||||
fn contains_disk_error(io_error: IoError, expected: DiskError) -> bool {
|
||||
if let Ok(disk_error) = io_error.downcast::<DiskError>() {
|
||||
std::mem::discriminant(&disk_error) == std::mem::discriminant(&expected)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_to_file_error_basic_conversions() {
|
||||
// Test NotFound -> FileNotFound
|
||||
let result = to_file_error(create_io_error(ErrorKind::NotFound));
|
||||
assert!(contains_disk_error(result, DiskError::FileNotFound));
|
||||
|
||||
// Test PermissionDenied -> FileAccessDenied
|
||||
let result = to_file_error(create_io_error(ErrorKind::PermissionDenied));
|
||||
assert!(contains_disk_error(result, DiskError::FileAccessDenied));
|
||||
|
||||
// Test IsADirectory -> IsNotRegular
|
||||
let result = to_file_error(create_io_error(ErrorKind::IsADirectory));
|
||||
assert!(contains_disk_error(result, DiskError::IsNotRegular));
|
||||
|
||||
// Test NotADirectory -> FileAccessDenied
|
||||
let result = to_file_error(create_io_error(ErrorKind::NotADirectory));
|
||||
assert!(contains_disk_error(result, DiskError::FileAccessDenied));
|
||||
|
||||
// Test DirectoryNotEmpty -> FileAccessDenied
|
||||
let result = to_file_error(create_io_error(ErrorKind::DirectoryNotEmpty));
|
||||
assert!(contains_disk_error(result, DiskError::FileAccessDenied));
|
||||
|
||||
// Test UnexpectedEof -> FaultyDisk
|
||||
let result = to_file_error(create_io_error(ErrorKind::UnexpectedEof));
|
||||
assert!(contains_disk_error(result, DiskError::FaultyDisk));
|
||||
|
||||
// Test TooManyLinks -> TooManyOpenFiles
|
||||
#[cfg(unix)]
|
||||
{
|
||||
let result = to_file_error(create_io_error(ErrorKind::TooManyLinks));
|
||||
assert!(contains_disk_error(result, DiskError::TooManyOpenFiles));
|
||||
}
|
||||
|
||||
// Test InvalidInput -> FileNotFound
|
||||
let result = to_file_error(create_io_error(ErrorKind::InvalidInput));
|
||||
assert!(contains_disk_error(result, DiskError::FileNotFound));
|
||||
|
||||
// Test InvalidData -> FileCorrupt
|
||||
let result = to_file_error(create_io_error(ErrorKind::InvalidData));
|
||||
assert!(contains_disk_error(result, DiskError::FileCorrupt));
|
||||
|
||||
// Test StorageFull -> DiskFull
|
||||
#[cfg(unix)]
|
||||
{
|
||||
let result = to_file_error(create_io_error(ErrorKind::StorageFull));
|
||||
assert!(contains_disk_error(result, DiskError::DiskFull));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_to_file_error_passthrough_unknown() {
|
||||
// Test that unknown error kinds are passed through unchanged
|
||||
let original = create_io_error(ErrorKind::Interrupted);
|
||||
let result = to_file_error(original);
|
||||
assert_eq!(result.kind(), ErrorKind::Interrupted);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_to_volume_error_basic_conversions() {
|
||||
// Test NotFound -> VolumeNotFound
|
||||
let result = to_volume_error(create_io_error(ErrorKind::NotFound));
|
||||
assert!(contains_disk_error(result, DiskError::VolumeNotFound));
|
||||
|
||||
// Test PermissionDenied -> DiskAccessDenied
|
||||
let result = to_volume_error(create_io_error(ErrorKind::PermissionDenied));
|
||||
assert!(contains_disk_error(result, DiskError::DiskAccessDenied));
|
||||
|
||||
// Test DirectoryNotEmpty -> VolumeNotEmpty
|
||||
let result = to_volume_error(create_io_error(ErrorKind::DirectoryNotEmpty));
|
||||
assert!(contains_disk_error(result, DiskError::VolumeNotEmpty));
|
||||
|
||||
// Test NotADirectory -> IsNotRegular
|
||||
let result = to_volume_error(create_io_error(ErrorKind::NotADirectory));
|
||||
assert!(contains_disk_error(result, DiskError::IsNotRegular));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_to_volume_error_other_with_disk_error() {
|
||||
// Test Other error kind with FileNotFound DiskError -> VolumeNotFound
|
||||
let io_error = create_io_error_with_disk_error(DiskError::FileNotFound);
|
||||
let result = to_volume_error(io_error);
|
||||
assert!(contains_disk_error(result, DiskError::VolumeNotFound));
|
||||
|
||||
// Test Other error kind with FileAccessDenied DiskError -> DiskAccessDenied
|
||||
let io_error = create_io_error_with_disk_error(DiskError::FileAccessDenied);
|
||||
let result = to_volume_error(io_error);
|
||||
assert!(contains_disk_error(result, DiskError::DiskAccessDenied));
|
||||
|
||||
// Test Other error kind with other DiskError -> passthrough
|
||||
let io_error = create_io_error_with_disk_error(DiskError::DiskFull);
|
||||
let result = to_volume_error(io_error);
|
||||
assert!(contains_disk_error(result, DiskError::DiskFull));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_to_volume_error_fallback_to_file_error() {
|
||||
// Test fallback to to_file_error for unknown error kinds
|
||||
let result = to_volume_error(create_io_error(ErrorKind::Interrupted));
|
||||
assert_eq!(result.kind(), ErrorKind::Interrupted);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_to_disk_error_basic_conversions() {
|
||||
// Test NotFound -> DiskNotFound
|
||||
let result = to_disk_error(create_io_error(ErrorKind::NotFound));
|
||||
assert!(contains_disk_error(result, DiskError::DiskNotFound));
|
||||
|
||||
// Test PermissionDenied -> DiskAccessDenied
|
||||
let result = to_disk_error(create_io_error(ErrorKind::PermissionDenied));
|
||||
assert!(contains_disk_error(result, DiskError::DiskAccessDenied));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_to_disk_error_other_with_disk_error() {
|
||||
// Test Other error kind with FileNotFound DiskError -> DiskNotFound
|
||||
let io_error = create_io_error_with_disk_error(DiskError::FileNotFound);
|
||||
let result = to_disk_error(io_error);
|
||||
assert!(contains_disk_error(result, DiskError::DiskNotFound));
|
||||
|
||||
// Test Other error kind with VolumeNotFound DiskError -> DiskNotFound
|
||||
let io_error = create_io_error_with_disk_error(DiskError::VolumeNotFound);
|
||||
let result = to_disk_error(io_error);
|
||||
assert!(contains_disk_error(result, DiskError::DiskNotFound));
|
||||
|
||||
// Test Other error kind with FileAccessDenied DiskError -> DiskAccessDenied
|
||||
let io_error = create_io_error_with_disk_error(DiskError::FileAccessDenied);
|
||||
let result = to_disk_error(io_error);
|
||||
assert!(contains_disk_error(result, DiskError::DiskAccessDenied));
|
||||
|
||||
// Test Other error kind with VolumeAccessDenied DiskError -> DiskAccessDenied
|
||||
let io_error = create_io_error_with_disk_error(DiskError::VolumeAccessDenied);
|
||||
let result = to_disk_error(io_error);
|
||||
assert!(contains_disk_error(result, DiskError::DiskAccessDenied));
|
||||
|
||||
// Test Other error kind with other DiskError -> passthrough
|
||||
let io_error = create_io_error_with_disk_error(DiskError::DiskFull);
|
||||
let result = to_disk_error(io_error);
|
||||
assert!(contains_disk_error(result, DiskError::DiskFull));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_to_disk_error_fallback_to_volume_error() {
|
||||
// Test fallback to to_volume_error for unknown error kinds
|
||||
let result = to_disk_error(create_io_error(ErrorKind::Interrupted));
|
||||
assert_eq!(result.kind(), ErrorKind::Interrupted);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_to_access_error_basic_conversions() {
|
||||
let permission_error = DiskError::FileAccessDenied;
|
||||
|
||||
// Test PermissionDenied -> specified permission error
|
||||
let result = to_access_error(create_io_error(ErrorKind::PermissionDenied), permission_error);
|
||||
assert!(contains_disk_error(result, DiskError::FileAccessDenied));
|
||||
|
||||
// Test NotADirectory -> specified permission error
|
||||
let result = to_access_error(create_io_error(ErrorKind::NotADirectory), DiskError::FileAccessDenied);
|
||||
assert!(contains_disk_error(result, DiskError::FileAccessDenied));
|
||||
|
||||
// Test NotFound -> VolumeNotFound
|
||||
let result = to_access_error(create_io_error(ErrorKind::NotFound), DiskError::FileAccessDenied);
|
||||
assert!(contains_disk_error(result, DiskError::VolumeNotFound));
|
||||
|
||||
// Test UnexpectedEof -> FaultyDisk
|
||||
let result = to_access_error(create_io_error(ErrorKind::UnexpectedEof), DiskError::FileAccessDenied);
|
||||
assert!(contains_disk_error(result, DiskError::FaultyDisk));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_to_access_error_other_with_disk_error() {
|
||||
let permission_error = DiskError::VolumeAccessDenied;
|
||||
|
||||
// Test Other error kind with DiskAccessDenied -> specified permission error
|
||||
let io_error = create_io_error_with_disk_error(DiskError::DiskAccessDenied);
|
||||
let result = to_access_error(io_error, permission_error);
|
||||
assert!(contains_disk_error(result, DiskError::VolumeAccessDenied));
|
||||
|
||||
// Test Other error kind with FileAccessDenied -> specified permission error
|
||||
let io_error = create_io_error_with_disk_error(DiskError::FileAccessDenied);
|
||||
let result = to_access_error(io_error, DiskError::VolumeAccessDenied);
|
||||
assert!(contains_disk_error(result, DiskError::VolumeAccessDenied));
|
||||
|
||||
// Test Other error kind with FileNotFound -> VolumeNotFound
|
||||
let io_error = create_io_error_with_disk_error(DiskError::FileNotFound);
|
||||
let result = to_access_error(io_error, DiskError::VolumeAccessDenied);
|
||||
assert!(contains_disk_error(result, DiskError::VolumeNotFound));
|
||||
|
||||
// Test Other error kind with other DiskError -> passthrough
|
||||
let io_error = create_io_error_with_disk_error(DiskError::DiskFull);
|
||||
let result = to_access_error(io_error, DiskError::VolumeAccessDenied);
|
||||
assert!(contains_disk_error(result, DiskError::DiskFull));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_to_access_error_fallback_to_volume_error() {
|
||||
let permission_error = DiskError::FileAccessDenied;
|
||||
|
||||
// Test fallback to to_volume_error for unknown error kinds
|
||||
let result = to_access_error(create_io_error(ErrorKind::Interrupted), permission_error);
|
||||
assert_eq!(result.kind(), ErrorKind::Interrupted);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_to_unformatted_disk_error_basic_conversions() {
|
||||
// Test NotFound -> UnformattedDisk
|
||||
let result = to_unformatted_disk_error(create_io_error(ErrorKind::NotFound));
|
||||
assert!(contains_disk_error(result, DiskError::UnformattedDisk));
|
||||
|
||||
// Test PermissionDenied -> DiskAccessDenied
|
||||
let result = to_unformatted_disk_error(create_io_error(ErrorKind::PermissionDenied));
|
||||
assert!(contains_disk_error(result, DiskError::DiskAccessDenied));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_to_unformatted_disk_error_other_with_disk_error() {
|
||||
// Test Other error kind with FileNotFound -> UnformattedDisk
|
||||
let io_error = create_io_error_with_disk_error(DiskError::FileNotFound);
|
||||
let result = to_unformatted_disk_error(io_error);
|
||||
assert!(contains_disk_error(result, DiskError::UnformattedDisk));
|
||||
|
||||
// Test Other error kind with DiskNotFound -> UnformattedDisk
|
||||
let io_error = create_io_error_with_disk_error(DiskError::DiskNotFound);
|
||||
let result = to_unformatted_disk_error(io_error);
|
||||
assert!(contains_disk_error(result, DiskError::UnformattedDisk));
|
||||
|
||||
// Test Other error kind with VolumeNotFound -> UnformattedDisk
|
||||
let io_error = create_io_error_with_disk_error(DiskError::VolumeNotFound);
|
||||
let result = to_unformatted_disk_error(io_error);
|
||||
assert!(contains_disk_error(result, DiskError::UnformattedDisk));
|
||||
|
||||
// Test Other error kind with FileAccessDenied -> DiskAccessDenied
|
||||
let io_error = create_io_error_with_disk_error(DiskError::FileAccessDenied);
|
||||
let result = to_unformatted_disk_error(io_error);
|
||||
assert!(contains_disk_error(result, DiskError::DiskAccessDenied));
|
||||
|
||||
// Test Other error kind with DiskAccessDenied -> DiskAccessDenied
|
||||
let io_error = create_io_error_with_disk_error(DiskError::DiskAccessDenied);
|
||||
let result = to_unformatted_disk_error(io_error);
|
||||
assert!(contains_disk_error(result, DiskError::DiskAccessDenied));
|
||||
|
||||
// Test Other error kind with other DiskError -> CorruptedBackend
|
||||
let io_error = create_io_error_with_disk_error(DiskError::DiskFull);
|
||||
let result = to_unformatted_disk_error(io_error);
|
||||
assert!(contains_disk_error(result, DiskError::CorruptedBackend));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_to_unformatted_disk_error_recursive_behavior() {
|
||||
// Test with non-Other error kind that should be handled without infinite recursion
|
||||
let result = to_unformatted_disk_error(create_io_error(ErrorKind::Interrupted));
|
||||
// This should not cause infinite recursion and should produce CorruptedBackend
|
||||
assert!(contains_disk_error(result, DiskError::CorruptedBackend));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_error_chain_conversions() {
|
||||
// Test complex error conversion chains
|
||||
let original_error = create_io_error(ErrorKind::NotFound);
|
||||
|
||||
// Chain: NotFound -> FileNotFound (via to_file_error) -> VolumeNotFound (via to_volume_error)
|
||||
let file_error = to_file_error(original_error);
|
||||
let volume_error = to_volume_error(file_error);
|
||||
assert!(contains_disk_error(volume_error, DiskError::VolumeNotFound));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cross_platform_error_kinds() {
|
||||
// Test error kinds that may not be available on all platforms
|
||||
#[cfg(unix)]
|
||||
{
|
||||
let result = to_file_error(create_io_error(ErrorKind::TooManyLinks));
|
||||
assert!(contains_disk_error(result, DiskError::TooManyOpenFiles));
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
{
|
||||
let result = to_file_error(create_io_error(ErrorKind::StorageFull));
|
||||
assert!(contains_disk_error(result, DiskError::DiskFull));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_error_conversion_with_different_kinds() {
|
||||
// Test multiple error kinds to ensure comprehensive coverage
|
||||
let test_cases = vec![
|
||||
(ErrorKind::NotFound, DiskError::FileNotFound),
|
||||
(ErrorKind::PermissionDenied, DiskError::FileAccessDenied),
|
||||
(ErrorKind::IsADirectory, DiskError::IsNotRegular),
|
||||
(ErrorKind::InvalidData, DiskError::FileCorrupt),
|
||||
];
|
||||
|
||||
for (kind, expected_disk_error) in test_cases {
|
||||
let result = to_file_error(create_io_error(kind));
|
||||
assert!(
|
||||
contains_disk_error(result, expected_disk_error.clone()),
|
||||
"Failed for ErrorKind::{kind:?} -> DiskError::{expected_disk_error:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_volume_error_conversion_chain() {
|
||||
// Test volume error conversion with different input types
|
||||
let test_cases = vec![
|
||||
(ErrorKind::NotFound, DiskError::VolumeNotFound),
|
||||
(ErrorKind::PermissionDenied, DiskError::DiskAccessDenied),
|
||||
(ErrorKind::DirectoryNotEmpty, DiskError::VolumeNotEmpty),
|
||||
];
|
||||
|
||||
for (kind, expected_disk_error) in test_cases {
|
||||
let result = to_volume_error(create_io_error(kind));
|
||||
assert!(
|
||||
contains_disk_error(result, expected_disk_error.clone()),
|
||||
"Failed for ErrorKind::{kind:?} -> DiskError::{expected_disk_error:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use super::error::Error;
|
||||
|
||||
pub static OBJECT_OP_IGNORED_ERRS: &[Error] = &[
|
||||
Error::DiskNotFound,
|
||||
Error::FaultyDisk,
|
||||
Error::FaultyRemoteDisk,
|
||||
Error::DiskAccessDenied,
|
||||
Error::DiskOngoingReq,
|
||||
Error::UnformattedDisk,
|
||||
];
|
||||
|
||||
pub static BUCKET_OP_IGNORED_ERRS: &[Error] = &[
|
||||
Error::DiskNotFound,
|
||||
Error::FaultyDisk,
|
||||
Error::FaultyRemoteDisk,
|
||||
Error::DiskAccessDenied,
|
||||
Error::UnformattedDisk,
|
||||
];
|
||||
|
||||
pub static BASE_IGNORED_ERRS: &[Error] = &[Error::DiskNotFound, Error::FaultyDisk, Error::FaultyRemoteDisk];
|
||||
|
||||
pub fn reduce_write_quorum_errs(errors: &[Option<Error>], ignored_errs: &[Error], quorun: usize) -> Option<Error> {
|
||||
reduce_quorum_errs(errors, ignored_errs, quorun, Error::ErasureWriteQuorum)
|
||||
}
|
||||
|
||||
pub fn reduce_read_quorum_errs(errors: &[Option<Error>], ignored_errs: &[Error], quorun: usize) -> Option<Error> {
|
||||
reduce_quorum_errs(errors, ignored_errs, quorun, Error::ErasureReadQuorum)
|
||||
}
|
||||
|
||||
pub fn reduce_quorum_errs(errors: &[Option<Error>], ignored_errs: &[Error], quorun: usize, quorun_err: Error) -> Option<Error> {
|
||||
let (max_count, err) = reduce_errs(errors, ignored_errs);
|
||||
if max_count >= quorun { err } else { Some(quorun_err) }
|
||||
}
|
||||
|
||||
pub fn reduce_errs(errors: &[Option<Error>], ignored_errs: &[Error]) -> (usize, Option<Error>) {
|
||||
let nil_error = Error::other("nil".to_string());
|
||||
|
||||
// 首先统计 None 的数量(作为 nil 错误)
|
||||
let nil_count = errors.iter().filter(|e| e.is_none()).count();
|
||||
|
||||
let err_counts = errors
|
||||
.iter()
|
||||
.filter_map(|e| e.as_ref()) // 只处理 Some 的错误
|
||||
.fold(std::collections::HashMap::new(), |mut acc, e| {
|
||||
if is_ignored_err(ignored_errs, e) {
|
||||
return acc;
|
||||
}
|
||||
*acc.entry(e.clone()).or_insert(0) += 1;
|
||||
acc
|
||||
});
|
||||
|
||||
// 找到最高频率的非 nil 错误
|
||||
let (best_err, best_count) = err_counts
|
||||
.into_iter()
|
||||
.max_by(|(_, c1), (_, c2)| c1.cmp(c2))
|
||||
.unwrap_or((nil_error.clone(), 0));
|
||||
|
||||
// 比较 nil 错误和最高频率的非 nil 错误, 优先选择 nil 错误
|
||||
if nil_count > best_count || (nil_count == best_count && nil_count > 0) {
|
||||
(nil_count, None)
|
||||
} else {
|
||||
(best_count, Some(best_err))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_ignored_err(ignored_errs: &[Error], err: &Error) -> bool {
|
||||
ignored_errs.iter().any(|e| e == err)
|
||||
}
|
||||
|
||||
pub fn count_errs(errors: &[Option<Error>], err: &Error) -> usize {
|
||||
errors.iter().filter(|&e| e.as_ref() == Some(err)).count()
|
||||
}
|
||||
|
||||
pub fn is_all_buckets_not_found(errs: &[Option<Error>]) -> bool {
|
||||
for err in errs.iter() {
|
||||
if let Some(err) = err {
|
||||
if err == &Error::DiskNotFound || err == &Error::VolumeNotFound {
|
||||
continue;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
!errs.is_empty()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn err_io(msg: &str) -> Error {
|
||||
Error::Io(std::io::Error::other(msg))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_reduce_errs_basic() {
|
||||
let e1 = err_io("a");
|
||||
let e2 = err_io("b");
|
||||
let errors = vec![Some(e1.clone()), Some(e1.clone()), Some(e2.clone()), None];
|
||||
let ignored = vec![];
|
||||
let (count, err) = reduce_errs(&errors, &ignored);
|
||||
assert_eq!(count, 2);
|
||||
assert_eq!(err, Some(e1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_reduce_errs_ignored() {
|
||||
let e1 = err_io("a");
|
||||
let e2 = err_io("b");
|
||||
let errors = vec![Some(e1.clone()), Some(e2.clone()), Some(e1.clone()), Some(e2.clone()), None];
|
||||
let ignored = vec![e2.clone()];
|
||||
let (count, err) = reduce_errs(&errors, &ignored);
|
||||
assert_eq!(count, 2);
|
||||
assert_eq!(err, Some(e1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_reduce_quorum_errs() {
|
||||
let e1 = err_io("a");
|
||||
let e2 = err_io("b");
|
||||
let errors = vec![Some(e1.clone()), Some(e1.clone()), Some(e2.clone()), None];
|
||||
let ignored = vec![];
|
||||
let quorum_err = Error::FaultyDisk;
|
||||
// quorum = 2, should return e1
|
||||
let res = reduce_quorum_errs(&errors, &ignored, 2, quorum_err.clone());
|
||||
assert_eq!(res, Some(e1));
|
||||
// quorum = 3, should return quorum error
|
||||
let res = reduce_quorum_errs(&errors, &ignored, 3, quorum_err.clone());
|
||||
assert_eq!(res, Some(quorum_err));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_count_errs() {
|
||||
let e1 = err_io("a");
|
||||
let e2 = err_io("b");
|
||||
let errors = vec![Some(e1.clone()), Some(e2.clone()), Some(e1.clone()), None];
|
||||
assert_eq!(count_errs(&errors, &e1), 2);
|
||||
assert_eq!(count_errs(&errors, &e2), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_ignored_err() {
|
||||
let e1 = err_io("a");
|
||||
let e2 = err_io("b");
|
||||
let ignored = vec![e1.clone()];
|
||||
assert!(is_ignored_err(&ignored, &e1));
|
||||
assert!(!is_ignored_err(&ignored, &e2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_reduce_errs_nil_tiebreak() {
|
||||
// Error::Nil and another error have the same count, should prefer Nil
|
||||
let e1 = err_io("a");
|
||||
let errors = vec![Some(e1.clone()), None, Some(e1.clone()), None]; // e1:2, Nil:2
|
||||
let ignored = vec![];
|
||||
let (count, err) = reduce_errs(&errors, &ignored);
|
||||
assert_eq!(count, 2);
|
||||
assert_eq!(err, None); // None means Error::Nil is preferred
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,546 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use super::error::{Error, Result};
|
||||
use super::{DiskInfo, error::DiskError};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Error as JsonError;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
|
||||
pub enum FormatMetaVersion {
|
||||
#[serde(rename = "1")]
|
||||
V1,
|
||||
|
||||
#[serde(other)]
|
||||
Unknown,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
|
||||
pub enum FormatBackend {
|
||||
#[serde(rename = "xl")]
|
||||
Erasure,
|
||||
#[serde(rename = "xl-single")]
|
||||
ErasureSingle,
|
||||
|
||||
#[serde(other)]
|
||||
Unknown,
|
||||
}
|
||||
|
||||
/// Represents the V3 backend disk structure version
|
||||
/// under `.rustfs.sys` and actual data namespace.
|
||||
///
|
||||
/// FormatErasureV3 - structure holds format config version '3'.
|
||||
///
|
||||
/// The V3 format to support "large bucket" support where a bucket
|
||||
/// can span multiple erasure sets.
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
|
||||
pub struct FormatErasureV3 {
|
||||
/// Version of 'xl' format.
|
||||
pub version: FormatErasureVersion,
|
||||
|
||||
/// This field carries assigned disk uuid.
|
||||
pub this: Uuid,
|
||||
|
||||
/// Sets field carries the input disk order generated the first
|
||||
/// time when fresh disks were supplied, it is a two-dimensional
|
||||
/// array second dimension represents list of disks used per set.
|
||||
pub sets: Vec<Vec<Uuid>>,
|
||||
|
||||
/// Distribution algorithm represents the hashing algorithm
|
||||
/// to pick the right set index for an object.
|
||||
#[serde(rename = "distributionAlgo")]
|
||||
pub distribution_algo: DistributionAlgoVersion,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
|
||||
pub enum FormatErasureVersion {
|
||||
#[serde(rename = "1")]
|
||||
V1,
|
||||
#[serde(rename = "2")]
|
||||
V2,
|
||||
#[serde(rename = "3")]
|
||||
V3,
|
||||
|
||||
#[serde(other)]
|
||||
Unknown,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
|
||||
pub enum DistributionAlgoVersion {
|
||||
#[serde(rename = "CRCMOD")]
|
||||
V1,
|
||||
#[serde(rename = "SIPMOD")]
|
||||
V2,
|
||||
#[serde(rename = "SIPMOD+PARITY")]
|
||||
V3,
|
||||
}
|
||||
|
||||
/// format.json currently has the format:
|
||||
///
|
||||
/// ```json
|
||||
/// {
|
||||
/// "version": "1",
|
||||
/// "format": "XXXXX",
|
||||
/// "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
|
||||
/// "XXXXX": {
|
||||
//
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// Ideally we will never have a situation where we will have to change the
|
||||
/// fields of this struct and deal with related migration.
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
|
||||
pub struct FormatV3 {
|
||||
/// Version of the format config.
|
||||
pub version: FormatMetaVersion,
|
||||
|
||||
/// Format indicates the backend format type, supports two values 'xl' and 'xl-single'.
|
||||
pub format: FormatBackend,
|
||||
|
||||
/// ID is the identifier for the rustfs deployment
|
||||
pub id: Uuid,
|
||||
|
||||
#[serde(rename = "xl")]
|
||||
pub erasure: FormatErasureV3,
|
||||
// /// DiskInfo is an extended type which returns current
|
||||
// /// disk usage per path.
|
||||
#[serde(skip)]
|
||||
pub disk_info: Option<DiskInfo>,
|
||||
}
|
||||
|
||||
impl TryFrom<&[u8]> for FormatV3 {
|
||||
type Error = JsonError;
|
||||
|
||||
fn try_from(data: &[u8]) -> std::result::Result<Self, Self::Error> {
|
||||
serde_json::from_slice(data)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&str> for FormatV3 {
|
||||
type Error = JsonError;
|
||||
|
||||
fn try_from(data: &str) -> std::result::Result<Self, Self::Error> {
|
||||
serde_json::from_str(data)
|
||||
}
|
||||
}
|
||||
|
||||
impl FormatV3 {
|
||||
/// Create a new format config with the given number of sets and set length.
|
||||
pub fn new(num_sets: usize, set_len: usize) -> Self {
|
||||
let format = if set_len == 1 {
|
||||
FormatBackend::ErasureSingle
|
||||
} else {
|
||||
FormatBackend::Erasure
|
||||
};
|
||||
|
||||
let erasure = FormatErasureV3 {
|
||||
version: FormatErasureVersion::V3,
|
||||
this: Uuid::nil(),
|
||||
sets: (0..num_sets)
|
||||
.map(|_| (0..set_len).map(|_| Uuid::new_v4()).collect())
|
||||
.collect(),
|
||||
distribution_algo: DistributionAlgoVersion::V3,
|
||||
};
|
||||
|
||||
Self {
|
||||
version: FormatMetaVersion::V1,
|
||||
format,
|
||||
id: Uuid::new_v4(),
|
||||
erasure,
|
||||
disk_info: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the number of drives in the erasure set.
|
||||
pub fn drives(&self) -> usize {
|
||||
self.erasure.sets.iter().map(|v| v.len()).sum()
|
||||
}
|
||||
|
||||
pub fn to_json(&self) -> std::result::Result<String, JsonError> {
|
||||
serde_json::to_string(self)
|
||||
}
|
||||
|
||||
/// returns the i,j'th position of the input `diskID` against the reference
|
||||
///
|
||||
/// format, after successful validation.
|
||||
/// - i'th position is the set index
|
||||
/// - j'th position is the disk index in the current set
|
||||
pub fn find_disk_index_by_disk_id(&self, disk_id: Uuid) -> Result<(usize, usize)> {
|
||||
if disk_id == Uuid::nil() {
|
||||
return Err(Error::from(DiskError::DiskNotFound));
|
||||
}
|
||||
if disk_id == Uuid::max() {
|
||||
return Err(Error::other("disk offline"));
|
||||
}
|
||||
|
||||
for (i, set) in self.erasure.sets.iter().enumerate() {
|
||||
for (j, d) in set.iter().enumerate() {
|
||||
if disk_id.eq(d) {
|
||||
return Ok((i, j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Err(Error::other(format!("disk id not found {disk_id}")))
|
||||
}
|
||||
|
||||
pub fn check_other(&self, other: &FormatV3) -> Result<()> {
|
||||
let mut tmp = other.clone();
|
||||
let this = tmp.erasure.this;
|
||||
tmp.erasure.this = Uuid::nil();
|
||||
|
||||
if self.erasure.sets.len() != other.erasure.sets.len() {
|
||||
return Err(Error::other(format!(
|
||||
"Expected number of sets {}, got {}",
|
||||
self.erasure.sets.len(),
|
||||
other.erasure.sets.len()
|
||||
)));
|
||||
}
|
||||
|
||||
for i in 0..self.erasure.sets.len() {
|
||||
if self.erasure.sets[i].len() != other.erasure.sets[i].len() {
|
||||
return Err(Error::other(format!(
|
||||
"Each set should be of same size, expected {}, got {}",
|
||||
self.erasure.sets[i].len(),
|
||||
other.erasure.sets[i].len()
|
||||
)));
|
||||
}
|
||||
|
||||
for j in 0..self.erasure.sets[i].len() {
|
||||
if self.erasure.sets[i][j] != other.erasure.sets[i][j] {
|
||||
return Err(Error::other(format!(
|
||||
"UUID on positions {}:{} do not match with, expected {:?} got {:?}: (%w)",
|
||||
i,
|
||||
j,
|
||||
self.erasure.sets[i][j].to_string(),
|
||||
other.erasure.sets[i][j].to_string(),
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for i in 0..tmp.erasure.sets.len() {
|
||||
for j in 0..tmp.erasure.sets[i].len() {
|
||||
if this == tmp.erasure.sets[i][j] {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Err(Error::other(format!(
|
||||
"DriveID {:?} not found in any drive sets {:?}",
|
||||
this, other.erasure.sets
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_format_v1() {
|
||||
let format = FormatV3::new(1, 4);
|
||||
|
||||
let str = serde_json::to_string(&format);
|
||||
println!("{str:?}");
|
||||
|
||||
let data = r#"
|
||||
{
|
||||
"version": "1",
|
||||
"format": "xl",
|
||||
"id": "321b3874-987d-4c15-8fa5-757c956b1243",
|
||||
"xl": {
|
||||
"version": "1",
|
||||
"this": null,
|
||||
"sets": [
|
||||
[
|
||||
"8ab9a908-f869-4f1f-8e42-eb067ffa7eb5",
|
||||
"c26315da-05cf-4778-a9ea-b44ea09f58c5",
|
||||
"fb87a891-18d3-44cf-a46f-bcc15093a038",
|
||||
"356a925c-57b9-4313-88b3-053edf1104dc"
|
||||
]
|
||||
],
|
||||
"distributionAlgo": "CRCMOD"
|
||||
}
|
||||
}"#;
|
||||
|
||||
let p = FormatV3::try_from(data);
|
||||
|
||||
println!("{p:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_v3_new_single_disk() {
|
||||
let format = FormatV3::new(1, 1);
|
||||
|
||||
assert_eq!(format.version, FormatMetaVersion::V1);
|
||||
assert_eq!(format.format, FormatBackend::ErasureSingle);
|
||||
assert_eq!(format.erasure.version, FormatErasureVersion::V3);
|
||||
assert_eq!(format.erasure.sets.len(), 1);
|
||||
assert_eq!(format.erasure.sets[0].len(), 1);
|
||||
assert_eq!(format.erasure.distribution_algo, DistributionAlgoVersion::V3);
|
||||
assert_eq!(format.erasure.this, Uuid::nil());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_v3_new_multiple_sets() {
|
||||
let format = FormatV3::new(2, 4);
|
||||
|
||||
assert_eq!(format.version, FormatMetaVersion::V1);
|
||||
assert_eq!(format.format, FormatBackend::Erasure);
|
||||
assert_eq!(format.erasure.version, FormatErasureVersion::V3);
|
||||
assert_eq!(format.erasure.sets.len(), 2);
|
||||
assert_eq!(format.erasure.sets[0].len(), 4);
|
||||
assert_eq!(format.erasure.sets[1].len(), 4);
|
||||
assert_eq!(format.erasure.distribution_algo, DistributionAlgoVersion::V3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_v3_drives() {
|
||||
let format = FormatV3::new(2, 4);
|
||||
assert_eq!(format.drives(), 8); // 2 sets * 4 drives each
|
||||
|
||||
let format_single = FormatV3::new(1, 1);
|
||||
assert_eq!(format_single.drives(), 1); // 1 set * 1 drive
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_v3_to_json() {
|
||||
let format = FormatV3::new(1, 2);
|
||||
let json_result = format.to_json();
|
||||
|
||||
assert!(json_result.is_ok());
|
||||
let json_str = json_result.unwrap();
|
||||
assert!(json_str.contains("\"version\":\"1\""));
|
||||
assert!(json_str.contains("\"format\":\"xl\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_v3_from_json() {
|
||||
let json_data = r#"{
|
||||
"version": "1",
|
||||
"format": "xl-single",
|
||||
"id": "321b3874-987d-4c15-8fa5-757c956b1243",
|
||||
"xl": {
|
||||
"version": "3",
|
||||
"this": "8ab9a908-f869-4f1f-8e42-eb067ffa7eb5",
|
||||
"sets": [
|
||||
[
|
||||
"8ab9a908-f869-4f1f-8e42-eb067ffa7eb5"
|
||||
]
|
||||
],
|
||||
"distributionAlgo": "SIPMOD+PARITY"
|
||||
}
|
||||
}"#;
|
||||
|
||||
let format = FormatV3::try_from(json_data);
|
||||
assert!(format.is_ok());
|
||||
|
||||
let format = format.unwrap();
|
||||
assert_eq!(format.format, FormatBackend::ErasureSingle);
|
||||
assert_eq!(format.erasure.version, FormatErasureVersion::V3);
|
||||
assert_eq!(format.erasure.distribution_algo, DistributionAlgoVersion::V3);
|
||||
assert_eq!(format.erasure.sets.len(), 1);
|
||||
assert_eq!(format.erasure.sets[0].len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_v3_from_bytes() {
|
||||
let json_data = r#"{
|
||||
"version": "1",
|
||||
"format": "xl",
|
||||
"id": "321b3874-987d-4c15-8fa5-757c956b1243",
|
||||
"xl": {
|
||||
"version": "2",
|
||||
"this": "00000000-0000-0000-0000-000000000000",
|
||||
"sets": [
|
||||
[
|
||||
"8ab9a908-f869-4f1f-8e42-eb067ffa7eb5",
|
||||
"c26315da-05cf-4778-a9ea-b44ea09f58c5"
|
||||
]
|
||||
],
|
||||
"distributionAlgo": "SIPMOD"
|
||||
}
|
||||
}"#;
|
||||
|
||||
let format = FormatV3::try_from(json_data.as_bytes());
|
||||
assert!(format.is_ok());
|
||||
|
||||
let format = format.unwrap();
|
||||
assert_eq!(format.erasure.version, FormatErasureVersion::V2);
|
||||
assert_eq!(format.erasure.distribution_algo, DistributionAlgoVersion::V2);
|
||||
assert_eq!(format.erasure.sets[0].len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_v3_invalid_json() {
|
||||
let invalid_json = r#"{"invalid": "json"}"#;
|
||||
let format = FormatV3::try_from(invalid_json);
|
||||
assert!(format.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_disk_index_by_disk_id() {
|
||||
let mut format = FormatV3::new(2, 2);
|
||||
let target_disk_id = Uuid::new_v4();
|
||||
format.erasure.sets[1][0] = target_disk_id;
|
||||
|
||||
let result = format.find_disk_index_by_disk_id(target_disk_id);
|
||||
assert!(result.is_ok());
|
||||
assert_eq!(result.unwrap(), (1, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_disk_index_nil_uuid() {
|
||||
let format = FormatV3::new(1, 2);
|
||||
let result = format.find_disk_index_by_disk_id(Uuid::nil());
|
||||
assert!(result.is_err());
|
||||
assert!(matches!(result.unwrap_err(), Error::DiskNotFound));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_disk_index_max_uuid() {
|
||||
let format = FormatV3::new(1, 2);
|
||||
let result = format.find_disk_index_by_disk_id(Uuid::max());
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_disk_index_not_found() {
|
||||
let format = FormatV3::new(1, 2);
|
||||
let non_existent_id = Uuid::new_v4();
|
||||
let result = format.find_disk_index_by_disk_id(non_existent_id);
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_check_other_identical() {
|
||||
let format1 = FormatV3::new(2, 4);
|
||||
let mut format2 = format1.clone();
|
||||
format2.erasure.this = format1.erasure.sets[0][0];
|
||||
|
||||
let result = format1.check_other(&format2);
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_check_other_different_set_count() {
|
||||
let format1 = FormatV3::new(2, 4);
|
||||
let format2 = FormatV3::new(3, 4);
|
||||
|
||||
let result = format1.check_other(&format2);
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_check_other_different_set_size() {
|
||||
let format1 = FormatV3::new(2, 4);
|
||||
let format2 = FormatV3::new(2, 6);
|
||||
|
||||
let result = format1.check_other(&format2);
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_check_other_different_disk_id() {
|
||||
let format1 = FormatV3::new(1, 2);
|
||||
let mut format2 = format1.clone();
|
||||
format2.erasure.sets[0][0] = Uuid::new_v4();
|
||||
|
||||
let result = format1.check_other(&format2);
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_check_other_disk_not_in_sets() {
|
||||
let format1 = FormatV3::new(1, 2);
|
||||
let mut format2 = format1.clone();
|
||||
format2.erasure.this = Uuid::new_v4(); // Set to a UUID not in any set
|
||||
|
||||
let result = format1.check_other(&format2);
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_meta_version_serialization() {
|
||||
let v1 = FormatMetaVersion::V1;
|
||||
let json = serde_json::to_string(&v1).unwrap();
|
||||
assert_eq!(json, "\"1\"");
|
||||
|
||||
let unknown = FormatMetaVersion::Unknown;
|
||||
let deserialized: FormatMetaVersion = serde_json::from_str("\"unknown\"").unwrap();
|
||||
assert_eq!(deserialized, unknown);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_backend_serialization() {
|
||||
let erasure = FormatBackend::Erasure;
|
||||
let json = serde_json::to_string(&erasure).unwrap();
|
||||
assert_eq!(json, "\"xl\"");
|
||||
|
||||
let single = FormatBackend::ErasureSingle;
|
||||
let json = serde_json::to_string(&single).unwrap();
|
||||
assert_eq!(json, "\"xl-single\"");
|
||||
|
||||
let unknown = FormatBackend::Unknown;
|
||||
let deserialized: FormatBackend = serde_json::from_str("\"unknown\"").unwrap();
|
||||
assert_eq!(deserialized, unknown);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_erasure_version_serialization() {
|
||||
let v1 = FormatErasureVersion::V1;
|
||||
let json = serde_json::to_string(&v1).unwrap();
|
||||
assert_eq!(json, "\"1\"");
|
||||
|
||||
let v2 = FormatErasureVersion::V2;
|
||||
let json = serde_json::to_string(&v2).unwrap();
|
||||
assert_eq!(json, "\"2\"");
|
||||
|
||||
let v3 = FormatErasureVersion::V3;
|
||||
let json = serde_json::to_string(&v3).unwrap();
|
||||
assert_eq!(json, "\"3\"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_distribution_algo_version_serialization() {
|
||||
let v1 = DistributionAlgoVersion::V1;
|
||||
let json = serde_json::to_string(&v1).unwrap();
|
||||
assert_eq!(json, "\"CRCMOD\"");
|
||||
|
||||
let v2 = DistributionAlgoVersion::V2;
|
||||
let json = serde_json::to_string(&v2).unwrap();
|
||||
assert_eq!(json, "\"SIPMOD\"");
|
||||
|
||||
let v3 = DistributionAlgoVersion::V3;
|
||||
let json = serde_json::to_string(&v3).unwrap();
|
||||
assert_eq!(json, "\"SIPMOD+PARITY\"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_v3_round_trip_serialization() {
|
||||
let original = FormatV3::new(2, 3);
|
||||
let json = original.to_json().unwrap();
|
||||
let deserialized = FormatV3::try_from(json.as_str()).unwrap();
|
||||
|
||||
assert_eq!(original.version, deserialized.version);
|
||||
assert_eq!(original.format, deserialized.format);
|
||||
assert_eq!(original.erasure.version, deserialized.erasure.version);
|
||||
assert_eq!(original.erasure.sets.len(), deserialized.erasure.sets.len());
|
||||
assert_eq!(original.erasure.distribution_algo, deserialized.erasure.distribution_algo);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,537 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use std::{fs::Metadata, path::Path};
|
||||
|
||||
use tokio::{
|
||||
fs::{self, File},
|
||||
io,
|
||||
};
|
||||
|
||||
#[cfg(not(windows))]
|
||||
pub fn same_file(f1: &Metadata, f2: &Metadata) -> bool {
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
|
||||
if f1.dev() != f2.dev() {
|
||||
return false;
|
||||
}
|
||||
|
||||
if f1.ino() != f2.ino() {
|
||||
return false;
|
||||
}
|
||||
|
||||
if f1.size() != f2.size() {
|
||||
return false;
|
||||
}
|
||||
if f1.permissions() != f2.permissions() {
|
||||
return false;
|
||||
}
|
||||
|
||||
if f1.mtime() != f2.mtime() {
|
||||
return false;
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
pub fn same_file(f1: &Metadata, f2: &Metadata) -> bool {
|
||||
if f1.permissions() != f2.permissions() {
|
||||
return false;
|
||||
}
|
||||
|
||||
if f1.file_type() != f2.file_type() {
|
||||
return false;
|
||||
}
|
||||
|
||||
if f1.len() != f2.len() {
|
||||
return false;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
type FileMode = usize;
|
||||
|
||||
pub const O_RDONLY: FileMode = 0x00000;
|
||||
pub const O_WRONLY: FileMode = 0x00001;
|
||||
pub const O_RDWR: FileMode = 0x00002;
|
||||
pub const O_CREATE: FileMode = 0x00040;
|
||||
// pub const O_EXCL: FileMode = 0x00080;
|
||||
// pub const O_NOCTTY: FileMode = 0x00100;
|
||||
pub const O_TRUNC: FileMode = 0x00200;
|
||||
// pub const O_NONBLOCK: FileMode = 0x00800;
|
||||
pub const O_APPEND: FileMode = 0x00400;
|
||||
// pub const O_SYNC: FileMode = 0x01000;
|
||||
// pub const O_ASYNC: FileMode = 0x02000;
|
||||
// pub const O_CLOEXEC: FileMode = 0x80000;
|
||||
|
||||
// read: bool,
|
||||
// write: bool,
|
||||
// append: bool,
|
||||
// truncate: bool,
|
||||
// create: bool,
|
||||
// create_new: bool,
|
||||
|
||||
pub async fn open_file(path: impl AsRef<Path>, mode: FileMode) -> io::Result<File> {
|
||||
let mut opts = fs::OpenOptions::new();
|
||||
|
||||
match mode & (O_RDONLY | O_WRONLY | O_RDWR) {
|
||||
O_RDONLY => {
|
||||
opts.read(true);
|
||||
}
|
||||
O_WRONLY => {
|
||||
opts.write(true);
|
||||
}
|
||||
O_RDWR => {
|
||||
opts.read(true);
|
||||
opts.write(true);
|
||||
}
|
||||
_ => (),
|
||||
};
|
||||
|
||||
if mode & O_CREATE != 0 {
|
||||
opts.create(true);
|
||||
}
|
||||
|
||||
if mode & O_APPEND != 0 {
|
||||
opts.append(true);
|
||||
}
|
||||
|
||||
if mode & O_TRUNC != 0 {
|
||||
opts.truncate(true);
|
||||
}
|
||||
|
||||
opts.open(path.as_ref()).await
|
||||
}
|
||||
|
||||
pub async fn access(path: impl AsRef<Path>) -> io::Result<()> {
|
||||
fs::metadata(path).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn access_std(path: impl AsRef<Path>) -> io::Result<()> {
|
||||
tokio::task::block_in_place(|| std::fs::metadata(path))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn lstat(path: impl AsRef<Path>) -> io::Result<Metadata> {
|
||||
fs::metadata(path).await
|
||||
}
|
||||
|
||||
pub fn lstat_std(path: impl AsRef<Path>) -> io::Result<Metadata> {
|
||||
tokio::task::block_in_place(|| std::fs::metadata(path))
|
||||
}
|
||||
|
||||
pub async fn make_dir_all(path: impl AsRef<Path>) -> io::Result<()> {
|
||||
fs::create_dir_all(path.as_ref()).await
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "debug", skip_all)]
|
||||
pub async fn remove(path: impl AsRef<Path>) -> io::Result<()> {
|
||||
let meta = fs::metadata(path.as_ref()).await?;
|
||||
if meta.is_dir() {
|
||||
fs::remove_dir(path.as_ref()).await
|
||||
} else {
|
||||
fs::remove_file(path.as_ref()).await
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn remove_all(path: impl AsRef<Path>) -> io::Result<()> {
|
||||
let meta = fs::metadata(path.as_ref()).await?;
|
||||
if meta.is_dir() {
|
||||
fs::remove_dir_all(path.as_ref()).await
|
||||
} else {
|
||||
fs::remove_file(path.as_ref()).await
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "debug", skip_all)]
|
||||
pub fn remove_std(path: impl AsRef<Path>) -> io::Result<()> {
|
||||
let path = path.as_ref();
|
||||
tokio::task::block_in_place(|| {
|
||||
let meta = std::fs::metadata(path)?;
|
||||
if meta.is_dir() {
|
||||
std::fs::remove_dir(path)
|
||||
} else {
|
||||
std::fs::remove_file(path)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn remove_all_std(path: impl AsRef<Path>) -> io::Result<()> {
|
||||
let path = path.as_ref();
|
||||
tokio::task::block_in_place(|| {
|
||||
let meta = std::fs::metadata(path)?;
|
||||
if meta.is_dir() {
|
||||
std::fs::remove_dir_all(path)
|
||||
} else {
|
||||
std::fs::remove_file(path)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn mkdir(path: impl AsRef<Path>) -> io::Result<()> {
|
||||
fs::create_dir(path.as_ref()).await
|
||||
}
|
||||
|
||||
pub async fn rename(from: impl AsRef<Path>, to: impl AsRef<Path>) -> io::Result<()> {
|
||||
fs::rename(from, to).await
|
||||
}
|
||||
|
||||
pub fn rename_std(from: impl AsRef<Path>, to: impl AsRef<Path>) -> io::Result<()> {
|
||||
tokio::task::block_in_place(|| std::fs::rename(from, to))
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "debug", skip_all)]
|
||||
pub async fn read_file(path: impl AsRef<Path>) -> io::Result<Vec<u8>> {
|
||||
fs::read(path.as_ref()).await
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use tempfile::TempDir;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_file_mode_constants() {
|
||||
assert_eq!(O_RDONLY, 0x00000);
|
||||
assert_eq!(O_WRONLY, 0x00001);
|
||||
assert_eq!(O_RDWR, 0x00002);
|
||||
assert_eq!(O_CREATE, 0x00040);
|
||||
assert_eq!(O_TRUNC, 0x00200);
|
||||
assert_eq!(O_APPEND, 0x00400);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_open_file_read_only() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let file_path = temp_dir.path().join("test_readonly.txt");
|
||||
|
||||
// Create a test file
|
||||
tokio::fs::write(&file_path, b"test content").await.unwrap();
|
||||
|
||||
// Test opening in read-only mode
|
||||
let file = open_file(&file_path, O_RDONLY).await;
|
||||
assert!(file.is_ok());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_open_file_write_only() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let file_path = temp_dir.path().join("test_writeonly.txt");
|
||||
|
||||
// Test opening in write-only mode with create flag
|
||||
let mut file = open_file(&file_path, O_WRONLY | O_CREATE).await.unwrap();
|
||||
|
||||
// Should be able to write
|
||||
file.write_all(b"write test").await.unwrap();
|
||||
file.flush().await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_open_file_read_write() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let file_path = temp_dir.path().join("test_readwrite.txt");
|
||||
|
||||
// Test opening in read-write mode with create flag
|
||||
let mut file = open_file(&file_path, O_RDWR | O_CREATE).await.unwrap();
|
||||
|
||||
// Should be able to write and read
|
||||
file.write_all(b"read-write test").await.unwrap();
|
||||
file.flush().await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_open_file_append() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let file_path = temp_dir.path().join("test_append.txt");
|
||||
|
||||
// Create initial content
|
||||
tokio::fs::write(&file_path, b"initial").await.unwrap();
|
||||
|
||||
// Open in append mode
|
||||
let mut file = open_file(&file_path, O_WRONLY | O_APPEND).await.unwrap();
|
||||
file.write_all(b" appended").await.unwrap();
|
||||
file.flush().await.unwrap();
|
||||
|
||||
// Verify content
|
||||
let content = tokio::fs::read_to_string(&file_path).await.unwrap();
|
||||
assert_eq!(content, "initial appended");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_open_file_truncate() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let file_path = temp_dir.path().join("test_truncate.txt");
|
||||
|
||||
// Create initial content
|
||||
tokio::fs::write(&file_path, b"initial content").await.unwrap();
|
||||
|
||||
// Open with truncate flag
|
||||
let mut file = open_file(&file_path, O_WRONLY | O_TRUNC).await.unwrap();
|
||||
file.write_all(b"new").await.unwrap();
|
||||
file.flush().await.unwrap();
|
||||
|
||||
// Verify content was truncated
|
||||
let content = tokio::fs::read_to_string(&file_path).await.unwrap();
|
||||
assert_eq!(content, "new");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_access() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let file_path = temp_dir.path().join("test_access.txt");
|
||||
|
||||
// Should fail for non-existent file
|
||||
assert!(access(&file_path).await.is_err());
|
||||
|
||||
// Create file and test again
|
||||
tokio::fs::write(&file_path, b"test").await.unwrap();
|
||||
assert!(access(&file_path).await.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_access_std() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let file_path = temp_dir.path().join("test_access_std.txt");
|
||||
|
||||
// Should fail for non-existent file
|
||||
assert!(access_std(&file_path).is_err());
|
||||
|
||||
// Create file and test again
|
||||
std::fs::write(&file_path, b"test").unwrap();
|
||||
assert!(access_std(&file_path).is_ok());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_lstat() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let file_path = temp_dir.path().join("test_lstat.txt");
|
||||
|
||||
// Create test file
|
||||
tokio::fs::write(&file_path, b"test content").await.unwrap();
|
||||
|
||||
// Test lstat
|
||||
let metadata = lstat(&file_path).await.unwrap();
|
||||
assert!(metadata.is_file());
|
||||
assert_eq!(metadata.len(), 12); // "test content" is 12 bytes
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_lstat_std() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let file_path = temp_dir.path().join("test_lstat_std.txt");
|
||||
|
||||
// Create test file
|
||||
std::fs::write(&file_path, b"test content").unwrap();
|
||||
|
||||
// Test lstat_std
|
||||
let metadata = lstat_std(&file_path).unwrap();
|
||||
assert!(metadata.is_file());
|
||||
assert_eq!(metadata.len(), 12); // "test content" is 12 bytes
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_make_dir_all() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let nested_path = temp_dir.path().join("level1").join("level2").join("level3");
|
||||
|
||||
// Should create nested directories
|
||||
assert!(make_dir_all(&nested_path).await.is_ok());
|
||||
assert!(nested_path.exists());
|
||||
assert!(nested_path.is_dir());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_remove_file() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let file_path = temp_dir.path().join("test_remove.txt");
|
||||
|
||||
// Create test file
|
||||
tokio::fs::write(&file_path, b"test").await.unwrap();
|
||||
assert!(file_path.exists());
|
||||
|
||||
// Remove file
|
||||
assert!(remove(&file_path).await.is_ok());
|
||||
assert!(!file_path.exists());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_remove_directory() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let dir_path = temp_dir.path().join("test_remove_dir");
|
||||
|
||||
// Create test directory
|
||||
tokio::fs::create_dir(&dir_path).await.unwrap();
|
||||
assert!(dir_path.exists());
|
||||
|
||||
// Remove directory
|
||||
assert!(remove(&dir_path).await.is_ok());
|
||||
assert!(!dir_path.exists());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_remove_all() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let dir_path = temp_dir.path().join("test_remove_all");
|
||||
let file_path = dir_path.join("nested_file.txt");
|
||||
|
||||
// Create nested structure
|
||||
tokio::fs::create_dir(&dir_path).await.unwrap();
|
||||
tokio::fs::write(&file_path, b"nested content").await.unwrap();
|
||||
|
||||
// Remove all
|
||||
assert!(remove_all(&dir_path).await.is_ok());
|
||||
assert!(!dir_path.exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_remove_std() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let file_path = temp_dir.path().join("test_remove_std.txt");
|
||||
|
||||
// Create test file
|
||||
std::fs::write(&file_path, b"test").unwrap();
|
||||
assert!(file_path.exists());
|
||||
|
||||
// Remove file
|
||||
assert!(remove_std(&file_path).is_ok());
|
||||
assert!(!file_path.exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_remove_all_std() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let dir_path = temp_dir.path().join("test_remove_all_std");
|
||||
let file_path = dir_path.join("nested_file.txt");
|
||||
|
||||
// Create nested structure
|
||||
std::fs::create_dir(&dir_path).unwrap();
|
||||
std::fs::write(&file_path, b"nested content").unwrap();
|
||||
|
||||
// Remove all
|
||||
assert!(remove_all_std(&dir_path).is_ok());
|
||||
assert!(!dir_path.exists());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_mkdir() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let dir_path = temp_dir.path().join("test_mkdir");
|
||||
|
||||
// Create directory
|
||||
assert!(mkdir(&dir_path).await.is_ok());
|
||||
assert!(dir_path.exists());
|
||||
assert!(dir_path.is_dir());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_rename() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let old_path = temp_dir.path().join("old_name.txt");
|
||||
let new_path = temp_dir.path().join("new_name.txt");
|
||||
|
||||
// Create test file
|
||||
tokio::fs::write(&old_path, b"test content").await.unwrap();
|
||||
assert!(old_path.exists());
|
||||
assert!(!new_path.exists());
|
||||
|
||||
// Rename file
|
||||
assert!(rename(&old_path, &new_path).await.is_ok());
|
||||
assert!(!old_path.exists());
|
||||
assert!(new_path.exists());
|
||||
|
||||
// Verify content preserved
|
||||
let content = tokio::fs::read_to_string(&new_path).await.unwrap();
|
||||
assert_eq!(content, "test content");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rename_std() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let old_path = temp_dir.path().join("old_name_std.txt");
|
||||
let new_path = temp_dir.path().join("new_name_std.txt");
|
||||
|
||||
// Create test file
|
||||
std::fs::write(&old_path, b"test content").unwrap();
|
||||
assert!(old_path.exists());
|
||||
assert!(!new_path.exists());
|
||||
|
||||
// Rename file
|
||||
assert!(rename_std(&old_path, &new_path).is_ok());
|
||||
assert!(!old_path.exists());
|
||||
assert!(new_path.exists());
|
||||
|
||||
// Verify content preserved
|
||||
let content = std::fs::read_to_string(&new_path).unwrap();
|
||||
assert_eq!(content, "test content");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_read_file() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let file_path = temp_dir.path().join("test_read.txt");
|
||||
|
||||
let test_content = b"This is test content for reading";
|
||||
tokio::fs::write(&file_path, test_content).await.unwrap();
|
||||
|
||||
// Read file
|
||||
let read_content = read_file(&file_path).await.unwrap();
|
||||
assert_eq!(read_content, test_content);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_read_file_nonexistent() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let file_path = temp_dir.path().join("nonexistent.txt");
|
||||
|
||||
// Should fail for non-existent file
|
||||
assert!(read_file(&file_path).await.is_err());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_same_file() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let file_path = temp_dir.path().join("test_same.txt");
|
||||
|
||||
// Create test file
|
||||
tokio::fs::write(&file_path, b"test content").await.unwrap();
|
||||
|
||||
// Get metadata twice
|
||||
let metadata1 = tokio::fs::metadata(&file_path).await.unwrap();
|
||||
let metadata2 = tokio::fs::metadata(&file_path).await.unwrap();
|
||||
|
||||
// Should be the same file
|
||||
assert!(same_file(&metadata1, &metadata2));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_different_files() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let file1_path = temp_dir.path().join("file1.txt");
|
||||
let file2_path = temp_dir.path().join("file2.txt");
|
||||
|
||||
// Create two different files
|
||||
tokio::fs::write(&file1_path, b"content1").await.unwrap();
|
||||
tokio::fs::write(&file2_path, b"content2").await.unwrap();
|
||||
|
||||
// Get metadata
|
||||
let metadata1 = tokio::fs::metadata(&file1_path).await.unwrap();
|
||||
let metadata2 = tokio::fs::metadata(&file2_path).await.unwrap();
|
||||
|
||||
// Should be different files
|
||||
assert!(!same_file(&metadata1, &metadata2));
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,229 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use std::{
|
||||
io,
|
||||
path::{Component, Path},
|
||||
};
|
||||
|
||||
use super::error::Result;
|
||||
use crate::disk::error_conv::to_file_error;
|
||||
use rustfs_utils::path::SLASH_SEPARATOR;
|
||||
use tokio::fs;
|
||||
use tracing::warn;
|
||||
|
||||
use super::error::DiskError;
|
||||
|
||||
pub fn check_path_length(path_name: &str) -> Result<()> {
|
||||
// Apple OS X path length is limited to 1016
|
||||
if cfg!(target_os = "macos") && path_name.len() > 1016 {
|
||||
return Err(DiskError::FileNameTooLong);
|
||||
}
|
||||
|
||||
// Disallow more than 1024 characters on windows, there
|
||||
// are no known name_max limits on Windows.
|
||||
if cfg!(target_os = "windows") && path_name.len() > 1024 {
|
||||
return Err(DiskError::FileNameTooLong);
|
||||
}
|
||||
|
||||
// On Unix we reject paths if they are just '.', '..' or '/'
|
||||
let invalid_paths = [".", "..", "/"];
|
||||
if invalid_paths.contains(&path_name) {
|
||||
return Err(DiskError::FileAccessDenied);
|
||||
}
|
||||
|
||||
// Check each path segment length is > 255 on all Unix
|
||||
// platforms, look for this value as NAME_MAX in
|
||||
// /usr/include/linux/limits.h
|
||||
let mut count = 0usize;
|
||||
for c in path_name.chars() {
|
||||
match c {
|
||||
'/' | '\\' if cfg!(target_os = "windows") => count = 0, // Reset
|
||||
_ => {
|
||||
count += 1;
|
||||
if count > 255 {
|
||||
return Err(DiskError::FileNameTooLong);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Success.
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn is_root_disk(disk_path: &str, root_disk: &str) -> Result<bool> {
|
||||
if cfg!(target_os = "windows") {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
rustfs_utils::os::same_disk(disk_path, root_disk).map_err(|e| to_file_error(e).into())
|
||||
}
|
||||
|
||||
pub async fn make_dir_all(path: impl AsRef<Path>, base_dir: impl AsRef<Path>) -> Result<()> {
|
||||
check_path_length(path.as_ref().to_string_lossy().to_string().as_str())?;
|
||||
|
||||
reliable_mkdir_all(path.as_ref(), base_dir.as_ref())
|
||||
.await
|
||||
.map_err(to_file_error)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn is_empty_dir(path: impl AsRef<Path>) -> bool {
|
||||
read_dir(path.as_ref(), 1).await.is_ok_and(|v| v.is_empty())
|
||||
}
|
||||
|
||||
// read_dir count read limit. when count == 0 unlimit.
|
||||
pub async fn read_dir(path: impl AsRef<Path>, count: i32) -> std::io::Result<Vec<String>> {
|
||||
let mut entries = fs::read_dir(path.as_ref()).await?;
|
||||
|
||||
let mut volumes = Vec::new();
|
||||
|
||||
let mut count = count;
|
||||
|
||||
while let Some(entry) = entries.next_entry().await? {
|
||||
let name = entry.file_name().to_string_lossy().to_string();
|
||||
|
||||
if name.is_empty() || name == "." || name == ".." {
|
||||
continue;
|
||||
}
|
||||
|
||||
let file_type = entry.file_type().await?;
|
||||
|
||||
if file_type.is_file() {
|
||||
volumes.push(name);
|
||||
} else if file_type.is_dir() {
|
||||
volumes.push(format!("{name}{SLASH_SEPARATOR}"));
|
||||
}
|
||||
count -= 1;
|
||||
if count == 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(volumes)
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "debug", skip_all)]
|
||||
pub async fn rename_all(
|
||||
src_file_path: impl AsRef<Path>,
|
||||
dst_file_path: impl AsRef<Path>,
|
||||
base_dir: impl AsRef<Path>,
|
||||
) -> Result<()> {
|
||||
reliable_rename(src_file_path, dst_file_path.as_ref(), base_dir)
|
||||
.await
|
||||
.map_err(to_file_error)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn reliable_rename(
|
||||
src_file_path: impl AsRef<Path>,
|
||||
dst_file_path: impl AsRef<Path>,
|
||||
base_dir: impl AsRef<Path>,
|
||||
) -> io::Result<()> {
|
||||
if let Some(parent) = dst_file_path.as_ref().parent() {
|
||||
if !file_exists(parent) {
|
||||
// info!("reliable_rename reliable_mkdir_all parent: {:?}", parent);
|
||||
reliable_mkdir_all(parent, base_dir.as_ref()).await?;
|
||||
}
|
||||
}
|
||||
|
||||
let mut i = 0;
|
||||
loop {
|
||||
if let Err(e) = super::fs::rename_std(src_file_path.as_ref(), dst_file_path.as_ref()) {
|
||||
if e.kind() == io::ErrorKind::NotFound {
|
||||
break;
|
||||
}
|
||||
|
||||
if i == 0 {
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
warn!(
|
||||
"reliable_rename failed. src_file_path: {:?}, dst_file_path: {:?}, base_dir: {:?}, err: {:?}",
|
||||
src_file_path.as_ref(),
|
||||
dst_file_path.as_ref(),
|
||||
base_dir.as_ref(),
|
||||
e
|
||||
);
|
||||
return Err(e);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn reliable_mkdir_all(path: impl AsRef<Path>, base_dir: impl AsRef<Path>) -> io::Result<()> {
|
||||
let mut i = 0;
|
||||
|
||||
let mut base_dir = base_dir.as_ref();
|
||||
loop {
|
||||
if let Err(e) = os_mkdir_all(path.as_ref(), base_dir).await {
|
||||
if e.kind() == io::ErrorKind::NotFound && i == 0 {
|
||||
i += 1;
|
||||
|
||||
if let Some(base_parent) = base_dir.parent() {
|
||||
if let Some(c) = base_parent.components().next() {
|
||||
if c != Component::RootDir {
|
||||
base_dir = base_parent
|
||||
}
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
return Err(e);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn os_mkdir_all(dir_path: impl AsRef<Path>, base_dir: impl AsRef<Path>) -> io::Result<()> {
|
||||
if !base_dir.as_ref().to_string_lossy().is_empty() && base_dir.as_ref().starts_with(dir_path.as_ref()) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if let Some(parent) = dir_path.as_ref().parent() {
|
||||
// 不支持递归,直接 create_dir_all 了
|
||||
if let Err(e) = super::fs::make_dir_all(&parent).await {
|
||||
if e.kind() == io::ErrorKind::AlreadyExists {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
return Err(e);
|
||||
}
|
||||
// Box::pin(os_mkdir_all(&parent, &base_dir)).await?;
|
||||
}
|
||||
|
||||
if let Err(e) = super::fs::mkdir(dir_path.as_ref()).await {
|
||||
if e.kind() == io::ErrorKind::AlreadyExists {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
return Err(e);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn file_exists(path: impl AsRef<Path>) -> bool {
|
||||
std::fs::metadata(path.as_ref()).map(|_| true).unwrap_or(false)
|
||||
}
|
||||
Reference in New Issue
Block a user