openat_ct 0.1.21

A wrapper around openat, symlinkat, and similar system calls forked and improved from https://crates.io/crates/openat
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
use std::io;
use std::mem;
use std::ffi::{OsString, CStr};
use std::fs::{File, read_link};
use std::os::unix::io::{AsRawFd, RawFd, FromRawFd, IntoRawFd};
use std::os::unix::ffi::{OsStringExt};
use std::path::{PathBuf};

use libc;
use crate::list::{open_dirfd, DirIter};
use crate::metadata::{self, Metadata};

use crate::{Dir, AsPath, DirFlags, DirMethodFlags};


/// Value if the libc::O_DIRECTORY flag when supported by the system, otherwise 0
#[cfg(feature = "o_directory")]
pub const O_DIRECTORY: libc::c_int = libc::O_DIRECTORY;
/// Value if the libc::O_DIRECTORY flag when supported by the system, otherwise 0
#[cfg(not(feature = "o_directory"))]
pub const O_DIRECTORY: libc::c_int = 0;

/// Value if the libc::O_PATH flag when supported by the system, otherwise 0
#[cfg(feature = "o_path")]
pub const O_PATH: libc::c_int = libc::O_PATH;
/// Value if the libc::O_PATH flag when supported by the system, otherwise 0
#[cfg(not(feature = "o_path"))]
pub const O_PATH: libc::c_int = 0;

/// Value if the libc::O_SEARCH flag when supported by the system, otherwise 0
#[cfg(feature = "o_search")]
pub const O_SEARCH: libc::c_int = libc::O_SEARCH;
/// Value if the libc::O_SEARCH flag when supported by the system, otherwise 0
#[cfg(not(feature = "o_search"))]
pub const O_SEARCH: libc::c_int = 0;

impl Dir {
    /// Creates a directory descriptor that resolves paths relative to current
    /// working directory (AT_FDCWD)
    #[deprecated(since="0.1.15", note="\
        Use `Dir::open(\".\")` instead. \
        Dir::cwd() doesn't open actual file descriptor and uses magic value \
        instead which resolves to current dir on any syscall invocation. \
        This is usually counter-intuitive and yields a broken \
        file descriptor when using `Dir::as_raw_fd`. \
        Will be removed in version v0.2 of the library.")]
    pub fn cwd() -> Dir {
        Dir(libc::AT_FDCWD)
    }

    /// Create a flags builder for Dir objects.  Initial flags default to `O_CLOEXEC'. More
    /// flags can be set added by 'with()' and existing/default flags can be removed by
    /// 'without()'. The flags builder can the be used to 'open()' to create
    /// a Dir handle.
    #[inline]
    pub fn flags() -> DirFlags {
        DirFlags::new(libc::O_CLOEXEC)
    }

    /// Open a directory descriptor at specified path with O_PATH when available.
    /// A descriptor obtained with this flag is restricted to do only certain operations:
    /// - It may be used as anchor for opening sub-objects
    /// - One can query metadata of this directory
    ///
    /// This handle is not suitable for a 'Dir::list()' call and may yield a runtime error.
    /// Use 'Dir::flags().open()' to get a handle without O_PATH defined or use
    /// 'Dir::list_self()' which clone-upgrades the handle it used for the iteration.
    // TODO(tailhook) maybe accept only absolute paths?
    pub fn open<P: AsPath>(path: P) -> io::Result<Dir> {
        Dir::_open(to_cstr(path)?.as_ref(), O_PATH | libc::O_CLOEXEC)
    }

    pub(crate) fn _open(path: &CStr, flags: libc::c_int) -> io::Result<Dir> {
        let fd = unsafe { libc_ok(libc::open(path.as_ptr(), O_DIRECTORY | flags))? };
        Ok(Dir(fd))
    }

    /// Checks if the fd associated with the Dir object is really a directory.
    /// There are subtle differences in how directories can be opened and what properties the
    /// resulting file handles have. On some platforms it is possible that
    /// Dir::open("somefile") succeeds. This will usually raise errors later when one tries to
    /// do Directory operations on this. While checking if such an handle comes with cost of a
    /// potential expensive 'stat()' operation. This library makes the assumption that in the
    /// 'usual' case Dir objects are only created on directories and operations on Dir handles
    /// handle errors properly. Still in some cases one may check a freshly created handle
    /// explicitly. Thats what 'is_dir()' is for. Returns 'true' when the underlying handles
    /// represents a directory and false otherwise.
    pub fn is_dir(&self) -> bool {
        match fd_type(self.0).unwrap_or(FdType::Other) {
            FdType::NormalDir | FdType::OPathDir => true,
            FdType::Other => false,
        }
    }

