Skip to main content

ostrya_core/
mode.rs

1//! Repository storage modes.
2
3/// The on-disk storage mode of a repository.
4///
5/// The mode decides how a repository stores a [`File`] object and which
6/// loose-path extension the object gets. The mode strings are the tokens of
7/// `[core] mode=` in `config`. The `ostree` command writes each of them except
8/// `bare-user-shared` and `bare-user-private`, the ostrya extensions.
9///
10/// [`File`]: crate::ObjectType::File
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12pub enum RepoMode {
13    /// Real files, with the uid, gid, mode, and xattrs on the inode.
14    Bare,
15    /// Files with the metadata in the `user.ostreemeta` xattr.
16    ///
17    /// A process with no privileges can write a repository of this mode.
18    BareUser,
19    /// Files with no xattr metadata.
20    ///
21    /// This mode drops the uid and the gid. The inode holds the canonical mode.
22    BareUserOnly,
23    /// Storage as in [`Bare`](Self::Bare), with the xattrs in separate
24    /// `.file-xattrs` objects and none on the inode.
25    BareSplitXattrs,
26    /// File content in `.filez` objects, compressed with raw DEFLATE.
27    ///
28    /// An HTTP server can serve a repository of this mode.
29    Archive,
30    /// Storage as in [`BareUser`](Self::BareUser), for a repository that a
31    /// group shares.
32    ///
33    /// This mode never applies the logical mode to the inode. It is an ostrya
34    /// extension for development only.
35    BareUserShared,
36    /// Storage as in [`BareUser`](Self::BareUser), with the inode mode 0400 on
37    /// each content object. Metadata objects keep 0644.
38    ///
39    /// The bytes of an object, its `user.ostreemeta` xattr, and each checksum
40    /// are the same as in [`BareUser`](Self::BareUser). A symlink object is a
41    /// regular file, and it also gets the inode mode 0400. A process that reads
42    /// a content object must be the owner of the object or have privilege. A
43    /// checkout copies or reflinks each regular file.
44    ///
45    /// This mode is an ostrya extension, for a host that mounts its
46    /// deployments with composefs. The `ostree` command refuses to open a
47    /// repository of this mode.
48    BareUserPrivate,
49}
50
51impl RepoMode {
52    /// Parses a `[core] mode=` string.
53    ///
54    /// Returns `None` for an unknown string. `archive` is an accepted alias
55    /// for `archive-z2`.
56    pub fn from_mode_str(s: &str) -> Option<RepoMode> {
57        Some(match s {
58            "bare" => RepoMode::Bare,
59            "bare-user" => RepoMode::BareUser,
60            "bare-user-only" => RepoMode::BareUserOnly,
61            "bare-split-xattrs" => RepoMode::BareSplitXattrs,
62            "archive-z2" | "archive" => RepoMode::Archive,
63            "bare-user-shared" => RepoMode::BareUserShared,
64            "bare-user-private" => RepoMode::BareUserPrivate,
65            _ => return None,
66        })
67    }
68
69    /// Returns the canonical `[core] mode=` string.
70    ///
71    /// [`Archive`](Self::Archive) always gives `archive-z2`.
72    pub fn as_mode_str(self) -> &'static str {
73        match self {
74            RepoMode::Bare => "bare",
75            RepoMode::BareUser => "bare-user",
76            RepoMode::BareUserOnly => "bare-user-only",
77            RepoMode::BareSplitXattrs => "bare-split-xattrs",
78            RepoMode::Archive => "archive-z2",
79            RepoMode::BareUserShared => "bare-user-shared",
80            RepoMode::BareUserPrivate => "bare-user-private",
81        }
82    }
83
84    /// Returns `true` if the mode stores [`File`] content objects compressed.
85    ///
86    /// The `z` loose-path suffix applies only to a [`File`] content object in
87    /// this mode. The auxiliary non-meta objects carry no suffix.
88    /// [`ObjectType::extension`] gives each extension.
89    ///
90    /// [`File`]: crate::ObjectType::File
91    /// [`ObjectType::extension`]: crate::ObjectType::extension
92    pub fn is_archive(self) -> bool {
93        matches!(self, RepoMode::Archive)
94    }
95}
96
97#[cfg(test)]
98mod tests {
99    use super::*;
100
101    #[test]
102    fn archive_alias_parses_and_canonicalizes() {
103        assert_eq!(RepoMode::from_mode_str("archive"), Some(RepoMode::Archive));
104        assert_eq!(
105            RepoMode::from_mode_str("archive-z2"),
106            Some(RepoMode::Archive)
107        );
108        assert_eq!(RepoMode::Archive.as_mode_str(), "archive-z2");
109    }
110
111    #[test]
112    fn every_mode_round_trips_through_its_canonical_string() {
113        for mode in [
114            RepoMode::Bare,
115            RepoMode::BareUser,
116            RepoMode::BareUserOnly,
117            RepoMode::BareSplitXattrs,
118            RepoMode::Archive,
119            RepoMode::BareUserShared,
120            RepoMode::BareUserPrivate,
121        ] {
122            assert_eq!(RepoMode::from_mode_str(mode.as_mode_str()), Some(mode));
123        }
124    }
125
126    #[test]
127    fn unknown_mode_is_none() {
128        assert_eq!(RepoMode::from_mode_str("bogus"), None);
129    }
130}