From c4ef5fd32551c879fe38aaaa6fa10473cd792723 Mon Sep 17 00:00:00 2001 From: Yohei Ueda Date: Fri, 24 Feb 2023 16:43:59 +0900 Subject: [PATCH] agent: don't set permission of existing directory This patch fixes the issue that do_copy_file changes the directory permission of the parent directory of a target file, even when the parent directory already exists. Fixes #6367 Signed-off-by: Yohei Ueda --- src/agent/src/rpc.rs | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/agent/src/rpc.rs b/src/agent/src/rpc.rs index c52d866d6..bb34c0f5e 100644 --- a/src/agent/src/rpc.rs +++ b/src/agent/src/rpc.rs @@ -1877,23 +1877,18 @@ fn do_copy_file(req: &CopyFileRequest) -> Result<()> { )); } - let parent = path.parent(); - - let dir = if let Some(parent) = parent { - parent.to_path_buf() - } else { - PathBuf::from("/") - }; - - fs::create_dir_all(&dir).or_else(|e| { - if e.kind() != std::io::ErrorKind::AlreadyExists { - return Err(e); + if let Some(parent) = path.parent() { + if !parent.exists() { + let dir = parent.to_path_buf(); + if let Err(e) = fs::create_dir_all(&dir) { + if e.kind() != std::io::ErrorKind::AlreadyExists { + return Err(e.into()); + } + } else { + std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(req.dir_mode))?; + } } - - Ok(()) - })?; - - std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(req.dir_mode))?; + } let mut tmpfile = path.clone(); tmpfile.set_extension("tmp");