vmm: memory_manager: Don't truncate backing file

In case the provided backing file is an actual file and not a directory,
we should not truncate it, as we expect the file to already be the right
size.

This change will be important once we try to map the same file through
multiple memory mappings. We can't let the file be truncated as the
second mapping wouldn't work properly.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2020-08-20 10:04:02 +02:00 committed by Samuel Ortiz
parent be475ddc22
commit 59d4a56ab7

View File

@ -519,7 +519,9 @@ impl MemoryManager {
let path_ptr = path.as_mut_ptr() as *mut _;
let fd = unsafe { libc::mkstemp(path_ptr) };
unsafe { libc::unlink(path_ptr) };
unsafe { File::from_raw_fd(fd) }
let f = unsafe { File::from_raw_fd(fd) };
f.set_len(size as u64).map_err(Error::SharedFileSetLen)?;
f
} else {
OpenOptions::new()
.read(true)
@ -528,8 +530,6 @@ impl MemoryManager {
.map_err(Error::SharedFileCreate)?
};
f.set_len(size as u64).map_err(Error::SharedFileSetLen)?;
let mut mmap_flags = if copy_on_write {
libc::MAP_NORESERVE | libc::MAP_PRIVATE
} else {