    /// List subdirectory of this dir
    ///
    /// You can list directory itself with `list_self`.
    pub fn list_dir<P: AsPath>(&self, path: P) -> io::Result<DirIter> {
        self.with(O_SEARCH).sub_dir(path)?.list()
    }

    /// List this dir
    pub fn list_self(&self) -> io::Result<DirIter> {
        self.with(O_SEARCH).clone_upgrade()?.list()
    }

    /// Create a DirIter from a Dir
    /// Dir must not be a handle opened with O_PATH.
    pub fn list(self) -> io::Result<DirIter> {
        let fd = self.0;
        std::mem::forget(self);
        open_dirfd(fd)
    }

    /// Create a flags builder for member methods. Defaults to `O_CLOEXEC | O_NOFOLLOW` plus
    /// the given flags. Further flags can be added/removed by the 'with()'/'without()'
    /// members. And finally be used by 'sub_dir()' and the different 'open()' calls.
    #[inline]
    pub fn with<'a>(&'a self, flags: libc::c_int) -> DirMethodFlags<'a> {
        DirMethodFlags::new(self, libc::O_CLOEXEC | libc::O_NOFOLLOW | flags)
    }

    /// Create a flags builder for member methods. Defaults to `O_CLOEXEC | O_NOFOLLOW` with
    /// the given flags (which may O_CLOEXEC or O_NOFOLLOW) removed. Further flags can be
    /// added/removed by the 'with()'/'without()' members. And finally be used by 'sub_dir()'
    /// and the different 'open()' calls.
    #[inline]
    pub fn without<'a>(&'a self, flags: libc::c_int) -> DirMethodFlags<'a> {
        DirMethodFlags::new(self, (libc::O_CLOEXEC | libc::O_NOFOLLOW) & !flags)
    }

    /// Open subdirectory with O_PATH when available.
    /// A descriptor obtained with this flag is restricted to do only certain operations:
    /// - It may be used as anchor for opening sub-objects
    /// - One can query metadata of this directory
    ///
    /// This handle is not suitable for a 'Dir::list()' call and may yield a runtime error.
    /// Use 'Dir::with(0).sub_dir()' to get a handle without O_PATH defined or use
    /// 'Dir::list_dir()' which clone-upgrades the handle it used for the iteration.
    ///
    /// Note that this method does not resolve symlinks by default, so you may have to call
    /// [`read_link`] to resolve the real path first or create a handle
    /// 'without(libc::O_NOFOLLOW)'.
    ///
    /// [`read_link`]: #method.read_link
    pub fn sub_dir<P: AsPath>(&self, path: P) -> io::Result<Dir> {
        self._sub_dir(
            to_cstr(path)?.as_ref(),
            O_PATH | libc::O_CLOEXEC | libc::O_NOFOLLOW,
        )
    }

    pub(crate) fn _sub_dir(&self, path: &CStr, flags: libc::c_int) -> io::Result<Dir> {
        Ok(Dir(unsafe {
            libc_ok(libc::openat(self.0, path.as_ptr(), flags | O_DIRECTORY))?
        }))
    }

    /// Read link in this directory
    pub fn read_link<P: AsPath>(&self, path: P) -> io::Result<PathBuf> {
        self._read_link(to_cstr(path)?.as_ref())
    }

    fn _read_link(&self, path: &CStr) -> io::Result<PathBuf> {
        let mut buf = vec![0u8; 4096];
        let res = unsafe {
            libc::readlinkat(self.0,
                        path.as_ptr(),
                        buf.as_mut_ptr() as *mut libc::c_char, buf.len())
        };
        if res < 0 {
            Err(io::Error::last_os_error())
        } else {
            buf.truncate(res as usize);
            Ok(OsString::from_vec(buf).into())
        }
    }

    /// Open file for reading in this directory
    ///
    /// Note that this method does not resolve symlinks by default, so you may have to call
    /// [`read_link`] to resolve the real path first.
    ///
    /// [`read_link`]: #method.read_link
    pub fn open_file<P: AsPath>(&self, path: P) -> io::Result<File> {
        self._open_file(to_cstr(path)?.as_ref(),
            libc::O_RDONLY, 0)
    }

    /// Open file for writing, create if necessary, truncate on open
    ///
    /// If there exists a symlink at the destination path, this method will fail. In that case, you
    /// will need to remove the symlink before calling this method. If you are on Linux, you can
    /// alternatively create an unnamed file with [`new_unnamed_file`] and then rename it,
    /// clobbering the symlink at the destination.
    ///
    /// [`new_unnamed_file`]: #method.new_unnamed_file
    pub fn write_file<P: AsPath>(&self, path: P, mode: libc::mode_t)
        -> io::Result<File>
    {
        self._open_file(to_cstr(path)?.as_ref(),
            libc::O_CREAT|libc::O_WRONLY|libc::O_TRUNC,
            mode)
    }

    /// Open file for append, create if necessary
    ///
    /// If there exists a symlink at the destination path, this method will fail. In that case, you
    /// will need to call [`read_link`] to resolve the real path first.
    ///
    /// [`read_link`]: #method.read_link
    pub fn append_file<P: AsPath>(&self, path: P, mode: libc::mode_t)
        -> io::Result<File>
    {
        self._open_file(to_cstr(path)?.as_ref(),
            libc::O_CREAT|libc::O_WRONLY|libc::O_APPEND,
            mode)
    }

    /// Create file for writing (and truncate) in this directory
    ///
    /// Deprecated alias for `write_file`
    ///
    /// If there exists a symlink at the destination path, this method will fail. In that case, you
    /// will need to remove the symlink before calling this method. If you are on Linux, you can
    /// alternatively create an unnamed file with [`new_unnamed_file`] and then rename it,
    /// clobbering the symlink at the destination.
    ///
    /// [`new_unnamed_file`]: #method.new_unnamed_file
    #[deprecated(since="0.1.7", note="please use `write_file` instead")]
    pub fn create_file<P: AsPath>(&self, path: P, mode: libc::mode_t)
        -> io::Result<File>
    {
        self._open_file(to_cstr(path)?.as_ref(),
            libc::O_CREAT|libc::O_WRONLY|libc::O_TRUNC,
            mode)
    }

    /// Create a tmpfile in this directory which isn't linked to any filename
    ///
    /// This works by passing `O_TMPFILE` into the openat call. The flag is
    /// supported only on linux. So this function always returns error on
    /// such systems.
    ///
    /// **WARNING!** On glibc < 2.22 file permissions of the newly created file
    /// may be arbitrary. Consider chowning after creating a file.
    ///
    /// Note: It may be unclear why creating unnamed file requires a dir. There
    /// are two reasons:
    ///
    /// 1. It's created (and occupies space) on a real filesystem, so the
    ///    directory is a way to find out which filesystem to attach file to
    /// 2. This method is mostly needed to initialize the file then link it
    ///    using ``link_file_at`` to the real directory entry. When linking
    ///    it must be linked into the same filesystem. But because for most
    ///    programs finding out filesystem layout is an overkill the rule of
    ///    thumb is to create a file in the the target directory.
    ///
    /// Currently, we recommend to fallback on any error if this operation
    /// can't be accomplished rather than relying on specific error codes,
    /// because semantics of errors are very ugly.
    #[cfg(feature = "o_tmpfile")]
    pub fn new_unnamed_file(&self, mode: libc::mode_t)
        -> io::Result<File>
    {
        self._open_file(unsafe { CStr::from_bytes_with_nul_unchecked(b".\0") },
            libc::O_TMPFILE|libc::O_WRONLY,
            mode)
    }

    /// Create a tmpfile in this directory which isn't linked to any filename
    ///
    /// This works by passing `O_TMPFILE` into the openat call. The flag is
    /// supported only on linux. So this function always returns error on
    /// such systems.
    ///
    /// Note: It may be unclear why creating unnamed file requires a dir. There
    /// are two reasons:
    ///
    /// 1. It's created (and occupies space) on a real filesystem, so the
    ///    directory is a way to find out which filesystem to attach file to
    /// 2. This method is mostly needed to initialize the file then link it
    ///    using ``link_file_at`` to the real directory entry. When linking
    ///    it must be linked into the same filesystem. But because for most
    ///    programs finding out filesystem layout is an overkill the rule of
    ///    thumb is to create a file in the the target directory.
    ///
    /// Currently, we recommend to fallback on any error if this operation
    /// can't be accomplished rather than relying on specific error codes,
    /// because semantics of errors are very ugly.
    #[cfg(not(feature = "o_tmpfile"))]
    pub fn new_unnamed_file<P: AsPath>(&self, _mode: libc::mode_t)
        -> io::Result<File>
    {
        Err(io::Error::new(io::ErrorKind::Other,
            "creating unnamed tmpfiles is only supported on linux"))
    }

    /// Link open file to a specified path
    ///
    /// This is used with ``new_unnamed_file()`` to create and initialize the
    /// file before linking it into a filesystem. This requires `/proc` to be
    /// mounted and works **only on linux**.
    ///
    /// On systems other than linux this always returns error. It's expected
    /// that in most cases this methos is not called if ``new_unnamed_file``
    /// fails. But in obscure scenarios where `/proc` is not mounted this
    /// method may fail even on linux. So your code should be able to fallback
    /// to a named file if this method fails too.
    #[cfg(feature = "link_file_at")]
    pub fn link_file_at<F: AsRawFd, P: AsPath>(&self, file: &F, path: P)
        -> io::Result<()>
    {
        let fd_path = format!("/proc/self/fd/{}", file.as_raw_fd());
        _hardlink(&Dir(libc::AT_FDCWD), to_cstr(fd_path)?.as_ref(),
            &self, to_cstr(path)?.as_ref(),
            libc::AT_SYMLINK_FOLLOW)
    }

    /// Link open file to a specified path
    ///
    /// This is used with ``new_unnamed_file()`` to create and initialize the
    /// file before linking it into a filesystem. This requires `/proc` to be
    /// mounted and works **only on linux**.
    ///
    /// On systems other than linux this always returns error. It's expected
    /// that in most cases this methos is not called if ``new_unnamed_file``
    /// fails. But in obscure scenarios where `/proc` is not mounted this
    /// method may fail even on linux. So your code should be able to fallback
    /// to a named file if this method fails too.
    //NOTE(cehteh): would it make sense to remove this function (for non linux), this will
    // generate a compile time error rather than a runtime error, which most likely is
    // favorable since the semantic cant easily emulated.
    #[cfg(not(feature = "link_file_at"))]
    pub fn link_file_at<F: AsRawFd, P: AsPath>(&self, _file: F, _path: P)
        -> io::Result<()>
    {
        Err(io::Error::new(io::ErrorKind::Other,
            "linking unnamed fd to directories is only supported on linux"))
    }

    /// Create file if not exists, fail if exists
    ///
    /// This function checks existence and creates file atomically with
    /// respect to other threads and processes.
    ///
    /// Technically it means passing `O_EXCL` flag to open.
    pub fn new_file<P: AsPath>(&self, path: P, mode: libc::mode_t)
        -> io::Result<File>
    {
        self._open_file(to_cstr(path)?.as_ref(),
            libc::O_CREAT|libc::O_EXCL|libc::O_WRONLY,
            mode)
    }

    /// Open file for reading and writing without truncation, create if needed
    ///
    /// If there exists a symlink at the destination path, this method will fail. In that case, you
    /// will need to call [`read_link`] to resolve the real path first.
    ///
    /// [`read_link`]: #method.read_link
    pub fn update_file<P: AsPath>(&self, path: P, mode: libc::mode_t)
        -> io::Result<File>
    {
        self._open_file(to_cstr(path)?.as_ref(),
            libc::O_CREAT|libc::O_RDWR,
            mode)
    }

    pub(crate) fn _open_file(&self, path: &CStr, flags: libc::c_int, mode: libc::mode_t)
        -> io::Result<File>
    {
        unsafe {
            // Note: In below call to `openat`, *mode* must be cast to
            // `unsigned` because the optional `mode` argument to `openat` is
            // variadic in the signature. Since integers are not implicitly
            // promoted as they are in C this would break on Freebsd where
            // *mode_t* is an alias for `uint16_t`.
            let res = libc_ok(
                libc::openat(self.0, path.as_ptr(),
                             flags|libc::O_CLOEXEC|libc::O_NOFOLLOW,
                             mode as libc::c_uint)
            )?;
            Ok(File::from_raw_fd(res))
        }
    }

    /// Make a symlink in this directory
    ///
    /// Note: the order of arguments differ from `symlinkat`
    pub fn symlink<P: AsPath, R: AsPath>(&self, path: P, value: R)
        -> io::Result<()>
    {
        self._symlink(to_cstr(path)?.as_ref(), to_cstr(value)?.as_ref())
    }
    fn _symlink(&self, path: &CStr, link: &CStr) -> io::Result<()> {
        unsafe {
            let res = libc::symlinkat(link.as_ptr(),
                self.0, path.as_ptr());
            if res < 0 {
                Err(io::Error::last_os_error())
            } else {
                Ok(())
            }
        }
    }

    /// Create a subdirectory in this directory
    pub fn create_dir<P: AsPath>(&self, path: P, mode: libc::mode_t)
        -> io::Result<()>
    {
        self._create_dir(to_cstr(path)?.as_ref(), mode)
    }
    fn _create_dir(&self, path: &CStr, mode: libc::mode_t) -> io::Result<()> {
        unsafe {
            libc_ok(libc::mkdirat(self.0, path.as_ptr(), mode))?;
        }
        Ok(())
    }

    /// Rename a file in this directory to another name (keeping same dir)
    pub fn local_rename<P: AsPath, R: AsPath>(&self, old: P, new: R)
        -> io::Result<()>
    {
        rename(self, to_cstr(old)?.as_ref(), self, to_cstr(new)?.as_ref())
    }

    /// Similar to `local_rename` but atomically swaps both paths
    ///
    /// Only supported on Linux.
    #[cfg(feature = "rename_exchange")]
    pub fn local_exchange<P: AsPath, R: AsPath>(&self, old: P, new: R)
        -> io::Result<()>
    {
        // Workaround https://github.com/tailhook/openat/issues/35
        // AKA https://github.com/rust-lang/libc/pull/2116
        // Unfortunately since we made this libc::c_int in our
        // public API, we can't easily change it right now.
        let flags = libc::RENAME_EXCHANGE as libc::c_int;
        rename_flags(self, to_cstr(old)?.as_ref(),
            self, to_cstr(new)?.as_ref(),
            flags)
    }

    /// Remove a subdirectory in this directory
    ///
    /// Note only empty directory may be removed
    pub fn remove_dir<P: AsPath>(&self, path: P)
        -> io::Result<()>
    {
        self._unlink(to_cstr(path)?.as_ref(), libc::AT_REMOVEDIR)
    }
    /// Remove a file in this directory
    pub fn remove_file<P: AsPath>(&self, path: P)
        -> io::Result<()>
    {
        self._unlink(to_cstr(path)?.as_ref(), 0)
    }
    fn _unlink(&self, path: &CStr, flags: libc::c_int) -> io::Result<()> {
        unsafe {
            libc_ok(libc::unlinkat(self.0, path.as_ptr(), flags))?;
        }
        Ok(())
    }

    /// Get the path of this directory (if possible)
    ///
    /// This uses symlinks in `/proc/self`, they sometimes may not be
    /// available so use with care.
    #[cfg(feature = "proc_self_fd")]
    pub fn recover_path(&self) -> io::Result<PathBuf> {
        let fd = self.0;
        if fd != libc::AT_FDCWD {
            read_link(format!("/proc/self/fd/{}", fd))
        } else {
            read_link("/proc/self/cwd")
        }
    }

    /// Returns metadata of an entry in this directory
    ///
    /// If the destination path is a symlink, this will return the metadata of the symlink itself.
    /// If you would like to follow the symlink and return the metadata of the target, you will
    /// have to call [`read_link`] to resolve the real path first.
    ///
    /// [`read_link`]: #method.read_link
    pub fn metadata<P: AsPath>(&self, path: P) -> io::Result<Metadata> {
        self._stat(to_cstr(path)?.as_ref(), libc::AT_SYMLINK_NOFOLLOW)
    }
    fn _stat(&self, path: &CStr, flags: libc::c_int) -> io::Result<Metadata> {
        unsafe {
            let mut stat = mem::zeroed(); // TODO(cehteh): uninit
            libc_ok(libc::fstatat(self.0, path.as_ptr(), &mut stat, flags))?;
            Ok(metadata::new(stat))
        }
    }

    /// Returns the metadata of the directory itself.
    pub fn self_metadata(&self) -> io::Result<Metadata> {
        unsafe {
            let mut stat = mem::zeroed(); // TODO(cehteh): uninit
            libc_ok(libc::fstat(self.0, &mut stat))?;
            Ok(metadata::new(stat))
        }
    }

    /// Constructs a new `Dir` from a given raw file descriptor,
    /// ensuring it is a directory file descriptor first.
    ///
    /// This function **consumes ownership** of the specified file
    /// descriptor. The returned `Dir` will take responsibility for
    /// closing it when it goes out of scope.
    pub unsafe fn from_raw_fd_checked(fd: RawFd) -> io::Result<Self> {
        match fd_type(fd)? {
            FdType::NormalDir | FdType::OPathDir => Ok(Dir(fd)),
            FdType::Other => Err(io::Error::from_raw_os_error(libc::ENOTDIR)),
        }
    }

    /// Creates a new independently owned handle to the underlying directory.
    /// The new handle has the same (Normal/O_PATH) semantics as the original handle.
    pub fn try_clone(&self) -> io::Result<Self> {
        Ok(Dir(clone_dirfd(self.0)?))
    }

    /// Creates a new 'Normal' independently owned handle to the underlying directory.
    pub fn clone_upgrade(&self) -> io::Result<Self> {
        Ok(Dir(clone_dirfd_upgrade(self.0, 0)?))
    }

    /// Creates a new 'O_PATH' restricted independently owned handle to the underlying directory.
    pub fn clone_downgrade(&self) -> io::Result<Self> {
        Ok(Dir(clone_dirfd_downgrade(self.0)?))
    }
}

