ic-host-fs 0.30.1

Local host filesystem reads, durable publication, file locking and a bounded file CLI
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
//! Held Unix directory capabilities, adapted from Query's confined file backend.
//!
//! Paths are selected without following symlink components. Handles retain the
//! selected directories after renames; they do not prove current pathname
//! containment, immutable contents or exclusive namespace custody. Callers own
//! those obligations, permission settings, byte budgets, schemas and recovery.
//! The cap-* backend is private. Regular-file opening and publication compose
//! with the existing Host descriptor engines rather than introducing new ones.

mod entries;
mod error;

pub use entries::{DirectoryEntries, DirectoryEntry, EntryKind};
pub use error::{CapabilityError, Operation, Refusal, RemovalError};

use crate::read::{OpenEntryError, OpenedEntry, open_entry_beneath};
use cap_fs_ext::{DirExt, FollowSymlinks, OpenOptionsFollowExt};
use cap_std::fs::{Dir, DirBuilder, DirBuilderExt, OpenOptions, OpenOptionsExt, PermissionsExt};
use std::{
    ffi::{OsStr, OsString},
    fs::File,
    io,
    os::fd::{AsFd, BorrowedFd},
    path::{Component, Path, PathBuf},
};

/// Explicit directory creation and admission settings; there are no defaults.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DirectoryOptions {
    /// `Some(mode)` permits missing directory creation, subject to umask.
    /// `None` observes absence without creating anything.
    pub create_permissions: Option<u32>,
    /// Existing and created selected directories must omit these low nine bits.
    /// Zero permits all ordinary permission bits. Existing modes are never repaired.
    pub forbidden_permissions: u32,
}

/// A held directory capability; dropping it never removes filesystem entries.
#[derive(Debug)]
pub struct Directory {
    inner: Dir,
    display: PathBuf,
}

impl Directory {
    /// Open a root without following any component, optionally creating parents.
    ///
    /// Relative inputs use the current directory through `std::path::absolute`.
    /// Parent traversal is refused before normalization. Existing ancestors above
    /// the selected root are not subjected to its permission mask; newly created
    /// ancestors and the final root are. Each creation re-admits the child after
    /// an `AlreadyExists` race and synchronizes its held parent after admission.
    ///
    /// # Errors
    /// Returns structural/permission refusals and original IO failures. Errors
    /// after creation can leave visible directories; callers own reconciliation.
    pub fn open_root(
        path: &Path,
        options: DirectoryOptions,
    ) -> Result<Option<Self>, CapabilityError> {
        validate_options(path, options)?;
        if path
            .components()
            .any(|part| matches!(part, Component::ParentDir | Component::Prefix(_)))
        {
            return Err(refused(path, Refusal::InvalidPath));
        }
        let absolute =
            std::path::absolute(path).map_err(|error| failure(Operation::Open, path, error))?;
        let mut directory = Self {
            inner: Dir::open_ambient_dir(Path::new("/"), cap_std::ambient_authority())
                .map_err(|error| failure(Operation::Open, path, error))?,
            display: PathBuf::from("/"),
        };
        for component in absolute.components() {
            if let Component::Normal(name) = component {
                let Some(child) = directory.component(name, options)? else {
                    return Ok(None);
                };
                directory = child;
            }
        }
        directory.display = path.to_path_buf();
        directory.admit_permissions(options.forbidden_permissions)?;
        Ok(Some(directory))
    }

    /// Clone the held capability, without re-resolving its original pathname.
    ///
    /// # Errors
    /// Returns the original descriptor duplication failure.
    pub fn try_clone(&self) -> Result<Self, CapabilityError> {
        Ok(Self {
            inner: self
                .inner
                .try_clone()
                .map_err(|error| failure(Operation::Open, &self.display, error))?,
            display: self.display.clone(),
        })
    }

    /// Select a normal root-relative file name and retain its admitted parent.
    ///
    /// Every traversed directory is checked against the caller's mask. Missing
    /// parents are created only when explicitly selected. The final file itself
    /// is neither opened nor created. Cache path construction stays with callers.
    ///
    /// # Errors
    /// Refuses empty/absolute/parent paths, links, types and permission violations;
    /// native failures retain the selected operation and original cause.
    pub fn resolve_parent(
        &self,
        relative: &Path,
        options: DirectoryOptions,
    ) -> Result<Option<FileEntry>, CapabilityError> {
        validate_options(relative, options)?;
        let mut parts = relative.components().peekable();
        if parts.peek().is_none()
            || parts
                .clone()
                .any(|part| !matches!(part, Component::Normal(_)))
        {
            return Err(refused(relative, Refusal::InvalidPath));
        }
        let mut parent = self.try_clone()?;
        while let Some(Component::Normal(name)) = parts.next() {
            if parts.peek().is_none() {
                return Ok(Some(FileEntry {
                    parent,
                    name: name.to_os_string(),
                }));
            }
            let Some(child) = parent.component(name, options)? else {
                return Ok(None);
            };
            child.admit_permissions(options.forbidden_permissions)?;
            parent = child;
        }
        unreachable!("validated nonempty normal path")
    }

