Skip to content

Commit

Permalink
fix: fix opendal paths on windows (#340)
Browse files Browse the repository at this point in the history
We now always use UnixPaths to generate the path used in opendal URLs.

closes rustic-rs/rustic#1162
closes rustic-rs/rustic#1302
closes rustic-rs/rustic#1305
  • Loading branch information
aawsome authored Oct 24, 2024
1 parent 996a10f commit b29f203
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion crates/backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ default = ["opendal", "rest", "rclone"]
cli = ["merge", "clap"]
merge = ["dep:conflate"]
clap = ["dep:clap"]
opendal = ["dep:opendal", "dep:rayon", "dep:tokio", "tokio/rt-multi-thread"]
opendal = [
"dep:opendal",
"dep:rayon",
"dep:tokio",
"tokio/rt-multi-thread",
"dep:typed-path",
]
rest = ["dep:reqwest", "dep:backoff"]
rclone = ["rest", "dep:rand", "dep:semver"]

Expand Down Expand Up @@ -84,6 +90,7 @@ semver = { version = "1.0.23", optional = true }
bytesize = "1.3.0"
rayon = { version = "1.10.0", optional = true }
tokio = { version = "1.40.0", optional = true, default-features = false }
typed-path = { version = "0.9.3", optional = true }

[target.'cfg(not(windows))'.dependencies]
# opendal backend - sftp is not supported on windows, see https://github.com/apache/incubator-opendal/issues/2963
Expand Down
17 changes: 9 additions & 8 deletions crates/backend/src/opendal.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// `OpenDAL` backend for rustic.
use std::{collections::HashMap, path::PathBuf, str::FromStr, sync::OnceLock};
use std::{collections::HashMap, str::FromStr, sync::OnceLock};

use anyhow::{anyhow, Error, Result};
use bytes::Bytes;
Expand All @@ -11,6 +11,7 @@ use opendal::{
};
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
use tokio::runtime::Runtime;
use typed_path::UnixPathBuf;

use rustic_core::{FileType, Id, ReadBackend, WriteBackend, ALL_FILE_TYPES};

Expand Down Expand Up @@ -126,11 +127,12 @@ impl OpenDALBackend {
fn path(&self, tpe: FileType, id: &Id) -> String {
let hex_id = id.to_hex();
match tpe {
FileType::Config => PathBuf::from("config"),
FileType::Pack => PathBuf::from("data").join(&hex_id[0..2]).join(hex_id),
_ => PathBuf::from(tpe.dirname()).join(hex_id),
FileType::Config => UnixPathBuf::from("config"),
FileType::Pack => UnixPathBuf::from("data")
.join(&hex_id[0..2])
.join(&hex_id[..]),
_ => UnixPathBuf::from(tpe.dirname()).join(&hex_id[..]),
}
.to_string_lossy()
.to_string()
}
}
Expand Down Expand Up @@ -243,7 +245,7 @@ impl WriteBackend for OpenDALBackend {
// creating 256 dirs can be slow on remote backends, hence we parallelize it.
(0u8..=255).into_par_iter().try_for_each(|i| {
self.operator.create_dir(
&(PathBuf::from("data")
&(UnixPathBuf::from("data")
.join(hex::encode([i]))
.to_string_lossy()
.to_string()
Expand Down Expand Up @@ -286,11 +288,10 @@ impl WriteBackend for OpenDALBackend {

#[cfg(test)]
mod tests {
use std::fs;

use super::*;
use rstest::rstest;
use serde::Deserialize;
use std::{fs, path::PathBuf};

#[rstest]
#[case("10kB,10MB", Throttle{bandwidth:10_000, burst:10_000_000})]
Expand Down

0 comments on commit b29f203

Please sign in to comment.