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    #[cfg(not(unix))]
141    let _ = (path, mode);
142    return Ok(());
143}
144
145/// What:
146///  Write one private file exclusively,
147///  refusing links,
148///  then make its bytes durable.
149///       `&[u8]` borrows the exact bytes.
150/// Why:
151///   A journal state file is created once and never rewritten,
152///  so a crash leaves either
153///       no file or a complete one after the caller syncs the directory.
154///
155/// In TS you'd write (pseudocode):
156/// ```ts
157/// async function writePrivateFile({ path, bytes }): Promise<void>;
158/// ```
159pub fn write_private_file(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
160    let mut file: File = exclusive_options().open(path)?;
161    file.write_all(bytes)?;
162    // `sync_all` flushes data and metadata, the incumbent's `handle.sync()`.
163    file.sync_all()?;
164    return protect_path(path, PRIVATE_FILE_MODE);
165}
166
167/// What:
168///  Write one file exclusively with mode `0600`,
169///  refusing links,
170///  and sync its bytes,
171///       without the explicit mode change.
172/// Why:
173///   Owner-lock candidates are written this way by the incumbent's `writeCandidate`:
174///  the
175///       creation mode alone,
176///  then a sync before publication.
177///
178/// In TS you'd write (pseudocode):
179/// ```ts
180/// const handle = await open(path, O_CREAT | O_EXCL | O_WRONLY | O_NOFOLLOW, 0o600); await handle.writeFile(bytes); await handle.sync();
181/// ```
182pub fn write_exclusive_synced(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
183    let mut file: File = exclusive_options().open(path)?;
184    file.write_all(bytes)?;
185    return file.sync_all();
186}
187
188/// What:
189///  Create one directory with mode `0700`;
190///  its parent must exist.
191/// Why:
192///   Lock candidates,
193///  transaction directories and stores are private to the account.
194///       Only the creation mode is applied,
195///  as the incumbent's `mkdir(path, { mode })`.
196///
197/// In TS you'd write (pseudocode):
198/// ```ts
199/// await mkdir(path, { mode: 0o700 });
200/// ```
201pub fn create_private_directory(path: &Path) -> std::io::Result<()> {
202    return private_directory_builder().create(path);
203}
204
205/// What:
206///  The directory builder that creates private directories on this platform.
207/// Why:
208///   Only Unix configures a mode;
209///  a separate function per platform keeps each body free of
210///       a `mut` the other platform would leave unused.
211///
212/// In TS you'd write (pseudocode):
213/// ```ts
214/// const builder = { mode: 0o700 };
215/// ```
216#[cfg(unix)]
217fn private_directory_builder() -> std::fs::DirBuilder {
218    // The trait adds `.mode` to directory builders on Unix.
219    /// The Unix directory-builder extensions,
220    ///  for the mode.
221    use std::os::unix::fs::DirBuilderExt;
222    // `mut` allows the builder to be configured before it is returned.
223    let mut builder: std::fs::DirBuilder = std::fs::DirBuilder::new();
224    builder.mode(PRIVATE_DIRECTORY_MODE);
225    return builder;
226}
227
228/// What:
229///  The directory builder that creates private directories on this platform.
230/// Why:
231///   Windows has no mode to configure,
232///  as `private_directory_builder` on Unix explains.
233///
234/// In TS you'd write (pseudocode):
235/// ```ts
236/// const builder = {};
237/// ```
238#[cfg(not(unix))]
239fn private_directory_builder() -> std::fs::DirBuilder {
240    return std::fs::DirBuilder::new();
241}
242
243/// What:
244///  Make a directory's entries durable,
245///  where the platform supports directory handles.
246/// Why:
247///   A new or renamed entry survives a crash only after its directory is synced;
248///  Windows
249///       has no directory handle for this,
250///  as the incumbent's `syncDirectory` notes.
251///
252/// In TS you'd write (pseudocode):
253/// ```ts
254/// async function syncDirectory(path: string): Promise<void>;
255/// ```
256pub fn sync_directory(path: &Path) -> std::io::Result<()> {
257    #[cfg(unix)]
258    {
259        // Opening a directory read-only and syncing it is the POSIX way to flush its entries.
260        let directory: File = File::open(path)?;
261        directory.sync_all()?;
262    }
263    #[cfg(not(unix))]
264    let _ = path;
265    return Ok(());
266}
267
268/// What:
269///  Remove a directory tree,
270///  treating an already absent tree as removed.
271/// Why:
272///   Retired locks and transactions are removed by name after a rename;
273///  another process
274///       may have removed them first,
275///  which is the same end state (`rm -rf` with `force`).
276///
277/// In TS you'd write (pseudocode):
278/// ```ts
279/// await rm(path, { recursive: true, force: true });
280/// ```
281pub fn remove_tree(path: &Path) -> std::io::Result<()> {
282    match std::fs::remove_dir_all(path) {
283        Ok(()) => return Ok(()),
284        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()),
285        Err(error) => return Err(error),
286    }
287}
288
289/// What:
290///  Open a file for reading without following a final link.
291/// Why:
292///   Records are always files the wrappers created;
293///  a link planted at a record path must
294///       be refused,
295///  never read through.
296///
297/// In TS you'd write (pseudocode):
298/// ```ts
299/// await open(path, O_RDONLY | O_NOFOLLOW);
300/// ```
301fn open_no_follow(path: &Path) -> std::io::Result<File> {
302    // `mut` allows the builder to be configured step by step.
303    let mut options: OpenOptions = OpenOptions::new();
304    options.read(true);
305    #[cfg(unix)]
306    {
307        // The trait adds `.custom_flags` to open options on Unix.
308        /// The Unix open-option extensions,
309        ///  for the no-follow flag.
310        use std::os::unix::fs::OpenOptionsExt;
311        options.custom_flags(libc::O_NOFOLLOW);
312    }
313    return options.open(path);
314}
315
316/// What:
317///  Read a whole regular file without following links,
318///  up to `limit` bytes.
319///       `u64` is an unsigned 64-bit size;
320///  `Result<Vec<u8>, ReadRefusal>` is "the bytes,
321///  or why
322///       not".
323/// Why:
324///   Recovery reads untrusted state:
325///  a link or a directory must be named as unsafe,
326///  and a
327///       runaway file must not be read into memory.
328///
329/// In TS you'd write (pseudocode):
330/// ```ts
331/// async function readPrivateFile(path: string, limit: number): Promise<Uint8Array>; // throws ReadRefusal
332/// ```
333pub fn read_regular_file(path: &Path, limit: u64) -> Result<Vec<u8>, ReadRefusal> {
334    let file: File = match open_no_follow(path) {
335        Ok(opened) => opened,
336        Err(error) => return Err(open_refusal(error)),
337    };
338    // The handle's own metadata, so a swap between the check and the read is impossible.
339    let metadata: std::fs::Metadata = file.metadata().map_err(ReadRefusal::Io)?;
340    if !metadata.is_file() {
341        return Err(ReadRefusal::NotRegular);
342    }
343    if metadata.len() > limit {
344        return Err(ReadRefusal::TooLarge);
345    }
346    // `.take(limit + 1)` caps the read even if the file grows after the size check.
347    let mut bytes: Vec<u8> = Vec::new();
348    file.take(limit + 1)
349        .read_to_end(&mut bytes)
350        .map_err(ReadRefusal::Io)?;
351    if bytes.len() as u64 > limit {
352        return Err(ReadRefusal::TooLarge);
353    }
354    return Ok(bytes);
355}
356
357/// What:
358///  Classify an open failure as missing,
359///  a refused link,
360///  or another error.
361/// Why:
362///   `O_NOFOLLOW` on a link fails with `ELOOP` on Linux and macOS;
363///  that is the unsafe-link
364///       answer,
365///  not an input-output failure.
366///  A path through a non-directory (`ENOTDIR`) names
367///       no file,
368///  which the incumbent treats like a missing one.
369///
370/// In TS you'd write (pseudocode):
371/// ```ts
372/// function openRefusal(error: NodeJS.ErrnoException): ReadRefusal;
373/// ```
374fn open_refusal(error: std::io::Error) -> ReadRefusal {
375    if error.kind() == std::io::ErrorKind::NotFound {
376        return ReadRefusal::Missing;
377    }
378    #[cfg(unix)]
379    {
380        if error.raw_os_error() == Some(libc::ENOTDIR) {
381            return ReadRefusal::Missing;
382        }
383        if error.raw_os_error() == Some(libc::ELOOP) {
384            return ReadRefusal::NotRegular;
385        }
386    }
387    return ReadRefusal::Io(error);
388}
389
390/// Disposable-directory controls stay out of the release executable.
391#[cfg(test)]
392#[path = "private_storage_tests.rs"]
393mod tests;