    /// Open/admit one directory component with explicit creation permissions.
    ///
    /// # Errors
    /// Returns the same structural, permission and native failures as traversal.
    pub fn open_directory(
        &self,
        name: &OsStr,
        options: DirectoryOptions,
    ) -> Result<Option<Self>, CapabilityError> {
        validate_name(name)?;
        validate_options(Path::new(name), options)?;
        let directory = self.component(name, options)?;
        if let Some(directory) = &directory {
            directory.admit_permissions(options.forbidden_permissions)?;
        }
        Ok(directory)
    }

    /// Observe low nine permission bits through the retained directory handle.
    ///
    /// # Errors
    /// Returns the original metadata failure.
    pub fn permissions(&self) -> Result<u32, CapabilityError> {
        self.inner
            .dir_metadata()
            .map(|metadata| metadata.permissions().mode() & 0o777)
            .map_err(|error| failure(Operation::Metadata, &self.display, error))
    }

    /// Open a readable, syncable descriptor relative to this same capability.
    ///
    /// Some cap-* handles use `O_PATH`. Use this result with existing Host durable
    /// writers rather than re-opening a display path or fsyncing an `O_PATH` handle.
    ///
    /// # Errors
    /// Returns the original opening failure.
    pub fn open_syncable(&self) -> Result<File, CapabilityError> {
        self.inner
            .open(Path::new("."))
            .map(cap_std::fs::File::into_std)
            .map_err(|error| failure(Operation::Open, &self.display, error))
    }

    /// Synchronize this held directory, without re-selecting its pathname.
    ///
    /// # Errors
    /// Returns the original opening or sync failure under the sync operation.
    pub fn sync(&self) -> Result<(), CapabilityError> {
        self.inner
            .open(Path::new("."))
            .and_then(|directory| directory.sync_all())
            .map_err(|error| failure(Operation::SyncDirectory, &self.display, error))
    }

    /// Lazily observe at most `maximum` entries, including nonmatching entries.
    ///
    /// The iterator reports another observed entry as `EntryLimit`, without
    /// interpreting candidate selection, sorting, recursion or truncation policy.
    ///
    /// # Errors
    /// Returns the original directory-open failure.
    pub fn entries(&self, maximum: usize) -> Result<DirectoryEntries, CapabilityError> {
        let entries = self
            .inner
            .entries()
            .map_err(|error| failure(Operation::ReadDirectory, &self.display, error))?;
        Ok(DirectoryEntries::new(
            entries,
            self.display.clone(),
            maximum,
        ))
    }

    fn component(
        &self,
        name: &OsStr,
        options: DirectoryOptions,
    ) -> Result<Option<Self>, CapabilityError> {
        if let Some(directory) = self.open_component(name)? {
            return Ok(Some(directory));
        }
        let Some(mode) = options.create_permissions else {
            return Ok(None);
        };
        self.create_component(name, mode, options.forbidden_permissions)
            .map(Some)
    }

    fn create_component(
        &self,
        name: &OsStr,
        mode: u32,
        forbidden: u32,
    ) -> Result<Self, CapabilityError> {
        let path = self.display.join(name);
        let mut builder = DirBuilder::new();
        builder.mode(mode);
        match self.inner.create_dir_with(name, &builder) {
            Ok(()) => {}
            Err(error) if error.kind() == io::ErrorKind::AlreadyExists => {}
            Err(error) => return Err(failure(Operation::CreateDirectory, &path, error)),
        }
        let directory = self.open_component(name)?.ok_or_else(|| {
            failure(
                Operation::Open,
                &path,
                io::Error::new(io::ErrorKind::NotFound, "created directory disappeared"),
            )
        })?;
        directory.admit_permissions(forbidden)?;
        self.sync()?;
        Ok(directory)
    }

    fn open_component(&self, name: &OsStr) -> Result<Option<Self>, CapabilityError> {
        let path = self.display.join(name);
        match self.inner.open_dir_nofollow(name) {
            Ok(inner) => Ok(Some(Self {
                inner,
                display: path,
            })),
            Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(None),
            Err(error) => match self.inner.symlink_metadata(name) {
                Ok(metadata) if metadata.is_symlink() => Err(refused(&path, Refusal::SymbolicLink)),
                Ok(metadata) if !metadata.is_dir() => Err(refused(&path, Refusal::NotDirectory)),
                _ => Err(failure(Operation::Open, &path, error)),
            },
        }
    }

    fn admit_permissions(&self, forbidden: u32) -> Result<(), CapabilityError> {
        let actual = self.permissions()?;
        if actual & forbidden != 0 {
            return Err(CapabilityError::Permissions {
                path: self.display.clone(),
                actual,
                forbidden,
            });
        }
        Ok(())
    }
}

impl AsFd for Directory {
    fn as_fd(&self) -> BorrowedFd<'_> {
        self.inner.as_fd()
    }
}

/// One normal file name and its retained parent; selection creates nothing.
#[derive(Debug)]
pub struct FileEntry {
    parent: Directory,
    name: OsString,
}