const CURRENT_DIRECTORY: [libc::c_char; 2] = [b'.' as libc::c_char, 0];

//TODO(cehteh): eventually the clone calls should replicate O_SEARCH, maybe other file flags
fn clone_dirfd(fd: libc::c_int) -> io::Result<libc::c_int> {
    unsafe {
        match fd_type(fd)? {
            FdType::NormalDir => libc_ok(libc::openat(
                fd,
                &CURRENT_DIRECTORY as *const libc::c_char,
                O_DIRECTORY | libc::O_CLOEXEC,
            )),
            #[cfg(feature = "o_path")]
            FdType::OPathDir => libc_ok(libc::dup(fd)),
            _ => Err(io::Error::from_raw_os_error(libc::ENOTDIR)),
        }
    }
}

pub(crate) fn clone_dirfd_upgrade(fd: libc::c_int, flags: libc::c_int) -> io::Result<libc::c_int> {
    unsafe {
        match fd_type(fd)? {
            FdType::NormalDir | FdType::OPathDir => libc_ok(libc::openat(
                fd,
                &CURRENT_DIRECTORY as *const libc::c_char,
                flags | O_DIRECTORY | libc::O_CLOEXEC,
            )),
            _ => Err(io::Error::from_raw_os_error(libc::ENOTDIR)),
        }
    }
}

