Skip to content

Commit

Permalink
Create a compat-libraries package when backing up libraries
Browse files Browse the repository at this point in the history
The version is always bumped and files appended to it.
  • Loading branch information
bapt committed Mar 18, 2020
1 parent 87e8073 commit 10920fb
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 10 deletions.
70 changes: 65 additions & 5 deletions libpkg/backup_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,73 @@

#include <errno.h>
#include <fcntl.h>
#include <time.h>

#include "pkg.h"
#include "private/event.h"
#include "private/pkg.h"

static int
register_backup(struct pkgdb *db, int fd, const char *path)
{
struct pkgdb_it *it;
struct pkg *pkg = NULL;
int rc = EPKG_OK;
time_t t;
char buf[BUFSIZ];
char *sum;
khint_t k;
struct pkg_file *f;
char *lpath;
struct stat st;

sum = pkg_checksum_generate_fileat(fd, RELATIVE_PATH(path), PKG_HASH_TYPE_SHA256_HEX);

it = pkgdb_query(db, "compat-libraries", MATCH_EXACT);
if (it != NULL) {
if (pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC|PKG_LOAD_FILES) != EPKG_OK)
rc = EPKG_FATAL;
pkgdb_it_free(it);
}
if (pkg == NULL) {
if (pkg_new(&pkg, PKG_FILE) != EPKG_OK) {
return (EPKG_FATAL);
}
pkg->name = xstrdup("compat-libraries");
pkg->origin = xstrdup("compat/libraries");
pkg->comment = xstrdup("Compatibility libraries saved during local packages upgrade\n");
pkg->desc = xstrdup("Compatibility libraries saved during local packages upgrade\n");
pkg->maintainer = xstrdup("root@localhost");
pkg->www = xstrdup("N/A");
pkg->prefix = xstrdup("/");
pkg->abi = "*";
}
free(pkg->version);
t = time(NULL);
strftime(buf, sizeof(buf), "%Y%m%d%H%M%S", localtime(&t));
if (pkg->filehash != NULL && (k = kh_get_pkg_files(pkg->filehash, path)) != kh_end(pkg->filehash)) {
f = kh_val(pkg->filehash, k);
kh_del_pkg_files(pkg->filehash, k);
DL_DELETE(pkg->files, f);
pkg_file_free(f);
}
xasprintf(&lpath, "%s/%s", ctx.backup_library_path, path);
pkg_addfile(pkg, lpath, sum, false);
free(lpath);
pkg->version = xstrdup(buf);
pkg_analyse_files(NULL, pkg, ctx.pkg_rootdir);
pkg_open_root_fd(pkg);
f = NULL;
while (pkg_files(pkg, &f) == EPKG_OK) {
if (fstatat(pkg->rootfd, RELATIVE_PATH(f->path), &st, AT_SYMLINK_NOFOLLOW) != -1)
pkg->flatsize += st.st_size;
}
pkgdb_register_finale(db, pkgdb_register_pkg(db, pkg, 0, "backuplib"), "backuplib");
return (EPKG_OK);
}

void
backup_library(struct pkg *p, const char *path)
backup_library(struct pkgdb *db, struct pkg *p, const char *path)
{
const char *libname = strrchr(path, '/');
char buf[BUFSIZ];
Expand All @@ -40,7 +100,6 @@ backup_library(struct pkg *p, const char *path)
ssize_t nread, nwritten;

pkg_open_root_fd(p);

from = to = backupdir = -1;

if (libname == NULL)
Expand Down Expand Up @@ -82,9 +141,6 @@ backup_library(struct pkg *p, const char *path)
goto out;
}

close(backupdir);
backupdir = -1;

while (nread = read(from, buf, sizeof(buf)), nread > 0) {
outbuf = buf;
do {
Expand All @@ -104,9 +160,13 @@ backup_library(struct pkg *p, const char *path)
goto out;
}
close(from);
register_backup(db, backupdir, libname);
close(backupdir);
backupdir = -1;
return;
}


