From 5f29f3e2931a5394126b614a93b12da925c64a5e Mon Sep 17 00:00:00 2001 From: lifupan Date: Mon, 25 Nov 2019 10:21:07 +0800 Subject: [PATCH] grpc: fix the issue of return ENOENT for chmod on a file/dir When call "C" func directly, it's needed to change the string to CString. To avoid using the unsafe calling, replace it with the rust safe function to set mode for a file/dir. Signed-off-by: lifupan --- src/agent/src/grpc.rs | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/src/agent/src/grpc.rs b/src/agent/src/grpc.rs index 562d70ef4b..d49671879e 100644 --- a/src/agent/src/grpc.rs +++ b/src/agent/src/grpc.rs @@ -41,6 +41,7 @@ use libc::{self, c_ushort, pid_t, winsize, TIOCSWINSZ}; use serde_json; use std::fs; use std::os::unix::io::RawFd; +use std::os::unix::prelude::PermissionsExt; use std::process::{Command, Stdio}; use std::sync::mpsc; use std::thread; @@ -1685,7 +1686,7 @@ fn do_set_guest_date_time(sec: i64, usec: i64) -> Result<()> { } fn do_copy_file(req: &CopyFileRequest) -> Result<()> { - let path = fs::canonicalize(req.path.as_str())?; + let path = PathBuf::from(req.path.as_str()); if !path.starts_with(CONTAINER_BASE) { return Err(nix::Error::Sys(Errno::EINVAL).into()); @@ -1705,14 +1706,10 @@ fn do_copy_file(req: &CopyFileRequest) -> Result<()> { } } - let ret = unsafe { - libc::chmod( - dir.to_str().unwrap().as_ptr() as *const libc::c_char, - req.dir_mode, - ) - }; - - let _ = Errno::result(ret).map(drop)?; + std::fs::set_permissions( + dir.to_str().unwrap(), + std::fs::Permissions::from_mode(req.dir_mode), + )?; let mut tmpfile = path.clone(); tmpfile.set_extension("tmp"); @@ -1722,22 +1719,16 @@ fn do_copy_file(req: &CopyFileRequest) -> Result<()> { .create(true) .truncate(false) .open(tmpfile.to_str().unwrap())?; - file.write_all_at(req.data.as_slice(), req.offset as u64)?; + file.write_all_at(req.data.as_slice(), req.offset as u64)?; let st = stat::stat(tmpfile.to_str().unwrap())?; if st.st_size != req.file_size { return Ok(()); } - let ret = unsafe { - libc::chmod( - tmpfile.to_str().unwrap().as_ptr() as *const libc::c_char, - req.file_mode, - ) - }; + file.set_permissions(std::fs::Permissions::from_mode(req.file_mode))?; - let _ = Errno::result(ret).map(drop)?; unistd::chown( tmpfile.to_str().unwrap(), Some(Uid::from_raw(req.uid as u32)),