fn clone_dirfd_downgrade(fd: libc::c_int) -> io::Result<libc::c_int> {
    unsafe {
        match fd_type(fd)? {
            #[cfg(feature = "o_path")]
            FdType::NormalDir => libc_ok(libc::openat(
                fd,
                &CURRENT_DIRECTORY as *const libc::c_char,
                libc::O_PATH | O_DIRECTORY | libc::O_CLOEXEC,
            )),
            #[cfg(not(feature = "o_path"))]
            FdType::NormalDir => libc_ok(libc::openat(
                fd,
                &CURRENT_DIRECTORY as *const libc::c_char,
                O_DIRECTORY | libc::O_CLOEXEC,
            )),
            #[cfg(feature = "o_path")]
            FdType::OPathDir => libc_ok(libc::dup(fd)),
            _ => Err(io::Error::from_raw_os_error(libc::ENOTDIR)),
        }
    }
}

enum FdType {
    NormalDir,
    OPathDir,
    Other,
}

// OSes with fcntl() that returns O_DIRECTORY
// Linux has O_PATH
#[cfg(all(feature = "o_path", feature = "fcntl_o_directory"))]
fn fd_type(fd: libc::c_int) -> io::Result<FdType> {
    let flags = unsafe { libc_ok(libc::fcntl(fd, libc::F_GETFL))? };
    if flags & libc::O_DIRECTORY != 0 {
        if flags & libc::O_PATH != 0 {
            Ok(FdType::OPathDir)
        } else {
            Ok(FdType::NormalDir)
        }
    } else {
        Ok(FdType::Other)
    }
}

