d-major 0.0.0

Traverse directory trees in parallel, using relative entries to minimize allocation and maximize parallelism.
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
/*
 * Description: Wrap POSIX OS resources.
 * This code is largely copied from the rust compiler's sys/fs/unix.rs: https://github.com/rust-lang/rust/blob/cbfdf0b014cb04982a9cbeec1578001001167f6e/library/std/src/sys/fs/unix.rs.
 * The rust compiler is distributed under both the MIT and Apache v2 licenses.
 *
 * Copyright (C) 2025 d@nny mc² <dmc2@hypnicjerk.ai>
 * SPDX-License-Identifier: LGPL-3.0-or-later
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#![cfg(unix)]

//! Wrap POSIX OS resources.
//!
//! This code is largely copied from the rust compiler's [`sys/fs/unix.rs`](https://github.com/rust-lang/rust/blob/cbfdf0b014cb04982a9cbeec1578001001167f6e/library/std/src/sys/fs/unix.rs).

use std::{io, iter, marker::PhantomData, mem::MaybeUninit, ops, os::fd, path::Path};

use parking_lot::{Mutex, MutexGuard};

use crate::{
  libc_imports,
  null_term_str::{NullTermStr, NullTermString},
};


#[repr(transparent)]
pub struct Dir(*mut libc::DIR);

unsafe impl Send for Dir {}
unsafe impl Sync for Dir {}

impl Dir {
  pub fn opendir(path: &Path) -> io::Result<Self> {
    crate::c_string::run_path_with_cstr(path, |p| {
      let dir_ptr = unsafe { libc::opendir(p.as_ptr()) };
      if dir_ptr.is_null() {
        Err(io::Error::last_os_error())
      } else {
        Ok(Self(dir_ptr))
      }
    })
  }

  fn do_readdir(
    &mut self,
  ) -> Result<Option<(NullTermStr<'_>, libc_imports::dirent64_min)>, io::Error> {
    // As of POSIX.1-2017, readdir() is not required to be thread safe; only
    // readdir_r() is. However, readdir_r() cannot correctly handle platforms
    // with unlimited or variable NAME_MAX. Many modern platforms guarantee
    // thread safety for readdir() as long an individual DIR* is not accessed
    // concurrently, which is sufficient for Rust.

    // To distinguish between errors and end-of-directory, we had to clear
    // errno beforehand and check for an error afterwards.
    let entry_ptr: *const libc_imports::dirent64 =
      crate::errno::with_cleared_errno(|| unsafe { libc_imports::readdir64(self.0) })?;
    /* End of directory, with no io error. */
    if entry_ptr.is_null() {
      return Ok(None);
    }
    debug_assert!(crate::errno::errno_is_unset());

    // The dirent64 struct is a weird imaginary thing that isn't ever supposed
    // to be worked with by value. Its trailing d_name field is declared
    // variously as [c_char; 256] or [c_char; 1] on different systems but
    // either way that size is meaningless; only the offset of d_name is
    // meaningful. The dirent64 pointers that libc returns from readdir64 are
    // allowed to point to allocations smaller _or_ LARGER than implied by the
    // definition of the struct.
    //
    // As such, we need to be even more careful with dirent64 than if its
    // contents were "simply" partially initialized data.
    //
    // Like for uninitialized contents, converting entry_ptr to `&dirent64`
    // would not be legal. However, we can use `&raw const (*entry_ptr).d_name`
    // to refer the fields individually, because that operation is equivalent
    // to `byte_offset` and thus does not require the full extent of `*entry_ptr`
    // to be in bounds of the same allocation, only the offset of the field
    // being referenced.

    // d_name is guaranteed to be null-terminated.
    let name = unsafe { NullTermStr::from_ptr((&raw const (*entry_ptr).d_name).cast()) };

    let entry = unsafe { libc_imports::dirent64_min::new(entry_ptr) };

    Ok(Some((name, entry)))
  }
}

impl ops::Drop for Dir {
  fn drop(&mut self) {
    let r = unsafe { libc::closedir(self.0) };
    if r != 0 {
      let err = io::Error::last_os_error();
      match err.kind() {
        io::ErrorKind::Interrupted => (),
        _ => panic!("unexpected error during closedir: {:?}", err),
      }
    }
  }
}


