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