#[cfg(all(not(feature = "o_path"), feature = "fcntl_o_directory"))]
fn fd_type(fd: libc::c_int) -> io::Result<FdType> {
    let flags = unsafe { libc_ok(libc::fcntl(fd, libc::F_GETFL))? };
    if flags & libc::O_DIRECTORY != 0 {
        Ok(FdType::NormalDir)
    } else {
        Ok(FdType::Other)
    }
}

// OSes where fcntl won't return O_DIRECTORY use stat()
#[cfg(not(feature = "fcntl_o_directory"))]
fn fd_type(fd: libc::c_int) -> io::Result<FdType> {
    unsafe {
        let mut stat = mem::zeroed(); // TODO(cehteh): uninit
        libc_ok(libc::fstat(fd, &mut stat))?;
        match stat.st_mode & libc::S_IFMT {
            libc::S_IFDIR => Ok(FdType::NormalDir),
            _ => Ok(FdType::Other),
        }
    }
}

#[inline]
fn libc_ok(ret: libc::c_int) -> io::Result<libc::c_int> {
    if ret != -1 {
        Ok(ret)
    } else {
        Err(io::Error::last_os_error())
    }
}

/// Rename (move) a file between directories
///
/// Files must be on a single filesystem anyway. This funtion does **not**
/// fallback to copying if needed.
pub fn rename<P, R>(old_dir: &Dir, old: P, new_dir: &Dir, new: R)
    -> io::Result<()>
    where P: AsPath, R: AsPath,
{
    _rename(old_dir, to_cstr(old)?.as_ref(), new_dir, to_cstr(new)?.as_ref())
}

