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

linux: fix invalid cross-device link error #8437

Merged
merged 5 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
11 changes: 9 additions & 2 deletions crates/fs/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use std::{
};
use tempfile::{NamedTempFile, TempDir};
use text::LineEnding;
use util::ResultExt;
use util::{ResultExt, paths};

#[cfg(any(test, feature = "test-support"))]
use collections::{btree_map, BTreeMap};
Expand Down Expand Up @@ -189,7 +189,14 @@ impl Fs for RealFs {

async fn atomic_write(&self, path: PathBuf, data: String) -> Result<()> {
smol::unblock(move || {
let mut tmp_file = NamedTempFile::new()?;
let mut tmp_file = if cfg!(target_os = "linux") {
// Use the directory of the destination as temp dir to avoid
// invalid cross-device link error, and XDG_CACHE_DIR for fallback.
// See https://github.com/zed-industries/zed/pull/8437 for more details.
NamedTempFile::new_in(path.parent().unwrap_or(&paths::TEMP_DIR))
} else {
NamedTempFile::new()
}?;
tmp_file.write_all(data.as_bytes())?;
tmp_file.persist(path)?;
Ok::<(), anyhow::Error>(())
Expand Down
1 change: 1 addition & 0 deletions crates/util/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ lazy_static::lazy_static! {
pub static ref LOG: PathBuf = LOGS_DIR.join("Zed.log");
pub static ref OLD_LOG: PathBuf = LOGS_DIR.join("Zed.log.old");
pub static ref LOCAL_SETTINGS_RELATIVE_PATH: &'static Path = Path::new(".zed/settings.json");
pub static ref TEMP_DIR: PathBuf = HOME.join(".cache").join("zed");
}

pub trait PathExt {
Expand Down
5 changes: 5 additions & 0 deletions crates/zed/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,11 @@ fn init_paths() {
std::fs::create_dir_all(&*util::paths::LANGUAGES_DIR).expect("could not create languages path");
std::fs::create_dir_all(&*util::paths::DB_DIR).expect("could not create database path");
std::fs::create_dir_all(&*util::paths::LOGS_DIR).expect("could not create logs path");

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we remove this comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

// Create a fallback temp dir to avoid cross-device issue in linux rename syscall
// See https://github.com/zed-industries/zed/pull/8437 for more details.
#[cfg(target_os = "linux")]
std::fs::create_dir_all(&*util::paths::TEMP_DIR).expect("could not create tmp path");
}

fn init_logger() {
Expand Down
Loading