Added creation of files archive

This commit is contained in:
Jef Roosens 2022-01-30 23:54:05 +01:00
parent 2e344eecc7
commit e26e2746de
Signed by untrusted user: Jef Roosens
GPG key ID: B580B976584B5F30
4 changed files with 85 additions and 61 deletions

95
src/repo/repo.v Normal file
View file

@ -0,0 +1,95 @@
module repo
import os
import package
// subpath where the uncompressed version of the files archive is stored
const files_subpath = 'files'
// subpath where the uncompressed version of the repo archive is stored
const repo_subpath = 'repo'
// Dummy struct to work around the fact that you can only share structs, maps &
// arrays
pub struct Dummy {
x int
}
// This struct manages a single repository.
pub struct Repo {
mut:
mutex shared Dummy
pub:
// Where to store repository files; should exist
repo_dir string [required]
// Where to find packages; packages are expected to all be in the same directory
pkg_dir string [required]
}
// new creates a new Repo & creates the directories as needed
pub fn new(repo_dir string, pkg_dir string) ?Repo {
if !os.is_dir(repo_dir) {
os.mkdir_all(repo_dir) or { return error('Failed to create repo directory.') }
}
if !os.is_dir(pkg_dir) {
os.mkdir_all(pkg_dir) or { return error('Failed to create package directory.') }
}
return Repo{
repo_dir: repo_dir
pkg_dir: pkg_dir
}
}
// add_from_path adds a package from an arbitrary path & moves it into the pkgs
// directory if necessary.
pub fn (r &Repo) add_from_path(pkg_path string) ?bool {
pkg := package.read_pkg(pkg_path) or { return error('Failed to read package file: $err.msg') }
added := r.add(pkg) ?
// 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()))
// 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) ?
}
}
return added
}
// add adds a given Pkg to the repository
fn (r &Repo) add(pkg &package.Pkg) ?bool {
pkg_dir := r.pkg_path(pkg)
// We can't add the same package twice
if os.exists(pkg_dir) {
return false
}
os.mkdir(pkg_dir) or { return error('Failed to create package directory.') }
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.')
}
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.')
}
r.sync() ?
return true
}
// Returns the path where the given package's desc & files files are stored
fn (r &Repo) pkg_path(pkg &package.Pkg) string {
return os.join_path(r.repo_dir, '$pkg.info.name-$pkg.info.version')
}

80
src/repo/sync.v Normal file
View file

@ -0,0 +1,80 @@
module repo
import os
fn archive_add_entry(archive &C.archive, entry &C.archive_entry, file_path &string, inner_path &string) {
st := C.stat{}
unsafe {
C.stat(&char(file_path.str), &st)
}
C.archive_entry_set_pathname(entry, &char(inner_path.str))
C.archive_entry_copy_stat(entry, &st)
C.archive_write_header(archive, entry)
mut fd := C.open(&char(file_path.str), C.O_RDONLY)
defer {
C.close(fd)
}
// Write the file to the archive
buf := [8192]byte{}
mut len := C.read(fd, &buf, sizeof(buf))
for len > 0 {
C.archive_write_data(archive, &buf, len)
len = C.read(fd, &buf, sizeof(buf))
}
}
// Re-generate the repo archive files
fn (r &Repo) sync() ? {
// TODO also write files archive
lock r.mutex {
a_db := C.archive_write_new()
a_files := C.archive_write_new()
entry := C.archive_entry_new()
// This makes the archive a gzip-compressed tarball
C.archive_write_add_filter_gzip(a_db)
C.archive_write_set_format_pax_restricted(a_db)
C.archive_write_add_filter_gzip(a_files)
C.archive_write_set_format_pax_restricted(a_files)
// TODO add symlink to .tar.gz version
db_path := os.join_path_single(r.repo_dir, 'repo.db')
files_path := os.join_path_single(r.repo_dir, 'repo.files')
C.archive_write_open_filename(a_db, &char(db_path.str))
C.archive_write_open_filename(a_files, &char(files_path.str))
// Iterate over each directory
for d in os.ls(r.repo_dir) ?.filter(os.is_dir(os.join_path_single(r.repo_dir,
it))) {
// desc
mut inner_path := os.join_path_single(d, 'desc')
mut actual_path := os.join_path_single(r.repo_dir, inner_path)
archive_add_entry(a_db, entry, actual_path, inner_path)
archive_add_entry(a_files, entry, actual_path, inner_path)
C.archive_entry_clear(entry)
// files
inner_path = os.join_path_single(d, 'files')
actual_path = os.join_path_single(r.repo_dir, inner_path)
archive_add_entry(a_files, entry, actual_path, inner_path)
C.archive_entry_clear(entry)
}
C.archive_write_close(a_db)
C.archive_write_free(a_db)
C.archive_write_close(a_files)
C.archive_write_free(a_files)
}
}