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: kata-containers#2247

Signed-off-by: fupan.lfp <fupan.lfp@antgroup.com>
  • Loading branch information
fupan.lfp authored and snir911 committed Aug 5, 2021
1 parent 8de95b7 commit 5863106
Showing 1 changed file with 47 additions and 9 deletions.
56 changes: 47 additions & 9 deletions src/agent/src/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,19 +746,21 @@ pub fn remove_mounts(mounts: &[String]) -> Result<()> {
// are created, their permissions are initialized to mountPerm(0755)
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 @@ -783,6 +785,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 @@ -1358,4 +1361,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 5863106

Please sign in to comment.