#[repr(u8)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum EagerFileType {
  File = libc::DT_REG,
  Dir = libc::DT_DIR,
  Symlink = libc::DT_LNK,
  Other(libc::c_uchar),
}

impl EagerFileType {
  pub(crate) const fn from_type(d_type: libc::c_uchar) -> Self {
    match d_type {
      libc::DT_REG => Self::File,
      libc::DT_DIR => Self::Dir,
      libc::DT_LNK => Self::Symlink,
      x => Self::Other(x),
    }
  }
}


pub mod traits {
  use std::io;

  use super::{Dir, EagerFileType};
  use crate::{
    libc_imports,
    null_term_str::{MeasuredNullTermStr, NullTermStr, NullTermString},
  };

  pub trait ReaddirErr {
    fn into_err(self) -> io::Error;
  }

  pub trait ReaddirInfo {}

  pub trait HasInode: ReaddirInfo {
    fn ino(&self) -> libc_imports::ino64_t;
  }

  pub trait HasEagerType: ReaddirInfo {
    fn eager_file_type(&self) -> EagerFileType;
  }
  pub trait HasNoEagerType: ReaddirInfo {}

  pub trait HasName: ReaddirInfo {
    fn name(&self) -> NullTermStr<'_>;
  }
  pub trait HasMeasuredName: HasName {
    fn measured_name(&self) -> MeasuredNullTermStr<'_>;
  }

  pub trait ReaddirResult<'d> {
    fn readdir(dir: &'d mut Dir) -> Self
    where Self: Sized+'d;

    type Info: ReaddirInfo;
    type Err: ReaddirErr;
    fn into_result(self) -> Result<Option<Self::Info>, Self::Err>;

    type OwnInfo: ReaddirInfo;
    fn into_owned(info: Self::Info, args: NullTermString) -> Self::OwnInfo;
  }
}


/// One particular implementation of [`traits`] which uses the most general POSIX interface.
///
/// These structs retain a reference to the [`Dir`] object, which is not necessary for platforms
/// that can use the `readdir_r()` method and impose a maximum size on filename length. The rust
/// stdlib uses this approach for several BSDs and macos.
pub mod result {
  use std::io;

  use super::{traits, Dir, EagerFileType};
  use crate::{
    libc_imports,
    null_term_str::{AsNullTermStr, MeasuredNullTermStr, NullTermStr, NullTermString},
  };

  #[repr(transparent)]
  pub struct ReaddirErr(io::Error);

  impl ReaddirErr {
    pub fn into_err(self) -> io::Error { self.0 }
  }

  impl traits::ReaddirErr for ReaddirErr {
    fn into_err(self) -> io::Error { Self::into_err(self) }
  }