fn _rename(old_dir: &Dir, old: &CStr, new_dir: &Dir, new: &CStr) -> io::Result<()> {
    unsafe {
        libc_ok(libc::renameat(old_dir.0, old.as_ptr(), new_dir.0, new.as_ptr()))?;
    }
    Ok(())
}

/// Create a hardlink to a file
///
/// Files must be on a single filesystem even if they are in different
/// directories.
///
/// Note: by default ``linkat`` syscall doesn't resolve symbolic links, and
/// it's also behavior of this function. It's recommended to resolve symlinks
/// manually if needed.
pub fn hardlink<P, R>(old_dir: &Dir, old: P, new_dir: &Dir, new: R)
    -> io::Result<()>
    where P: AsPath, R: AsPath,
{
    _hardlink(old_dir, to_cstr(old)?.as_ref(),
              new_dir, to_cstr(new)?.as_ref(),
              0)
}

fn _hardlink(
    old_dir: &Dir,
    old: &CStr,
    new_dir: &Dir,
    new: &CStr,
    flags: libc::c_int,
) -> io::Result<()> {
    unsafe {
        libc_ok(libc::linkat(old_dir.0, old.as_ptr(), new_dir.0, new.as_ptr(), flags))?;
    }
    Ok(())
}

/// Rename (move) a file between directories with flags
///
/// Files must be on a single filesystem anyway. This funtion does **not**
/// fallback to copying if needed.
///
/// Only supported on Linux.
#[cfg(feature = "renameat_flags")]
pub fn rename_flags<P, R>(old_dir: &Dir, old: P, new_dir: &Dir, new: R,
    flags: libc::c_int)
    -> io::Result<()>
    where P: AsPath, R: AsPath,
{
    _rename_flags(old_dir, to_cstr(old)?.as_ref(),
        new_dir, to_cstr(new)?.as_ref(),
        flags)
}

