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
use crate::getdent::DirentBuf;

use core::mem;
use std::io;
use std::ffi::{CStr, CString, OsStr, OsString};
use std::path::{Component, Path, PathBuf};
use std::sync::Arc;
use std::os::unix::fs::FileTypeExt;
use std::os::unix::ffi::OsStrExt;
use once_cell::sync::OnceCell;

use super::UnixFileType as FileTypeInner;
use super::getdent::{DirentErr, Entry, More};

/// Configure walking over all files in a directory tree.
pub struct WalkDir {
    /// The user supplied configuration.
    config: Configuration,
    path: PathBuf,
}

/// The main iterator.
pub struct IntoIter {
    /// The user supplied configuration.
    config: Configuration,
    /// The current 'finger' within the tree of directories.
    stack: Vec<WorkItem>,
    /// The number of file descriptors we are still allowed to open.
    open_budget: usize,
    /// Statistics about the system calls etc.
    stats: Stats,
}

/// Describes a file that was found.
///
/// All parents of this entry have already been yielded before.
#[derive(Debug, Clone)]
pub struct DirEntry {
    /// The file type reported by the call to `getdent`.
    file_type: FileType,
    /// The depth at which this entry was found.
    depth: usize,
    /// The file name of this entry.
    file_name: EntryPath,
    /// The normalized full path of the entry.
    full_path: OnceCell<PathBuf>,
}

#[derive(Debug, Clone)]
enum EntryPath {
    /// We have already allocate the whole path in its own buffer.
    Full(PathBuf),
    /// The path is given as the filename alone.
    Name {
        name: OsString,
        /// The parent directory of the entry.
        parent: Arc<Node>,
    },
}

#[derive(Debug)]
pub struct Error {
    _private: (),
}

/// The type of a file entry.
///
/// Accessing this will not cause any system calls and is very cheap. However, the type may not
/// always be known. In these cases you need to manually query the file meta data.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct FileType {
    inner: Option<FileTypeInner>,
}

#[derive(Copy, Clone)]
struct Configuration {
    min_depth: usize,
    max_depth: usize,
    max_open: usize,
    follow_links: bool,
    contents_first: bool,
    same_file_system: bool,
}

#[derive(Debug, Default)]
struct Stats {
    nr_close: usize,
    nr_getdent: usize,
    nr_open: usize,
    nr_openat: usize,
    nr_stat: usize,
}

/// Completed directory nodes that are parents of still open nodes or active entries.
#[derive(Debug)]
struct Node {
    /// The depth at which this node occurs.
    depth: usize,
    /// The path of this node.
    path: EntryPath,
}

enum WorkItem {
    /// A directory which is still open.
    Open(Open),
    /// A directory that was closed.
    Closed(Closed),
}

/// Directories with a file descriptor.
struct Open {
    /// The open file descriptor.
    fd: DirFd,
    /// The buffer for reading entries of this directory.
    buffer: DirentBuf,
    /// The directory depth of this descriptor.
    depth: usize,
    /// The parent representation of this node.
    /// Not to be confused with the potentially still open parent directory.
    as_parent: Arc<Node>,
}

/// Describes a directory that had to be closed, and its entries read to memory.
struct Closed {
    /// The directory depth of the directory.
    depth: usize,
    /// The children.
    children: Vec<Backlog>,
    /// The parent representation of this node.
    /// The parent directory is also surely closed but children might not be.
    as_parent: Option<Arc<Node>>,
}

struct DirFd(libc::c_int);

/// Describes an item of a closed directory.
///
/// The directories represented by this type are no-one's parent yet.
///
/// Note that by using `openat` we can avoid having to construct the complete path as a single
/// `PathBuf` but this requires keeping the parent `fd` open.
///
/// TODO: what if we use a dequeue to actually allocate these consecutively in memory?
struct Backlog {
    /// The complete path up to here.
    /// Since the file descriptor was closed we can't use `openat` but need to reconstruct the full
    /// path. We might want to track statistics on this since it really is annoying.
    file_path: PathBuf,
    file_type: Option<FileTypeInner>,
}

// Public interfaces.

impl WalkDir {
    pub fn new(path: impl AsRef<Path>) -> Self {
        WalkDir {
            config: Configuration::default(),
            path: path.as_ref().to_owned(),
        }
    }

    pub fn min_depth(mut self, n: usize) -> Self {
        self.config.min_depth = n;
        self
    }

    pub fn max_depth(mut self, n: usize) -> Self {
        self.config.max_depth = n;
        self
    }

    pub fn max_open(mut self, n: usize) -> Self {
        self.config.max_open = n;
        self
    }

    pub fn follow_links(mut self, yes: bool) -> Self {
        self.config.follow_links = yes;
        self
    }

