1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
extern crate base64; extern crate crypto; extern crate lzma; extern crate std; extern crate tar; use crypto::digest::Digest; use std::io::Read; #[derive(Debug)] pub enum Error { Io(std::io::Error), Lzma(lzma::LzmaError), ParseInt(std::num::ParseIntError), Custom(std::borrow::Cow<'static, str>), } impl From<std::io::Error> for Error { fn from(e: std::io::Error) -> Self { Error::Io(e) } } impl From<lzma::LzmaError> for Error { fn from(e: lzma::LzmaError) -> Self { Error::Lzma(e) } } impl From<std::num::ParseIntError> for Error { fn from(e: std::num::ParseIntError) -> Self { Error::ParseInt(e) } } impl From<&'static str> for Error { fn from(e: &'static str) -> Self { Error::Custom(std::borrow::Cow::from(e)) } } impl From<String> for Error { fn from(e: String) -> Self { Error::Custom(std::borrow::Cow::from(e)) } } #[derive(Debug, Clone)] pub struct Package { pkginfo: PkgInfo, size: u64, filename: std::ffi::OsString, pgpsig: String, md5sum: String, sha256sum: String, files: Vec<std::path::PathBuf>, } impl Package { pub fn load<P>(path: P) -> Result<Package, Error> where P: AsRef<std::path::Path>, { let path = path.as_ref(); let (pkginfo, files) = try!(PkgInfo::load(path)); let mut sig_path = path.as_os_str().to_os_string(); sig_path.push(".sig"); let pgpsig = if let Ok(mut f) = std::fs::File::open(sig_path) { let mut buf = vec![]; try!(f.read_to_end(&mut buf)); base64::encode(&buf) } else { "".to_owned() }; let mut md5 = crypto::md5::Md5::new(); let mut sha256 = crypto::sha2::Sha256::new(); let mut f = try!(std::fs::File::open(path)); loop { let mut buf = [0; 1024]; match f.read(&mut buf) { Ok(0) => { break; } Ok(len) => { md5.input(&buf[..len]); sha256.input(&buf[..len]); } Err(e) => { return Err(Error::from(e)); } } } Ok(Package { pkginfo: pkginfo, size: try!(std::fs::metadata(path)).len(), filename: path.file_name() .expect("Unable to find file_name from package path") .to_os_string(), pgpsig: pgpsig, md5sum: md5.result_str(), sha256sum: sha256.result_str(), files: files, }) } pub fn groups(&self) -> &Vec<String> { &self.pkginfo.groups } pub fn license(&self) -> &Vec<String> { &self.pkginfo.license } pub fn replaces(&self) -> &Vec<String> { &self.pkginfo.replaces } pub fn filename(&self) -> &std::ffi::OsStr { &self.filename } pub fn pkgname(&self) -> &str { &self.pkginfo.pkgname } pub fn pkgbase(&self) -> &str { &self.pkginfo.pkgbase } pub fn pkgver(&self) -> &str { &self.pkginfo.pkgver } pub fn pkgdesc(&self) -> &str { &self.pkginfo.pkgdesc } pub fn csize(&self) -> u64 { self.size } pub fn isize(&self) -> u64 { self.pkginfo.size } pub fn md5sum(&self) -> &str { &self.md5sum } pub fn sha256sum(&self) -> &str { &self.sha256sum } pub fn pgpsig(&self) -> &str { &self.pgpsig } pub fn url(&self) -> &str { &self.pkginfo.url } pub fn arch(&self) -> &str { &self.pkginfo.arch } pub fn builddate(&self) -> u64 { self.pkginfo.builddate } pub fn packager(&self) -> &str { &self.pkginfo.packager } pub fn conflicts(&self) -> &Vec<String> { &self.pkginfo.conflicts } pub fn provides(&self) -> &Vec<String> { &self.pkginfo.provides } pub fn backups(&self) -> &Vec<String> { &self.pkginfo.backups } pub fn depends(&self) -> &Vec<String> { &self.pkginfo.depends } pub fn makedepends(&self) -> &Vec<String> { &self.pkginfo.makedepends } pub fn checkdepends(&self) -> &Vec<String> { &self.pkginfo.checkdepends } pub fn optdepends(&self) -> &Vec<String> { &self.pkginfo.optdepends } pub fn files(&self) -> &Vec<std::path::PathBuf> { &self.files } } #[derive(Debug, Default, Clone)] pub struct PkgInfo { pub pkgname: String, pub pkgbase: String, pub pkgver: String, pub pkgdesc: String, pub url: String, pub builddate: u64, pub packager: String, pub size: u64, pub arch: String, pub license: Vec<String>, pub groups: Vec<String>, pub depends: Vec<String>, pub makedepends: Vec<String>, pub checkdepends: Vec<String>, pub optdepends: Vec<String>, pub conflicts: Vec<String>, pub backups: Vec<String>, pub provides: Vec<String>, pub replaces: Vec<String>, } impl PkgInfo { fn load<P>(path: P) -> Result<(Self, Vec<std::path::PathBuf>), Error> where P: AsRef<std::path::Path>, { let file = try!(std::fs::File::open(path)); let xz_reader = try!(lzma::LzmaReader::new_decompressor(file)); let mut tar_reader = tar::Archive::new(xz_reader); let mut pkginfo = None; let mut files = vec![]; for entry_result in try!(tar_reader.entries()) { let mut entry = try!(entry_result); let path = try!(entry.path()).into_owned(); if path.as_os_str() == ".PKGINFO" && entry.header().entry_type() == tar::EntryType::Regular { let mut body = String::new(); try!(entry.read_to_string(&mut body)); pkginfo = Some(try!(parse_pkginfo(&body))); } if !path.starts_with(".") { files.push(path.to_path_buf()); } } if let Some(pkginfo) = pkginfo { Ok((pkginfo, files)) } else { Err(Error::from(".PKGINFO not found")) } } } fn parse_pkginfo(body: &str) -> Result<PkgInfo, Error> { let mut info = PkgInfo::default(); for line in body.lines() { if line.starts_with('#') { continue; } let mut splitn = line.splitn(2, '='); let key = splitn.next(); let val = splitn.next(); let rest = splitn.next(); if let (Some(key), Some(val), None) = (key, val, rest) { let key = key.trim(); let val = val.trim(); match key { "pkgname" => info.pkgname = val.to_owned(), "pkgbase" => info.pkgbase = val.to_owned(), "pkgver" => info.pkgver = val.to_owned(), "pkgdesc" => info.pkgdesc = val.to_owned(), "url" => info.url = val.to_owned(), "builddate" => info.builddate = try!(val.parse()), "packager" => info.packager = val.to_owned(), "size" => info.size = try!(val.parse()), "arch" => info.arch = val.to_owned(), "license" => info.license.push(val.to_owned()), "group" => info.groups.push(val.to_owned()), "depend" => info.depends.push(val.to_owned()), "makedepend" => info.makedepends.push(val.to_owned()), "checkdepend" => info.checkdepends.push(val.to_owned()), "optdepend" => info.optdepends.push(val.to_owned()), "conflict" => info.conflicts.push(val.to_owned()), "provides" => info.provides.push(val.to_owned()), "backup" => info.backups.push(val.to_owned()), "replaces" => info.replaces.push(val.to_owned()), _ => { return Err(Error::from(format!( "Unknown PKGINFO entry '{}': {}", key, line ))) } } } else { return Err(Error::from(format!("Invalid line: {}", line))); } } Ok(info) }