  #[derive(Copy, Clone, Debug)]
  pub struct ReaddirInfo<'d, Info> {
    name: NullTermStr<'d>,
    info: Info,
  }

  impl<'d, Info> ReaddirInfo<'d, Info> {
    pub fn ino(&self) -> libc_imports::ino64_t
    where Info: libc_imports::HasInode {
      self.info.ino()
    }

    pub fn eager_file_type(&self) -> EagerFileType
    where Info: libc_imports::HasEagerType {
      EagerFileType::from_type(self.info.eager_file_type())
    }

    pub const fn name(&self) -> NullTermStr<'d> { self.name }

    pub fn measured_name(&self) -> MeasuredNullTermStr<'d>
    where Info: libc_imports::MeasureNameLen {
      self.info.measure_name_len(self.name())
    }
  }

  impl<Info> traits::ReaddirInfo for ReaddirInfo<'_, Info> {}

  impl<Info> traits::HasInode for ReaddirInfo<'_, Info>
  where Info: libc_imports::HasInode
  {
    fn ino(&self) -> libc_imports::ino64_t { Self::ino(self) }
  }

  impl<Info> traits::HasEagerType for ReaddirInfo<'_, Info>
  where Info: libc_imports::HasEagerType
  {
    fn eager_file_type(&self) -> EagerFileType { Self::eager_file_type(self) }
  }
  impl<Info> traits::HasNoEagerType for ReaddirInfo<'_, Info> where Info: libc_imports::HasNoEagerType {}

  impl<'d, Info> traits::HasName for ReaddirInfo<'d, Info> {
    fn name(&self) -> NullTermStr<'_> { Self::name(self) }
  }

  impl<'d, Info> traits::HasMeasuredName for ReaddirInfo<'d, Info>
  where Info: libc_imports::MeasureNameLen
  {
    fn measured_name(&self) -> MeasuredNullTermStr<'_> { Self::measured_name(self) }
  }

  pub struct OwnedInfo<Info> {
    name: NullTermString,
    info: Info,
  }

  impl<Info> OwnedInfo<Info> {
    pub fn from_info(info: ReaddirInfo<'_, Info>, mut s: NullTermString) -> Self
    where Info: libc_imports::MeasureNameLen {
      info.measured_name().clone_into(&mut s);
      Self {
        name: s,
        info: info.info,
      }
    }

    pub fn ino(&self) -> libc_imports::ino64_t
    where Info: libc_imports::HasInode {
      self.info.ino()
    }

    pub fn eager_file_type(&self) -> EagerFileType
    where Info: libc_imports::HasEagerType {
      EagerFileType::from_type(self.info.eager_file_type())
    }

    pub fn name(&self) -> NullTermStr<'_> { self.measured_name().as_unmeasured() }

    pub fn measured_name(&self) -> MeasuredNullTermStr<'_> { self.name.as_null_term_str() }
  }
  impl<Info> traits::ReaddirInfo for OwnedInfo<Info> {}
  impl<Info> traits::HasInode for OwnedInfo<Info>
  where Info: libc_imports::HasInode
  {
    fn ino(&self) -> libc_imports::ino64_t { Self::ino(self) }
  }
  impl<Info> traits::HasEagerType for OwnedInfo<Info>
  where Info: libc_imports::HasEagerType
  {
    fn eager_file_type(&self) -> EagerFileType { Self::eager_file_type(self) }
  }
  impl<Info> traits::HasNoEagerType for OwnedInfo<Info> where Info: libc_imports::HasNoEagerType {}
  impl<Info> traits::HasName for OwnedInfo<Info> {
    fn name(&self) -> NullTermStr<'_> { Self::name(self) }
  }
  impl<Info> traits::HasMeasuredName for OwnedInfo<Info> {
    fn measured_name(&self) -> MeasuredNullTermStr<'_> { Self::measured_name(self) }
  }

  #[repr(transparent)]
  pub struct ReaddirResult<'d>(
    Result<Option<(NullTermStr<'d>, libc_imports::dirent64_min)>, io::Error>,
  );

  impl<'d> ReaddirResult<'d> {
    pub fn readdir(dir: &'d mut Dir) -> Self { Self(dir.do_readdir()) }

    pub fn into_result(
      self,
    ) -> Result<Option<ReaddirInfo<'d, libc_imports::dirent64_min>>, ReaddirErr> {
      match self {
        Self(Err(e)) => Err(ReaddirErr(e)),
        Self(Ok(v)) => Ok(v.map(|(name, info)| ReaddirInfo { name, info })),
      }
    }
  }

  impl<'d> traits::ReaddirResult<'d> for ReaddirResult<'d> {
    fn readdir(dir: &'d mut Dir) -> Self
    where Self: Sized+'d {
      Self::readdir(dir)
    }

    type Info = ReaddirInfo<'d, libc_imports::dirent64_min>;
    type Err = ReaddirErr;
    fn into_result(self) -> Result<Option<Self::Info>, Self::Err> { Self::into_result(self) }

    type OwnInfo = OwnedInfo<libc_imports::dirent64_min>;
    fn into_owned(info: Self::Info, args: NullTermString) -> Self::OwnInfo {
      OwnedInfo::from_info(info, args)
    }
  }

  #[cfg(test)]
  mod test {
    use std::{fs, io};

    use tempdir::TempDir;

    use super::{super::*, *};

    #[test]
    fn read_single() -> io::Result<()> {
      let td = TempDir::new("asdf")?;
      fs::write(td.path().join("f.txt"), "asdf\n")?;

      let mut dir = Dir::opendir(td.path())?;

      let d = ReaddirResult::readdir(&mut dir)
        .into_result()
        .map_err(|e| e.into_err())?
        .unwrap();
      assert_eq!(d.measured_name().as_os_str().to_str().unwrap(), ".");

      let d = ReaddirResult::readdir(&mut dir)
        .into_result()
        .map_err(|e| e.into_err())?
        .unwrap();
      assert_eq!(d.measured_name().as_os_str().to_str().unwrap(), "..");

      let f = ReaddirResult::readdir(&mut dir)
        .into_result()
        .map_err(|e| e.into_err())?
        .unwrap();
      assert_eq!(f.measured_name().as_os_str().to_str().unwrap(), "f.txt");

      assert!(ReaddirResult::readdir(&mut dir)
        .into_result()
        .map_err(|e| e.into_err())?
        .is_none());


      Ok(())
    }
  }
}


