| 1 | //! A library for encoding/decoding Unix archive files. |
| 2 | //! |
| 3 | //! This library provides utilities necessary to manage [Unix archive |
| 4 | //! files](https://en.wikipedia.org/wiki/Ar_(Unix)) (as generated by the |
| 5 | //! standard `ar` command line utility) abstracted over a reader or writer. |
| 6 | //! This library provides a streaming interface that avoids having to ever load |
| 7 | //! a full archive entry into memory. |
| 8 | //! |
| 9 | //! The API of this crate is meant to be similar to that of the |
| 10 | //! [`tar`](https://crates.io/crates/tar) crate. |
| 11 | //! |
| 12 | |
| 13 | mod errorrust-analyzer cargo implib 0.3.3 ar/error/; |
| 14 | mod writerust-analyzer cargo implib 0.3.3 ar/write/; |
| 15 | |
| 16 | use craterust-analyzer cargo implib 0.3.3 crate/::{bailrust-analyzer cargo implib 0.3.3 ar/error/bail!, ensurerust-analyzer cargo implib 0.3.3 ar/error/ensure!, errrust-analyzer cargo implib 0.3.3 ar/error/err!}; |
| 17 | pub use writerust-analyzer cargo implib 0.3.3 ar/write/::GnuBuilderrust-analyzer cargo implib 0.3.3 ar/write/GnuBuilder#; |
| 18 | |
| 19 | // ========================================================================= // |
| 20 | |
| 21 | const GLOBAL_HEADER_LENrust-analyzer cargo implib 0.3.3 ar/GLOBAL_HEADER_LEN.: usize = 8; |
| 22 | const GLOBAL_HEADERrust-analyzer cargo implib 0.3.3 ar/GLOBAL_HEADER.: &[u8; GLOBAL_HEADER_LENrust-analyzer cargo implib 0.3.3 ar/GLOBAL_HEADER_LEN.] = b"!<arch>\n"; |
| 23 | |
| 24 | const GNU_NAME_TABLE_IDrust-analyzer cargo implib 0.3.3 ar/GNU_NAME_TABLE_ID.: &str = "//"; |
| 25 | const GNU_SYMBOL_LOOKUP_TABLE_IDrust-analyzer cargo implib 0.3.3 ar/GNU_SYMBOL_LOOKUP_TABLE_ID.: &str = "/"; |
| 26 | |
| 27 | // ========================================================================= // |
| 28 | |
| 29 | /// Representation of an archive entry header. |
| 30 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)] |
| 31 | pub struct Headerrust-analyzer cargo implib 0.3.3 ar/Header# { |
| 32 | identifierrust-analyzer cargo implib 0.3.3 ar/Header#identifier.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, |
| 33 | mtimerust-analyzer cargo implib 0.3.3 ar/Header#mtime.: u64, |
| 34 | uidrust-analyzer cargo implib 0.3.3 ar/Header#uid.: u32, |
| 35 | gidrust-analyzer cargo implib 0.3.3 ar/Header#gid.: u32, |
| 36 | moderust-analyzer cargo implib 0.3.3 ar/Header#mode.: u32, |
| 37 | sizerust-analyzer cargo implib 0.3.3 ar/Header#size.: u64, |
| 38 | } |
| 39 | |
| 40 | impl Headerrust-analyzer cargo implib 0.3.3 ar/Header# { |
| 41 | /// Creates a header with the given file identifier and size, and all |
| 42 | /// other fields set to zero. |
| 43 | pub fn newrust-analyzer cargo implib 0.3.3 ar/impl#[Header]new().(identifier: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, size: u64) -> Headerrust-analyzer cargo implib 0.3.3 ar/Header# { |
| 44 | Headerrust-analyzer cargo implib 0.3.3 ar/Header# { |
| 45 | identifierrust-analyzer cargo implib 0.3.3 ar/Header#identifier., |
| 46 | mtimerust-analyzer cargo implib 0.3.3 ar/Header#mtime.: 0, |
| 47 | uidrust-analyzer cargo implib 0.3.3 ar/Header#uid.: 0, |
| 48 | gidrust-analyzer cargo implib 0.3.3 ar/Header#gid.: 0, |
| 49 | moderust-analyzer cargo implib 0.3.3 ar/Header#mode.: 0o644, |
| 50 | sizerust-analyzer cargo implib 0.3.3 ar/Header#size., |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /// Returns the file identifier. |
| 55 | pub fn identifierrust-analyzer cargo implib 0.3.3 ar/impl#[Header]identifier().(&self) -> &[u8] { |
| 56 | &self.identifierrust-analyzer cargo implib 0.3.3 ar/Header#identifier. |
| 57 | } |
| 58 | |
| 59 | /// Sets the mode bits for this file. |
| 60 | pub fn set_moderust-analyzer cargo implib 0.3.3 ar/impl#[Header]set_mode().(&mut self, mode: u32) { |
| 61 | self.moderust-analyzer cargo implib 0.3.3 ar/Header#mode. = mode; |
| 62 | } |
| 63 | |
| 64 | /// Returns the length of the file, in bytes. |
| 65 | pub fn sizerust-analyzer cargo implib 0.3.3 ar/impl#[Header]size().(&self) -> u64 { |
| 66 | self.sizerust-analyzer cargo implib 0.3.3 ar/Header#size. |
| 67 | } |
| 68 | |
| 69 | /// Validates the header is somewhat sane against the specification. |
| 70 | pub fn validaterust-analyzer cargo implib 0.3.3 ar/impl#[Header]validate().(&self) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> { |
| 71 | ensurerust-analyzer cargo implib 0.3.3 ar/error/ensure!!( |
| 72 | num_digitsrust-analyzer cargo implib 0.3.3 ar/num_digits().(self.mtimerust-analyzer cargo implib 0.3.3 ar/Header#mtime., 10) <= 12, |
| 73 | "MTime `{}` > 12 digits", |
| 74 | self.mtimerust-analyzer cargo implib 0.3.3 ar/Header#mtime. |
| 75 | ); |
| 76 | ensurerust-analyzer cargo implib 0.3.3 ar/error/ensure!!( |
| 77 | num_digitsrust-analyzer cargo implib 0.3.3 ar/num_digits().(self.uidrust-analyzer cargo implib 0.3.3 ar/Header#uid., 10) <= 6, |
| 78 | "UID `{}` > 6 digits", |
| 79 | self.uidrust-analyzer cargo implib 0.3.3 ar/Header#uid. |
| 80 | ); |
| 81 | ensurerust-analyzer cargo implib 0.3.3 ar/error/ensure!!( |
| 82 | num_digitsrust-analyzer cargo implib 0.3.3 ar/num_digits().(self.gidrust-analyzer cargo implib 0.3.3 ar/Header#gid., 10) <= 6, |
| 83 | "GID `{}` > 6 digits", |
| 84 | self.gidrust-analyzer cargo implib 0.3.3 ar/Header#gid. |
| 85 | ); |
| 86 | ensurerust-analyzer cargo implib 0.3.3 ar/error/ensure!!( |
| 87 | num_digitsrust-analyzer cargo implib 0.3.3 ar/num_digits().(self.moderust-analyzer cargo implib 0.3.3 ar/Header#mode., 8) <= 8, |
| 88 | "Mode `{:o}` > 8 octal digits", |
| 89 | self.moderust-analyzer cargo implib 0.3.3 ar/Header#mode. |
| 90 | ); |
| 91 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | #[inline] |
| 96 | fn num_digitsrust-analyzer cargo implib 0.3.3 ar/num_digits().<N: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<u64>>(val: N, radix: u64) -> u64 { |
| 97 | let mut val = val.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().(); |
| 98 | if val rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialEq<Self>`]eq().0 { |
| 99 | return 1; |
| 100 | } |
| 101 | |
| 102 | let mut digits = 0; |
| 103 | while val rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialEq<Self>`]ne().0 { |
| 104 | val rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`DivAssign<Self>`]div_assign()./=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`DivAssign<Self>`]div_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`DivAssign<Self>`]div_assign().radix; |
| 105 | digits rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().1; |
| 106 | } |
| 107 | digits |
| 108 | } |