@@ -375,6 +375,13 @@ pub fn getcwd() -> Result<PathBuf> {
375
375
}
376
376
}
377
377
378
+ // According to the POSIX specification, -1 is used to indicate that
379
+ // owner and group, respectively, are not to be changed. Since uid_t and
380
+ // gid_t are unsigned types, we use wrapping_sub to get '-1'.
381
+ fn optional_arg ( val : Option < u32 > ) -> u32 {
382
+ val. unwrap_or ( 0 ) . wrapping_sub ( 1 )
383
+ }
384
+
378
385
/// Change the ownership of the file at `path` to be owned by the specified
379
386
/// `owner` (user) and `group` (see
380
387
/// [chown(2)](http://man7.org/linux/man-pages/man2/lchown.2.html)).
@@ -389,12 +396,57 @@ pub fn getcwd() -> Result<PathBuf> {
389
396
#[ inline]
390
397
pub fn chown < P : ?Sized + NixPath > ( path : & P , owner : Option < uid_t > , group : Option < gid_t > ) -> Result < ( ) > {
391
398
let res = try!( path. with_nix_path ( |cstr| {
392
- // According to the POSIX specification, -1 is used to indicate that
393
- // owner and group, respectively, are not to be changed. Since uid_t and
394
- // gid_t are unsigned types, we use wrapping_sub to get '-1'.
395
- unsafe { libc:: chown ( cstr. as_ptr ( ) ,
396
- owner. unwrap_or ( ( 0 as uid_t ) . wrapping_sub ( 1 ) ) ,
397
- group. unwrap_or ( ( 0 as gid_t ) . wrapping_sub ( 1 ) ) ) }
399
+ unsafe {
400
+ libc:: chown ( cstr. as_ptr ( ) ,
401
+ optional_arg ( owner) ,
402
+ optional_arg ( group) )
403
+ }
404
+ } ) ) ;
405
+
406
+ Errno :: result ( res) . map ( drop)
407
+ }
408
+
409
+ /// Change ownership of a file
410
+ /// (see [lchown(2)](http://man7.org/linux/man-pages/man2/lchown.2.html))
411
+ pub fn lchown < P : ?Sized + NixPath > ( path : & P , owner : Option < uid_t > , group : Option < gid_t > ) -> Result < ( ) > {
412
+ let res = try!( path. with_nix_path ( |cstr| {
413
+ unsafe {
414
+ libc:: lchown ( cstr. as_ptr ( ) ,
415
+ optional_arg ( owner) ,
416
+ optional_arg ( group) )
417
+ }
418
+ } ) ) ;
419
+
420
+ Errno :: result ( res) . map ( drop)
421
+ }
422
+
423
+ /// Change ownership of a file
424
+ /// (see [fchown(2)](http://man7.org/linux/man-pages/man2/fchown.2.html))
425
+ pub fn fchown ( fd : RawFd , owner : Option < uid_t > , group : Option < gid_t > ) -> Result < ( ) > {
426
+ let res = unsafe {
427
+ libc:: fchown ( fd,
428
+ optional_arg ( owner) ,
429
+ optional_arg ( group) )
430
+ } ;
431
+
432
+ Errno :: result ( res) . map ( drop)
433
+ }
434
+
435
+ /// Change ownership of a file
436
+ /// (see [fchownat(2)](http://man7.org/linux/man-pages/man2/fchownat.2.html))
437
+ pub fn fchownat < P : ?Sized + NixPath > ( dirfd : RawFd ,
438
+ pathname : & P ,
439
+ owner : Option < uid_t > ,
440
+ group : Option < gid_t > ,
441
+ flags : AtFlags ) -> Result < ( ) > {
442
+ let res = try!( pathname. with_nix_path ( |cstr| {
443
+ unsafe {
444
+ libc:: fchownat ( dirfd,
445
+ cstr. as_ptr ( ) ,
446
+ optional_arg ( owner) ,
447
+ optional_arg ( group) ,
448
+ flags. bits ( ) )
449
+ }
398
450
} ) ) ;
399
451
400
452
Errno :: result ( res) . map ( drop)
0 commit comments