Skip to main content

git_policy_cli/
private_storage.rs

1//! What:
2//!  Private,
3//!  crash-safe file primitives shared by every durable record of the wrapper:
4//!       exclusive no-follow creation with private modes,
5//!  file and directory sync,
6//!  and
7//!       no-follow reads that refuse links and non-regular files.
8//! Why:
9//!  Lock records,
10//!  transaction journals and capture records must survive a crash in a
11//!      readable state,
12//!  must never be written through a planted link,
13//!  and must be private to
14//!      the account.
15//!  The incumbent's `src/trust/registry-io.ts` defines these semantics;
16//!  both
17//!      wrappers write and read the same files.
18//!
19//! In TS you'd write (pseudocode):
20//! ```ts
21//! await writePrivateFile({ path, bytes }); await syncDirectory(dirname(path));
22//! ```
23
24/// What:
25///  `File` is an open file handle;
26///  `OpenOptions` configures how one is opened.
27/// Why:
28///   Exclusive creation and no-follow opening need the configurable form.
29///
30/// In TS you'd write (pseudocode):
31/// ```ts
32/// import { open, constants } from 'node:fs/promises';
33/// ```
34use std::fs::{File, OpenOptions};
35/// The trait that gives files `.read_to_end` and `.take`.
36use std::io::Read;
37/// The trait that gives files `.write_all`.
38use std::io::Write;
39/// `Path` is a borrowed filesystem path of raw bytes.
40use std::path::Path;
41
42/// Mode of every private directory the wrapper creates:
43///  owner only.
44pub const PRIVATE_DIRECTORY_MODE: u32 = 0o700;
45
46/// Mode of every private file the wrapper creates:
47///  owner read and write only.
48pub const PRIVATE_FILE_MODE: u32 = 0o600;
49
50/// What:
51///  Why a no-follow read refused a path.
52///  An `enum` is a closed set of alternatives.
53/// Why:
54///   A missing file is an expected answer for optional records;
55///  a link,
56///  a directory or an
57///       over-long file is unsafe or corrupt state that recovery must name,
58///  never follow.
59///
60/// In TS you'd write (pseudocode):
61/// ```ts
62/// type ReadRefusal = { kind: 'missing' } | { kind: 'not-regular' } | { kind: 'too-large' } | { kind: 'io'; error: Error };
63/// ```
64#[derive(Debug)]
65pub enum ReadRefusal {
66    /// Nothing exists at the path.
67    Missing,
68    /// The path names a link,
69    ///  directory or other non-regular file.
70    NotRegular,
71    /// The file holds more bytes than the caller allows.
72    TooLarge,
73    /// The operating system refused the read for another reason.
74    Io(
75        /// The operating-system error that refused the read.
76        std::io::Error,
77    ),
78}
79
80/// What:
81///  Open options for an exclusive,
82///  no-follow,
83///  private-mode creation.
84///       `#[cfg(unix)]` compiles the block only on Unix-like systems.
85/// Why:
86///   `O_EXCL` makes a second writer fail instead of replacing the record;
87///  `O_NOFOLLOW`
88///       refuses a planted final link;
89///  mode `0600` keeps the record private from the start.
90///
91/// In TS you'd write (pseudocode):
92/// ```ts
93/// open(path, O_CREAT | O_EXCL | O_WRONLY | O_NOFOLLOW, 0o600)
94/// ```
95fn exclusive_options() -> OpenOptions {
96    // `mut` allows the builder to be configured step by step.
97    let mut options: OpenOptions = OpenOptions::new();
98    options.write(true).create_new(true);
99    #[cfg(unix)]
100    {
101        // The trait adds `.mode` and `.custom_flags` to open options on Unix.
102        /// The Unix open-option extensions,
103        ///  for the mode and no-follow flags.
104        use std::os::unix::fs::OpenOptionsExt;
105        options
106            .mode(PRIVATE_FILE_MODE)
107            .custom_flags(libc::O_NOFOLLOW);
108    }
109    return options;
110}
111
112/// What:
113///  Set a path's permission bits to `mode`,
114///  on Unix only.
115///       `std::io::Result<()>` is "nothing,
116///  or an operating-system error".
117/// Why:
118///   The creation mode is filtered by the process `umask`;
119///  an explicit change makes the
120///       mode exactly private whatever the caller's `umask` is,
121///  as the incumbent's
122///       `protectPath` does.
123///  Windows keeps its inherited access rules (recorded under
124///       "Choices open to veto" in `doc/handover/cli-git-native-transactions.md`).
125///
126/// In TS you'd write (pseudocode):
127/// ```ts
128/// if (process.platform !== 'win32') await chmod(path, mode);
129/// ```
130pub fn protect_path(path: &Path, mode: u32) -> std::io::Result<()> {
131    #[cfg(unix)]
132    {
133        // The trait adds `.from_mode` to permissions on Unix.
134        /// The Unix permission extensions,
135        ///  for the mode.
136        use std::os::unix::fs::PermissionsExt;
137        std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode))?;
138    }
139    // `let _ = mode;` marks the argument as used where no mode exists.
140    // UNTESTED ON WINDOWS: no gate compiles or runs this branch (the crate does not build for Windows; README, Requirements).
141    #[cfg(not(unix))]
142    let _ = (path, mode);
143    return Ok(());
144}
145
146/// What:
147///  Write one private file exclusively,
148///  refusing links,
149///  then make its bytes durable.
150///       `&[u8]` borrows the exact bytes.
151/// Why:
152///   A journal state file is created once and never rewritten,
153///  so a crash leaves either
154///       no file or a complete one after the caller syncs the directory.
155///
156/// In TS you'd write (pseudocode):
157/// ```ts
158/// async function writePrivateFile({ path, bytes }): Promise<void>;
159/// ```
160pub fn write_private_file(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
161    let mut file: File = exclusive_options().open(path)?;
162    file.write_all(bytes)?;
163    // `sync_all` flushes data and metadata, the incumbent's `handle.sync()`.
164    file.sync_all()?;
165    return protect_path(path, PRIVATE_FILE_MODE);
166}
167
168/// What:
169///  Write one file exclusively with mode `0600`,
170///  refusing links,
171///  and sync its bytes,
172///       without the explicit mode change.
173/// Why:
174///   Owner-lock candidates are written this way by the incumbent's `writeCandidate`:
175///  the
176///       creation mode alone,
177///  then a sync before publication.
178///
179/// In TS you'd write (pseudocode):
180/// ```ts
181/// const handle = await open(path, O_CREAT | O_EXCL | O_WRONLY | O_NOFOLLOW, 0o600); await handle.writeFile(bytes); await handle.sync();
182/// ```
183pub fn write_exclusive_synced(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
184    let mut file: File = exclusive_options().open(path)?;
185    file.write_all(bytes)?;
186    return file.sync_all();
187}
188
189/// What:
190///  Create one directory with mode `0700`;
191///  its parent must exist.
192/// Why:
193///   Lock candidates,
194///  transaction directories and stores are private to the account.
195///       Only the creation mode is applied,
196///  as the incumbent's `mkdir(path, { mode })`.
197///
198/// In TS you'd write (pseudocode):
199/// ```ts
200/// await mkdir(path, { mode: 0o700 });
201/// ```
202pub fn create_private_directory(path: &Path) -> std::io::Result<()> {
203    return private_directory_builder().create(path);
204}
205
206/// What:
207///  The directory builder that creates private directories on this platform.
208/// Why:
209///   Only Unix configures a mode;
210///  a separate function per platform keeps each body free of
211///       a `mut` the other platform would leave unused.
212///
213/// In TS you'd write (pseudocode):
214/// ```ts
215/// const builder = { mode: 0o700 };
216/// ```
217#[cfg(unix)]
218fn private_directory_builder() -> std::fs::DirBuilder {
219    // The trait adds `.mode` to directory builders on Unix.
220    /// The Unix directory-builder extensions,
221    ///  for the mode.
222    use std::os::unix::fs::DirBuilderExt;
223    // `mut` allows the builder to be configured before it is returned.
224    let mut builder: std::fs::DirBuilder = std::fs::DirBuilder::new();
225    builder.mode(PRIVATE_DIRECTORY_MODE);
226    return builder;
227}
228
229/// What:
230///  The directory builder that creates private directories on this platform.
231/// Why:
232///   Windows has no mode to configure,
233///  as `private_directory_builder` on Unix explains.
234///
235/// In TS you'd write (pseudocode):
236/// ```ts
237/// const builder = {};
238/// ```
239// UNTESTED ON WINDOWS: no gate compiles or runs this branch (the crate does not build for Windows; README, Requirements).
240#[cfg(not(unix))]
241fn private_directory_builder() -> std::fs::DirBuilder {
242    return std::fs::DirBuilder::new();
243}
244
245/// What:
246///  Make a directory's entries durable,
247///  where the platform supports directory handles.
248/// Why:
249///   A new or renamed entry survives a crash only after its directory is synced;
250///  Windows
251///       has no directory handle for this,
252///  as the incumbent's `syncDirectory` notes.
253///
254/// In TS you'd write (pseudocode):
255/// ```ts
256/// async function syncDirectory(path: string): Promise<void>;
257/// ```
258pub fn sync_directory(path: &Path) -> std::io::Result<()> {
259    #[cfg(unix)]
260    {
261        // Opening a directory read-only and syncing it is the POSIX way to flush its entries.
262        let directory: File = File::open(path)?;
263        directory.sync_all()?;
264    }
265    // UNTESTED ON WINDOWS: no gate compiles or runs this branch (the crate does not build for Windows; README, Requirements).
266    #[cfg(not(unix))]
267    let _ = path;
268    return Ok(());
269}
270
271/// What:
272///  Remove a directory tree,
273///  treating an already absent tree as removed.
274/// Why:
275///   Retired locks and transactions are removed by name after a rename;
276///  another process
277///       may have removed them first,
278///  which is the same end state (`rm -rf` with `force`).
279///
280/// In TS you'd write (pseudocode):
281/// ```ts
282/// await rm(path, { recursive: true, force: true });
283/// ```
284pub fn remove_tree(path: &Path) -> std::io::Result<()> {
285    match std::fs::remove_dir_all(path) {
286        Ok(()) => return Ok(()),
287        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()),
288        Err(error) => return Err(error),
289    }
290}
291
292/// What:
293///  Open a file for reading without following a final link.
294/// Why:
295///   Records are always files the wrappers created;
296///  a link planted at a record path must
297///       be refused,
298///  never read through.
299///
300/// In TS you'd write (pseudocode):
301/// ```ts
302/// await open(path, O_RDONLY | O_NOFOLLOW);
303/// ```
304fn open_no_follow(path: &Path) -> std::io::Result<File> {
305    // `mut` allows the builder to be configured step by step.
306    let mut options: OpenOptions = OpenOptions::new();
307    options.read(true);
308    #[cfg(unix)]
309    {
310        // The trait adds `.custom_flags` to open options on Unix.
311        /// The Unix open-option extensions,
312        ///  for the no-follow flag.
313        use std::os::unix::fs::OpenOptionsExt;
314        options.custom_flags(libc::O_NOFOLLOW);
315    }
316    return options.open(path);
317}
318
319/// What:
320///  Read a whole regular file without following links,
321///  up to `limit` bytes.
322///       `u64` is an unsigned 64-bit size;
323///  `Result<Vec<u8>, ReadRefusal>` is "the bytes,
324///  or why
325///       not".
326/// Why:
327///   Recovery reads untrusted state:
328///  a link or a directory must be named as unsafe,
329///  and a
330///       runaway file must not be read into memory.
331///
332/// In TS you'd write (pseudocode):
333/// ```ts
334/// async function readPrivateFile(path: string, limit: number): Promise<Uint8Array>; // throws ReadRefusal
335/// ```
336pub fn read_regular_file(path: &Path, limit: u64) -> Result<Vec<u8>, ReadRefusal> {
337    let file: File = match open_no_follow(path) {
338        Ok(opened) => opened,
339        Err(error) => return Err(open_refusal(error)),
340    };
341    // The handle's own metadata, so a swap between the check and the read is impossible.
342    let metadata: std::fs::Metadata = file.metadata().map_err(ReadRefusal::Io)?;
343    if !metadata.is_file() {
344        return Err(ReadRefusal::NotRegular);
345    }
346    if metadata.len() > limit {
347        return Err(ReadRefusal::TooLarge);
348    }
349    // `.take(limit + 1)` caps the read even if the file grows after the size check.
350    let mut bytes: Vec<u8> = Vec::new();
351    file.take(limit + 1)
352        .read_to_end(&mut bytes)
353        .map_err(ReadRefusal::Io)?;
354    if bytes.len() as u64 > limit {
355        return Err(ReadRefusal::TooLarge);
356    }
357    return Ok(bytes);
358}
359
360/// What:
361///  Classify an open failure as missing,
362///  a refused link,
363///  or another error.
364/// Why:
365///   `O_NOFOLLOW` on a link fails with `ELOOP` on Linux and macOS;
366///  that is the unsafe-link
367///       answer,
368///  not an input-output failure.
369///  A path through a non-directory (`ENOTDIR`) names
370///       no file,
371///  which the incumbent treats like a missing one.
372///
373/// In TS you'd write (pseudocode):
374/// ```ts
375/// function openRefusal(error: NodeJS.ErrnoException): ReadRefusal;
376/// ```
377fn open_refusal(error: std::io::Error) -> ReadRefusal {
378    if error.kind() == std::io::ErrorKind::NotFound {
379        return ReadRefusal::Missing;
380    }
381    #[cfg(unix)]
382    {
383        if error.raw_os_error() == Some(libc::ENOTDIR) {
384            return ReadRefusal::Missing;
385        }
386        if error.raw_os_error() == Some(libc::ELOOP) {
387            return ReadRefusal::NotRegular;
388        }
389    }
390    return ReadRefusal::Io(error);
391}
392
393/// Disposable-directory controls stay out of the release executable.
394#[cfg(test)]
395#[path = "private_storage_tests.rs"]
396mod tests;