2022-01-30 23:54:05 +01:00
|
|
|
module repo
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
2022-04-30 20:22:03 +02:00
|
|
|
// archive_add_entry writes a file to an archive, given its path & inner path
|
|
|
|
// inside the archive.
|
2022-01-30 23:54:05 +01:00
|
|
|
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
|
2022-04-30 11:43:06 +02:00
|
|
|
buf := [8192]u8{}
|
2022-01-30 23:54:05 +01:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-30 20:22:03 +02:00
|
|
|
// sync regenerates the repository archive files.
|
2022-03-27 16:33:06 +02:00
|
|
|
fn (r &RepoGroupManager) sync(repo string, arch string) ? {
|
2022-04-07 15:21:27 +02:00
|
|
|
subrepo_path := os.join_path(r.repos_dir, repo, arch)
|
2022-03-27 16:33:06 +02:00
|
|
|
|
2022-01-30 23:54:05 +01:00
|
|
|
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)
|
|
|
|
|
2022-03-27 20:26:58 +02:00
|
|
|
db_path := os.join_path_single(subrepo_path, '${repo}.db.tar.gz')
|
|
|
|
files_path := os.join_path_single(subrepo_path, '${repo}.files.tar.gz')
|
2022-01-30 23:54:05 +01:00
|
|
|
|
|
|
|
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
|
2022-05-14 20:06:08 +02:00
|
|
|
for d in os.ls(subrepo_path)?.filter(os.is_dir(os.join_path_single(subrepo_path,
|
2022-01-30 23:54:05 +01:00
|
|
|
it))) {
|
|
|
|
// desc
|
|
|
|
mut inner_path := os.join_path_single(d, 'desc')
|
2022-03-27 16:33:06 +02:00
|
|
|
mut actual_path := os.join_path_single(subrepo_path, inner_path)
|
2022-01-30 23:54:05 +01:00
|
|
|
|
|
|
|
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')
|
2022-03-27 16:33:06 +02:00
|
|
|
actual_path = os.join_path_single(subrepo_path, inner_path)
|
2022-01-30 23:54:05 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|