Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/core/src/raw/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ pub fn normalize_path(path: &str) -> String {
/// Finally, we will get path like `/path/to/root/`.
pub fn normalize_root(v: &str) -> String {
let mut v = v
.trim()
.split('/')
.filter(|v| !v.is_empty())
.collect::<Vec<&str>>()
Expand Down Expand Up @@ -303,6 +304,7 @@ mod tests {
("abs file path with extra /", "///abc/def", "/abc/def/"),
("abs dir path with extra /", "///abc/def/", "/abc/def/"),
("dir path contains ///", "abc///def///", "/abc/def/"),
("dir path with whitespace", " abc/def/ ", "/abc/def/"),
];

for (name, input, expect) in cases {
Expand Down
41 changes: 39 additions & 2 deletions core/services/ftp/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl Access for FtpBackend {
}
}

let tmp_path = (!op.append()).then_some(build_tmp_path_of(path));
let tmp_path = (!op.append()).then_some(build_ftp_tmp_path(path));
let w = FtpWriter::new(ftp_stream, path.to_string(), tmp_path);

Ok((RpWrite::new(), w))
Expand Down Expand Up @@ -332,9 +332,20 @@ impl FtpBackend {
}
}

fn build_ftp_tmp_path(path: &str) -> String {
let parent = get_parent(path);
let tmp_name = build_tmp_path_of(path);

if parent == "/" {
tmp_name
} else {
format!("{parent}{tmp_name}")
}
}

#[cfg(test)]
mod build_test {
use super::FtpBuilder;
use super::{FtpBuilder, build_ftp_tmp_path};
use crate::FtpConfig;
use opendal_core::*;

Expand Down Expand Up @@ -396,4 +407,30 @@ mod build_test {
assert_eq!(cfg.user.as_deref(), Some("alice"));
assert_eq!(cfg.password.as_deref(), Some("secret"));
}

#[test]
fn test_build_ftp_tmp_path() {
let cases = vec![
("root file", "example.txt", "example.txt."),
("nested file", "folder/example.txt", "folder/example.txt."),
(
"deep nested file",
"folder/sub/example.txt",
"folder/sub/example.txt.",
),
];

for (name, path, expect_starts_with) in cases {
let actual = build_ftp_tmp_path(path);
assert!(
actual.starts_with(expect_starts_with),
"{name}: got `{actual}`, but expect `{expect_starts_with}`"
);
assert_eq!(
actual.len(),
expect_starts_with.len() + 8,
"{name}: got `{actual}`, but expect `{expect_starts_with}`"
);
}
}
}
Loading