12
12
13
13
use io:: { fs, IoResult } ;
14
14
use io;
15
- use iter:: range;
16
15
use libc;
17
16
use ops:: Drop ;
18
17
use option:: { Option , None , Some } ;
@@ -33,35 +32,40 @@ impl TempDir {
33
32
/// will have the suffix `suffix`. The directory will be automatically
34
33
/// deleted once the returned wrapper is destroyed.
35
34
///
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 > {
38
37
if !tmpdir. is_absolute ( ) {
39
38
return TempDir :: new_in ( & os:: make_absolute ( tmpdir) , suffix) ;
40
39
}
41
40
42
41
static mut CNT : atomic:: AtomicUint = atomic:: INIT_ATOMIC_UINT ;
43
42
44
- for _ in range ( 0 u, 1000 ) {
43
+ let mut attempts = 0 u;
44
+ loop {
45
45
let filename =
46
46
format ! ( "rs-{}-{}-{}" ,
47
47
unsafe { libc:: getpid( ) } ,
48
48
unsafe { CNT . fetch_add( 1 , atomic:: SeqCst ) } ,
49
49
suffix) ;
50
50
let p = tmpdir. join ( filename) ;
51
51
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 } )
54
59
}
55
60
}
56
- None
57
61
}
58
62
59
63
/// Attempts to make a temporary directory inside of `os::tmpdir()` whose
60
64
/// name will have the suffix `suffix`. The directory will be automatically
61
65
/// deleted once the returned wrapper is destroyed.
62
66
///
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 > {
65
69
TempDir :: new_in ( & os:: tmpdir ( ) , suffix)
66
70
}
67
71
0 commit comments