2016-04-16 18:56:44 +00:00
|
|
|
|
//! Wrapper types for the values returned from `File`s.
|
|
|
|
|
//!
|
|
|
|
|
//! The methods of `File` that return information about the entry on the
|
|
|
|
|
//! filesystem -- size, modification date, block count, or Git status -- used
|
|
|
|
|
//! to just return these as formatted strings, but this became inflexible once
|
|
|
|
|
//! customisable output styles landed.
|
|
|
|
|
//!
|
|
|
|
|
//! Instead, they will return a wrapper type from this module, which tags the
|
|
|
|
|
//! type with what field it is while containing the actual raw value.
|
|
|
|
|
//!
|
|
|
|
|
//! The `output::details` module, among others, uses these types to render and
|
|
|
|
|
//! display the information as formatted strings.
|
|
|
|
|
|
|
|
|
|
// C-style `blkcnt_t` types don’t follow Rust’s rules!
|
2016-04-16 17:59:25 +00:00
|
|
|
|
#![allow(non_camel_case_types)]
|
2020-10-13 00:46:17 +00:00
|
|
|
|
#![allow(clippy::struct_excessive_bools)]
|
2016-04-16 17:59:25 +00:00
|
|
|
|
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
|
|
|
|
/// The type of a file’s block count.
|
2016-04-16 17:59:25 +00:00
|
|
|
|
pub type blkcnt_t = u64;
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
|
|
|
|
/// The type of a file’s group ID.
|
2016-04-16 17:59:25 +00:00
|
|
|
|
pub type gid_t = u32;
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
|
|
|
|
/// The type of a file’s inode.
|
2016-04-16 17:59:25 +00:00
|
|
|
|
pub type ino_t = u64;
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
|
|
|
|
/// The type of a file’s number of links.
|
2016-04-16 17:59:25 +00:00
|
|
|
|
pub type nlink_t = u64;
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
|
|
|
|
/// The type of a file’s timestamp (creation, modification, access, etc).
|
2016-04-16 17:59:25 +00:00
|
|
|
|
pub type time_t = i64;
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
|
|
|
|
/// The type of a file’s user ID.
|
2016-04-16 17:59:25 +00:00
|
|
|
|
pub type uid_t = u32;
|
|
|
|
|
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
|
|
|
|
/// The file’s base type, which gets displayed in the very first column of the
|
|
|
|
|
/// details output.
|
|
|
|
|
///
|
|
|
|
|
/// This type is set entirely by the filesystem, rather than relying on a
|
|
|
|
|
/// file’s contents. So “link” is a type, but “image” is just a type of
|
|
|
|
|
/// regular file. (See the `filetype` module for those checks.)
|
2017-06-29 13:50:39 +00:00
|
|
|
|
///
|
|
|
|
|
/// Its ordering is used when sorting by type.
|
2020-10-10 14:30:19 +00:00
|
|
|
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
|
2016-04-16 17:59:25 +00:00
|
|
|
|
pub enum Type {
|
2020-10-10 18:49:46 +00:00
|
|
|
|
Directory,
|
|
|
|
|
File,
|
|
|
|
|
Link,
|
|
|
|
|
Pipe,
|
|
|
|
|
Socket,
|
|
|
|
|
CharDevice,
|
|
|
|
|
BlockDevice,
|
|
|
|
|
Special,
|
2016-04-16 17:59:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-16 19:01:45 +00:00
|
|
|
|
impl Type {
|
2020-10-10 14:30:19 +00:00
|
|
|
|
pub fn is_regular_file(self) -> bool {
|
|
|
|
|
matches!(self, Self::File)
|
2016-04-16 19:01:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-04-16 18:56:44 +00:00
|
|
|
|
/// The file’s Unix permission bitfield, with one entry per bit.
|
2020-10-10 14:30:19 +00:00
|
|
|
|
#[derive(Copy, Clone)]
|
2016-04-16 17:59:25 +00:00
|
|
|
|
pub struct Permissions {
|
|
|
|
|
pub user_read: bool,
|
|
|
|
|
pub user_write: bool,
|
|
|
|
|
pub user_execute: bool,
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
2016-04-16 17:59:25 +00:00
|
|
|
|
pub group_read: bool,
|
|
|
|
|
pub group_write: bool,
|
|
|
|
|
pub group_execute: bool,
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
2016-04-16 17:59:25 +00:00
|
|
|
|
pub other_read: bool,
|
|
|
|
|
pub other_write: bool,
|
|
|
|
|
pub other_execute: bool,
|
2017-05-30 14:29:29 +00:00
|
|
|
|
|
|
|
|
|
pub sticky: bool,
|
|
|
|
|
pub setgid: bool,
|
|
|
|
|
pub setuid: bool,
|
2016-04-16 17:59:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 09:13:00 +00:00
|
|
|
|
/// The file's FileAttributes field, available only on Windows.
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
|
pub struct Attributes {
|
|
|
|
|
pub archive: bool,
|
|
|
|
|
pub directory: bool,
|
|
|
|
|
pub readonly: bool,
|
|
|
|
|
pub hidden: bool,
|
|
|
|
|
pub system: bool,
|
|
|
|
|
pub reparse_point: bool,
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-21 09:48:27 +00:00
|
|
|
|
/// The three pieces of information that are displayed as a single column in
|
|
|
|
|
/// the details view. These values are fused together to make the output a
|
|
|
|
|
/// little more compressed.
|
2020-10-10 14:30:19 +00:00
|
|
|
|
#[derive(Copy, Clone)]
|
2017-05-21 09:48:27 +00:00
|
|
|
|
pub struct PermissionsPlus {
|
|
|
|
|
pub file_type: Type,
|
2020-05-02 04:15:50 +00:00
|
|
|
|
#[cfg(unix)]
|
2017-05-21 09:48:27 +00:00
|
|
|
|
pub permissions: Permissions,
|
2021-03-30 09:13:00 +00:00
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
pub attributes: Attributes,
|
2017-05-21 09:48:27 +00:00
|
|
|
|
pub xattrs: bool,
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
2020-07-24 11:47:34 +00:00
|
|
|
|
/// The permissions encoded as octal values
|
2020-10-10 14:30:19 +00:00
|
|
|
|
#[derive(Copy, Clone)]
|
2020-07-24 11:47:34 +00:00
|
|
|
|
pub struct OctalPermissions {
|
|
|
|
|
pub permissions: Permissions,
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-16 18:56:44 +00:00
|
|
|
|
/// A file’s number of hard links on the filesystem.
|
|
|
|
|
///
|
|
|
|
|
/// Under Unix, a file can exist on the filesystem only once but appear in
|
|
|
|
|
/// multiple directories. However, it’s rare (but occasionally useful!) for a
|
|
|
|
|
/// regular file to have a link count greater than 1, so we highlight the
|
|
|
|
|
/// block count specifically for this case.
|
2020-10-10 14:30:19 +00:00
|
|
|
|
#[derive(Copy, Clone)]
|
2016-04-16 17:59:25 +00:00
|
|
|
|
pub struct Links {
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
|
|
|
|
/// The actual link count.
|
2016-04-16 17:59:25 +00:00
|
|
|
|
pub count: nlink_t,
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
|
|
|
|
/// Whether this file is a regular file with more than one hard link.
|
2016-04-16 17:59:25 +00:00
|
|
|
|
pub multiple: bool,
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
|
|
|
|
/// A file’s inode. Every directory entry on a Unix filesystem has an inode,
|
|
|
|
|
/// including directories and links, so this is applicable to everything exa
|
|
|
|
|
/// can deal with.
|
2020-10-10 14:30:19 +00:00
|
|
|
|
#[derive(Copy, Clone)]
|
2016-04-16 17:59:25 +00:00
|
|
|
|
pub struct Inode(pub ino_t);
|
|
|
|
|
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
|
|
|
|
/// The number of blocks that a file takes up on the filesystem, if any.
|
2020-10-10 14:30:19 +00:00
|
|
|
|
#[derive(Copy, Clone)]
|
2016-04-16 17:59:25 +00:00
|
|
|
|
pub enum Blocks {
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
|
|
|
|
/// This file has the given number of blocks.
|
2016-04-16 17:59:25 +00:00
|
|
|
|
Some(blkcnt_t),
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
|
|
|
|
/// This file isn’t of a type that can take up blocks.
|
2016-04-16 17:59:25 +00:00
|
|
|
|
None,
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
|
|
|
|
/// The ID of the user that owns a file. This will only ever be a number;
|
|
|
|
|
/// looking up the username is done in the `display` module.
|
2020-10-10 14:30:19 +00:00
|
|
|
|
#[derive(Copy, Clone)]
|
2016-04-16 17:59:25 +00:00
|
|
|
|
pub struct User(pub uid_t);
|
|
|
|
|
|
2016-04-16 18:56:44 +00:00
|
|
|
|
/// The ID of the group that a file belongs to.
|
2020-10-10 14:30:19 +00:00
|
|
|
|
#[derive(Copy, Clone)]
|
2016-04-16 17:59:25 +00:00
|
|
|
|
pub struct Group(pub gid_t);
|
|
|
|
|
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
|
|
|
|
/// A file’s size, in bytes. This is usually formatted by the `number_prefix`
|
|
|
|
|
/// crate into something human-readable.
|
2020-10-10 14:30:19 +00:00
|
|
|
|
#[derive(Copy, Clone)]
|
2016-04-16 17:59:25 +00:00
|
|
|
|
pub enum Size {
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
|
|
|
|
/// This file has a defined size.
|
2016-04-16 17:59:25 +00:00
|
|
|
|
Some(u64),
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
|
|
|
|
/// This file has no size, or has a size but we aren’t interested in it.
|
|
|
|
|
///
|
|
|
|
|
/// Under Unix, directory entries that aren’t regular files will still
|
|
|
|
|
/// have a file size. For example, a directory will just contain a list of
|
|
|
|
|
/// its files as its “contents” and will be specially flagged as being a
|
|
|
|
|
/// directory, rather than a file. However, seeing the “file size” of this
|
2020-10-10 18:49:46 +00:00
|
|
|
|
/// data is rarely useful — I can’t think of a time when I’ve seen it and
|
2016-04-16 18:56:44 +00:00
|
|
|
|
/// learnt something. So we discard it and just output “-” instead.
|
|
|
|
|
///
|
2022-07-17 06:35:14 +00:00
|
|
|
|
/// See this answer for more: <https://unix.stackexchange.com/a/68266>
|
2016-04-16 17:59:25 +00:00
|
|
|
|
None,
|
2017-05-19 08:20:47 +00:00
|
|
|
|
|
|
|
|
|
/// This file is a block or character device, so instead of a size, print
|
|
|
|
|
/// out the file’s major and minor device IDs.
|
|
|
|
|
///
|
|
|
|
|
/// This is what ls does as well. Without it, the devices will just have
|
|
|
|
|
/// file sizes of zero.
|
2017-05-20 20:55:18 +00:00
|
|
|
|
DeviceIDs(DeviceIDs),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The major and minor device IDs that gets displayed for device files.
|
|
|
|
|
///
|
|
|
|
|
/// You can see what these device numbers mean:
|
2020-10-10 14:57:40 +00:00
|
|
|
|
/// - <http://www.lanana.org/docs/device-list/>
|
|
|
|
|
/// - <http://www.lanana.org/docs/device-list/devices-2.6+.txt>
|
2020-10-10 14:30:19 +00:00
|
|
|
|
#[derive(Copy, Clone)]
|
2017-05-20 20:55:18 +00:00
|
|
|
|
pub struct DeviceIDs {
|
|
|
|
|
pub major: u8,
|
|
|
|
|
pub minor: u8,
|
2016-04-16 17:59:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
|
|
|
|
/// One of a file’s timestamps (created, accessed, or modified).
|
2018-11-20 16:27:16 +00:00
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
2017-07-05 22:07:27 +00:00
|
|
|
|
pub struct Time {
|
|
|
|
|
pub seconds: time_t,
|
|
|
|
|
pub nanoseconds: time_t,
|
|
|
|
|
}
|
2016-04-16 17:59:25 +00:00
|
|
|
|
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
|
|
|
|
/// A file’s status in a Git repository. Whether a file is in a repository or
|
|
|
|
|
/// not is handled by the Git module, rather than having a “null” variant in
|
|
|
|
|
/// this enum.
|
2022-10-01 02:46:59 +00:00
|
|
|
|
#[derive(PartialEq, Eq, Copy, Clone)]
|
2016-04-16 17:59:25 +00:00
|
|
|
|
pub enum GitStatus {
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
|
|
|
|
/// This file hasn’t changed since the last commit.
|
2016-04-16 17:59:25 +00:00
|
|
|
|
NotModified,
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
|
|
|
|
/// This file didn’t exist for the last commit, and is not specified in
|
|
|
|
|
/// the ignored files list.
|
2016-04-16 17:59:25 +00:00
|
|
|
|
New,
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
|
|
|
|
/// A file that’s been modified since the last commit.
|
2016-04-16 17:59:25 +00:00
|
|
|
|
Modified,
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
|
|
|
|
/// A deleted file. This can’t ever be shown, but it’s here anyway!
|
2016-04-16 17:59:25 +00:00
|
|
|
|
Deleted,
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
|
|
|
|
/// A file that Git has tracked a rename for.
|
2016-04-16 17:59:25 +00:00
|
|
|
|
Renamed,
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
|
|
|
|
/// A file that’s had its type (such as the file permissions) changed.
|
2016-04-16 17:59:25 +00:00
|
|
|
|
TypeChange,
|
2018-11-20 17:12:38 +00:00
|
|
|
|
|
|
|
|
|
/// A file that’s ignored (that matches a line in .gitignore)
|
|
|
|
|
Ignored,
|
2020-05-29 14:23:22 +00:00
|
|
|
|
|
2020-10-10 18:49:46 +00:00
|
|
|
|
/// A file that’s updated but unmerged.
|
2020-05-29 14:23:22 +00:00
|
|
|
|
Conflicted,
|
2016-04-16 17:59:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-10 18:49:46 +00:00
|
|
|
|
|
2016-04-16 18:56:44 +00:00
|
|
|
|
/// A file’s complete Git status. It’s possible to make changes to a file, add
|
|
|
|
|
/// it to the staging area, then make *more* changes, so we need to list each
|
|
|
|
|
/// file’s status for both of these.
|
2020-10-10 14:30:19 +00:00
|
|
|
|
#[derive(Copy, Clone)]
|
2016-04-16 17:59:25 +00:00
|
|
|
|
pub struct Git {
|
|
|
|
|
pub staged: GitStatus,
|
|
|
|
|
pub unstaged: GitStatus,
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-01 18:13:47 +00:00
|
|
|
|
impl Default for Git {
|
2016-04-16 18:56:44 +00:00
|
|
|
|
|
|
|
|
|
/// Create a Git status for a file with nothing done to it.
|
2020-10-10 12:55:26 +00:00
|
|
|
|
fn default() -> Self {
|
2020-10-10 18:49:46 +00:00
|
|
|
|
Self {
|
|
|
|
|
staged: GitStatus::NotModified,
|
|
|
|
|
unstaged: GitStatus::NotModified,
|
|
|
|
|
}
|
2016-04-16 17:59:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|