|
| 1 | +use std::fs; |
| 2 | +use std::io::{self, Read, Seek}; |
| 3 | +use std::ops::{Deref, DerefMut}; |
| 4 | +use std::path::Path; |
| 5 | + |
| 6 | +use zip8::{ |
| 7 | + read::ZipFile, |
| 8 | + result::{ZipError, ZipResult}, |
| 9 | +}; |
| 10 | + |
| 11 | +#[derive(Clone, Debug)] |
| 12 | +pub struct ZipArchive<R: Read + Seek>(zip8::ZipArchive<R>); |
| 13 | + |
| 14 | +impl<R: Read + Seek> Deref for ZipArchive<R> { |
| 15 | + type Target = zip8::ZipArchive<R>; |
| 16 | + |
| 17 | + fn deref(&self) -> &Self::Target { |
| 18 | + &self.0 |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +impl<R: Read + Seek> DerefMut for ZipArchive<R> { |
| 23 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 24 | + &mut self.0 |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +impl<R: Read + Seek> ZipArchive<R> { |
| 29 | + pub fn new(reader: R) -> ZipResult<Self> { |
| 30 | + zip8::ZipArchive::new(reader).map(Self) |
| 31 | + } |
| 32 | + |
| 33 | + /// We need this custom extract function to support symlinks. |
| 34 | + /// This is based on https://github.com/zip-rs/zip/pull/213. |
| 35 | + /// |
| 36 | + /// We must be careful with this implementation since it is not |
| 37 | + /// protected against malicious symlinks, but we trust the binaries |
| 38 | + /// provided by chromium. |
| 39 | + pub fn extract<P: AsRef<Path>>(&mut self, directory: P) -> ZipResult<()> { |
| 40 | + for i in 0..self.len() { |
| 41 | + let mut file = self.by_index(i)?; |
| 42 | + |
| 43 | + let filepath = file |
| 44 | + .enclosed_name() |
| 45 | + .ok_or(ZipError::InvalidArchive("Invalid file path".into()))?; |
| 46 | + let outpath = directory.as_ref().join(filepath); |
| 47 | + |
| 48 | + if file.is_dir() { |
| 49 | + fs::create_dir_all(&outpath)?; |
| 50 | + } else { |
| 51 | + if let Some(p) = outpath.parent() { |
| 52 | + if !p.exists() { |
| 53 | + fs::create_dir_all(p)?; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + match read_symlink(&mut file)? { |
| 58 | + Some(target) => { |
| 59 | + create_symlink(target, &outpath)?; |
| 60 | + } |
| 61 | + None => { |
| 62 | + let mut outfile = fs::File::create(&outpath)?; |
| 63 | + io::copy(&mut file, &mut outfile)?; |
| 64 | + |
| 65 | + // Get and Set permissions |
| 66 | + #[cfg(unix)] |
| 67 | + { |
| 68 | + use std::os::unix::fs::PermissionsExt; |
| 69 | + if let Some(mode) = file.unix_mode() { |
| 70 | + fs::set_permissions(&outpath, fs::Permissions::from_mode(mode))?; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + Ok(()) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +fn read_symlink<R: Read + Seek>(entry: &mut ZipFile<'_, R>) -> ZipResult<Option<Vec<u8>>> { |
| 82 | + if let Some(mode) = entry.unix_mode() { |
| 83 | + const S_IFLNK: u32 = 0o120000; // symbolic link |
| 84 | + if mode & S_IFLNK == S_IFLNK { |
| 85 | + let mut contents = Vec::new(); |
| 86 | + entry.read_to_end(&mut contents)?; |
| 87 | + return Ok(Some(contents)); |
| 88 | + } |
| 89 | + } |
| 90 | + Ok(None) |
| 91 | +} |
| 92 | + |
| 93 | +#[cfg(target_family = "unix")] |
| 94 | +fn create_symlink(link_target: Vec<u8>, link_path: &Path) -> ZipResult<()> { |
| 95 | + use std::os::unix::ffi::OsStringExt as _; |
| 96 | + |
| 97 | + let link_target = std::ffi::OsString::from_vec(link_target); |
| 98 | + std::os::unix::fs::symlink(link_target, link_path)?; |
| 99 | + |
| 100 | + Ok(()) |
| 101 | +} |
| 102 | + |
| 103 | +#[cfg(target_family = "windows")] |
| 104 | +fn create_symlink(link_target: Vec<u8>, link_path: &Path) -> ZipResult<()> { |
| 105 | + // Only supports UTF-8 paths which is enough for our usecase |
| 106 | + let link_target = String::from_utf8(link_target) |
| 107 | + .map_err(|_| ZipError::InvalidArchive("Invalid symlink target name".into()))?; |
| 108 | + std::os::windows::fs::symlink_file(link_target, link_path)?; |
| 109 | + |
| 110 | + Ok(()) |
| 111 | +} |
0 commit comments