Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add an identifier before the extension to prevent file conflicts rather than after #316

Merged
merged 3 commits into from
Oct 27, 2023
Merged
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
22 changes: 19 additions & 3 deletions yazi-shared/src/fns.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{borrow::Cow, env, path::{Component, Path, PathBuf}};
use std::{borrow::Cow, env, ffi::OsString, path::{Component, Path, PathBuf}};

use tokio::fs;

Expand Down Expand Up @@ -44,15 +44,31 @@ pub fn expand_url(mut u: Url) -> Url {
}

pub async fn unique_path(mut p: Url) -> Url {
let Some(name) = p.file_name().map(|n| n.to_owned()) else {
let Some(stem) = p.file_stem().map(|s| s.to_owned()) else {
return p;
};

let ext = p
.extension()
.map(|s| {
let mut n = OsString::with_capacity(s.len() + 1);
n.push(".");
n.push(s);
n
})
.unwrap_or_default();

let mut i = 0;
while fs::symlink_metadata(&p).await.is_ok() {
i += 1;
let mut name = name.clone();

let mut name = OsString::with_capacity(stem.len() + ext.len() + 5);
name.push(&stem);
name.push(format!("_{i}"));
if !ext.is_empty() {
name.push(&ext);
}

p.set_file_name(name);
}
p
Expand Down