From ad16f3511eb7a5cde9043c350c30219eddb0a1a4 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Thu, 13 Feb 2025 10:46:03 +0300 Subject: [PATCH 01/41] fix CreateFileMapping arguments: use default pagefile instead of real file on disk --- src/windows.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/windows.rs b/src/windows.rs index 96f4a59..407ecd2 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -169,7 +169,7 @@ fn new_map( let low_size: u32 = (map_size as u64 & 0xFFFF_FFFF_u64) as u32; trace!( "CreateFileMapping({:?}, NULL, {:X}, {}, {}, '{}')", - HANDLE(f.as_raw_handle() as _), + INVALID_HANDLE_VALUE, PAGE_READWRITE.0, high_size, low_size, @@ -177,7 +177,7 @@ fn new_map( ); match CreateFileMapping( - HANDLE(f.as_raw_handle() as _), + INVALID_HANDLE_VALUE, None, PAGE_READWRITE, high_size, From b360fe9523e0bfd92007946badf7c6877463070f Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Thu, 13 Feb 2025 10:58:01 +0300 Subject: [PATCH 02/41] fix clippy --- examples/mutex.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/mutex.rs b/examples/mutex.rs index dbf60df..3ea2e5c 100644 --- a/examples/mutex.rs +++ b/examples/mutex.rs @@ -57,7 +57,7 @@ fn increment_value(shmem_flink: &str, thread_num: usize) { let is_init: &mut AtomicU8; unsafe { - is_init = &mut *(raw_ptr as *mut u8 as *mut AtomicU8); + is_init = &mut *(raw_ptr as *mut AtomicU8); raw_ptr = raw_ptr.add(8); }; From 3803baf4d66eca959131dc004fce7ba8e4b71173 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Thu, 13 Feb 2025 11:02:43 +0300 Subject: [PATCH 03/41] fix clippy --- src/windows.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/windows.rs b/src/windows.rs index 407ecd2..0025868 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -1,6 +1,6 @@ use std::fs::{File, OpenOptions}; use std::io::ErrorKind; -use std::os::windows::{fs::OpenOptionsExt, io::AsRawHandle}; +use std::os::windows::fs::OpenOptionsExt; use std::path::PathBuf; use crate::{log::*, ShmemConf}; @@ -73,7 +73,7 @@ impl Drop for MapData { { Ok(_) => { // 2. Rename file to prevent further use - base_path.push(&format!( + base_path.push(format!( "{}_deleted", self.unique_id.trim_start_matches('/') )); From bd20b80eb4a9f1ef613ad2d47936189865cbdaeb Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Thu, 13 Feb 2025 14:35:02 +0300 Subject: [PATCH 04/41] split new_map and open_map --- src/windows.rs | 172 ++++++++++++++++++++++++++++++------------------- 1 file changed, 104 insertions(+), 68 deletions(-) diff --git a/src/windows.rs b/src/windows.rs index 0025868..3f4631d 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -131,40 +131,112 @@ fn get_tmp_dir() -> Result { } } -fn new_map( - unique_id: &str, - mut map_size: usize, - create: bool, - allow_raw: bool, -) -> Result { +fn open_map(unique_id: &str, mut map_size: usize, allow_raw: bool) -> Result { // Create file to back the shared memory let mut file_path = get_tmp_dir()?; file_path.push(unique_id.trim_start_matches('/')); - debug!( - "{} persistent_file at {}", - if create { "Creating" } else { "Openning" }, - file_path.to_string_lossy() - ); + debug!("Opening persistent_file at {}", file_path.to_string_lossy()); - let mut opt = OpenOptions::new(); - opt.read(true) + let mut persistent_file = None; + let map_h = match OpenOptions::new() + .read(true) .write(true) .share_mode((FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE).0) - .attributes((FILE_ATTRIBUTE_TEMPORARY).0); - if create { - opt.create_new(true); - } else { - opt.create(false); + .attributes((FILE_ATTRIBUTE_TEMPORARY).0) + .create(false) + .open(&file_path) + { + Ok(f) => { + //Open Mapping using persistent file + debug!("Openning memory mapping",); + + trace!( + "OpenFileMappingW({:?}, {}, '{}')", + FILE_MAP_ALL_ACCESS, + false, + unique_id, + ); + match OpenFileMapping(FILE_MAP_ALL_ACCESS, false, unique_id) { + Ok(h) => h, + Err(e) => { + return Err(ShmemError::MapOpenFailed(e.win32_error().unwrap().0)); + } + } + } + Err(e) => { + if !allow_raw { + return Err(ShmemError::MapOpenFailed(ERROR_FILE_NOT_FOUND.0)); + } + + // This may be a mapping that isnt managed by this crate + // Try to open the mapping without any backing file + trace!( + "OpenFileMappingW({:?}, {}, '{}')", + FILE_MAP_ALL_ACCESS, + false, + unique_id, + ); + match OpenFileMapping(FILE_MAP_ALL_ACCESS, false, unique_id) { + Ok(h) => h, + Err(e) => { + return Err(ShmemError::MapOpenFailed(e.win32_error().unwrap().0)); + } + } + } }; + trace!("0x{:X}", map_h); + + //Map mapping into address space + debug!("Loading mapping into address space"); + trace!( + "MapViewOfFile(0x{:X}, {:X}, 0, 0, 0)", + map_h, + (FILE_MAP_READ | FILE_MAP_WRITE).0, + ); + let map_ptr = match MapViewOfFile(map_h.as_handle(), FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0) { + Ok(v) => v, + Err(e) => return Err(ShmemError::MapOpenFailed(e.win32_error().unwrap().0)), + }; + trace!("\t{:p}", map_ptr); + + //Get the real size of the openned mapping + let mut info = MEMORY_BASIC_INFORMATION::default(); + if let Err(e) = VirtualQuery(map_ptr.as_mut_ptr(), &mut info) { + return Err(ShmemError::UnknownOsError(e.win32_error().unwrap().0)); + } + map_size = info.RegionSize; + + Ok(MapData { + owner: false, + file_map: map_h, + persistent_file, + unique_id: unique_id.to_string(), + map_size, + view: map_ptr, + }) +} + +fn new_map(unique_id: &str, mut map_size: usize, allow_raw: bool) -> Result { + // Create file to back the shared memory + let mut file_path = get_tmp_dir()?; + file_path.push(unique_id.trim_start_matches('/')); + debug!( + "Creating persistent_file at {}", + file_path.to_string_lossy() + ); let mut persistent_file = None; - let map_h = match opt.open(&file_path) { + let map_h = match OpenOptions::new() + .read(true) + .write(true) + .share_mode((FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE).0) + .attributes((FILE_ATTRIBUTE_TEMPORARY).0) + .create_new(true) + .open(&file_path) + { Ok(f) => { - //Create/open Mapping using persistent file - debug!( - "{} memory mapping", - if create { "Creating" } else { "Openning" }, - ); + //Create Mapping using persistent file + debug!("Creating memory mapping",); let high_size: u32 = ((map_size as u64 & 0xFFFF_FFFF_0000_0000_u64) >> 32) as u32; let low_size: u32 = (map_size as u64 & 0xFFFF_FFFF_u64) as u32; trace!( @@ -190,40 +262,17 @@ fn new_map( } Err(e) => { let err_code = e.win32_error().unwrap(); - return if err_code == ERROR_ALREADY_EXISTS { - Err(ShmemError::MappingIdExists) + return Err(if err_code == ERROR_ALREADY_EXISTS { + ShmemError::MappingIdExists } else { - Err(if create { - ShmemError::MapCreateFailed(err_code.0) - } else { - ShmemError::MapOpenFailed(err_code.0) - }) - }; + ShmemError::MapCreateFailed(err_code.0) + }); } } } Err(e) if e.kind() == ErrorKind::AlreadyExists => return Err(ShmemError::MappingIdExists), Err(e) => { - if create { - return Err(ShmemError::MapCreateFailed(e.raw_os_error().unwrap() as _)); - } else if !allow_raw { - return Err(ShmemError::MapOpenFailed(ERROR_FILE_NOT_FOUND.0)); - } - - // This may be a mapping that isnt managed by this crate - // Try to open the mapping without any backing file - trace!( - "OpenFileMappingW({:?}, {}, '{}')", - FILE_MAP_ALL_ACCESS, - false, - unique_id, - ); - match OpenFileMapping(FILE_MAP_ALL_ACCESS, false, unique_id) { - Ok(h) => h, - Err(e) => { - return Err(ShmemError::MapOpenFailed(e.win32_error().unwrap().0)); - } - } + return Err(ShmemError::MapCreateFailed(e.raw_os_error().unwrap() as _)); } }; trace!("0x{:X}", map_h); @@ -238,26 +287,13 @@ fn new_map( let map_ptr = match MapViewOfFile(map_h.as_handle(), FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0) { Ok(v) => v, Err(e) => { - return Err(if create { - ShmemError::MapCreateFailed(e.win32_error().unwrap().0) - } else { - ShmemError::MapOpenFailed(e.win32_error().unwrap().0) - }) + return Err(ShmemError::MapCreateFailed(e.win32_error().unwrap().0)); } }; trace!("\t{:p}", map_ptr); - if !create { - //Get the real size of the openned mapping - let mut info = MEMORY_BASIC_INFORMATION::default(); - if let Err(e) = VirtualQuery(map_ptr.as_mut_ptr(), &mut info) { - return Err(ShmemError::UnknownOsError(e.win32_error().unwrap().0)); - } - map_size = info.RegionSize; - } - Ok(MapData { - owner: create, + owner: true, file_map: map_h, persistent_file, unique_id: unique_id.to_string(), @@ -277,5 +313,5 @@ pub fn open_mapping( map_size: usize, ext: &ShmemConfExt, ) -> Result { - new_map(unique_id, map_size, false, ext.allow_raw) + open_map(unique_id, map_size, false, ext.allow_raw) } From 471c0f954eb8754a33b3e24d7eb13902f8c08d09 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Thu, 13 Feb 2025 14:45:39 +0300 Subject: [PATCH 05/41] Update windows.rs --- src/windows.rs | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/src/windows.rs b/src/windows.rs index 3f4631d..0b7a354 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -131,14 +131,13 @@ fn get_tmp_dir() -> Result { } } -fn open_map(unique_id: &str, mut map_size: usize, allow_raw: bool) -> Result { +fn open_map(unique_id: &str, allow_raw: bool) -> Result { // Create file to back the shared memory let mut file_path = get_tmp_dir()?; file_path.push(unique_id.trim_start_matches('/')); debug!("Opening persistent_file at {}", file_path.to_string_lossy()); - let mut persistent_file = None; - let map_h = match OpenOptions::new() + let (persistent_file, map_h) = match OpenOptions::new() .read(true) .write(true) .share_mode((FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE).0) @@ -157,7 +156,7 @@ fn open_map(unique_id: &str, mut map_size: usize, allow_raw: bool) -> Result h, + Ok(h) => (Some(f), h), Err(e) => { return Err(ShmemError::MapOpenFailed(e.win32_error().unwrap().0)); } @@ -199,24 +198,23 @@ fn open_map(unique_id: &str, mut map_size: usize, allow_raw: bool) -> Result Result { +fn new_map(unique_id: &str, map_size: usize) -> Result { // Create file to back the shared memory let mut file_path = get_tmp_dir()?; file_path.push(unique_id.trim_start_matches('/')); @@ -225,8 +223,7 @@ fn new_map(unique_id: &str, mut map_size: usize, allow_raw: bool) -> Result Result { - persistent_file = Some(f); - v - } + Ok(v) => (Some(f), v), Err(e) => { let err_code = e.win32_error().unwrap(); return Err(if err_code == ERROR_ALREADY_EXISTS { @@ -304,7 +298,7 @@ fn new_map(unique_id: &str, mut map_size: usize, allow_raw: bool) -> Result Result { - new_map(unique_id, map_size, true, false) + new_map(unique_id, map_size, true) } //Opens an existing mapping specified by its uid @@ -313,5 +307,5 @@ pub fn open_mapping( map_size: usize, ext: &ShmemConfExt, ) -> Result { - open_map(unique_id, map_size, false, ext.allow_raw) + open_map(unique_id, false, ext.allow_raw) } From 0a43b2d2a302fbba4f6afb588df72843f08cb7b5 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Thu, 13 Feb 2025 14:52:52 +0300 Subject: [PATCH 06/41] Update windows.rs --- src/windows.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/windows.rs b/src/windows.rs index 0b7a354..f349845 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -176,7 +176,7 @@ fn open_map(unique_id: &str, allow_raw: bool) -> Result { unique_id, ); match OpenFileMapping(FILE_MAP_ALL_ACCESS, false, unique_id) { - Ok(h) => h, + Ok(h) => (None, h), Err(e) => { return Err(ShmemError::MapOpenFailed(e.win32_error().unwrap().0)); } @@ -298,7 +298,7 @@ fn new_map(unique_id: &str, map_size: usize) -> Result { //Creates a mapping specified by the uid and size pub fn create_mapping(unique_id: &str, map_size: usize) -> Result { - new_map(unique_id, map_size, true) + new_map(unique_id, map_size) } //Opens an existing mapping specified by its uid @@ -307,5 +307,5 @@ pub fn open_mapping( map_size: usize, ext: &ShmemConfExt, ) -> Result { - open_map(unique_id, false, ext.allow_raw) + open_map(unique_id, ext.allow_raw) } From c5bca7cb71478e5474b171cefd1bfdf3d42feb03 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Thu, 13 Feb 2025 14:59:36 +0300 Subject: [PATCH 07/41] Update windows.rs --- src/windows.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/windows.rs b/src/windows.rs index f349845..7989ac9 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -164,7 +164,7 @@ fn open_map(unique_id: &str, allow_raw: bool) -> Result { } Err(e) => { if !allow_raw { - return Err(ShmemError::MapOpenFailed(ERROR_FILE_NOT_FOUND.0)); + return Err(ShmemError::MapOpenFailed(e.win32_error().unwrap().0)); } // This may be a mapping that isnt managed by this crate @@ -304,7 +304,7 @@ pub fn create_mapping(unique_id: &str, map_size: usize) -> Result Result { open_map(unique_id, ext.allow_raw) From ca26cebdfb6b152c706a3b390225aee829af4546 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Thu, 13 Feb 2025 15:03:46 +0300 Subject: [PATCH 08/41] Update windows.rs --- src/windows.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/windows.rs b/src/windows.rs index 7989ac9..96be1f1 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -164,7 +164,7 @@ fn open_map(unique_id: &str, allow_raw: bool) -> Result { } Err(e) => { if !allow_raw { - return Err(ShmemError::MapOpenFailed(e.win32_error().unwrap().0)); + return Err(ShmemError::MapOpenFailed(e.raw_os_error().unwrap() as _)); } // This may be a mapping that isnt managed by this crate From eee79d7e312441ac540b51ab61e1edcaf534b217 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Thu, 13 Feb 2025 20:24:27 +0300 Subject: [PATCH 09/41] Update lib.rs, unix.rs, and windows.rs --- src/lib.rs | 4 +- src/unix.rs | 6 +- src/windows.rs | 202 ++++++++++++++++++++++++++----------------------- 3 files changed, 114 insertions(+), 98 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3f05937..1eca78a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -118,7 +118,7 @@ impl ShmemConf { // Generate random ID until one works loop { let cur_id = format!("/shmem_{:X}", rand::random::()); - match os_impl::create_mapping(&cur_id, self.size) { + match os_impl::create_mapping(&cur_id, self.size, &self.ext) { Err(ShmemError::MappingIdExists) => continue, Ok(m) => break m, Err(e) => { @@ -127,7 +127,7 @@ impl ShmemConf { }; } } - Some(ref specific_id) => os_impl::create_mapping(specific_id, self.size)?, + Some(ref specific_id) => os_impl::create_mapping(specific_id, self.size, &self.ext)?, }; debug!("Created shared memory mapping '{}'", mapping.unique_id); diff --git a/src/unix.rs b/src/unix.rs index 86b40c0..45cee78 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -81,7 +81,11 @@ impl MapData { } /// Creates a mapping specified by the uid and size -pub fn create_mapping(unique_id: &str, map_size: usize) -> Result { +pub fn create_mapping( + unique_id: &str, + map_size: usize, + _: &ShmemConfExt, +) -> Result { //Create shared memory file descriptor debug!("Creating persistent mapping at {}", unique_id); diff --git a/src/windows.rs b/src/windows.rs index 96be1f1..684e537 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -11,6 +11,7 @@ use crate::ShmemError; #[derive(Clone, Default)] pub struct ShmemConfExt { allow_raw: bool, + suppress_persistency: bool, } impl ShmemConf { @@ -19,6 +20,12 @@ impl ShmemConf { self.ext.allow_raw = allow; self } + + /// If set to true, the persistency of the object will be suppressed + pub fn suppress_persistency(mut self, suppress: bool) -> Self { + self.ext.suppress_persistency = suppress; + self + } } pub struct MapData { @@ -131,55 +138,45 @@ fn get_tmp_dir() -> Result { } } -fn open_map(unique_id: &str, allow_raw: bool) -> Result { +fn open_map(unique_id: &str, ext: &ShmemConfExt) -> Result { // Create file to back the shared memory let mut file_path = get_tmp_dir()?; file_path.push(unique_id.trim_start_matches('/')); debug!("Opening persistent_file at {}", file_path.to_string_lossy()); - let (persistent_file, map_h) = match OpenOptions::new() - .read(true) - .write(true) - .share_mode((FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE).0) - .attributes((FILE_ATTRIBUTE_TEMPORARY).0) - .create(false) - .open(&file_path) - { - Ok(f) => { - //Open Mapping using persistent file - debug!("Openning memory mapping",); - - trace!( - "OpenFileMappingW({:?}, {}, '{}')", - FILE_MAP_ALL_ACCESS, - false, - unique_id, - ); - match OpenFileMapping(FILE_MAP_ALL_ACCESS, false, unique_id) { - Ok(h) => (Some(f), h), - Err(e) => { - return Err(ShmemError::MapOpenFailed(e.win32_error().unwrap().0)); + let persistent_file = match ext.suppress_persistency { + false => match OpenOptions::new() + .read(true) + .write(true) + .share_mode((FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE).0) + .attributes((FILE_ATTRIBUTE_TEMPORARY).0) + .create(false) + .open(&file_path) + { + Ok(f) => Some(f), + Err(e) => { + if !allow_raw { + return Err(ShmemError::MapOpenFailed(e.raw_os_error().unwrap() as _)); } } - } - Err(e) => { - if !allow_raw { - return Err(ShmemError::MapOpenFailed(e.raw_os_error().unwrap() as _)); - } + }, + true => None, + }; - // This may be a mapping that isnt managed by this crate - // Try to open the mapping without any backing file - trace!( - "OpenFileMappingW({:?}, {}, '{}')", - FILE_MAP_ALL_ACCESS, - false, - unique_id, - ); - match OpenFileMapping(FILE_MAP_ALL_ACCESS, false, unique_id) { - Ok(h) => (None, h), - Err(e) => { - return Err(ShmemError::MapOpenFailed(e.win32_error().unwrap().0)); - } + let map_h = { + //Open Mapping + debug!("Opening memory mapping",); + + trace!( + "OpenFileMappingW({:?}, {}, '{}')", + FILE_MAP_ALL_ACCESS, + false, + unique_id, + ); + match OpenFileMapping(FILE_MAP_ALL_ACCESS, false, unique_id) { + Ok(h) => (Some(f), h), + Err(e) => { + return Err(ShmemError::MapOpenFailed(e.win32_error().unwrap().0)); } } }; @@ -214,60 +211,45 @@ fn open_map(unique_id: &str, allow_raw: bool) -> Result { }) } -fn new_map(unique_id: &str, map_size: usize) -> Result { - // Create file to back the shared memory - let mut file_path = get_tmp_dir()?; - file_path.push(unique_id.trim_start_matches('/')); - debug!( - "Creating persistent_file at {}", - file_path.to_string_lossy() - ); - - let (persistent_file, map_h) = match OpenOptions::new() - .read(true) - .write(true) - .share_mode((FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE).0) - .attributes((FILE_ATTRIBUTE_TEMPORARY).0) - .create_new(true) - .open(&file_path) - { - Ok(f) => { - //Create Mapping using persistent file - debug!("Creating memory mapping",); - let high_size: u32 = ((map_size as u64 & 0xFFFF_FFFF_0000_0000_u64) >> 32) as u32; - let low_size: u32 = (map_size as u64 & 0xFFFF_FFFF_u64) as u32; - trace!( - "CreateFileMapping({:?}, NULL, {:X}, {}, {}, '{}')", - INVALID_HANDLE_VALUE, - PAGE_READWRITE.0, - high_size, - low_size, - unique_id, - ); +fn create_file_mapping( + unique_id: &str, + map_size: usize, + persistent_file: Option, +) -> Result { + let map_h = { + debug!("Creating memory mapping",); + let h_handle = persistent_file + .map(|f| f.as_raw_handle() as _) + .unwrap_or(INVALID_HANDLE_VALUE); + let high_size: u32 = ((map_size as u64 & 0xFFFF_FFFF_0000_0000_u64) >> 32) as u32; + let low_size: u32 = (map_size as u64 & 0xFFFF_FFFF_u64) as u32; + trace!( + "CreateFileMapping({:?}, NULL, {:X}, {}, {}, '{}')", + h_handle, + PAGE_READWRITE.0, + high_size, + low_size, + unique_id, + ); - match CreateFileMapping( - INVALID_HANDLE_VALUE, - None, - PAGE_READWRITE, - high_size, - low_size, - unique_id, - ) { - Ok(v) => (Some(f), v), - Err(e) => { - let err_code = e.win32_error().unwrap(); - return Err(if err_code == ERROR_ALREADY_EXISTS { - ShmemError::MappingIdExists - } else { - ShmemError::MapCreateFailed(err_code.0) - }); - } + match CreateFileMapping( + h_handle, + None, + PAGE_READWRITE, + high_size, + low_size, + unique_id, + ) { + Ok(v) => v, + Err(e) => { + let err_code = e.win32_error().unwrap(); + return Err(if err_code == ERROR_ALREADY_EXISTS { + ShmemError::MappingIdExists + } else { + ShmemError::MapCreateFailed(err_code.0) + }); } } - Err(e) if e.kind() == ErrorKind::AlreadyExists => return Err(ShmemError::MappingIdExists), - Err(e) => { - return Err(ShmemError::MapCreateFailed(e.raw_os_error().unwrap() as _)); - } }; trace!("0x{:X}", map_h); @@ -296,9 +278,39 @@ fn new_map(unique_id: &str, map_size: usize) -> Result { }) } +fn create_persistent_file_mapping(unique_id: &str, map_size: usize) -> Result { + // Create file to back the shared memory + let mut file_path = get_tmp_dir()?; + file_path.push(unique_id.trim_start_matches('/')); + debug!( + "Creating persistent_file at {}", + file_path.to_string_lossy() + ); + + match OpenOptions::new() + .read(true) + .write(true) + .share_mode((FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE).0) + .attributes((FILE_ATTRIBUTE_TEMPORARY).0) + .create_new(true) + .open(&file_path) + { + Ok(f) => create_file_mapping(unique_id, map_size, Some(f)), + Err(e) if e.kind() == ErrorKind::AlreadyExists => Err(ShmemError::MappingIdExists), + Err(e) => Err(ShmemError::MapCreateFailed(e.raw_os_error().unwrap() as _)), + } +} + //Creates a mapping specified by the uid and size -pub fn create_mapping(unique_id: &str, map_size: usize) -> Result { - new_map(unique_id, map_size) +pub fn create_mapping( + unique_id: &str, + map_size: usize, + ext: &ShmemConfExt, +) -> Result { + match ext.suppress_persistency { + true => create_file_mapping(unique_id, map_size, None), + false => create_persistent_file_mapping(unique_id, map_size), + } } //Opens an existing mapping specified by its uid @@ -307,5 +319,5 @@ pub fn open_mapping( _map_size: usize, ext: &ShmemConfExt, ) -> Result { - open_map(unique_id, ext.allow_raw) + open_map(unique_id, ext) } From 3afc7161ef359a261802b3873a13da95bbf03e37 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Thu, 13 Feb 2025 20:38:32 +0300 Subject: [PATCH 10/41] Update windows.rs --- src/windows.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/windows.rs b/src/windows.rs index 684e537..f746298 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -1,6 +1,6 @@ use std::fs::{File, OpenOptions}; use std::io::ErrorKind; -use std::os::windows::fs::OpenOptionsExt; +use std::os::windows::fs::{OpenOptionsExt, AsRawHandle}; use std::path::PathBuf; use crate::{log::*, ShmemConf}; @@ -155,7 +155,7 @@ fn open_map(unique_id: &str, ext: &ShmemConfExt) -> Result { Ok(f) => Some(f), Err(e) => { - if !allow_raw { + if !ext.allow_raw { return Err(ShmemError::MapOpenFailed(e.raw_os_error().unwrap() as _)); } } @@ -174,7 +174,7 @@ fn open_map(unique_id: &str, ext: &ShmemConfExt) -> Result unique_id, ); match OpenFileMapping(FILE_MAP_ALL_ACCESS, false, unique_id) { - Ok(h) => (Some(f), h), + Ok(h) => h, Err(e) => { return Err(ShmemError::MapOpenFailed(e.win32_error().unwrap().0)); } From 3f558458fc01d56c495d051aa8032a5282ab2ce6 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Thu, 13 Feb 2025 20:40:54 +0300 Subject: [PATCH 11/41] Update windows.rs --- src/windows.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/windows.rs b/src/windows.rs index f746298..3ee95bb 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -1,6 +1,6 @@ use std::fs::{File, OpenOptions}; use std::io::ErrorKind; -use std::os::windows::fs::{OpenOptionsExt, AsRawHandle}; +use std::os::windows::fs::{AsRawHandle, OpenOptionsExt}; use std::path::PathBuf; use crate::{log::*, ShmemConf}; From 8e2f2247cf7dae18964caecc10da9e4b9c256093 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Thu, 13 Feb 2025 20:45:23 +0300 Subject: [PATCH 12/41] Update windows.rs --- src/windows.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/windows.rs b/src/windows.rs index 3ee95bb..c1a7c4d 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -1,6 +1,6 @@ use std::fs::{File, OpenOptions}; use std::io::ErrorKind; -use std::os::windows::fs::{AsRawHandle, OpenOptionsExt}; +use std::os::windows::{fs::OpenOptionsExt, io::AsRawHandle}; use std::path::PathBuf; use crate::{log::*, ShmemConf}; From 57e628ec283252d81aa06df88f2788e54904fb08 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Thu, 13 Feb 2025 20:48:58 +0300 Subject: [PATCH 13/41] Update windows.rs --- src/windows.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/windows.rs b/src/windows.rs index c1a7c4d..9564a0f 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -158,6 +158,7 @@ fn open_map(unique_id: &str, ext: &ShmemConfExt) -> Result if !ext.allow_raw { return Err(ShmemError::MapOpenFailed(e.raw_os_error().unwrap() as _)); } + None } }, true => None, From 8ad701b8ca04b2e519498e12702c27bb9a4695f2 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Thu, 13 Feb 2025 20:52:59 +0300 Subject: [PATCH 14/41] Update windows.rs --- src/windows.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/windows.rs b/src/windows.rs index 9564a0f..27ba1a9 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -220,7 +220,7 @@ fn create_file_mapping( let map_h = { debug!("Creating memory mapping",); let h_handle = persistent_file - .map(|f| f.as_raw_handle() as _) + .map(|f| HANDLE(f.as_raw_handle() as _)) .unwrap_or(INVALID_HANDLE_VALUE); let high_size: u32 = ((map_size as u64 & 0xFFFF_FFFF_0000_0000_u64) >> 32) as u32; let low_size: u32 = (map_size as u64 & 0xFFFF_FFFF_u64) as u32; From 247c246bda8b8518ae40dd06e4f29b6563f42a4a Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Thu, 13 Feb 2025 20:57:17 +0300 Subject: [PATCH 15/41] Update windows.rs --- src/windows.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/windows.rs b/src/windows.rs index 27ba1a9..12b70ff 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -220,6 +220,7 @@ fn create_file_mapping( let map_h = { debug!("Creating memory mapping",); let h_handle = persistent_file + .as_ref() .map(|f| HANDLE(f.as_raw_handle() as _)) .unwrap_or(INVALID_HANDLE_VALUE); let high_size: u32 = ((map_size as u64 & 0xFFFF_FFFF_0000_0000_u64) >> 32) as u32; From d07248e548bf189239c196a177b0ea9b9798e9c6 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Fri, 14 Feb 2025 10:53:32 +0300 Subject: [PATCH 16/41] Update windows.rs --- src/windows.rs | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/src/windows.rs b/src/windows.rs index 12b70ff..8cbe191 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -139,36 +139,37 @@ fn get_tmp_dir() -> Result { } fn open_map(unique_id: &str, ext: &ShmemConfExt) -> Result { - // Create file to back the shared memory - let mut file_path = get_tmp_dir()?; - file_path.push(unique_id.trim_start_matches('/')); - debug!("Opening persistent_file at {}", file_path.to_string_lossy()); - let persistent_file = match ext.suppress_persistency { - false => match OpenOptions::new() - .read(true) - .write(true) - .share_mode((FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE).0) - .attributes((FILE_ATTRIBUTE_TEMPORARY).0) - .create(false) - .open(&file_path) - { - Ok(f) => Some(f), - Err(e) => { - if !ext.allow_raw { - return Err(ShmemError::MapOpenFailed(e.raw_os_error().unwrap() as _)); + false => { + let mut file_path = get_tmp_dir()?; + file_path.push(unique_id.trim_start_matches('/')); + info!("Opening persistent_file at {}", file_path.to_string_lossy()); + + match OpenOptions::new() + .read(true) + .write(true) + .share_mode((FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE).0) + .attributes((FILE_ATTRIBUTE_TEMPORARY).0) + .create(false) + .open(&file_path) + { + Ok(f) => Some(f), + Err(e) => { + if !ext.allow_raw { + return Err(ShmemError::MapOpenFailed(e.raw_os_error().unwrap() as _)); + } + None } - None } - }, + } true => None, }; let map_h = { //Open Mapping - debug!("Opening memory mapping",); + info!("Opening memory mapping",); - trace!( + info!( "OpenFileMappingW({:?}, {}, '{}')", FILE_MAP_ALL_ACCESS, false, @@ -218,14 +219,14 @@ fn create_file_mapping( persistent_file: Option, ) -> Result { let map_h = { - debug!("Creating memory mapping",); + info!("Creating memory mapping",); let h_handle = persistent_file .as_ref() .map(|f| HANDLE(f.as_raw_handle() as _)) .unwrap_or(INVALID_HANDLE_VALUE); let high_size: u32 = ((map_size as u64 & 0xFFFF_FFFF_0000_0000_u64) >> 32) as u32; let low_size: u32 = (map_size as u64 & 0xFFFF_FFFF_u64) as u32; - trace!( + info!( "CreateFileMapping({:?}, NULL, {:X}, {}, {}, '{}')", h_handle, PAGE_READWRITE.0, @@ -284,7 +285,7 @@ fn create_persistent_file_mapping(unique_id: &str, map_size: usize) -> Result Date: Fri, 14 Feb 2025 10:54:48 +0300 Subject: [PATCH 17/41] Update windows.rs --- src/windows.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/windows.rs b/src/windows.rs index 8cbe191..8339c8d 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -171,9 +171,7 @@ fn open_map(unique_id: &str, ext: &ShmemConfExt) -> Result info!( "OpenFileMappingW({:?}, {}, '{}')", - FILE_MAP_ALL_ACCESS, - false, - unique_id, + FILE_MAP_ALL_ACCESS, false, unique_id, ); match OpenFileMapping(FILE_MAP_ALL_ACCESS, false, unique_id) { Ok(h) => h, @@ -228,11 +226,7 @@ fn create_file_mapping( let low_size: u32 = (map_size as u64 & 0xFFFF_FFFF_u64) as u32; info!( "CreateFileMapping({:?}, NULL, {:X}, {}, {}, '{}')", - h_handle, - PAGE_READWRITE.0, - high_size, - low_size, - unique_id, + h_handle, PAGE_READWRITE.0, high_size, low_size, unique_id, ); match CreateFileMapping( From 076fcb38decc01ab51b2e3b92a3a1549925a5715 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Fri, 14 Feb 2025 11:06:28 +0300 Subject: [PATCH 18/41] Update rust.yml and windows.rs --- .github/workflows/rust.yml | 3 +++ src/windows.rs | 22 ++++++++++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 56facc2..b15fc46 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -44,6 +44,9 @@ jobs: with: command: build + - name: Set RUST_LOG + run: echo "RUST_LOG=trace" >> $GITHUB_ENV + - name: Tests uses: actions-rs/cargo@v1.0.3 with: diff --git a/src/windows.rs b/src/windows.rs index 8339c8d..d132293 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -143,7 +143,7 @@ fn open_map(unique_id: &str, ext: &ShmemConfExt) -> Result false => { let mut file_path = get_tmp_dir()?; file_path.push(unique_id.trim_start_matches('/')); - info!("Opening persistent_file at {}", file_path.to_string_lossy()); + debug!("Opening persistent_file at {}", file_path.to_string_lossy()); match OpenOptions::new() .read(true) @@ -167,11 +167,13 @@ fn open_map(unique_id: &str, ext: &ShmemConfExt) -> Result let map_h = { //Open Mapping - info!("Opening memory mapping",); + debug!("Opening memory mapping",); - info!( + trace!( "OpenFileMappingW({:?}, {}, '{}')", - FILE_MAP_ALL_ACCESS, false, unique_id, + FILE_MAP_ALL_ACCESS, + false, + unique_id, ); match OpenFileMapping(FILE_MAP_ALL_ACCESS, false, unique_id) { Ok(h) => h, @@ -217,16 +219,20 @@ fn create_file_mapping( persistent_file: Option, ) -> Result { let map_h = { - info!("Creating memory mapping",); + debug!("Creating memory mapping",); let h_handle = persistent_file .as_ref() .map(|f| HANDLE(f.as_raw_handle() as _)) .unwrap_or(INVALID_HANDLE_VALUE); let high_size: u32 = ((map_size as u64 & 0xFFFF_FFFF_0000_0000_u64) >> 32) as u32; let low_size: u32 = (map_size as u64 & 0xFFFF_FFFF_u64) as u32; - info!( + trace!( "CreateFileMapping({:?}, NULL, {:X}, {}, {}, '{}')", - h_handle, PAGE_READWRITE.0, high_size, low_size, unique_id, + h_handle, + PAGE_READWRITE.0, + high_size, + low_size, + unique_id, ); match CreateFileMapping( @@ -279,7 +285,7 @@ fn create_persistent_file_mapping(unique_id: &str, map_size: usize) -> Result Date: Fri, 14 Feb 2025 11:12:19 +0300 Subject: [PATCH 19/41] Update rust.yml --- .github/workflows/rust.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index b15fc46..9056f91 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -51,6 +51,7 @@ jobs: uses: actions-rs/cargo@v1.0.3 with: command: test + args: -- --nocapture - name: Examples uses: actions-rs/cargo@v1.0.3 From 7223179fa9e49531b666a93d090bfda0bc726c94 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Fri, 14 Feb 2025 11:23:35 +0300 Subject: [PATCH 20/41] Update rust.yml --- .github/workflows/rust.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 9056f91..27751e9 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -43,6 +43,7 @@ jobs: uses: actions-rs/cargo@v1.0.3 with: command: build + args: --features log - name: Set RUST_LOG run: echo "RUST_LOG=trace" >> $GITHUB_ENV From 4d7241795057f433b1b2061a39b82976dd77b449 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Fri, 14 Feb 2025 11:23:59 +0300 Subject: [PATCH 21/41] Update rust.yml --- .github/workflows/rust.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 27751e9..171801c 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -43,7 +43,7 @@ jobs: uses: actions-rs/cargo@v1.0.3 with: command: build - args: --features log + args: --features logging - name: Set RUST_LOG run: echo "RUST_LOG=trace" >> $GITHUB_ENV From f2f2d9127535d98c18a5f33f937fd0aaa5548515 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Fri, 14 Feb 2025 11:28:57 +0300 Subject: [PATCH 22/41] Update rust.yml --- .github/workflows/rust.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 171801c..a310848 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -43,7 +43,6 @@ jobs: uses: actions-rs/cargo@v1.0.3 with: command: build - args: --features logging - name: Set RUST_LOG run: echo "RUST_LOG=trace" >> $GITHUB_ENV @@ -52,7 +51,7 @@ jobs: uses: actions-rs/cargo@v1.0.3 with: command: test - args: -- --nocapture + args: --features logging -- --nocapture - name: Examples uses: actions-rs/cargo@v1.0.3 From 26d4b64da6c5bb08c4dd2c01d7a7c7e423b6691e Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Fri, 14 Feb 2025 11:37:24 +0300 Subject: [PATCH 23/41] - add public Ext conf access - add suppress_persistency test for windows --- src/lib.rs | 7 +++++++ tests/windows.rs | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 tests/windows.rs diff --git a/src/lib.rs b/src/lib.rs index 1eca78a..6661478 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,6 +43,7 @@ cfg_if! { compile_error!("shared_memory isnt implemented for this platform..."); } } +pub use os_impl::ShmemConfExt; #[derive(Clone, Default)] /// Struct used to configure different parameters before creating a shared memory mapping @@ -100,6 +101,12 @@ impl ShmemConf { self } + /// Sets the platform-specific parameters + pub fn ext(mut self, ext: os_impl::ShmemConfExt) -> Self { + self.ext = ext; + self + } + /// Create a new mapping using the current configuration pub fn create(mut self) -> Result { if self.size == 0 { diff --git a/tests/windows.rs b/tests/windows.rs new file mode 100644 index 0000000..0667d8a --- /dev/null +++ b/tests/windows.rs @@ -0,0 +1,21 @@ +#[cfg(target_os="windows")] +mod windows_tests { + use shared_memory::{ShmemConf, ShmemConfExt}; + + #[test] + fn suppress_persistency() { + let os_id = { + let mut shmem = ShmemConf::new() + .size(4096) + .ext(ShmemConfExt { + allow_raw: false, + suppress_persistency: true, + }) + .create() + .unwrap(); + shmem.set_owner(false); + String::from(shmem.get_os_id()) + }; + assert!(ShmemConf::new().os_id(os_id).open().is_err()); + } +} From 2cb18d88c1e622e30a00f357d8a7bfbe68a2a701 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Fri, 14 Feb 2025 11:37:40 +0300 Subject: [PATCH 24/41] Update windows.rs --- tests/windows.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/windows.rs b/tests/windows.rs index 0667d8a..ad36dec 100644 --- a/tests/windows.rs +++ b/tests/windows.rs @@ -1,4 +1,4 @@ -#[cfg(target_os="windows")] +#[cfg(target_os = "windows")] mod windows_tests { use shared_memory::{ShmemConf, ShmemConfExt}; From 05fc0e685c3bd909e52363341bd172a8e4a0b229 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Fri, 14 Feb 2025 14:57:39 +0300 Subject: [PATCH 25/41] Update windows.rs --- src/windows.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/windows.rs b/src/windows.rs index d132293..ca7764d 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -308,12 +308,13 @@ fn create_persistent_file_mapping(unique_id: &str, map_size: usize) -> Result Result { - match ext.suppress_persistency { - true => create_file_mapping(unique_id, map_size, None), - false => create_persistent_file_mapping(unique_id, map_size), - } + create_persistent_file_mapping(unique_id, map_size) + //match ext.suppress_persistency { + // true => create_file_mapping(unique_id, map_size, None), + // false => create_persistent_file_mapping(unique_id, map_size), + //} } //Opens an existing mapping specified by its uid From 6d8f6fafb730b20ff42753c02d10cf403a50a511 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Fri, 14 Feb 2025 15:03:55 +0300 Subject: [PATCH 26/41] Update windows.rs --- src/windows.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/windows.rs b/src/windows.rs index ca7764d..df2762c 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -10,8 +10,8 @@ use crate::ShmemError; #[derive(Clone, Default)] pub struct ShmemConfExt { - allow_raw: bool, - suppress_persistency: bool, + pub allow_raw: bool, + pub suppress_persistency: bool, } impl ShmemConf { From 7cbef68608600e2ae20130cfb5aae6eb9d96ac76 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Fri, 14 Feb 2025 15:25:35 +0300 Subject: [PATCH 27/41] Update windows.rs --- src/windows.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/windows.rs b/src/windows.rs index df2762c..9cdf8e3 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -65,6 +65,7 @@ impl Drop for MapData { // deleted once all handles have been closed and no new handles can be opened // because the file has been renamed. This matches the behavior of shm_unlink() // on unix. + /* if self.owner { let mut base_path = get_tmp_dir().unwrap(); @@ -106,6 +107,7 @@ impl Drop for MapData { } }; } + */ } } @@ -308,13 +310,12 @@ fn create_persistent_file_mapping(unique_id: &str, map_size: usize) -> Result Result { - create_persistent_file_mapping(unique_id, map_size) - //match ext.suppress_persistency { - // true => create_file_mapping(unique_id, map_size, None), - // false => create_persistent_file_mapping(unique_id, map_size), - //} + match ext.suppress_persistency { + true => create_file_mapping(unique_id, map_size, None), + false => create_persistent_file_mapping(unique_id, map_size), + } } //Opens an existing mapping specified by its uid From 4bf42d2825a12d3457f80ca675b362cdebd83272 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Fri, 14 Feb 2025 16:31:42 +0300 Subject: [PATCH 28/41] Update windows.rs --- src/windows.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/windows.rs b/src/windows.rs index 9cdf8e3..b1b3b93 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -65,7 +65,6 @@ impl Drop for MapData { // deleted once all handles have been closed and no new handles can be opened // because the file has been renamed. This matches the behavior of shm_unlink() // on unix. - /* if self.owner { let mut base_path = get_tmp_dir().unwrap(); @@ -107,7 +106,6 @@ impl Drop for MapData { } }; } - */ } } From 32c2ab00d6cf9dfb17e545c5687f003f701ca429 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Fri, 14 Feb 2025 16:40:03 +0300 Subject: [PATCH 29/41] Update windows.rs --- src/windows.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/windows.rs b/src/windows.rs index b1b3b93..654e66f 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -149,7 +149,7 @@ fn open_map(unique_id: &str, ext: &ShmemConfExt) -> Result .read(true) .write(true) .share_mode((FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE).0) - .attributes((FILE_ATTRIBUTE_TEMPORARY).0) + //.attributes((FILE_ATTRIBUTE_TEMPORARY).0) .create(false) .open(&file_path) { @@ -294,7 +294,7 @@ fn create_persistent_file_mapping(unique_id: &str, map_size: usize) -> Result Date: Fri, 14 Feb 2025 16:45:43 +0300 Subject: [PATCH 30/41] Update windows.rs --- src/windows.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/windows.rs b/src/windows.rs index 654e66f..8cce69f 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -143,13 +143,13 @@ fn open_map(unique_id: &str, ext: &ShmemConfExt) -> Result false => { let mut file_path = get_tmp_dir()?; file_path.push(unique_id.trim_start_matches('/')); - debug!("Opening persistent_file at {}", file_path.to_string_lossy()); + println!("Opening persistent_file at {}", file_path.to_string_lossy()); match OpenOptions::new() .read(true) .write(true) .share_mode((FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE).0) - //.attributes((FILE_ATTRIBUTE_TEMPORARY).0) + .attributes((FILE_ATTRIBUTE_TEMPORARY).0) .create(false) .open(&file_path) { @@ -285,7 +285,7 @@ fn create_persistent_file_mapping(unique_id: &str, map_size: usize) -> Result Result Date: Fri, 14 Feb 2025 17:01:20 +0300 Subject: [PATCH 31/41] Update windows.rs --- src/windows.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/windows.rs b/src/windows.rs index 8cce69f..df3bf3a 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -143,7 +143,7 @@ fn open_map(unique_id: &str, ext: &ShmemConfExt) -> Result false => { let mut file_path = get_tmp_dir()?; file_path.push(unique_id.trim_start_matches('/')); - println!("Opening persistent_file at {}", file_path.to_string_lossy()); + debug!("Opening persistent_file at {}", file_path.to_string_lossy()); match OpenOptions::new() .read(true) @@ -271,6 +271,8 @@ fn create_file_mapping( }; trace!("\t{:p}", map_ptr); + *map_ptr = 0; + Ok(MapData { owner: true, file_map: map_h, @@ -285,7 +287,7 @@ fn create_persistent_file_mapping(unique_id: &str, map_size: usize) -> Result Date: Fri, 14 Feb 2025 17:12:06 +0300 Subject: [PATCH 32/41] Update windows.rs and posix_semantics.rs --- src/windows.rs | 2 -- tests/posix_semantics.rs | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/windows.rs b/src/windows.rs index df3bf3a..b1b3b93 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -271,8 +271,6 @@ fn create_file_mapping( }; trace!("\t{:p}", map_ptr); - *map_ptr = 0; - Ok(MapData { owner: true, file_map: map_h, diff --git a/tests/posix_semantics.rs b/tests/posix_semantics.rs index dc10536..7b15a25 100644 --- a/tests/posix_semantics.rs +++ b/tests/posix_semantics.rs @@ -7,6 +7,7 @@ fn persistence() { let os_id = { let mut shmem = ShmemConf::new().size(4096).create().unwrap(); shmem.set_owner(false); + unsafe { shmem.as_slice_mut().fill(0) }; String::from(shmem.get_os_id()) }; let mut shmem = ShmemConf::new().os_id(os_id).open().unwrap(); From 3acb763143aa1b697438df6b643e70fa2a1b4a57 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Fri, 14 Feb 2025 17:23:27 +0300 Subject: [PATCH 33/41] Update windows.rs and posix_semantics.rs --- src/windows.rs | 6 +++--- tests/posix_semantics.rs | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/windows.rs b/src/windows.rs index b1b3b93..6bc9b5d 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -155,9 +155,9 @@ fn open_map(unique_id: &str, ext: &ShmemConfExt) -> Result { Ok(f) => Some(f), Err(e) => { - if !ext.allow_raw { - return Err(ShmemError::MapOpenFailed(e.raw_os_error().unwrap() as _)); - } + //if !ext.allow_raw { + // return Err(ShmemError::MapOpenFailed(e.raw_os_error().unwrap() as _)); + //} None } } diff --git a/tests/posix_semantics.rs b/tests/posix_semantics.rs index 7b15a25..dc10536 100644 --- a/tests/posix_semantics.rs +++ b/tests/posix_semantics.rs @@ -7,7 +7,6 @@ fn persistence() { let os_id = { let mut shmem = ShmemConf::new().size(4096).create().unwrap(); shmem.set_owner(false); - unsafe { shmem.as_slice_mut().fill(0) }; String::from(shmem.get_os_id()) }; let mut shmem = ShmemConf::new().os_id(os_id).open().unwrap(); From 141adbb38850f194d9aedd855fd4bf9b6aab2eed Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Fri, 14 Feb 2025 17:25:41 +0300 Subject: [PATCH 34/41] Update windows.rs --- src/windows.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/windows.rs b/src/windows.rs index 6bc9b5d..49fb5ad 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -154,7 +154,7 @@ fn open_map(unique_id: &str, ext: &ShmemConfExt) -> Result .open(&file_path) { Ok(f) => Some(f), - Err(e) => { + Err(_e) => { //if !ext.allow_raw { // return Err(ShmemError::MapOpenFailed(e.raw_os_error().unwrap() as _)); //} From 9d3ceff58f7d078513c9d28cc5a5129608562015 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Fri, 14 Feb 2025 17:37:37 +0300 Subject: [PATCH 35/41] Update rust.yml, windows.rs, and 2 more files... --- .github/workflows/rust.yml | 3 +++ src/windows.rs | 8 ++++---- tests/general.rs | 2 +- tests/posix_semantics.rs | 14 ++++++++++++-- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index a310848..82c6b48 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -27,6 +27,9 @@ jobs: override: true components: rustfmt, clippy + - name: Setup rust-cache + uses: Swatinem/rust-cache@v2 + - name: Check Code Format uses: actions-rs/cargo@v1.0.3 with: diff --git a/src/windows.rs b/src/windows.rs index 49fb5ad..b1b3b93 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -154,10 +154,10 @@ fn open_map(unique_id: &str, ext: &ShmemConfExt) -> Result .open(&file_path) { Ok(f) => Some(f), - Err(_e) => { - //if !ext.allow_raw { - // return Err(ShmemError::MapOpenFailed(e.raw_os_error().unwrap() as _)); - //} + Err(e) => { + if !ext.allow_raw { + return Err(ShmemError::MapOpenFailed(e.raw_os_error().unwrap() as _)); + } None } } diff --git a/tests/general.rs b/tests/general.rs index f689f54..b247826 100644 --- a/tests/general.rs +++ b/tests/general.rs @@ -57,7 +57,7 @@ fn open_os_id() { // Drop the owner of the mapping drop(s1); - // Make sure it can be openned again + // Make sure it can't be openned again assert!(ShmemConf::new().os_id(&os_id).open().is_err()); drop(s2); diff --git a/tests/posix_semantics.rs b/tests/posix_semantics.rs index dc10536..0169b3b 100644 --- a/tests/posix_semantics.rs +++ b/tests/posix_semantics.rs @@ -1,4 +1,4 @@ -use shared_memory::ShmemConf; +use shared_memory::{ShmemConf, ShmemConfExt}; use std::sync::mpsc::channel; use std::thread; @@ -9,7 +9,17 @@ fn persistence() { shmem.set_owner(false); String::from(shmem.get_os_id()) }; - let mut shmem = ShmemConf::new().os_id(os_id).open().unwrap(); + + let mut shmem = ShmemConf::new() + .os_id(os_id) + .ext(ShmemConfExt { + #[cfg(target_os = "windows")] + allow_raw: true, + #[cfg(target_os = "windows")] + suppress_persistency: false, + }) + .open() + .unwrap(); shmem.set_owner(true); } From 9a44e58948885f875be0437983a37b6faa397968 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Fri, 14 Feb 2025 17:43:46 +0300 Subject: [PATCH 36/41] Update posix_semantics.rs --- tests/posix_semantics.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/posix_semantics.rs b/tests/posix_semantics.rs index 0169b3b..3ca619a 100644 --- a/tests/posix_semantics.rs +++ b/tests/posix_semantics.rs @@ -5,7 +5,16 @@ use std::thread; #[test] fn persistence() { let os_id = { - let mut shmem = ShmemConf::new().size(4096).create().unwrap(); + let mut shmem = ShmemConf::new() + .size(4096) + .ext(ShmemConfExt { + #[cfg(target_os = "windows")] + allow_raw: true, + #[cfg(target_os = "windows")] + suppress_persistency: false, + }) + .create() + .unwrap(); shmem.set_owner(false); String::from(shmem.get_os_id()) }; From 3312e9e11e83d3b7c927bed9548a5c600a9e98ce Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Fri, 14 Feb 2025 17:47:25 +0300 Subject: [PATCH 37/41] Update rust.yml and posix_semantics.rs --- .github/workflows/rust.yml | 3 --- tests/posix_semantics.rs | 24 +++--------------------- 2 files changed, 3 insertions(+), 24 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 82c6b48..a310848 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -27,9 +27,6 @@ jobs: override: true components: rustfmt, clippy - - name: Setup rust-cache - uses: Swatinem/rust-cache@v2 - - name: Check Code Format uses: actions-rs/cargo@v1.0.3 with: diff --git a/tests/posix_semantics.rs b/tests/posix_semantics.rs index 3ca619a..b100233 100644 --- a/tests/posix_semantics.rs +++ b/tests/posix_semantics.rs @@ -1,34 +1,16 @@ -use shared_memory::{ShmemConf, ShmemConfExt}; +use shared_memory::ShmemConf; use std::sync::mpsc::channel; use std::thread; #[test] fn persistence() { let os_id = { - let mut shmem = ShmemConf::new() - .size(4096) - .ext(ShmemConfExt { - #[cfg(target_os = "windows")] - allow_raw: true, - #[cfg(target_os = "windows")] - suppress_persistency: false, - }) - .create() - .unwrap(); + let mut shmem = ShmemConf::new().size(4096).create().unwrap(); shmem.set_owner(false); String::from(shmem.get_os_id()) }; - let mut shmem = ShmemConf::new() - .os_id(os_id) - .ext(ShmemConfExt { - #[cfg(target_os = "windows")] - allow_raw: true, - #[cfg(target_os = "windows")] - suppress_persistency: false, - }) - .open() - .unwrap(); + let mut shmem = ShmemConf::new().os_id(os_id).open().unwrap(); shmem.set_owner(true); } From d45fb93f971a4961cfb5fd12ed6f38f397648fb2 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Fri, 14 Feb 2025 17:54:18 +0300 Subject: [PATCH 38/41] Update rust.yml --- .github/workflows/rust.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index a310848..bf233a8 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -44,14 +44,13 @@ jobs: with: command: build - - name: Set RUST_LOG - run: echo "RUST_LOG=trace" >> $GITHUB_ENV - - name: Tests uses: actions-rs/cargo@v1.0.3 + env: + RUST_LOG: "trace" with: command: test - args: --features logging -- --nocapture + args: --all-features -- --nocapture - name: Examples uses: actions-rs/cargo@v1.0.3 From d74976e7f6b67b388d3cca6914bb897dcf72c79a Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Mon, 17 Feb 2025 15:52:58 +0300 Subject: [PATCH 39/41] refactor to respect both persistent and non-persistent modes on win --- src/windows.rs | 208 ++++++++++++++++++++++--------------------------- 1 file changed, 94 insertions(+), 114 deletions(-) diff --git a/src/windows.rs b/src/windows.rs index b1b3b93..d9380ae 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -138,47 +138,73 @@ fn get_tmp_dir() -> Result { } } -fn open_map(unique_id: &str, ext: &ShmemConfExt) -> Result { - let persistent_file = match ext.suppress_persistency { - false => { - let mut file_path = get_tmp_dir()?; - file_path.push(unique_id.trim_start_matches('/')); - debug!("Opening persistent_file at {}", file_path.to_string_lossy()); +fn map_error(error: u32) -> Result { + if CREATE { + ShmemError::MapCreateFailed(error) + } else { + ShmemError::MapOpenFailed(error) + } +} - match OpenOptions::new() - .read(true) - .write(true) - .share_mode((FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE).0) - .attributes((FILE_ATTRIBUTE_TEMPORARY).0) - .create(false) - .open(&file_path) - { - Ok(f) => Some(f), +fn attach_file_mapping( + unique_id: &str, + map_size: usize, + persistent_file: Option, +) -> Result { + let map_h = { + debug!( + "{} memory mapping", + if CREATE { "Creating" } else { "Opening" }, + ); + + // if there is an existing persistency file OR we create a new mapping, then use CreateFileMapping + if persistent_file.is_some() || CREATE { + let h_handle = persistent_file + .as_ref() + .map(|f| HANDLE(f.as_raw_handle() as _)) + .unwrap_or(INVALID_HANDLE_VALUE); + let high_size: u32 = ((map_size as u64 & 0xFFFF_FFFF_0000_0000_u64) >> 32) as u32; + let low_size: u32 = (map_size as u64 & 0xFFFF_FFFF_u64) as u32; + trace!( + "CreateFileMapping({:?}, NULL, {:X}, {}, {}, '{}')", + h_handle, + PAGE_READWRITE.0, + high_size, + low_size, + unique_id, + ); + + match CreateFileMapping( + h_handle, + None, + PAGE_READWRITE, + high_size, + low_size, + unique_id, + ) { + Ok(v) => v, Err(e) => { - if !ext.allow_raw { - return Err(ShmemError::MapOpenFailed(e.raw_os_error().unwrap() as _)); - } - None + let err_code = e.win32_error().unwrap(); + return Err(if err_code == ERROR_ALREADY_EXISTS { + ShmemError::MappingIdExists + } else { + map_error::(err_code.0) + }); } } - } - true => None, - }; - - let map_h = { - //Open Mapping - debug!("Opening memory mapping",); - - trace!( - "OpenFileMappingW({:?}, {}, '{}')", - FILE_MAP_ALL_ACCESS, - false, - unique_id, - ); - match OpenFileMapping(FILE_MAP_ALL_ACCESS, false, unique_id) { - Ok(h) => h, - Err(e) => { - return Err(ShmemError::MapOpenFailed(e.win32_error().unwrap().0)); + } else { + // open existing mapping instead + trace!( + "OpenFileMappingW({:?}, {}, '{}')", + FILE_MAP_ALL_ACCESS, + false, + unique_id, + ); + match OpenFileMapping(FILE_MAP_ALL_ACCESS, false, unique_id) { + Ok(h) => h, + Err(e) => { + return Err(ShmemError::MapOpenFailed(e.win32_error().unwrap().0)); + } } } }; @@ -193,7 +219,7 @@ fn open_map(unique_id: &str, ext: &ShmemConfExt) -> Result ); let map_ptr = match MapViewOfFile(map_h.as_handle(), FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0) { Ok(v) => v, - Err(e) => return Err(ShmemError::MapOpenFailed(e.win32_error().unwrap().0)), + Err(e) => return Err(ShmemError::UnknownOsError(e.win32_error().unwrap().0)), }; trace!("\t{:p}", map_ptr); @@ -204,7 +230,7 @@ fn open_map(unique_id: &str, ext: &ShmemConfExt) -> Result } Ok(MapData { - owner: false, + owner: CREATE, file_map: map_h, persistent_file, unique_id: unique_id.to_string(), @@ -213,80 +239,17 @@ fn open_map(unique_id: &str, ext: &ShmemConfExt) -> Result }) } -fn create_file_mapping( +fn attach_persistent_file_mapping( unique_id: &str, map_size: usize, - persistent_file: Option, + allow_raw: bool, ) -> Result { - let map_h = { - debug!("Creating memory mapping",); - let h_handle = persistent_file - .as_ref() - .map(|f| HANDLE(f.as_raw_handle() as _)) - .unwrap_or(INVALID_HANDLE_VALUE); - let high_size: u32 = ((map_size as u64 & 0xFFFF_FFFF_0000_0000_u64) >> 32) as u32; - let low_size: u32 = (map_size as u64 & 0xFFFF_FFFF_u64) as u32; - trace!( - "CreateFileMapping({:?}, NULL, {:X}, {}, {}, '{}')", - h_handle, - PAGE_READWRITE.0, - high_size, - low_size, - unique_id, - ); - - match CreateFileMapping( - h_handle, - None, - PAGE_READWRITE, - high_size, - low_size, - unique_id, - ) { - Ok(v) => v, - Err(e) => { - let err_code = e.win32_error().unwrap(); - return Err(if err_code == ERROR_ALREADY_EXISTS { - ShmemError::MappingIdExists - } else { - ShmemError::MapCreateFailed(err_code.0) - }); - } - } - }; - trace!("0x{:X}", map_h); - - //Map mapping into address space - debug!("Loading mapping into address space"); - trace!( - "MapViewOfFile(0x{:X}, {:X}, 0, 0, 0)", - map_h, - (FILE_MAP_READ | FILE_MAP_WRITE).0, - ); - let map_ptr = match MapViewOfFile(map_h.as_handle(), FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0) { - Ok(v) => v, - Err(e) => { - return Err(ShmemError::MapCreateFailed(e.win32_error().unwrap().0)); - } - }; - trace!("\t{:p}", map_ptr); - - Ok(MapData { - owner: true, - file_map: map_h, - persistent_file, - unique_id: unique_id.to_string(), - map_size, - view: map_ptr, - }) -} - -fn create_persistent_file_mapping(unique_id: &str, map_size: usize) -> Result { // Create file to back the shared memory let mut file_path = get_tmp_dir()?; file_path.push(unique_id.trim_start_matches('/')); debug!( - "Creating persistent_file at {}", + "{} persistent_file at {}", + if CREATE { "Creating" } else { "Opening" }, file_path.to_string_lossy() ); @@ -295,32 +258,49 @@ fn create_persistent_file_mapping(unique_id: &str, map_size: usize) -> Result create_file_mapping(unique_id, map_size, Some(f)), + Ok(f) => attach_file_mapping::(unique_id, map_size, Some(f)), Err(e) if e.kind() == ErrorKind::AlreadyExists => Err(ShmemError::MappingIdExists), - Err(e) => Err(ShmemError::MapCreateFailed(e.raw_os_error().unwrap() as _)), + Err(e) => { + if !CREATE && allow_raw { + // This may be a mapping that isnt managed by this crate + // Try to open the mapping without any backing file + attach_file_mapping::(unique_id, map_size, None) + } + map_error::(e.raw_os_error().unwrap() as _) + } } } //Creates a mapping specified by the uid and size -pub fn create_mapping( +pub fn attach( unique_id: &str, map_size: usize, ext: &ShmemConfExt, ) -> Result { match ext.suppress_persistency { - true => create_file_mapping(unique_id, map_size, None), - false => create_persistent_file_mapping(unique_id, map_size), + true => attach_file_mapping::(unique_id, map_size, None), + false => attach_persistent_file_mapping::(unique_id, map_size), } } +//Creates a mapping specified by the uid and size +pub fn create_mapping( + unique_id: &str, + map_size: usize, + ext: &ShmemConfExt, +) -> Result { + attach::(unique_id, map_size) +} + //Opens an existing mapping specified by its uid pub fn open_mapping( unique_id: &str, - _map_size: usize, + map_size: usize, ext: &ShmemConfExt, ) -> Result { - open_map(unique_id, ext) + attach::(unique_id, map_size) } From 2ca5c03daf6704ce24826210f96a0fe554fbae55 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Mon, 17 Feb 2025 16:16:22 +0300 Subject: [PATCH 40/41] Update windows.rs --- src/windows.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/windows.rs b/src/windows.rs index d9380ae..26224df 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -139,11 +139,11 @@ fn get_tmp_dir() -> Result { } fn map_error(error: u32) -> Result { - if CREATE { + Err(if CREATE { ShmemError::MapCreateFailed(error) } else { ShmemError::MapOpenFailed(error) - } + }) } fn attach_file_mapping( @@ -268,7 +268,7 @@ fn attach_persistent_file_mapping( if !CREATE && allow_raw { // This may be a mapping that isnt managed by this crate // Try to open the mapping without any backing file - attach_file_mapping::(unique_id, map_size, None) + return attach_file_mapping::(unique_id, map_size, None); } map_error::(e.raw_os_error().unwrap() as _) } @@ -283,7 +283,7 @@ pub fn attach( ) -> Result { match ext.suppress_persistency { true => attach_file_mapping::(unique_id, map_size, None), - false => attach_persistent_file_mapping::(unique_id, map_size), + false => attach_persistent_file_mapping::(unique_id, map_size, ext.allow_raw), } } @@ -293,7 +293,7 @@ pub fn create_mapping( map_size: usize, ext: &ShmemConfExt, ) -> Result { - attach::(unique_id, map_size) + attach::(unique_id, map_size, ext) } //Opens an existing mapping specified by its uid @@ -302,5 +302,5 @@ pub fn open_mapping( map_size: usize, ext: &ShmemConfExt, ) -> Result { - attach::(unique_id, map_size) + attach::(unique_id, map_size, ext) } From f41afd84a508fc16a22f74cff4f1fad25d2cbd3b Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Mon, 17 Feb 2025 16:20:50 +0300 Subject: [PATCH 41/41] Update windows.rs --- src/windows.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/windows.rs b/src/windows.rs index 26224df..19b85a1 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -185,11 +185,11 @@ fn attach_file_mapping( Ok(v) => v, Err(e) => { let err_code = e.win32_error().unwrap(); - return Err(if err_code == ERROR_ALREADY_EXISTS { - ShmemError::MappingIdExists + return if err_code == ERROR_ALREADY_EXISTS { + Err(ShmemError::MappingIdExists) } else { map_error::(err_code.0) - }); + }; } } } else {