Skip to main content

tar/
entry_type.rs

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