Skip to content

Commit

Permalink
doc: Expand from_fs doc
Browse files Browse the repository at this point in the history
This expands and slightly corrects the `gix_index::entry::from_fs`
documentation:

- To be more precise about the expectation of where the metadata
  come from (it needn't specifically be the `symlink_metadata`
  function, and it is sometimes even an `fstat`-like function
  rather than an `lstat`-like function, though not `stat`).

- To mention that default values are used for unavailable data.

- To mention that truncation is sometimes performed on some fields.

Comments are also updated regarding some cases of truncation: why
casting down to 32-bit should be okay for `st_dev` and `st_ino`
(see #1817).
  • Loading branch information
EliahKagan committed Jan 30, 2025
1 parent ffb73b5 commit 426c669
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions gix-index/src/entry/stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,14 @@ impl Stat {
}
}

/// Creates stat information from the result of `symlink_metadata`.
/// Creates stat information from file metadata.
///
/// The information passed to this function should originate from a function like
/// `symlink_metadata`/`lstat` or `File::metadata`/`fstat`.
///
/// The data are adjusted for use in the index, using default values of fields that are not
/// meaningful on the target operating system or that are unavailable, and truncating data
/// where doing so does not lose essential information for keeping track of file status.
pub fn from_fs(stat: &crate::fs::Metadata) -> Result<Stat, SystemTimeError> {
let mtime = stat.modified().unwrap_or(std::time::UNIX_EPOCH);
let ctime = stat.created().unwrap_or(std::time::UNIX_EPOCH);
Expand All @@ -88,23 +95,22 @@ impl Stat {
ino: 0,
uid: 0,
gid: 0,
// truncation to 32 bits is on purpose (git does the same).
// Truncation to 32 bits is on purpose (git does the same).
size: stat.len() as u32,
};
#[cfg(not(windows))]
let res = {
Stat {
mtime: mtime.try_into().unwrap_or_default(),
ctime: ctime.try_into().unwrap_or_default(),
// truncating to 32 bits is fine here because
// that's what the linux syscalls returns
// just rust upcasts to 64 bits for some reason?
// numbers this large are impractical anyway (that's a lot of hard-drives).
// Truncating the device and inode numbers to 32 bits should be fine even on
// targets where they are represented as 64 bits, since we do not use them
// precisely for tracking changes and we do not map them back to the inode.
dev: stat.dev() as u32,
ino: stat.ino() as u32,
uid: stat.uid(),
gid: stat.gid(),
// truncation to 32 bits is on purpose (git does the same).
// Truncation to 32 bits is on purpose (git does the same).
size: stat.len() as u32,
}
};
Expand Down

0 comments on commit 426c669

Please sign in to comment.