Skip to content

Commit

Permalink
mount: fix the issue of missing check file exists
Browse files Browse the repository at this point in the history
It's better to check whether the destination file exists
before creating them, if it had been existed, then return
directly.

Fixes: #2247

Signed-off-by: fupan.lfp <fupan.lfp@antgroup.com>
  • Loading branch information
fupan.lfp committed Jul 15, 2021
1 parent 594ff3a commit 5371b92
Showing 1 changed file with 47 additions and 9 deletions.
56 changes: 47 additions & 9 deletions src/agent/src/mount.rs
Expand Up @@ -822,19 +822,21 @@ pub fn remove_mounts(mounts: &[String]) -> Result<()> {
#[instrument]
fn ensure_destination_exists(destination: &str, fs_type: &str) -> Result<()> {
let d = Path::new(destination);
if !d.exists() {
let dir = d
.parent()
.ok_or_else(|| anyhow!("mount destination {} doesn't exist", destination))?;
if !dir.exists() {
fs::create_dir_all(dir).context(format!("create dir all failed on {:?}", dir))?;
}
if d.exists() {
return Ok(());
}
let dir = d
.parent()
.ok_or_else(|| anyhow!("mount destination {} doesn't exist", destination))?;

if !dir.exists() {
fs::create_dir_all(dir).context(format!("create dir all {:?}", dir))?;
}

if fs_type != "bind" || d.is_dir() {
fs::create_dir_all(d).context(format!("create dir all failed on {:?}", d))?;
fs::create_dir_all(d).context(format!("create dir all {:?}", d))?;
} else {
fs::OpenOptions::new().create(true).open(d)?;
fs::File::create(d).context(format!("create file {:?}", d))?;
}

Ok(())
Expand All @@ -860,6 +862,7 @@ mod tests {
use super::*;
use crate::{skip_if_not_root, skip_loop_if_not_root, skip_loop_if_root};
use libc::umount;
use std::fs::metadata;
use std::fs::File;
use std::fs::OpenOptions;
use std::io::Write;
Expand Down Expand Up @@ -1435,4 +1438,39 @@ mod tests {
assert!(mounts[1].eq(&cg_devices_mount), "{}", msg);
}
}

#[test]
fn test_ensure_destination_exists() {
let dir = tempdir().expect("failed to create tmpdir");

let mut testfile = dir.into_path();
testfile.push("testfile");

let result = ensure_destination_exists(testfile.to_str().unwrap(), "bind");

assert!(result.is_ok());
assert!(testfile.exists());

let result = ensure_destination_exists(testfile.to_str().unwrap(), "bind");
assert!(result.is_ok());

let meta = metadata(testfile).unwrap();

assert!(meta.is_file());

let dir = tempdir().expect("failed to create tmpdir");
let mut testdir = dir.into_path();
testdir.push("testdir");

let result = ensure_destination_exists(testdir.to_str().unwrap(), "ext4");
assert!(result.is_ok());
assert!(testdir.exists());

let result = ensure_destination_exists(testdir.to_str().unwrap(), "ext4");
assert!(result.is_ok());

//let meta = metadata(testdir.to_str().unwrap()).unwrap();
let meta = metadata(testdir).unwrap();
assert!(meta.is_dir());
}
}

0 comments on commit 5371b92

Please sign in to comment.