#[cfg(feature = "renameat_flags")]
fn _rename_flags(old_dir: &Dir, old: &CStr, new_dir: &Dir, new: &CStr,
    flags: libc::c_int)
    -> io::Result<()>
{
    unsafe {
        let res = libc::syscall(
            libc::SYS_renameat2,
            old_dir.0, old.as_ptr(),
            new_dir.0, new.as_ptr(), flags);
        if res < 0 {
            Err(io::Error::last_os_error())
        } else {
            Ok(())
        }
    }
}

impl AsRawFd for Dir {
    #[inline]
    fn as_raw_fd(&self) -> RawFd {
        self.0
    }
}

impl FromRawFd for Dir {
    /// The user must guarantee that the passed in `RawFd` is in fact
    /// a directory file descriptor.
    #[inline]
    unsafe fn from_raw_fd(fd: RawFd) -> Dir {
        Dir(fd)
    }
}

impl IntoRawFd for Dir {
    #[inline]
    fn into_raw_fd(self) -> RawFd {
        let result = self.0;
        mem::forget(self);
        return result;
    }
}

impl Drop for Dir {
    fn drop(&mut self) {
        let fd = self.0;
        if fd != libc::AT_FDCWD {
            unsafe {
                libc::close(fd);
            }
        }
    }
}

