File tree Expand file tree Collapse file tree 3 files changed +34
-0
lines changed Expand file tree Collapse file tree 3 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -28,6 +28,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
28
28
([ #457 ] ( https://github.com/nix-rust/nix/pull/457 ) )
29
29
- Added ` getpgrp ` in ` ::nix::unistd `
30
30
([ #491 ] ( https://github.com/nix-rust/nix/pull/491 ) )
31
+ - Added ` fchdir ` in ` ::nix::unistd `
32
+ ([ #497 ] ( https://github.com/nix-rust/nix/pull/497 ) )
31
33
32
34
### Changed
33
35
- ` epoll_ctl ` now could accept None as argument ` event `
Original file line number Diff line number Diff line change @@ -249,6 +249,19 @@ pub fn chdir<P: ?Sized + NixPath>(path: &P) -> Result<()> {
249
249
Errno :: result ( res) . map ( drop)
250
250
}
251
251
252
+ /// Change the current working directory of the process to the one
253
+ /// given as an open file descriptor (see
254
+ /// [fchdir(2)](http://man7.org/linux/man-pages/man2/fchdir.2.html)).
255
+ ///
256
+ /// This function may fail in a number of different scenarios. See the man
257
+ /// pages for additional details on possible failure cases.
258
+ #[ inline]
259
+ pub fn fchdir ( dirfd : RawFd ) -> Result < ( ) > {
260
+ let res = unsafe { libc:: fchdir ( dirfd) } ;
261
+
262
+ Errno :: result ( res) . map ( drop)
263
+ }
264
+
252
265
/// Creates new directory `path` with access rights `mode`.
253
266
///
254
267
/// # Errors
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ use nix::sys::wait::*;
6
6
use nix:: sys:: stat;
7
7
use std:: iter;
8
8
use std:: ffi:: CString ;
9
+ use std:: fs:: File ;
9
10
use std:: io:: { Write , Read } ;
10
11
use std:: os:: unix:: prelude:: * ;
11
12
use std:: env:: current_dir;
@@ -141,6 +142,24 @@ macro_rules! execve_test_factory(
141
142
)
142
143
) ;
143
144
145
+ #[ test]
146
+ fn test_fchdir ( ) {
147
+ let tmpdir = TempDir :: new ( "test_fchdir" ) . unwrap ( ) ;
148
+ let tmpdir_path = tmpdir. path ( ) . canonicalize ( ) . unwrap ( ) ;
149
+ let tmpdir_fd = File :: open ( & tmpdir_path) . unwrap ( ) . into_raw_fd ( ) ;
150
+ let olddir_path = getcwd ( ) . unwrap ( ) ;
151
+ let olddir_fd = File :: open ( & olddir_path) . unwrap ( ) . into_raw_fd ( ) ;
152
+
153
+ assert ! ( fchdir( tmpdir_fd) . is_ok( ) ) ;
154
+ assert_eq ! ( getcwd( ) . unwrap( ) , tmpdir_path) ;
155
+
156
+ assert ! ( fchdir( olddir_fd) . is_ok( ) ) ;
157
+ assert_eq ! ( getcwd( ) . unwrap( ) , olddir_path) ;
158
+
159
+ assert ! ( close( olddir_fd) . is_ok( ) ) ;
160
+ assert ! ( close( tmpdir_fd) . is_ok( ) ) ;
161
+ }
162
+
144
163
#[ test]
145
164
fn test_getcwd ( ) {
146
165
let tmp_dir = TempDir :: new ( "test_getcwd" ) . unwrap ( ) ;
You can’t perform that action at this time.
0 commit comments