Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

linux: fix invalid cross-device link error (#8437)

Aryan Sjet2f6b290084a3
diff --git a/crates/fs/src/fs.rs b/crates/fs/src/fs.rs
index 6452b08127a..632247f57e7 100644
--- a/crates/fs/src/fs.rs
+++ b/crates/fs/src/fs.rs
@@ -30,7 +30,7 @@ use std::{
};
use tempfile::{NamedTempFile, TempDir};
use text::LineEnding;
-use util::ResultExt;
+use util::{paths, ResultExt};
#[cfg(any(test, feature = "test-support"))]
use collections::{btree_map, BTreeMap};
@@ -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>(())
diff --git a/crates/util/src/paths.rs b/crates/util/src/paths.rs
index 9a1bc73c9d3..002c0998b9c 100644
--- a/crates/util/src/paths.rs
+++ b/crates/util/src/paths.rs
@@ -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 {
diff --git a/crates/zed/src/main.rs b/crates/zed/src/main.rs
index c31c768858b..c509a8d0c12 100644
--- a/crates/zed/src/main.rs
+++ b/crates/zed/src/main.rs
@@ -493,6 +493,8 @@ 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");
+ #[cfg(target_os = "linux")]
+ std::fs::create_dir_all(&*util::paths::TEMP_DIR).expect("could not create tmp path");
}
fn init_logger() {