impl FileEntry {
    /// Borrow the selected parent capability.
    #[must_use]
    pub const fn parent(&self) -> &Directory {
        &self.parent
    }

    /// Borrow the final native name, without conversion to Unicode.
    #[must_use]
    pub fn file_name(&self) -> &OsStr {
        &self.name
    }

    /// Open a regular file without blocking on a raced-in FIFO.
    ///
    /// Missing files return None. Permission admission and bounded reading remain
    /// explicit caller steps over the returned standard file descriptor.
    ///
    /// # Errors
    /// Rejects observed links/special files and retains native opening failures.
    pub fn open_regular_file(&self) -> Result<Option<File>, CapabilityError> {
        let path = self.parent.display.join(&self.name);
        match self.parent.inner.symlink_metadata(&self.name) {
            Ok(metadata) if metadata.is_symlink() => {
                return Err(refused(&path, Refusal::SymbolicLink));
            }
            Ok(metadata) if !metadata.is_file() => {
                return Err(refused(&path, Refusal::NotRegularFile));
            }
            Ok(_) => {}
            Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(None),
            Err(error) => return Err(failure(Operation::Metadata, &path, error)),
        }
        match open_entry_beneath(self.parent.as_fd(), Path::new(&self.name)) {
            Ok(OpenedEntry::RegularFile(file)) => Ok(Some(file)),
            Ok(OpenedEntry::Directory(_)) | Err(OpenEntryError::UnsupportedEntry { .. }) => {
                Err(refused(&path, Refusal::NotRegularFile))
            }
            Err(OpenEntryError::Io(error)) => Err(failure(Operation::Open, &path, error)),
        }
    }

    /// Exclusively create one read/write regular file with caller-selected mode.
    ///
    /// Existing files and links return the original `AlreadyExists` failure.
    /// No file or directory synchronization is implicit in this creation step.
    ///
    /// # Errors
    /// Returns invalid permissions or the original exclusive-create failure.
    pub fn create_new_file(&self, permissions: u32) -> Result<File, CapabilityError> {
        let path = self.parent.display.join(&self.name);
        validate_options(
            &path,
            DirectoryOptions {
                create_permissions: Some(permissions),
                forbidden_permissions: 0,
            },
        )?;
        let mut options = OpenOptions::new();
        options
            .read(true)
            .write(true)
            .create_new(true)
            .follow(FollowSymlinks::No)
            .mode(permissions);
        self.parent
            .inner
            .open_with(&self.name, &options)
            .map(cap_std::fs::File::into_std)
            .map_err(|error| failure(Operation::CreateFile, &path, error))
    }

    /// Unlink this name and synchronize the same held parent.
    ///
    /// Callers must first admit the permitted leaf type/mode and retain namespace
    /// custody. A replacement race is not an identity-conditional unlink. Links
    /// are never followed, but an intervening link can itself be unlinked. Missing
    /// entries return false; no recursive removal or recovery is performed.
    ///
    /// # Errors
    /// `removed` remains true if unlink completed before parent synchronization failed.
    pub fn remove_file_and_sync(&self) -> Result<bool, RemovalError> {
        self.remove_file_and_sync_with(Directory::sync)
    }

    fn remove_file_and_sync_with(
        &self,
        sync: impl FnOnce(&Directory) -> Result<(), CapabilityError>,
    ) -> Result<bool, RemovalError> {
        match self.parent.inner.remove_file(&self.name) {
            Ok(()) => {}
            Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(false),
            Err(error) => {
                return Err(RemovalError {
                    removed: false,
                    source: failure(
                        Operation::RemoveFile,
                        &self.parent.display.join(&self.name),
                        error,
                    ),
                });
            }
        }
        sync(&self.parent).map_err(|source| RemovalError {
            removed: true,
            source,
        })?;
        Ok(true)
    }
}

fn validate_name(name: &OsStr) -> Result<(), CapabilityError> {
    let mut parts = Path::new(name).components();
    if !matches!((parts.next(), parts.next()), (Some(Component::Normal(part)), None) if part == name)
    {
        return Err(refused(Path::new(name), Refusal::InvalidPath));
    }
    Ok(())
}

fn validate_options(path: &Path, options: DirectoryOptions) -> Result<(), CapabilityError> {
    if options.forbidden_permissions & !0o777 != 0
        || options
            .create_permissions
            .is_some_and(|mode| mode & !0o777 != 0)
    {
        return Err(failure(
            Operation::Open,
            path,
            io::Error::new(
                io::ErrorKind::InvalidInput,
                "permissions must fit the low nine bits",
            ),
        ));
    }
    Ok(())
}

fn failure(operation: Operation, path: &Path, source: io::Error) -> CapabilityError {
    CapabilityError::Io {
        operation,
        path: path.to_path_buf(),
        source,
    }
}

fn refused(path: &Path, reason: Refusal) -> CapabilityError {
    CapabilityError::Refused {
        path: path.to_path_buf(),
        reason,
    }
}

#[cfg(test)]
mod tests;