pub struct DirCollector<R> {
  dir: Dir,
  _ph: PhantomData<R>,
  done: bool,
}

impl<R> DirCollector<R> {
  pub const fn new(dir: Dir) -> Self {
    Self {
      dir,
      _ph: PhantomData,
      done: false,
    }
  }

  pub const fn is_done(&self) -> bool { self.done }
}

impl<R> Iterator for DirCollector<R>
where
  for<'e> R: traits::ReaddirResult<'e>,
  for<'e> <R as traits::ReaddirResult<'e>>::Info: traits::HasName,
{
  type Item = Result<<R as traits::ReaddirResult<'static>>::OwnInfo, io::Error>;

  fn next(&mut self) -> Option<Self::Item> {
    if self.done {
      return None;
    }

    loop {
      use traits::{HasName, ReaddirErr};

      let Some(result) = R::readdir(&mut self.dir).into_result().transpose() else {
        self.done = true;
        return None;
      };
      return Some(match result {
        Err(e) => Err(e.into_err()),
        Ok(info) => {
          /* If it matches '.' or '..', pretend it's not there. */
          if info.name().match_dir_entries_unmeasured() {
            continue;
          }
          /* TODO: pool allocations for strings? */
          let owned = R::into_owned(info, NullTermString::new());
          Ok(owned)
        },
      });
    }
  }
}

impl<R> iter::FusedIterator for DirCollector<R>
where
  for<'e> R: traits::ReaddirResult<'e>,
  for<'e> <R as traits::ReaddirResult<'e>>::Info: traits::HasName,
{
}


/// Pair a [`Dir`] struct with a file descriptor, usable for dir-relative operations like
/// [`Self::openat()`] and [`Self::fstatat()`].
///
/// # Lifetimes and Mutability
/// We want to achieve several things at once here:
/// 1. The [`DirCollector`] (with a **mutable** handle to the [`Dir`] struct) can be iterated *in
///    parallel* with the `OwnInfo` entries it has already spawned.
/// 2. Each entry has a handle to the file descriptor, which can be sent to another thread and
///    operated upon indepdendently of the `readdir()` iteration.
/// 3. This [`DirFd`] struct (and the internal [`Dir`] instance it owns) is not dropped until all
///    entries have been processed.
/// 4. We want to be able to implement some form of backpressure in order to limit the number of
///    open file handles across the crawl.
///
/// This likely involves some dependency on the end-user API in [`crate::crawl`], as the user will
/// define when the handle should be dropped, especially given (4) (limiting open file handles).
///
/// In [`crate::crawl`], we define an "end-user API", but which we ourselves will be using in order
/// to limit open file handles. We would *like* to avoid explicitly sending entries to threads yet,
/// but this may be a false hope--if we want to implement resource management like limiting open
/// file handles, and we *also* need to e.g. invoke [`Self::fstatat()`] for platforms which do not
/// provide [`EagerFileType`], we will probably need to architect resource management and then
/// expose a separate external end-user API from [`crate::crawl`].
///
/// I *think* we can use an [`UnsafeCell`](std::cell::UnsafeCell) for this, but we will need to be
/// very careful to demarcate iteration vs dereferencing the fd. We would *like* to use
/// [`Weak`](std::sync::Weak) pointers, but we do actually need each entry to keep this [`DirFd`]
/// alive until all have been processed.
///
/// ## Proposal: Global Registry
/// I think this implies that [`DirFd`] instances should be kept within some global registry, and
/// explicitly dropped when we are *positive* all their entries have been processed.
pub struct DirFd<R> {
  dir: Mutex<DirCollector<R>>,
  fd: fd::RawFd,
}

