manned/indexer/src/sys_alpine.rs
Yorhel 83ab6c3671 Get rid of package categories
Whether or not the package name itself or the (category,name) tuple
uniquely identified a package within a system has been a source of
confusion for a long time. Back in
03d278e4ff I ended up playing playing it
"safe" by going for (category,name), but in practice this doesn't make a
whole lot of sense. While it's *possible* for the same package name to
refer to completely different packages in different "categories", in
reality distributions can't sanely support this anyway.

For distributions where the category referred to a repository, the only
cases where the same package name was used in different repos was when
the package has moved from one repo to another. Those should certainly
not be treated as different packages.

For distributions where the category really referred to a category,
there's the Debian approach where the category is purely a tag and
doesn't help identify the package in any way, and then there's FreeBSD
where the category technically ought to be part of the name.  There were
a few cases where FreeBSD used categories to separate out different
versions of the same package (e.g. ipv6 vs non-ipv6), but none were
relevant for man pages so I ended up merging those as well.

Getting rid of the categories simplifies and shortens URLs, unclutters
the UI a little bit and merges the packages in listings that should've
been merged all along.

Migration script:

  -- Merge packages that are in multiple categories.
  -- All versions are moved to the package with the lowest ID.
  -- If the same version already exists in a lower ID, the higher-ID version is deleted.
  BEGIN;
  WITH migrate(old, new, second) AS (
    SELECT q.id, MIN(p.id), MAX(p.id)
      FROM packages p
      JOIN packages q ON q.id > p.id AND p.system = q.system AND p.name = q.name
     GROUP BY q.id
  ), ded(n) AS (
    UPDATE packages SET dead = false
      FROM migrate m
      JOIN packages q ON q.id = m.old
     WHERE packages.id = m.new AND packages.dead AND NOT q.dead
    RETURNING 1
  ), mov(n) AS (
    UPDATE package_versions SET package = m.new
      FROM migrate m
     WHERE package_versions.package = m.old
       AND NOT EXISTS(
          SELECT 1
            FROM package_versions v
           WHERE v.package IN(m.new, m.second)
             AND v.version = package_versions.version)
    RETURNING 1
  ), del(n) AS (
    DELETE FROM packages WHERE id IN(SELECT old FROM migrate)
    RETURNING 1
  ) SELECT (SELECT count(*) FROM migrate) AS migrate,
           (SELECT count(*) FROM ded) AS ded,
           (SELECT count(*) FROM mov) AS mov,
           (SELECT count(*) FROM del) AS del;

  ALTER TABLE packages DROP CONSTRAINT packages_system_name_category_key;
  CREATE UNIQUE INDEX packages_system_name_key ON packages (system, name);
  ALTER TABLE packages DROP COLUMN category;
  COMMIT;
2024-04-28 10:37:04 +02:00

98 lines
2.9 KiB
Rust

use std::str::FromStr;
use std::io::{Read,BufRead,BufReader};
use postgres;
use crate::archive;
use crate::open;
use crate::pkg;
// https://git.alpinelinux.org/apk-tools/tree/doc/apk-repositories.5.scd
// https://git.alpinelinux.org/apk-tools/tree/src/package.c#n874 (apk_pkg_write_index_entry)
pub fn read_index<T: postgres::GenericClient, R: Read>(pg: &mut T, sys: i32, mirror: &str, repo: &str, lst: R) {
let rd = BufReader::new(lst);
let mut name = None;
let mut version = None;
let mut builddate = None;
let mut arch = None;
let mut lineno: u32 = 0;
for line in rd.lines() {
lineno += 1;
let line = match line {
Err(e) => { error!("Can't read package index: {}", e); return },
Ok(x) => x,
};
if line.starts_with("P:") {
name = Some(line[2..].to_string());
} else if line.starts_with("V:") {
version = Some(line[2..].to_string());
} else if line.starts_with("t:") {
builddate = i64::from_str(&line[2..]).ok();
} else if line.starts_with("A:") {
arch = Some(line[2..].to_string());
}
if line != "" {
continue;
}
if name.is_none() || version.is_none() {
warn!("Package without name or version on line {}", lineno);
return;
}
let pname = name.as_ref().unwrap();
let pver = version.as_ref().unwrap();
if pname == "man-pages" || pname.ends_with("-doc") {
let p = format!("{}/{}/x86_64/{}-{}.apk", mirror, repo, pname, pver);
pkg::pkg(pg, pkg::PkgOpt{
force: false,
sys: sys,
pkg: pname,
ver: pver,
date: builddate.map(pkg::Date::Found).unwrap_or(pkg::Date::Max),
arch: arch.as_deref(),
file: open::Path{
path: &p,
cache: false,
canbelocal: false,
},
});
}
name = None;
version = None;
builddate = None;
arch = None;
}
}
pub fn sync<T: postgres::GenericClient>(pg: &mut T, sys: i32, mirror: &str, repo: &str) {
info!("Reading packages from {} {}", mirror, repo);
let path = format!("{}/{}/x86_64/APKINDEX.tar.gz", mirror, repo);
let path = open::Path{ path: &path, cache: true, canbelocal: false };
let mut index = match path.open() {
Err(e) => { error!("Can't read package index: {}", e); return },
Ok(x) => x,
};
let ent = match archive::Archive::open_archive(&mut index) {
Err(e) => { error!("Can't read package index: {}", e); return },
Ok(x) => x,
};
let r = archive::walk(ent, |x| {
if x.path() == Some("APKINDEX") {
read_index(pg, sys, mirror, repo, x);
}
Ok(true)
});
if let Err(e) = r {
error!("Error reading package index: {}", e);
}
}