@@ -6,11 +6,10 @@ pub use libc::stat as FileStat;
6
6
pub use libc:: { dev_t, mode_t} ;
7
7
8
8
#[ cfg( not( target_os = "redox" ) ) ]
9
- use crate :: fcntl:: { at_rawfd , AtFlags } ;
9
+ use crate :: fcntl:: AtFlags ;
10
10
use crate :: sys:: time:: { TimeSpec , TimeVal } ;
11
11
use crate :: { errno:: Errno , NixPath , Result } ;
12
12
use std:: mem;
13
- use std:: os:: unix:: io:: RawFd ;
14
13
15
14
libc_bitflags ! (
16
15
/// "File type" flags for `mknod` and related functions.
@@ -168,17 +167,18 @@ pub fn mknod<P: ?Sized + NixPath>(
168
167
169
168
/// Create a special or ordinary file, relative to a given directory.
170
169
#[ cfg( not( any( apple_targets, target_os = "redox" , target_os = "haiku" ) ) ) ]
171
- pub fn mknodat < P : ?Sized + NixPath > (
172
- dirfd : Option < RawFd > ,
170
+ pub fn mknodat < Fd : std :: os :: fd :: AsFd , P : ?Sized + NixPath > (
171
+ dirfd : Fd ,
173
172
path : & P ,
174
173
kind : SFlag ,
175
174
perm : Mode ,
176
175
dev : dev_t ,
177
176
) -> Result < ( ) > {
178
- let dirfd = at_rawfd ( dirfd) ;
177
+ use std:: os:: fd:: AsRawFd ;
178
+
179
179
let res = path. with_nix_path ( |cstr| unsafe {
180
180
libc:: mknodat (
181
- dirfd,
181
+ dirfd. as_fd ( ) . as_raw_fd ( ) ,
182
182
cstr. as_ptr ( ) ,
183
183
kind. bits ( ) | perm. bits ( ) as mode_t ,
184
184
dev,
@@ -233,26 +233,29 @@ pub fn lstat<P: ?Sized + NixPath>(path: &P) -> Result<FileStat> {
233
233
Ok ( unsafe { dst. assume_init ( ) } )
234
234
}
235
235
236
- pub fn fstat ( fd : RawFd ) -> Result < FileStat > {
236
+ pub fn fstat < Fd : std:: os:: fd:: AsFd > ( fd : Fd ) -> Result < FileStat > {
237
+ use std:: os:: fd:: AsRawFd ;
238
+
237
239
let mut dst = mem:: MaybeUninit :: uninit ( ) ;
238
- let res = unsafe { libc:: fstat ( fd, dst. as_mut_ptr ( ) ) } ;
240
+ let res = unsafe { libc:: fstat ( fd. as_fd ( ) . as_raw_fd ( ) , dst. as_mut_ptr ( ) ) } ;
239
241
240
242
Errno :: result ( res) ?;
241
243
242
244
Ok ( unsafe { dst. assume_init ( ) } )
243
245
}
244
246
245
247
#[ cfg( not( target_os = "redox" ) ) ]
246
- pub fn fstatat < P : ?Sized + NixPath > (
247
- dirfd : Option < RawFd > ,
248
+ pub fn fstatat < Fd : std :: os :: fd :: AsFd , P : ?Sized + NixPath > (
249
+ dirfd : Fd ,
248
250
pathname : & P ,
249
251
f : AtFlags ,
250
252
) -> Result < FileStat > {
251
- let dirfd = at_rawfd ( dirfd) ;
253
+ use std:: os:: fd:: AsRawFd ;
254
+
252
255
let mut dst = mem:: MaybeUninit :: uninit ( ) ;
253
256
let res = pathname. with_nix_path ( |cstr| unsafe {
254
257
libc:: fstatat (
255
- dirfd,
258
+ dirfd. as_fd ( ) . as_raw_fd ( ) ,
256
259
cstr. as_ptr ( ) ,
257
260
dst. as_mut_ptr ( ) ,
258
261
f. bits ( ) as libc:: c_int ,
@@ -269,8 +272,11 @@ pub fn fstatat<P: ?Sized + NixPath>(
269
272
/// # References
270
273
///
271
274
/// [fchmod(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/fchmod.html).
272
- pub fn fchmod ( fd : RawFd , mode : Mode ) -> Result < ( ) > {
273
- let res = unsafe { libc:: fchmod ( fd, mode. bits ( ) as mode_t ) } ;
275
+ pub fn fchmod < Fd : std:: os:: fd:: AsFd > ( fd : Fd , mode : Mode ) -> Result < ( ) > {
276
+ use std:: os:: fd:: AsRawFd ;
277
+
278
+ let res =
279
+ unsafe { libc:: fchmod ( fd. as_fd ( ) . as_raw_fd ( ) , mode. bits ( ) as mode_t ) } ;
274
280
275
281
Errno :: result ( res) . map ( drop)
276
282
}
@@ -299,19 +305,21 @@ pub enum FchmodatFlags {
299
305
///
300
306
/// [fchmodat(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/fchmodat.html).
301
307
#[ cfg( not( target_os = "redox" ) ) ]
302
- pub fn fchmodat < P : ?Sized + NixPath > (
303
- dirfd : Option < RawFd > ,
308
+ pub fn fchmodat < Fd : std :: os :: fd :: AsFd , P : ?Sized + NixPath > (
309
+ dirfd : Fd ,
304
310
path : & P ,
305
311
mode : Mode ,
306
312
flag : FchmodatFlags ,
307
313
) -> Result < ( ) > {
314
+ use std:: os:: fd:: AsRawFd ;
315
+
308
316
let atflag = match flag {
309
317
FchmodatFlags :: FollowSymlink => AtFlags :: empty ( ) ,
310
318
FchmodatFlags :: NoFollowSymlink => AtFlags :: AT_SYMLINK_NOFOLLOW ,
311
319
} ;
312
320
let res = path. with_nix_path ( |cstr| unsafe {
313
321
libc:: fchmodat (
314
- at_rawfd ( dirfd) ,
322
+ dirfd. as_fd ( ) . as_raw_fd ( ) ,
315
323
cstr. as_ptr ( ) ,
316
324
mode. bits ( ) as mode_t ,
317
325
atflag. bits ( ) as libc:: c_int ,
@@ -383,9 +391,15 @@ pub fn lutimes<P: ?Sized + NixPath>(
383
391
///
384
392
/// [futimens(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/futimens.html).
385
393
#[ inline]
386
- pub fn futimens ( fd : RawFd , atime : & TimeSpec , mtime : & TimeSpec ) -> Result < ( ) > {
394
+ pub fn futimens < Fd : std:: os:: fd:: AsFd > (
395
+ fd : Fd ,
396
+ atime : & TimeSpec ,
397
+ mtime : & TimeSpec ,
398
+ ) -> Result < ( ) > {
399
+ use std:: os:: fd:: AsRawFd ;
400
+
387
401
let times: [ libc:: timespec ; 2 ] = [ * atime. as_ref ( ) , * mtime. as_ref ( ) ] ;
388
- let res = unsafe { libc:: futimens ( fd, & times[ 0 ] ) } ;
402
+ let res = unsafe { libc:: futimens ( fd. as_fd ( ) . as_raw_fd ( ) , & times[ 0 ] ) } ;
389
403
390
404
Errno :: result ( res) . map ( drop)
391
405
}
@@ -418,21 +432,23 @@ pub enum UtimensatFlags {
418
432
///
419
433
/// [utimensat(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/utimens.html).
420
434
#[ cfg( not( target_os = "redox" ) ) ]
421
- pub fn utimensat < P : ?Sized + NixPath > (
422
- dirfd : Option < RawFd > ,
435
+ pub fn utimensat < Fd : std :: os :: fd :: AsFd , P : ?Sized + NixPath > (
436
+ dirfd : Fd ,
423
437
path : & P ,
424
438
atime : & TimeSpec ,
425
439
mtime : & TimeSpec ,
426
440
flag : UtimensatFlags ,
427
441
) -> Result < ( ) > {
442
+ use std:: os:: fd:: AsRawFd ;
443
+
428
444
let atflag = match flag {
429
445
UtimensatFlags :: FollowSymlink => AtFlags :: empty ( ) ,
430
446
UtimensatFlags :: NoFollowSymlink => AtFlags :: AT_SYMLINK_NOFOLLOW ,
431
447
} ;
432
448
let times: [ libc:: timespec ; 2 ] = [ * atime. as_ref ( ) , * mtime. as_ref ( ) ] ;
433
449
let res = path. with_nix_path ( |cstr| unsafe {
434
450
libc:: utimensat (
435
- at_rawfd ( dirfd) ,
451
+ dirfd. as_fd ( ) . as_raw_fd ( ) ,
436
452
cstr. as_ptr ( ) ,
437
453
& times[ 0 ] ,
438
454
atflag. bits ( ) as libc:: c_int ,
@@ -443,14 +459,19 @@ pub fn utimensat<P: ?Sized + NixPath>(
443
459
}
444
460
445
461
#[ cfg( not( target_os = "redox" ) ) ]
446
- pub fn mkdirat < P : ?Sized + NixPath > (
447
- fd : Option < RawFd > ,
462
+ pub fn mkdirat < Fd : std :: os :: fd :: AsFd , P : ?Sized + NixPath > (
463
+ fd : Fd ,
448
464
path : & P ,
449
465
mode : Mode ,
450
466
) -> Result < ( ) > {
451
- let fd = at_rawfd ( fd) ;
467
+ use std:: os:: fd:: AsRawFd ;
468
+
452
469
let res = path. with_nix_path ( |cstr| unsafe {
453
- libc:: mkdirat ( fd, cstr. as_ptr ( ) , mode. bits ( ) as mode_t )
470
+ libc:: mkdirat (
471
+ fd. as_fd ( ) . as_raw_fd ( ) ,
472
+ cstr. as_ptr ( ) ,
473
+ mode. bits ( ) as mode_t ,
474
+ )
454
475
} ) ?;
455
476
456
477
Errno :: result ( res) . map ( drop)
0 commit comments