@@ -38,7 +38,7 @@ pub use self::MapError::*;
38
38
use clone:: Clone ;
39
39
use error:: { FromError , Error } ;
40
40
use fmt;
41
- use io:: IoResult ;
41
+ use io:: { IoResult , IoError } ;
42
42
use iter:: Iterator ;
43
43
use libc:: { c_void, c_int} ;
44
44
use libc;
@@ -76,72 +76,83 @@ pub fn num_cpus() -> uint {
76
76
pub const TMPBUF_SZ : uint = 1000 u;
77
77
const BUF_BYTES : uint = 2048 u;
78
78
79
- /// Returns the current working directory as a Path.
79
+ /// Returns the current working directory as a ` Path` .
80
80
///
81
- /// # Failure
81
+ /// # Errors
82
82
///
83
- /// Fails if the current working directory value is invalid:
83
+ /// Returns an `Err` if the current working directory value is invalid.
84
84
/// Possible cases:
85
85
///
86
86
/// * Current directory does not exist.
87
87
/// * There are insufficient permissions to access the current directory.
88
+ /// * The internal buffer is not large enough to hold the path.
88
89
///
89
90
/// # Example
90
91
///
91
92
/// ```rust
92
93
/// use std::os;
93
94
///
94
95
/// // We assume that we are in a valid directory like "/home".
95
- /// let current_working_directory = os::getcwd();
96
+ /// let current_working_directory = os::getcwd().unwrap() ;
96
97
/// println!("The current directory is {}", current_working_directory.display());
97
98
/// // /home
98
99
/// ```
99
100
#[ cfg( unix) ]
100
- pub fn getcwd ( ) -> Path {
101
+ pub fn getcwd ( ) -> IoResult < Path > {
101
102
use c_str:: CString ;
102
103
103
104
let mut buf = [ 0 as c_char , ..BUF_BYTES ] ;
104
105
unsafe {
105
106
if libc:: getcwd ( buf. as_mut_ptr ( ) , buf. len ( ) as libc:: size_t ) . is_null ( ) {
106
- panic ! ( )
107
+ Err ( IoError :: last_error ( ) )
108
+ } else {
109
+ Ok ( Path :: new ( CString :: new ( buf. as_ptr ( ) , false ) ) )
107
110
}
108
- Path :: new ( CString :: new ( buf. as_ptr ( ) , false ) )
109
111
}
110
112
}
111
113
112
- /// Returns the current working directory as a Path.
114
+ /// Returns the current working directory as a ` Path` .
113
115
///
114
- /// # Failure
116
+ /// # Errors
115
117
///
116
- /// Fails if the current working directory value is invalid.
117
- /// Possibles cases:
118
+ /// Returns an `Err` if the current working directory value is invalid.
119
+ /// Possible cases:
118
120
///
119
121
/// * Current directory does not exist.
120
122
/// * There are insufficient permissions to access the current directory.
123
+ /// * The internal buffer is not large enough to hold the path.
121
124
///
122
125
/// # Example
123
126
///
124
127
/// ```rust
125
128
/// use std::os;
126
129
///
127
130
/// // We assume that we are in a valid directory like "C:\\Windows".
128
- /// let current_working_directory = os::getcwd();
131
+ /// let current_working_directory = os::getcwd().unwrap() ;
129
132
/// println!("The current directory is {}", current_working_directory.display());
130
133
/// // C:\\Windows
131
134
/// ```
132
135
#[ cfg( windows) ]
133
- pub fn getcwd ( ) -> Path {
136
+ pub fn getcwd ( ) -> IoResult < Path > {
134
137
use libc:: DWORD ;
135
138
use libc:: GetCurrentDirectoryW ;
139
+ use io:: OtherIoError ;
136
140
137
141
let mut buf = [ 0 as u16 , ..BUF_BYTES ] ;
138
142
unsafe {
139
143
if libc:: GetCurrentDirectoryW ( buf. len ( ) as DWORD , buf. as_mut_ptr ( ) ) == 0 as DWORD {
140
- panic ! ( ) ;
144
+ return Err ( IoError :: last_error ( ) ) ;
141
145
}
142
146
}
143
- Path :: new ( String :: from_utf16 ( :: str:: truncate_utf16_at_nul ( & buf) )
144
- . expect ( "GetCurrentDirectoryW returned invalid UTF-16" ) )
147
+
148
+ match String :: from_utf16 ( :: str:: truncate_utf16_at_nul ( & buf) ) {
149
+ Some ( ref cwd) => Ok ( Path :: new ( cwd) ) ,
150
+ None => Err ( IoError {
151
+ kind : OtherIoError ,
152
+ desc : "GetCurrentDirectoryW returned invalid UTF-16" ,
153
+ detail : None ,
154
+ } ) ,
155
+ }
145
156
}
146
157
147
158
#[ cfg( windows) ]
@@ -829,20 +840,21 @@ pub fn tmpdir() -> Path {
829
840
///
830
841
/// // Assume we're in a path like /home/someuser
831
842
/// let rel_path = Path::new("..");
832
- /// let abs_path = os::make_absolute(&rel_path);
843
+ /// let abs_path = os::make_absolute(&rel_path).unwrap() ;
833
844
/// println!("The absolute path is {}", abs_path.display());
834
845
/// // Prints "The absolute path is /home"
835
846
/// ```
836
847
// NB: this is here rather than in path because it is a form of environment
837
848
// querying; what it does depends on the process working directory, not just
838
849
// the input paths.
839
- pub fn make_absolute ( p : & Path ) -> Path {
850
+ pub fn make_absolute ( p : & Path ) -> IoResult < Path > {
840
851
if p. is_absolute ( ) {
841
- p. clone ( )
852
+ Ok ( p. clone ( ) )
842
853
} else {
843
- let mut ret = getcwd ( ) ;
844
- ret. push ( p) ;
845
- ret
854
+ getcwd ( ) . map ( |mut cwd| {
855
+ cwd. push ( p) ;
856
+ cwd
857
+ } )
846
858
}
847
859
}
848
860
@@ -1881,11 +1893,11 @@ mod tests {
1881
1893
fn test ( ) {
1882
1894
assert ! ( ( !Path :: new( "test-path" ) . is_absolute( ) ) ) ;
1883
1895
1884
- let cwd = getcwd ( ) ;
1896
+ let cwd = getcwd ( ) . unwrap ( ) ;
1885
1897
debug ! ( "Current working directory: {}" , cwd. display( ) ) ;
1886
1898
1887
- debug ! ( "{}" , make_absolute( & Path :: new( "test-path" ) ) . display( ) ) ;
1888
- debug ! ( "{}" , make_absolute( & Path :: new( "/usr/bin" ) ) . display( ) ) ;
1899
+ debug ! ( "{}" , make_absolute( & Path :: new( "test-path" ) ) . unwrap ( ) . display( ) ) ;
1900
+ debug ! ( "{}" , make_absolute( & Path :: new( "/usr/bin" ) ) . unwrap ( ) . display( ) ) ;
1889
1901
}
1890
1902
1891
1903
#[ test]
0 commit comments