1212
1313use io:: { fs, IoResult } ;
1414use io;
15- use iter:: range;
1615use libc;
1716use ops:: Drop ;
1817use option:: { Option , None , Some } ;
@@ -33,35 +32,40 @@ impl TempDir {
3332 /// will have the suffix `suffix`. The directory will be automatically
3433 /// deleted once the returned wrapper is destroyed.
3534 ///
36- /// If no directory can be created, None is returned.
37- pub fn new_in ( tmpdir : & Path , suffix : & str ) -> Option < TempDir > {
35+ /// If no directory can be created, `Err` is returned.
36+ pub fn new_in ( tmpdir : & Path , suffix : & str ) -> IoResult < TempDir > {
3837 if !tmpdir. is_absolute ( ) {
3938 return TempDir :: new_in ( & os:: make_absolute ( tmpdir) , suffix) ;
4039 }
4140
4241 static mut CNT : atomic:: AtomicUint = atomic:: INIT_ATOMIC_UINT ;
4342
44- for _ in range ( 0 u, 1000 ) {
43+ let mut attempts = 0 u;
44+ loop {
4545 let filename =
4646 format ! ( "rs-{}-{}-{}" ,
4747 unsafe { libc:: getpid( ) } ,
4848 unsafe { CNT . fetch_add( 1 , atomic:: SeqCst ) } ,
4949 suffix) ;
5050 let p = tmpdir. join ( filename) ;
5151 match fs:: mkdir ( & p, io:: UserRWX ) {
52- Err ( ..) => { }
53- Ok ( ( ) ) => return Some ( TempDir { path : Some ( p) , disarmed : false } )
52+ Err ( error) => {
53+ if attempts >= 1000 {
54+ return Err ( error)
55+ }
56+ attempts += 1 ;
57+ }
58+ Ok ( ( ) ) => return Ok ( TempDir { path : Some ( p) , disarmed : false } )
5459 }
5560 }
56- None
5761 }
5862
5963 /// Attempts to make a temporary directory inside of `os::tmpdir()` whose
6064 /// name will have the suffix `suffix`. The directory will be automatically
6165 /// deleted once the returned wrapper is destroyed.
6266 ///
63- /// If no directory can be created, None is returned.
64- pub fn new ( suffix : & str ) -> Option < TempDir > {
67+ /// If no directory can be created, `Err` is returned.
68+ pub fn new ( suffix : & str ) -> IoResult < TempDir > {
6569 TempDir :: new_in ( & os:: tmpdir ( ) , suffix)
6670 }
6771
0 commit comments