@@ -3,11 +3,24 @@ use std::io::ErrorKind;
33use std:: os:: windows:: { fs:: OpenOptionsExt , io:: AsRawHandle } ;
44use std:: path:: PathBuf ;
55
6- use crate :: log:: * ;
6+ use crate :: { log:: * , ShmemConf } ;
77use win_sys:: * ;
88
99use crate :: ShmemError ;
1010
11+ #[ derive( Clone , Default ) ]
12+ pub struct ShmemConfExt {
13+ allow_raw : bool ,
14+ }
15+
16+ impl ShmemConf {
17+ /// If set to true, enables openning raw shared memory that is not managed by this crate
18+ pub fn allow_raw ( mut self , allow : bool ) -> Self {
19+ self . ext . allow_raw = allow;
20+ self
21+ }
22+ }
23+
1124pub struct MapData {
1225 owner : bool ,
1326
@@ -118,7 +131,12 @@ fn get_tmp_dir() -> Result<PathBuf, ShmemError> {
118131 }
119132}
120133
121- fn new_map ( unique_id : & str , map_size : usize , create : bool ) -> Result < MapData , ShmemError > {
134+ fn new_map (
135+ unique_id : & str ,
136+ mut map_size : usize ,
137+ create : bool ,
138+ allow_raw : bool ,
139+ ) -> Result < MapData , ShmemError > {
122140 // Create file to back the shared memory
123141 let mut file_path = get_tmp_dir ( ) ?;
124142 file_path. push ( unique_id. trim_start_matches ( '/' ) ) ;
@@ -188,6 +206,8 @@ fn new_map(unique_id: &str, map_size: usize, create: bool) -> Result<MapData, Sh
188206 Err ( e) => {
189207 if create {
190208 return Err ( ShmemError :: MapCreateFailed ( e. raw_os_error ( ) . unwrap ( ) as _ ) ) ;
209+ } else if !allow_raw {
210+ return Err ( ShmemError :: MapOpenFailed ( ERROR_FILE_NOT_FOUND . 0 ) ) ;
191211 }
192212
193213 // This may be a mapping that isnt managed by this crate
@@ -248,10 +268,14 @@ fn new_map(unique_id: &str, map_size: usize, create: bool) -> Result<MapData, Sh
248268
249269//Creates a mapping specified by the uid and size
250270pub fn create_mapping ( unique_id : & str , map_size : usize ) -> Result < MapData , ShmemError > {
251- new_map ( unique_id, map_size, true )
271+ new_map ( unique_id, map_size, true , false )
252272}
253273
254274//Opens an existing mapping specified by its uid
255- pub fn open_mapping ( unique_id : & str , map_size : usize ) -> Result < MapData , ShmemError > {
256- new_map ( unique_id, map_size, false )
275+ pub fn open_mapping (
276+ unique_id : & str ,
277+ map_size : usize ,
278+ ext : & ShmemConfExt ,
279+ ) -> Result < MapData , ShmemError > {
280+ new_map ( unique_id, map_size, false , ext. allow_raw )
257281}
0 commit comments