out:
pkg_emit_errno("Fail to backup the library", libname);
if (backupdir >= 0)
Expand Down
2 changes: 1 addition & 1 deletion libpkg/pkg_add.c
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ pkg_add_cleanup_old(struct pkgdb *db, struct pkg *old, struct pkg *new, int flag
libname = strrchr(f->path, '/');
if (libname != NULL &&
kh_contains(strings, old->shlibs_provided, libname+1)) {
backup_library(old, f->path);
backup_library(db, old, f->path);
}
}
pkg_delete_file(old, f, flags & PKG_DELETE_FORCE ? 1 : 0);
Expand Down
2 changes: 1 addition & 1 deletion libpkg/private/pkg.h
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ int pkg_add_fromdir(struct pkg *, const char *);
struct pkg_dep* pkg_adddep_chain(struct pkg_dep *chain,
struct pkg *pkg, const char *name, const char *origin, const
char *version, bool locked);
void backup_library(struct pkg *pkg, const char *name);
void backup_library(struct pkgdb *, struct pkg *, const char *);
int suggest_arch(struct pkg *, bool);

#endif
47 changes: 44 additions & 3 deletions tests/frontend/backup_lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ basic_body() {
files: {
${TMPDIR}/libempty.so.1: "",
}
EOF

atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "foo" "foo" "1"
cat << EOF >> foo.ucl
files: {
${TMPDIR}/libfoo.so.1: "",
}
EOF
mkdir ${TMPDIR}/target
touch empty.c
cc -shared -Wl,-soname=libempty.so.1 empty.c -o libempty.so.1
cc -shared -Wl,-soname=libfoo.so.1 empty.c -o libfoo.so.1
sum=$(openssl dgst -sha256 -hex libempty.so.1)

atf_check \
Expand All @@ -25,17 +31,27 @@ EOF
-s exit:0 \
pkg create -M test.ucl

atf_check \
-o empty \
-e empty \
-s exit:0 \
pkg create -M foo.ucl

atf_check \
-e empty \
-o empty \
-s exit:0 \
pkg -o BACKUP_LIBRARIES=true -o REPOS_DIR=/dev/null -r ${TMPDIR}/target install -qfy ${TMPDIR}/test-1.txz

atf_check \
-e empty \
-o empty \
-s exit:0 \
pkg -o BACKUP_LIBRARIES=true -o REPOS_DIR=/dev/null -r ${TMPDIR}/target install -qfy ${TMPDIR}/foo-1.txz

atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "test" "test" "2"

atf_check \
-o empty \
-e empty \
-s exit:0 \
pkg create -M test.ucl

Expand All @@ -62,4 +78,29 @@ EOF
-e empty \
-s exit:0 \
ls target/back/libempty.so.1
atf_check \
-o inline:"/back/libempty.so.1\n" \
pkg -r ${TMPDIR}/target query "%Fp" compat-libraries
rm foo-1.txz
atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "foo" "foo" "2"
atf_check \
-s exit:0 \
pkg create -M foo.ucl
atf_check \
-o ignore \
-s exit:0 pkg repo .
atf_check \
-o ignore \
pkg -o BACKUP_LIBRARY_PATH=/back/ -o BACKUP_LIBRARIES=true -o REPOS_DIR=${TMPDIR}/reposconf -r ${TMPDIR}/target update -f
version1=$(pkg -r ${TMPDIR}/target query "%v" compat-libraries)
atf_check \
-e empty \
-o ignore \
-s exit:0 \
pkg -o BACKUP_LIBRARY_PATH=/back/ -o BACKUP_LIBRARIES=true -o REPOS_DIR=${TMPDIR}/reposconf -r ${TMPDIR}/target upgrade -y
atf_check \
-o inline:"/back/libempty.so.1\n/back/libfoo.so.1\n" \
pkg -r ${TMPDIR}/target query "%Fp" compat-libraries
version2=$(pkg -r ${TMPDIR}/target query "%v" compat-libraries)
[ ${version2} -ge ${version1} ] || atf_fail "the version hasn't been bumped ${version2} >= ${version1}"
}

1 comment on commit 10920fb

@bdrewery
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool!

Please sign in to comment.