    pub fn sort_by<F>(self, cmp: F) -> Self where
        F: FnMut(&DirEntry, &DirEntry) -> core::cmp::Ordering + Send + Sync + 'static,
    {
        todo!()
    }

    pub fn contents_first(mut self, yes: bool) -> Self {
        self.config.contents_first = yes;
        self
    }

    pub fn same_file_system(mut self, yes: bool) -> Self {
        self.config.same_file_system = yes;
        self
    }

    pub fn build(mut self) -> IntoIter {
        self.config.assert_consistent();
        let first_item = self.initial_closed();

        IntoIter {
            config: self.config,
            stack: vec![WorkItem::Closed(first_item)],
            open_budget: 128,
            stats: Stats::default(),
        }
    }

    fn initial_closed(&mut self) -> Closed {
        let backlog = Backlog {
            file_path: core::mem::take(&mut self.path),
            // We do not _know_ this file type yet, recover and check on iteration.
            file_type: None,
        };

        Closed {
            depth: 0,
            children: vec![backlog],
            as_parent: None,
        }
    }
}

impl Configuration {
    fn assert_consistent(&self) {
        assert!(self.min_depth <= self.max_depth);
        assert!(self.max_open > 0);
        assert!(!self.follow_links, "Unsupported");
        assert!(!self.same_file_system , "Unsupported");
    }
}

impl Default for Configuration {
    fn default() -> Self {
        Configuration {
            min_depth: 0,
            max_depth: usize::MAX,
            max_open: 10,
            follow_links: false,
            contents_first: false,
            same_file_system: false,
        }
    }
}

impl IntoIter {
    pub fn skip_current_dir(&mut self) {
        todo!()
    }

    pub fn filter_entry<P>(self, predicate: P) -> FilterEntry<Self, P> where
        P: FnMut(&DirEntry) -> bool,
    {
        todo!()
    }

    pub fn stats(&self) -> &dyn core::fmt::Debug {
        &self.stats
    }
}

pub struct FilterEntry<I, P> {
    unused: core::marker::PhantomData<(I, P)>,
}

impl FileType {
    pub fn is_dir(&self) -> bool {
        self.inner == Some(FileTypeInner::Directory)
    }

    pub fn is_file(&self) -> bool {
        self.inner == Some(FileTypeInner::File)
    }

    pub fn is_symlink(&self) -> bool {
        self.inner == Some(FileTypeInner::SymbolicLink)
    }

    fn set(&mut self, inner: FileTypeInner) {
        self.inner = Some(inner);
    }
}

impl DirEntry {
    // TODO: enable `openat`?

    /// Inspect the path of this entry.
    pub fn path(&self) -> &Path {
        self.full_path.get_or_init(|| {
            self.file_name.make_path()
        })
    }

    pub fn path_is_symlink(&self) -> bool {
        self.file_type.is_symlink()
    }

    /// Read the full meta data.
    pub fn metadata(&self) -> io::Result<std::fs::Metadata> {
        std::fs::metadata(self.path())
    }

    /// Convert the entry into a path
    ///
    /// Potentially more efficient than `as_path().to_owned()`.
    pub fn into_path(self) -> PathBuf {
        let file_name = self.file_name;
        self.full_path.into_inner().unwrap_or_else(|| {
            file_name.make_path()
        })
    }

    pub fn file_type(&self) -> FileType {
        self.file_type
    }

    /// Return the filename of this entry.
    pub fn file_name(&self) -> &OsStr {
        match &self.file_name {
            EntryPath::Full(buf) => buf.file_name().unwrap(),
            EntryPath::Name { name, .. } => name,
        }
    }

    /// The depth at which this entry is in the directory tree.
    ///
    /// When iterating items in depth-first order and following symbolic links then this is not
    /// necessarily the smallest depth at which it might appear.
    pub fn depth(&self) -> usize {
        self.depth
    }
}

impl Open {
    fn openat_os(&self, path: &OsStr, stats: &mut Stats) -> io::Result<Self> {
        let bytes = path.as_bytes().to_owned();
        let cstr = CString::new(bytes).unwrap();
        self.openat(&cstr, stats)
    }

    fn openat(&self, path: &CStr, stats: &mut Stats) -> io::Result<Self> {
        stats.nr_openat += 1;
        let fd = self.fd.openat(path)?;
        let filename = OsStr::from_bytes(path.to_bytes()).to_owned();

        Ok(Open {
            fd,
            buffer: DirentBuf::with_size(1 << 14),
            depth: self.depth + 1,
            as_parent: Arc::new(Node {
                path: EntryPath::Name {
                    name: filename,
                    parent: self.as_parent.clone(),
                },
                depth: self.depth + 1,
            }),
        })
    }

