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
/*
* Copyright (c) 2019 Jonathan Perkin <jonathan@perkin.org.uk>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* pkgdb.rs - handle the package database
*/
/*!
* Module supporting the package database. WIP.
*/
use crate::metadata::MetadataEntry;
use std::fs;
use std::fs::ReadDir;
use std::io;
use std::path::{Path, PathBuf};
/**
* Type of pkgdb. Currently supported formats are `Files` for the legacy
* directory of `+*` files, and `Database` for a sqlite3 backend.
*/
#[derive(Debug)]
pub enum DBType {
/**
* Standard pkg_install pkgdb using files.
*/
Files,
/**
* Future work to support sqlite3 backend. Currently unimplemented.
*/
Database,
}
/**
* A handle to an opened package database.
*/
#[derive(Debug)]
pub struct PkgDB {
dbtype: DBType,
path: PathBuf,
readdir: Option<ReadDir>,
}
/**
* An installed package in a PkgDB.
*/
#[derive(Debug, Default)]
pub struct Package {
path: PathBuf,
pkgbase: String,
pkgname: String,
pkgversion: String,
}
impl PkgDB {
/**
* Open an existing `PkgDB`.
*/
pub fn open(p: &std::path::Path) -> Result<PkgDB, io::Error> {
let mut db = PkgDB {
dbtype: DBType::Files,
path: PathBuf::new(),
readdir: None,
};
/*
* Nothing fancy for now, assume that what the user passed is valid,
* we'll find out soon enough if it isn't.
*/
if p.is_dir() {
db.dbtype = DBType::Files;
db.path = PathBuf::from(p);
db.readdir = Some(fs::read_dir(&db.path).expect("fail"));
} else if p.is_file() {
db.dbtype = DBType::Database;
db.path = PathBuf::from(p);
} else {
return Err(io::Error::new(
io::ErrorKind::NotFound,
"Invalid pkgdb",
));
}
Ok(db)
}
/**
* Ensure package directory is valid. Only for `DBType::Files`.
*/
fn is_valid_pkgdir(&self, pkgdir: &Path) -> bool {
/*
* Skip files such as pkg-vulnerabilities and pkgdb.byfile.db, we're
* only interested in directories.
*/
if pkgdir.is_file() {
return false;
}
/*
* These 3 metadata files are mandatory.
*/
let reqd = vec![
MetadataEntry::Comment.to_filename(),
MetadataEntry::Contents.to_filename(),
MetadataEntry::Desc.to_filename(),
];
for file in reqd {
if !pkgdir.join(file).exists() {
return false;
}
}
true
}
}
impl Package {
/**
* Return a new empty `Package` container.
*/
pub fn new() -> Package {
let package: Package = Default::default();
package
}
/**
* Package basename (no version information).
*/
pub fn pkgbase(&self) -> &String {
&self.pkgbase
}
/**
* Full package name including version.
*/
pub fn pkgname(&self) -> &String {
&self.pkgname
}
/**
* Package version.
*/
pub fn pkgversion(&self) -> &String {
&self.pkgversion
}
/**
* Read metadata for a package. Return a string representation of the
* complete metadata entry.
*
* XXX: Only supports Files for now.
*/
pub fn read_metadata(
&self,
mentry: MetadataEntry,
) -> Result<String, io::Error> {
let fname = self.path.as_path().join(mentry.to_filename());
fs::read_to_string(fname)
}
}
/**
* An iterator over the entries of a package database, returning either a
* valid `Package` handle, an ``io::Error`, or None.
*/
impl Iterator for PkgDB {
type Item = io::Result<Package>;
fn next(&mut self) -> Option<Self::Item> {
let mut package = Package::new();
match self.dbtype {
DBType::Files => loop {
match self.readdir.as_mut().expect("Bad pkgdb read").next()? {
Ok(dir) => {
if !self.is_valid_pkgdir(&dir.path()) {
continue;
}
match dir.file_name().to_str() {
Some(p) => {
let v: Vec<&str> = p.rsplitn(2, '-').collect();
package.path = dir.path();
package.pkgname = p.to_string();
package.pkgbase = v[0].to_string();
package.pkgversion = v[1].to_string();
return Some(Ok(package));
}
_ => {
return Some(Err(io::Error::new(
io::ErrorKind::InvalidData,
"Could not parse package directory",
)))
}
};
}
_ => return None,
};
},
DBType::Database => None,
}
}
}