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/).
2828 ([ #457 ] ( https://github.com/nix-rust/nix/pull/457 ) )
2929- Added ` getpgrp ` in ` ::nix::unistd `
3030 ([ #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 ) )
3133
3234### Changed
3335- ` 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<()> {
249249 Errno :: result ( res) . map ( drop)
250250}
251251
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+
252265/// Creates new directory `path` with access rights `mode`.
253266///
254267/// # Errors
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ use nix::sys::wait::*;
66use nix:: sys:: stat;
77use std:: iter;
88use std:: ffi:: CString ;
9+ use std:: fs:: File ;
910use std:: io:: { Write , Read } ;
1011use std:: os:: unix:: prelude:: * ;
1112use std:: env:: current_dir;
@@ -141,6 +142,24 @@ macro_rules! execve_test_factory(
141142 )
142143) ;
143144
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+
144163#[ test]
145164fn test_getcwd ( ) {
146165 let tmp_dir = TempDir :: new ( "test_getcwd" ) . unwrap ( ) ;
You can’t perform that action at this time.
0 commit comments