    /// Get the next item from this directory.
    fn pop(&mut self) -> Option<Entry<'_>> {
        self.buffer.drain().next().map(Self::okay)
    }

    fn ready_entry(&mut self) -> Option<DirEntry> {
        let depth = self.depth;
        let parent = self.as_parent.clone();
        let entry = self.pop()?;

        let entry = match Self::sub_entry(entry) {
            None => return self.ready_entry(),
            Some(entry) => entry,
        };

        Some(DirEntry {
            file_name: EntryPath::Name {
                name: entry.file_name().to_owned(),
                parent,
            },
            depth,
            file_type: FileType {
                inner: entry.file_type(),
            },
            full_path: OnceCell::new(),
        })
    }

    fn fill_buffer(&mut self, stats: &mut Stats) -> io::Result<More> {
        stats.nr_getdent += 1;
        self.buffer.fill_buf(self.fd.0)
    }

    /// Forcibly close this directory entry.
    /// Returns None if its already finished and Some with the remaining backlog items otherwise.
    fn close(mut self, stats: &mut Stats) -> io::Result<Option<Closed>> {
        let mut backlog = vec![];
        let base = self.as_parent.make_path();

        loop {
            let entries = self.buffer
                .drain()
                .map(Self::okay)
                .filter_map(Self::sub_entry)
                .map(|entry| Self::backlog(&base, entry));
            backlog.extend(entries);
            stats.nr_getdent += 1;
            match self.buffer.fill_buf(self.fd.0)? {
                More::Blocked => unreachable!("Just drained buffer is blocked"),
                More::More => {},
                More::Done => break,
            }
        }

        if backlog.is_empty() {
            stats.nr_close += 1;
            self.fd.close()?;
            Ok(None)
        } else {
            let closed = Closed::from_backlog(&self, backlog);
            stats.nr_close += 1;
            self.fd.close()?;
            Ok(Some(closed))
        }
    }

    /// Filter an entry that we got from the internal buffer.
    /// Handles kernel errors and setup faults which mustn't occur in regular operation.
    fn okay(entry: Result<Entry<'_>, DirentErr>) -> Entry<'_> {
        match entry {
            Ok(entry) => entry,
            Err(DirentErr::TooShort) => unreachable!("Inconsistent buffer state"),
            Err(DirentErr::InvalidLength) => unreachable!("You must have hit a kernel bug!"),
        }
    }

    fn sub_entry(entry: Entry<'_>) -> Option<Entry<'_>> {
        // Never recurse into current or parent directory.
        match Path::new(entry.file_name()).components().next() {
            Some(Component::CurDir) | Some(Component::ParentDir) => None,
            _ => Some(entry),
        }

    }

    fn backlog(base: &Path, entry: Entry<'_>) -> Backlog {
        Backlog {
            file_path: base.join(entry.file_name()),
            file_type: entry.file_type(),
        }
    }
}

impl DirFd {
    fn open(path: &Path) -> io::Result<Self> {
        let raw_name = path.as_os_str().as_bytes().to_owned();
        let unix_name = CString::new(raw_name).expect("No interior NULL byte in Path");

        let result = unsafe {
            libc::open(unix_name.as_c_str().as_ptr(), libc::O_RDONLY | libc::O_DIRECTORY)
        };

        if result == -1 {
            return Err(io::Error::last_os_error());
        }

        Ok(DirFd(result))
    }

    fn openat(&self, path: &CStr) -> io::Result<Self> {
        let result = unsafe {
            libc::openat(self.0, path.as_ptr(), libc::O_RDONLY | libc::O_DIRECTORY)
        };

        if result == -1 {
            return Err(io::Error::last_os_error());
        }

        Ok(DirFd(result))
    }

    fn close(self) -> io::Result<()> {
        match unsafe { libc::close(self.0) } {
            0 => Ok(()),
            _ => Err(io::Error::last_os_error()),
        }
    }
}

impl Closed {
    fn from_backlog(open: &Open, children: Vec<Backlog>) -> Self {
        Closed {
            depth: open.depth + 1,
            children,
            as_parent: None,
        }
    }

    fn open(&self, backlog: &DirEntry, stats: &mut Stats) -> io::Result<Open> {
        let path = backlog.file_name.make_path();
        stats.nr_open += 1;
        let fd = DirFd::open(&path)?;

        Ok(Open {
            fd,
            buffer: DirentBuf::with_size(1 << 14),
            depth: self.depth + 1,
            as_parent: Arc::new(Node {
                depth: self.depth + 1,
                path: EntryPath::Full(path),
            })
        })
    }

    fn ready_entry(&mut self) -> Option<DirEntry> {
        let backlog = self.children.pop()?;
        Some(DirEntry {
            file_name: EntryPath::Full(backlog.file_path),
            file_type: FileType {
                inner: backlog.file_type
            },
            depth: self.depth,
            full_path: OnceCell::new(),
        })
    }
}

impl EntryPath {
    fn make_path(&self) -> PathBuf {
        match self {
            EntryPath::Full(buf) => buf.clone(),
            EntryPath::Name { name, parent } => {
                let mut buf = parent.make_path();
                buf.push(&name);
                buf
            }
        }
    }
}

impl Node {
    /// Allocate a path buffer for the path described.
    fn make_path(&self) -> PathBuf {
        self.path.make_path()
    }
}

impl IntoIter {
    /// See if we should descend to the newly found entry.
    fn iter_entry(&mut self, entry: &mut DirEntry) -> Result<(), Error> {
        let is_dir = match entry.file_type.inner {
            Some(FileTypeInner::Directory) => true,
            Some(_) => false,
            None => {
                //can we make fstatat work?
                self.stats.nr_stat += 1;
                let meta = std::fs::metadata(entry.file_name.make_path())
                    .map_err(Error::from_io)?
                    .file_type();
                if meta.is_dir() {
                    entry.file_type.set(FileTypeInner::Directory);
                    true
                } else if meta.is_file() {
                    entry.file_type.set(FileTypeInner::File);
                    false
                } else if meta.is_symlink() {
                    entry.file_type.set(FileTypeInner::SymbolicLink);
                    false
                } else if meta.is_block_device() {
                    entry.file_type.set(FileTypeInner::BlockDevice);
                    false
                } else if meta.is_char_device() {
                    entry.file_type.set(FileTypeInner::CharDevice);
                    false
                } else if meta.is_fifo() {
                    entry.file_type.set(FileTypeInner::File);
                    false
                } else if meta.is_socket() {
                    entry.file_type.set(FileTypeInner::UnixSocket);
                    false
                } else {
                    false
                }
            }
        };

        if is_dir {
            // TODO: filter? min_depth? max_depth?

            let can_open = self.open_budget > 0;
            let mut next: WorkItem = match self.stack.last().unwrap() {
                WorkItem::Open(open) if can_open => {
                    open.openat_os(entry.file_name(), &mut self.stats)
                        .map_err(Error::from_io)
                        .map(WorkItem::Open)?
                }
                WorkItem::Open(open) => {
                    if self.config.contents_first {
                        // TODO: close and open the actual next.
                    } else {
                        // TODO: add the sub directory as a closed one.
                    }

                    todo!()
                }
                WorkItem::Closed(closed) => {
                    assert!(can_open, "No more budget but only closed work items");
                    closed.open(entry, &mut self.stats)
                        .map_err(Error::from_io)
                        .map(WorkItem::Open)?
                }
            };

            if !self.config.contents_first {
                mem::swap(&mut next, self.stack.last_mut().unwrap());
            }

            self.stack.push(next);
        }

        Ok({})
    }
}

impl IntoIterator for WalkDir {
    type IntoIter = IntoIter;
    type Item = Result<DirEntry, Error>;
    fn into_iter(self) -> IntoIter {
        WalkDir::build(self)
    }
}

impl Iterator for IntoIter {
    type Item = Result<DirEntry, Error>;
    fn next(&mut self) -> Option<Self::Item> {
        let mut current = self.stack.last_mut()?;

        // First try to get an item that is ripe for reaping.
        let mut found = match &mut current {
            WorkItem::Open(open) => match open.ready_entry() {
                Some(entry) => entry,
                // No more items, try refilling.
                None => {
                    match open.fill_buffer(&mut self.stats) {
                        Err(err) => todo!(),
                        Ok(More::More) => return self.next(),
                        Ok(More::Blocked) => unreachable!("Empty buffer blocked"),
                        Ok(More::Done) => {
                            let _ = self.stack.pop();
                            return self.next();
                        }
                    }
                },
            }
            WorkItem::Closed(closed) => match closed.ready_entry() {
                Some(entry) => entry,
                None => {
                    // Nothing to do, try the next entry.
                    let _ = self.stack.pop();
                    return self.next();
                }
            }
        };

        Some(self.iter_entry(&mut found).map(|_| found))
    }
}

// Private implementation items.

impl Open {
}

impl Error {
    fn new() -> Self {
        Error { _private: () }
    }

    pub fn path(&self) -> Option<&Path> {
        todo!()
    }

    pub fn loop_ancestor(&self) -> Option<&Path> {
        todo!()
    }

    pub fn depth(&self) -> usize {
        todo!()
    }

    pub fn io_error(&self) -> Option<&std::io::Error> {
        todo!()
    }

    pub fn into_io_error(&self) -> Option<std::io::Error> {
        todo!()
    }

    fn from_io(_: io::Error) -> Self {
        Error::new()
    }
}

impl<P> Iterator for FilterEntry<IntoIter, P> {
    type Item = Result<DirEntry, Error>;
    fn next(&mut self) -> Option<Self::Item> {
        unimplemented!()
    }
}