2022-01-09 22:30:07 +01:00
|
|
|
module repo
|
|
|
|
|
|
|
|
import os
|
2022-01-19 18:54:33 +01:00
|
|
|
import package
|
2022-02-21 22:22:36 +01:00
|
|
|
import util
|
2022-01-11 18:19:41 +01:00
|
|
|
|
2022-03-27 16:33:06 +02:00
|
|
|
// Manages a group of repositories. Each repository contains one or more
|
|
|
|
// arch-repositories, each of which represent a specific architecture.
|
|
|
|
pub struct RepoGroupManager {
|
2022-01-11 16:37:49 +01:00
|
|
|
mut:
|
2022-02-21 22:22:36 +01:00
|
|
|
mutex shared util.Dummy
|
2022-01-11 16:08:11 +01:00
|
|
|
pub:
|
2022-03-27 16:33:06 +02:00
|
|
|
// Where to store repositories' files
|
|
|
|
data_dir string [required]
|
|
|
|
// Where packages are stored; each architecture gets its own subdirectory
|
2022-01-13 21:47:14 +01:00
|
|
|
pkg_dir string [required]
|
2022-03-27 16:33:06 +02:00
|
|
|
// The default architecture to use for a repository. In reality, this value
|
|
|
|
// is only required when a package with architecture "any" is added as the
|
2022-03-27 19:54:49 +02:00
|
|
|
// first package of a repository.
|
2022-03-27 16:33:06 +02:00
|
|
|
default_arch string [required]
|
2022-01-11 16:08:11 +01:00
|
|
|
}
|
|
|
|
|
2022-02-02 13:24:31 +01:00
|
|
|
pub struct RepoAddResult {
|
|
|
|
pub:
|
|
|
|
added bool [required]
|
|
|
|
pkg &package.Pkg [required]
|
|
|
|
}
|
|
|
|
|
2022-03-27 16:33:06 +02:00
|
|
|
// new creates a new RepoGroupManager & creates the directories as needed
|
|
|
|
pub fn new(data_dir string, pkg_dir string, default_arch string) ?RepoGroupManager {
|
|
|
|
if !os.is_dir(data_dir) {
|
|
|
|
os.mkdir_all(data_dir) or { return error('Failed to create repo directory: $err.msg') }
|
2022-01-19 18:54:33 +01:00
|
|
|
}
|
2022-01-13 21:47:14 +01:00
|
|
|
|
2022-01-19 18:54:33 +01:00
|
|
|
if !os.is_dir(pkg_dir) {
|
2022-02-02 13:09:03 +01:00
|
|
|
os.mkdir_all(pkg_dir) or { return error('Failed to create package directory: $err.msg') }
|
2022-01-19 18:54:33 +01:00
|
|
|
}
|
2022-01-13 21:47:14 +01:00
|
|
|
|
2022-03-27 16:33:06 +02:00
|
|
|
return RepoGroupManager{
|
|
|
|
data_dir: data_dir
|
2022-01-19 18:54:33 +01:00
|
|
|
pkg_dir: pkg_dir
|
2022-03-27 16:33:06 +02:00
|
|
|
default_arch: default_arch
|
2022-01-19 18:54:33 +01:00
|
|
|
}
|
2022-01-11 18:19:41 +01:00
|
|
|
}
|
|
|
|
|
2022-03-27 19:54:49 +02:00
|
|
|
// add_pkg_from_path adds a package to a given repo, given the file path to the
|
|
|
|
// pkg archive. It's a wrapper around add_pkg_in_repo that parses the archive
|
|
|
|
// file, passes the result to add_pkg_in_repo, and moves the archive to
|
|
|
|
// r.pkg_dir if it was successfully added.
|
2022-03-27 16:33:06 +02:00
|
|
|
pub fn (r &RepoGroupManager) add_pkg_from_path(repo string, pkg_path string) ?RepoAddResult {
|
2022-03-27 19:54:49 +02:00
|
|
|
pkg := package.read_pkg_archive(pkg_path) or {
|
|
|
|
return error('Failed to read package file: $err.msg')
|
|
|
|
}
|
2022-01-11 16:08:11 +01:00
|
|
|
|
2022-03-27 16:33:06 +02:00
|
|
|
added := r.add_pkg_in_repo(repo, pkg) ?
|
2022-01-19 18:54:33 +01:00
|
|
|
|
|
|
|
// If the add was successful, we move the file to the packages directory
|
|
|
|
if added {
|
|
|
|
dest_path := os.real_path(os.join_path_single(r.pkg_dir, pkg.filename()))
|
2022-01-11 16:08:11 +01:00
|
|
|
|
2022-01-19 18:54:33 +01:00
|
|
|
// Only move the file if it's not already in the package directory
|
|
|
|
if dest_path != os.real_path(pkg_path) {
|
|
|
|
os.mv(pkg_path, dest_path) ?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-02 13:24:31 +01:00
|
|
|
return RepoAddResult{
|
|
|
|
added: added
|
|
|
|
pkg: &pkg
|
|
|
|
}
|
2022-01-11 14:40:25 +01:00
|
|
|
}
|
|
|
|
|
2022-03-27 19:54:49 +02:00
|
|
|
// add_pkg_in_repo adds a package to a given repo. This function is responsible
|
|
|
|
// for inspecting the package architecture. If said architecture is 'any', the
|
|
|
|
// package is added to each arch-repository within the given repo. If none
|
|
|
|
// exist, one is created for provided r.default_arch value. If the architecture
|
|
|
|
// isn't 'any', the package is only added to the specific architecture.
|
2022-03-27 16:33:06 +02:00
|
|
|
fn (r &RepoGroupManager) add_pkg_in_repo(repo string, pkg &package.Pkg) ?bool {
|
2022-03-27 19:54:49 +02:00
|
|
|
if pkg.info.arch != 'any' {
|
|
|
|
return r.add_pkg_in_arch_repo(repo, pkg.info.arch, pkg)
|
|
|
|
}
|
2022-03-27 16:33:06 +02:00
|
|
|
|
2022-03-27 19:54:49 +02:00
|
|
|
repo_dir := os.join_path_single(r.data_dir, repo)
|
2022-03-27 17:00:11 +02:00
|
|
|
|
2022-03-27 19:54:49 +02:00
|
|
|
mut arch_repos := []string{}
|
2022-03-27 16:33:06 +02:00
|
|
|
|
2022-03-27 19:54:49 +02:00
|
|
|
// If this is the first package to be added to the repository, it won't
|
|
|
|
// contain any arch-repos yet.
|
|
|
|
if os.exists(repo_dir) {
|
|
|
|
// We get a listing of all currently present arch-repos in the given repo
|
|
|
|
arch_repos = os.ls(repo_dir) ?.filter(os.is_dir(os.join_path_single(repo_dir,
|
|
|
|
it)))
|
|
|
|
}
|
2022-03-27 16:33:06 +02:00
|
|
|
|
2022-03-27 19:54:49 +02:00
|
|
|
if arch_repos.len == 0 {
|
|
|
|
arch_repos << r.default_arch
|
|
|
|
}
|
2022-03-27 18:22:30 +02:00
|
|
|
|
2022-03-27 19:54:49 +02:00
|
|
|
mut added := false
|
2022-03-27 18:22:30 +02:00
|
|
|
|
2022-03-27 19:54:49 +02:00
|
|
|
// We add the package to each repository. If any of the repositories
|
|
|
|
// return true, the result of the function is also true.
|
|
|
|
for arch in arch_repos {
|
|
|
|
added = added || r.add_pkg_in_arch_repo(repo, arch, pkg) ?
|
2022-03-27 16:33:06 +02:00
|
|
|
}
|
2022-03-27 19:54:49 +02:00
|
|
|
|
|
|
|
return added
|
2022-03-27 16:33:06 +02:00
|
|
|
}
|
|
|
|
|
2022-03-27 19:54:49 +02:00
|
|
|
// add_pkg_in_arch_repo is the function that actually adds a package to a given
|
|
|
|
// arch-repo. It records the package's data in the arch-repo's desc & files
|
|
|
|
// files, and afterwards updates the db & files archives to reflect these
|
|
|
|
// changes. The function returns false if the package was already present in
|
|
|
|
// the repo, and true otherwise.
|
2022-03-27 16:33:06 +02:00
|
|
|
fn (r &RepoGroupManager) add_pkg_in_arch_repo(repo string, arch string, pkg &package.Pkg) ?bool {
|
|
|
|
pkg_dir := os.join_path(r.data_dir, repo, arch, '$pkg.info.name-$pkg.info.version')
|
2022-01-11 16:08:11 +01:00
|
|
|
|
2022-01-19 18:54:33 +01:00
|
|
|
// We can't add the same package twice
|
|
|
|
if os.exists(pkg_dir) {
|
|
|
|
return false
|
2022-01-11 16:08:11 +01:00
|
|
|
}
|
2022-01-09 22:30:07 +01:00
|
|
|
|
2022-02-18 21:59:33 +01:00
|
|
|
// We remove the older package version first, if present
|
2022-03-27 16:33:06 +02:00
|
|
|
r.remove_pkg_from_arch_repo(repo, arch, pkg, false) ?
|
2022-02-18 21:59:33 +01:00
|
|
|
|
2022-03-27 16:33:06 +02:00
|
|
|
os.mkdir_all(pkg_dir) or { return error('Failed to create package directory.') }
|
2022-01-19 18:54:33 +01:00
|
|
|
|
|
|
|
os.write_file(os.join_path_single(pkg_dir, 'desc'), pkg.to_desc()) or {
|
|
|
|
os.rmdir_all(pkg_dir) ?
|
|
|
|
|
|
|
|
return error('Failed to write desc file.')
|
2022-01-09 22:30:07 +01:00
|
|
|
}
|
2022-01-19 18:54:33 +01:00
|
|
|
os.write_file(os.join_path_single(pkg_dir, 'files'), pkg.to_files()) or {
|
|
|
|
os.rmdir_all(pkg_dir) ?
|
|
|
|
|
|
|
|
return error('Failed to write files file.')
|
|
|
|
}
|
2022-01-20 17:11:03 +01:00
|
|
|
|
2022-03-27 16:33:06 +02:00
|
|
|
r.sync(repo, arch) ?
|
2022-01-19 18:54:33 +01:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-03-27 19:54:49 +02:00
|
|
|
// remove_pkg_from_arch_repo removes a package from an arch-repo's database. It
|
|
|
|
// returns false if the package wasn't present in the database. It also
|
|
|
|
// optionally re-syncs the repo archives.
|
2022-03-27 16:33:06 +02:00
|
|
|
fn (r &RepoGroupManager) remove_pkg_from_arch_repo(repo string, arch string, pkg &package.Pkg, sync bool) ?bool {
|
|
|
|
repo_dir := os.join_path(r.data_dir, repo, arch)
|
|
|
|
|
|
|
|
// If the repository doesn't exist yet, the result is automatically false
|
|
|
|
if !os.exists(repo_dir) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-02-18 21:59:33 +01:00
|
|
|
// We iterate over every directory in the repo dir
|
2022-03-27 16:33:06 +02:00
|
|
|
// TODO filter so we only check directories
|
|
|
|
for d in os.ls(repo_dir) ? {
|
2022-03-27 19:54:49 +02:00
|
|
|
// Because a repository only allows a single version of each package,
|
|
|
|
// we need only compare whether the name of the package is the same,
|
|
|
|
// not the version.
|
2022-02-18 21:59:33 +01:00
|
|
|
name := d.split('-')#[..-2].join('-')
|
|
|
|
|
2022-03-27 16:33:06 +02:00
|
|
|
if name == pkg.info.name {
|
2022-02-18 21:59:33 +01:00
|
|
|
// We lock the mutex here to prevent other routines from creating a
|
2022-03-27 19:54:49 +02:00
|
|
|
// new archive while we remove an entry
|
2022-02-18 21:59:33 +01:00
|
|
|
lock r.mutex {
|
2022-03-27 16:33:06 +02:00
|
|
|
os.rmdir_all(os.join_path_single(repo_dir, d)) ?
|
2022-02-18 21:59:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if sync {
|
2022-03-27 16:33:06 +02:00
|
|
|
r.sync(repo, arch) ?
|
2022-02-18 21:59:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|