impl<R> DirFd<R> {
  const fn new(dir: Dir, fd: fd::RawFd) -> Self {
    Self {
      dir: Mutex::new(DirCollector::new(dir)),
      fd,
    }
  }

  #[allow(clippy::self_named_constructors)]
  pub fn dirfd(dir: Dir) -> io::Result<Self> {
    let fd = unsafe { libc::dirfd(dir.0) };
    if fd == -1 {
      Err(io::Error::last_os_error())
    } else {
      Ok(Self::new(dir, fd))
    }
  }

  pub fn fdopendir(fd: fd::RawFd) -> io::Result<Self> {
    let dir_ptr = unsafe { libc::fdopendir(fd) };
    if dir_ptr.is_null() {
      Err(io::Error::last_os_error())
    } else {
      let dir = Dir(dir_ptr);
      Ok(Self::new(dir, fd))
    }
  }

  fn lock_dir(&self) -> MutexGuard<'_, DirCollector<R>> { self.dir.lock() }

  pub fn dir(
    &self,
  ) -> impl ops::DerefMut<
    Target=impl Iterator<Item=Result<<R as traits::ReaddirResult<'static>>::OwnInfo, io::Error>>+iter::FusedIterator,
  >
  where
    for<'e> R: traits::ReaddirResult<'e>,
    for<'e> <R as traits::ReaddirResult<'e>>::Info: traits::HasName,
  {
    self.lock_dir()
  }

  pub const fn fd(&self) -> fd::RawFd { self.fd }

  pub fn openat(&self, name: NullTermStr<'_>) -> io::Result<fd::RawFd> {
    debug_assert!(
      !name.match_dir_entries_unmeasured(),
      "can't open dir entries '.' or '..'"
    );
    /* FIXME: allow the user to do whatever they want with the file, not just open read-only! */
    /* FIXME: open directories separately from files! */
    /* FIXME: determine whether to follow symlinks! */
    let fd = unsafe { libc_imports::openat64(self.fd, name.as_ptr(), libc::O_RDONLY) };
    if fd == -1 {
      Err(io::Error::last_os_error())
    } else {
      Ok(fd)
    }
  }

  pub fn fstatat(&self, name: NullTermStr<'_>) -> io::Result<libc_imports::stat64> {
    debug_assert!(
      !name.match_dir_entries_unmeasured(),
      "can't stat dir entries '.' or '..'"
    );
    let mut stat = MaybeUninit::<libc_imports::stat64>::uninit();
    let rc = unsafe {
      libc_imports::fstatat64(
        self.fd,
        name.as_ptr(),
        stat.as_mut_ptr(),
        /* FIXME: determine whether to follow symlinks! */
        libc::AT_SYMLINK_NOFOLLOW,
      )
    };
    if rc == -1 {
      Err(io::Error::last_os_error())
    } else {
      debug_assert_eq!(rc, 0);
      let stat = unsafe { stat.assume_init() };
      Ok(stat)
    }
  }
}

/// Check if a file descriptor is not open.
///
/// Many IO syscalls can't be fully trusted about EBADF error codes because those
/// might get bubbled up from a remote FUSE server rather than the file descriptor
/// in the current process being invalid.
///
/// So we check file flags instead which live on the file descriptor and not the underlying file.
/// The downside is that it costs an extra syscall, so we only do it for debug.
#[inline]
fn fd_is_invalid(fd: fd::RawFd) -> bool {
  (unsafe { libc::fcntl(fd, libc::F_GETFD) } == -1) && crate::errno::errno() == libc::EBADF
}

impl<R> ops::Drop for DirFd<R> {
  fn drop(&mut self) {
    debug_assert!(!fd_is_invalid(self.fd));
  }
}