@@ -159,7 +159,7 @@ pub fn getcwd() -> IoResult<Path> {
159159}
160160
161161#[ cfg( windows) ]
162- pub mod windows {
162+ pub mod windoze {
163163 use libc:: types:: os:: arch:: extra:: DWORD ;
164164 use libc;
165165 use option:: Option ;
@@ -385,7 +385,7 @@ pub fn getenv_as_bytes(n: &str) -> Option<Vec<u8>> {
385385pub fn getenv ( n : & str ) -> Option < String > {
386386 unsafe {
387387 with_env_lock ( || {
388- use os:: windows :: { fill_utf16_buf_and_decode} ;
388+ use os:: windoze :: { fill_utf16_buf_and_decode} ;
389389 let mut n: Vec < u16 > = n. utf16_units ( ) . collect ( ) ;
390390 n. push ( 0 ) ;
391391 fill_utf16_buf_and_decode ( |buf, sz| {
@@ -712,7 +712,7 @@ pub fn self_exe_name() -> Option<Path> {
712712 #[ cfg( windows) ]
713713 fn load_self ( ) -> Option < Vec < u8 > > {
714714 unsafe {
715- use os:: windows :: fill_utf16_buf_and_decode;
715+ use os:: windoze :: fill_utf16_buf_and_decode;
716716 fill_utf16_buf_and_decode ( |buf, sz| {
717717 libc:: GetModuleFileNameW ( 0 u as libc:: DWORD , buf, sz)
718718 } ) . map ( |s| s. into_string ( ) . into_bytes ( ) )
@@ -1207,7 +1207,11 @@ pub enum MapOption {
12071207 /// Create a map for a specific address range. Corresponds to `MAP_FIXED` on
12081208 /// POSIX.
12091209 MapAddr ( * const u8 ) ,
1210+ /// Create a memory mapping for a file with a given HANDLE.
1211+ #[ cfg( windows) ]
1212+ MapFd ( libc:: HANDLE ) ,
12101213 /// Create a memory mapping for a file with a given fd.
1214+ #[ cfg( not( windows) ) ]
12111215 MapFd ( c_int ) ,
12121216 /// When using `MapFd`, the start of the map is `uint` bytes from the start
12131217 /// of the file.
@@ -1401,7 +1405,7 @@ impl MemoryMap {
14011405 let mut readable = false ;
14021406 let mut writable = false ;
14031407 let mut executable = false ;
1404- let mut fd : c_int = - 1 ;
1408+ let mut handle : HANDLE = libc :: INVALID_HANDLE_VALUE ;
14051409 let mut offset: uint = 0 ;
14061410 let len = round_up ( min_len, page_size ( ) ) ;
14071411
@@ -1411,23 +1415,23 @@ impl MemoryMap {
14111415 MapWritable => { writable = true ; } ,
14121416 MapExecutable => { executable = true ; }
14131417 MapAddr ( addr_) => { lpAddress = addr_ as LPVOID ; } ,
1414- MapFd ( fd_ ) => { fd = fd_ ; } ,
1418+ MapFd ( handle_ ) => { handle = handle_ ; } ,
14151419 MapOffset ( offset_) => { offset = offset_; } ,
14161420 MapNonStandardFlags ( ..) => { }
14171421 }
14181422 }
14191423
14201424 let flProtect = match ( executable, readable, writable) {
1421- ( false , false , false ) if fd == - 1 => libc:: PAGE_NOACCESS ,
1425+ ( false , false , false ) if handle == libc :: INVALID_HANDLE_VALUE => libc:: PAGE_NOACCESS ,
14221426 ( false , true , false ) => libc:: PAGE_READONLY ,
14231427 ( false , true , true ) => libc:: PAGE_READWRITE ,
1424- ( true , false , false ) if fd == - 1 => libc:: PAGE_EXECUTE ,
1428+ ( true , false , false ) if handle == libc :: INVALID_HANDLE_VALUE => libc:: PAGE_EXECUTE ,
14251429 ( true , true , false ) => libc:: PAGE_EXECUTE_READ ,
14261430 ( true , true , true ) => libc:: PAGE_EXECUTE_READWRITE ,
14271431 _ => return Err ( ErrUnsupProt )
14281432 } ;
14291433
1430- if fd == - 1 {
1434+ if handle == libc :: INVALID_HANDLE_VALUE {
14311435 if offset != 0 {
14321436 return Err ( ErrUnsupOffset ) ;
14331437 }
@@ -1455,7 +1459,7 @@ impl MemoryMap {
14551459 // we should never get here.
14561460 } ;
14571461 unsafe {
1458- let hFile = libc :: get_osfhandle ( fd ) as HANDLE ;
1462+ let hFile = handle ;
14591463 let mapping = libc:: CreateFileMappingW ( hFile,
14601464 ptr:: null_mut ( ) ,
14611465 flProtect,
@@ -1979,55 +1983,47 @@ mod tests {
19791983
19801984 #[ test]
19811985 fn memory_map_file ( ) {
1982- use result:: Result :: { Ok , Err } ;
19831986 use os:: * ;
1984- use libc:: * ;
1985- use io:: fs;
1986-
1987- #[ cfg( unix) ]
1988- fn lseek_ ( fd : c_int , size : uint ) {
1989- unsafe {
1990- assert ! ( lseek( fd, size as off_t, SEEK_SET ) == size as off_t) ;
1991- }
1987+ use io:: fs:: { File , unlink} ;
1988+ use io:: SeekStyle :: SeekSet ;
1989+ use io:: FileMode :: Open ;
1990+ use io:: FileAccess :: ReadWrite ;
1991+ use libc:: HANDLE ;
1992+
1993+ #[ cfg( not( windows) ) ]
1994+ fn get_fd ( file : & File ) -> c_int {
1995+ use os:: unix:: AsRawFd ;
1996+ file. as_raw_fd ( )
19921997 }
1998+
19931999 #[ cfg( windows) ]
1994- fn lseek_ ( fd : c_int , size : uint ) {
1995- unsafe {
1996- assert ! ( lseek( fd, size as c_long, SEEK_SET ) == size as c_long) ;
1997- }
2000+ fn get_fd ( file : & File ) -> HANDLE {
2001+ use os:: windows:: AsRawHandle ;
2002+ file. as_raw_handle ( )
19982003 }
19992004
20002005 let mut path = tmpdir ( ) ;
20012006 path. push ( "mmap_file.tmp" ) ;
20022007 let size = MemoryMap :: granularity ( ) * 2 ;
2008+ let mut file = File :: open_mode ( & path, Open , ReadWrite ) . unwrap ( ) ;
2009+ file. seek ( size as i64 , SeekSet ) ;
2010+ file. write_u8 ( 0 ) ;
20032011
2004- let fd = unsafe {
2005- let fd = path. with_c_str ( |path| {
2006- open ( path, O_CREAT | O_RDWR | O_TRUNC , S_IRUSR | S_IWUSR )
2007- } ) ;
2008- lseek_ ( fd, size) ;
2009- "x" . with_c_str ( |x| assert ! ( write( fd, x as * const c_void, 1 ) == 1 ) ) ;
2010- fd
2011- } ;
2012- let chunk = match MemoryMap :: new ( size / 2 , & [
2012+ let chunk = MemoryMap :: new ( size / 2 , & [
20132013 MapReadable ,
20142014 MapWritable ,
2015- MapFd ( fd ) ,
2015+ MapFd ( get_fd ( & file ) ) ,
20162016 MapOffset ( size / 2 )
2017- ] ) {
2018- Ok ( chunk) => chunk,
2019- Err ( msg) => panic ! ( "{}" , msg)
2020- } ;
2017+ ] ) . unwrap ( ) ;
20212018 assert ! ( chunk. len > 0 ) ;
20222019
20232020 unsafe {
20242021 * chunk. data = 0xbe ;
20252022 assert ! ( * chunk. data == 0xbe ) ;
2026- close ( fd) ;
20272023 }
20282024 drop ( chunk) ;
20292025
2030- fs :: unlink ( & path) . unwrap ( ) ;
2026+ unlink ( & path) . unwrap ( ) ;
20312027 }
20322028
20332029 #[ test]
0 commit comments