async_tar/
entry_type.rs

1//! Indicate for the type of file described by a header.
2//!
3//! Each `Header` has an `entry_type` method returning an instance of this type
4//! which can be used to inspect what the header is describing.
5//!
6//! See <https://en.wikipedia.org/wiki/Tar_%28computing%29#UStar_format>
7
8/// A non-exhaustive enum representing the possible entry types
9#[derive(Clone, Copy, PartialEq, Eq, Debug)]
10#[non_exhaustive]
11pub enum EntryType {
12    /// Regular file
13    Regular,
14    /// Hard link
15    Link,
16    /// Symbolic link
17    Symlink,
18    /// Character device
19    Char,
20    /// Block device
21    Block,
22    /// Directory
23    Directory,
24    /// Named pipe (fifo)
25    Fifo,
26    /// Implementation-defined 'high-performance' type, treated as regular file
27    Continuous,
28    /// GNU extension - long file name
29    GNULongName,
30    /// GNU extension - long link name (link target)
31    GNULongLink,
32    /// GNU extension - sparse file
33    GNUSparse,
34    /// Global extended header
35    XGlobalHeader,
36    /// Extended Header
37    XHeader,
38    /// Unknown header,
39    Other(u8),
40}
41
42impl EntryType {
43    /// Creates a new entry type from a raw byte.
44    ///
45    /// Note that the other named constructors of entry type may be more
46    /// appropriate to create a file type from.
47    pub fn new(byte: u8) -> EntryType {
48        match byte {
49            b'\x00' | b'0' => EntryType::Regular,
50            b'1' => EntryType::Link,
51            b'2' => EntryType::Symlink,
52            b'3' => EntryType::Char,
53            b'4' => EntryType::Block,
54            b'5' => EntryType::Directory,
55            b'6' => EntryType::Fifo,
56            b'7' => EntryType::Continuous,
57            b'x' => EntryType::XHeader,
58            b'g' => EntryType::XGlobalHeader,
59            b'L' => EntryType::GNULongName,
60            b'K' => EntryType::GNULongLink,
61            b'S' => EntryType::GNUSparse,
62            other => EntryType::Other(other),
63        }
64    }
65
66    /// Returns the raw underlying byte that this entry type represents.
67    pub fn as_byte(self) -> u8 {
68        match self {
69            EntryType::Regular => b'0',
70            EntryType::Link => b'1',
71            EntryType::Symlink => b'2',
72            EntryType::Char => b'3',
73            EntryType::Block => b'4',
74            EntryType::Directory => b'5',
75            EntryType::Fifo => b'6',
76            EntryType::Continuous => b'7',
77            EntryType::XHeader => b'x',
78            EntryType::XGlobalHeader => b'g',
79            EntryType::GNULongName => b'L',
80            EntryType::GNULongLink => b'K',
81            EntryType::GNUSparse => b'S',
82            EntryType::Other(other) => other,
83        }
84    }
85
86    /// Creates a new entry type representing a regular file.
87    pub fn file() -> EntryType {
88        EntryType::Regular
89    }
90
91    /// Creates a new entry type representing a hard link.
92    pub fn hard_link() -> EntryType {
93        EntryType::Link
94    }
95
96    /// Creates a new entry type representing a symlink.
97    pub fn symlink() -> EntryType {
98        EntryType::Symlink
99    }
100
101    /// Creates a new entry type representing a character special device.
102    pub fn character_special() -> EntryType {
103        EntryType::Char
104    }
105
106    /// Creates a new entry type representing a block special device.
107    pub fn block_special() -> EntryType {
108        EntryType::Block
109    }
110
111    /// Creates a new entry type representing a directory.
112    pub fn dir() -> EntryType {
113        EntryType::Directory
114    }
115
116    /// Creates a new entry type representing a FIFO.
117    pub fn fifo() -> EntryType {
118        EntryType::Fifo
119    }
120
121    /// Creates a new entry type representing a contiguous file.
122    pub fn contiguous() -> EntryType {
123        EntryType::Continuous
124    }
125
126    /// Returns whether this type represents a regular file.
127    pub fn is_file(self) -> bool {
128        self == EntryType::Regular
129    }
130
131    /// Returns whether this type represents a hard link.
132    pub fn is_hard_link(self) -> bool {
133        self == EntryType::Link
134    }
135
136    /// Returns whether this type represents a symlink.
137    pub fn is_symlink(self) -> bool {
138        self == EntryType::Symlink
139    }
140
141    /// Returns whether this type represents a character special device.
142    pub fn is_character_special(self) -> bool {
143        self == EntryType::Char
144    }
145
146    /// Returns whether this type represents a block special device.
147    pub fn is_block_special(self) -> bool {
148        self == EntryType::Block
149    }
150
151    /// Returns whether this type represents a directory.
152    pub fn is_dir(self) -> bool {
153        self == EntryType::Directory
154    }
155
156    /// Returns whether this type represents a FIFO.
157    pub fn is_fifo(self) -> bool {
158        self == EntryType::Fifo
159    }
160
161    /// Returns whether this type represents a contiguous file.
162    pub fn is_contiguous(self) -> bool {
163        self == EntryType::Continuous
164    }
165
166    /// Returns whether this type represents a GNU long name header.
167    pub fn is_gnu_longname(self) -> bool {
168        self == EntryType::GNULongName
169    }
170
171    /// Returns whether this type represents a GNU sparse header.
172    pub fn is_gnu_sparse(self) -> bool {
173        self == EntryType::GNUSparse
174    }
175
176    /// Returns whether this type represents a GNU long link header.
177    pub fn is_gnu_longlink(self) -> bool {
178        self == EntryType::GNULongLink
179    }
180
181    /// Returns whether this type represents a GNU long name header.
182    pub fn is_pax_global_extensions(self) -> bool {
183        self == EntryType::XGlobalHeader
184    }
185
186    /// Returns whether this type represents a GNU long link header.
187    pub fn is_pax_local_extensions(self) -> bool {
188        self == EntryType::XHeader
189    }
190}