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