pub(crate) fn to_cstr<P: AsPath>(path: P) -> io::Result<P::Buffer> {
    path.to_path()
    .ok_or_else(|| {
        io::Error::new(io::ErrorKind::InvalidInput,
                       "nul byte in file name")
    })
}

#[cfg(test)]
mod test {
    use std::io::{Read};
    use std::path::Path;
    use std::os::unix::io::{FromRawFd, IntoRawFd};
    use crate::{Dir};

    #[test]
    fn test_open_ok() {
        assert!(Dir::open("src").is_ok());
    }

    #[test]
    fn test_read_file() {
        let dir = Dir::open("src").unwrap();
        let mut buf = String::new();
        dir.open_file("lib.rs").unwrap()
            .read_to_string(&mut buf).unwrap();
        assert!(buf.find("extern crate libc;").is_some());
    }

    #[test]
    fn test_from_into() {
        let dir = Dir::open("src").unwrap();
        let dir = unsafe { Dir::from_raw_fd(dir.into_raw_fd()) };
        let mut buf = String::new();
        dir.open_file("lib.rs").unwrap()
            .read_to_string(&mut buf).unwrap();
        assert!(buf.find("extern crate libc;").is_some());
    }

    #[test]
    #[should_panic(expected="No such file or directory")]
    fn test_open_no_dir() {
        Dir::open("src/some-non-existent-file").unwrap();
    }

    #[test]
    fn test_list() {
        let dir = Dir::flags().open("src").unwrap();
        let me = dir.list().unwrap();
        assert!(me
            .collect::<Result<Vec<_>, _>>()
            .unwrap()
            .iter()
            .find(|x| { x.file_name() == Path::new("lib.rs").as_os_str() })
            .is_some());
    }

    #[test]
    #[cfg(feature = "o_path")]
    #[should_panic(expected = "Bad file descriptor")]
    fn test_list_opath_fail() {
        let dir = Dir::open("src").unwrap();
        let me = dir.list().unwrap();
        assert!(me
            .collect::<Result<Vec<_>, _>>()
            .unwrap()
            .iter()
            .find(|x| { x.file_name() == Path::new("lib.rs").as_os_str() })
            .is_some());
    }

    #[test]
    fn test_list_self() {
        let dir = Dir::open("src").unwrap();
        let me = dir.list_self().unwrap();
        assert!(me
            .collect::<Result<Vec<_>, _>>()
            .unwrap()
            .iter()
            .find(|x| { x.file_name() == Path::new("lib.rs").as_os_str() })
            .is_some());
    }

    #[test]
    fn test_list_dot() {
        let dir = Dir::open("src").unwrap();
        let me = dir.list_dir(".").unwrap();
        assert!(me
            .collect::<Result<Vec<_>, _>>()
            .unwrap()
            .iter()
            .find(|x| { x.file_name() == Path::new("lib.rs").as_os_str() })
            .is_some());
    }

    #[test]
    fn test_list_dir() {
        let dir = Dir::open(".").unwrap();
        let me = dir.list_dir("src").unwrap();
        assert!(me
            .collect::<Result<Vec<_>, _>>()
            .unwrap()
            .iter()
            .find(|x| { x.file_name() == Path::new("lib.rs").as_os_str() })
            .is_some());
    }

    #[test]
    fn test_from_raw_fd_checked() {
        let fd = Dir::open(".").unwrap().into_raw_fd();
        let dir = unsafe { Dir::from_raw_fd_checked(fd) }.unwrap();
        let filefd = dir.open_file("src/lib.rs").unwrap().into_raw_fd();
        match unsafe { Dir::from_raw_fd_checked(filefd) } {
            Ok(_) => assert!(false, "from_raw_fd_checked succeeded on a non-directory fd!"),
            Err(e) => assert_eq!(e.raw_os_error().unwrap(), libc::ENOTDIR)
        }
    }

    #[test]
    fn test_try_clone() {
        let d = Dir::open(".").unwrap();
        let d2 = d.try_clone().unwrap();
        drop(d);
        let _file = d2.open_file("src/lib.rs").unwrap();
    }
}