Skip to content

Commit

Permalink
pkg query: Fix '%X' to not use the version
Browse files Browse the repository at this point in the history
We don't want the version for '%X' (internal checksum) as we want to use
it to compare if two packages have the same content.
Name, Origin and Arch are still taken in account.
  • Loading branch information
evadot committed Sep 21, 2020
1 parent f8b72fe commit ed15479
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 14 deletions.
2 changes: 1 addition & 1 deletion libpkg/pkg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1490,7 +1490,7 @@ pkg_validate(struct pkg *pkg, struct pkgdb *db)
strlen(pkg->digest))) {
/* Calculate new digest */
if (pkgdb_ensure_loaded(db, pkg, flags)) {
return (pkg_checksum_calculate(pkg, db, false));
return (pkg_checksum_calculate(pkg, db, false, true));
}
return (EPKG_FATAL);
}
Expand Down
11 changes: 7 additions & 4 deletions libpkg/pkg_checksum.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ pkg_checksum_entry_cmp(struct pkg_checksum_entry *e1,

int
pkg_checksum_generate(struct pkg *pkg, char *dest, size_t destlen,
pkg_checksum_type_t type, bool inc_scripts)
pkg_checksum_type_t type, bool inc_scripts, bool inc_version)
{
unsigned char *bdigest;
char *olduid, *buf;
Expand All @@ -227,7 +227,8 @@ pkg_checksum_generate(struct pkg *pkg, char *dest, size_t destlen,

pkg_checksum_add_entry("name", pkg->name, &entries);
pkg_checksum_add_entry("origin", pkg->origin, &entries);
pkg_checksum_add_entry("version", pkg->version, &entries);
if (inc_version)
pkg_checksum_add_entry("version", pkg->version, &entries);
pkg_checksum_add_entry("arch", pkg->arch, &entries);

while (pkg_options(pkg, &option) == EPKG_OK) {
Expand Down Expand Up @@ -617,7 +618,8 @@ pkg_checksum_type_size(pkg_checksum_type_t type)
}

int
pkg_checksum_calculate(struct pkg *pkg, struct pkgdb *db, bool inc_scripts)
pkg_checksum_calculate(struct pkg *pkg, struct pkgdb *db, bool inc_scripts,
bool inc_version)
{
char *new_digest;
struct pkg_repo *repo;
Expand All @@ -637,7 +639,8 @@ pkg_checksum_calculate(struct pkg *pkg, struct pkgdb *db, bool inc_scripts)
}

new_digest = xmalloc(pkg_checksum_type_size(type));
if (pkg_checksum_generate(pkg, new_digest, pkg_checksum_type_size(type), type, inc_scripts)
if (pkg_checksum_generate(pkg, new_digest, pkg_checksum_type_size(type),
type, inc_scripts, inc_version)
!= EPKG_OK) {
free(new_digest);
return (EPKG_FATAL);
Expand Down
2 changes: 1 addition & 1 deletion libpkg/pkg_jobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ pkg_jobs_process_remote_pkg(struct pkg_jobs *j, struct pkg *rp,
struct pkg_dep *rdep = NULL;

if (rp->digest == NULL) {
if (pkg_checksum_calculate(rp, j->db, false) != EPKG_OK) {
if (pkg_checksum_calculate(rp, j->db, false, true) != EPKG_OK) {
return (EPKG_FATAL);
}
}
Expand Down
5 changes: 3 additions & 2 deletions libpkg/pkg_jobs_universe.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ pkg_jobs_universe_add_pkg(struct pkg_jobs_universe *universe, struct pkg *pkg,
if (pkg->digest == NULL) {
pkg_debug(3, "no digest found for package %s (%s-%s)",
pkg->uid, pkg->name, pkg->version);
if (pkg_checksum_calculate(pkg, universe->j->db, false) != EPKG_OK) {
if (pkg_checksum_calculate(pkg, universe->j->db, false, true) != EPKG_OK) {
*found = NULL;
return (EPKG_FATAL);
}
Expand Down Expand Up @@ -420,7 +420,8 @@ pkg_jobs_universe_handle_provide(struct pkg_jobs_universe *universe,
if (unit == NULL) {
if (rpkg->digest == NULL) {
pkg_debug(3, "no digest found for package %s", rpkg->uid);
if (pkg_checksum_calculate(rpkg, universe->j->db, false) != EPKG_OK) {
if (pkg_checksum_calculate(rpkg,
universe->j->db, false, true) != EPKG_OK) {
return (EPKG_FATAL);
}
}
Expand Down
2 changes: 1 addition & 1 deletion libpkg/pkg_printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,7 @@ format_int_checksum(UT_string *buf, const void *data, struct percent_esc *p)
{
struct pkg *pkg = (struct pkg *)data;

pkg_checksum_calculate(pkg, NULL, true);
pkg_checksum_calculate(pkg, NULL, true, false);
return (string_val(buf, pkg->digest, p));
}

Expand Down
2 changes: 1 addition & 1 deletion libpkg/pkg_repo_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ pkg_create_repo_worker(struct pkg_fts_item *start, size_t nelts,
if (meta->version == 1) {
if (pkg_checksum_generate(pkg, mdigest,
pkg_checksum_type_size(meta->digest_format),
meta->digest_format, false) != EPKG_OK) {
meta->digest_format, false, true) != EPKG_OK) {
pkg_emit_error("Cannot generate digest for a package");
ret = EPKG_FATAL;

Expand Down
2 changes: 1 addition & 1 deletion libpkg/pkgdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -3167,7 +3167,7 @@ pkgdb_begin_solver(struct pkgdb *db)
if (it != NULL) {
kv_init(pkglist);
while (pkgdb_it_next(it, &p, PKG_LOAD_BASIC|PKG_LOAD_OPTIONS) == EPKG_OK) {
pkg_checksum_calculate(p, NULL, false);
pkg_checksum_calculate(p, NULL, false, true);
kv_prepend(typeof(p), pkglist, p);
p = NULL;
cnt ++;
Expand Down
5 changes: 3 additions & 2 deletions libpkg/private/pkg.h
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ bool ucl_object_emit_file(const ucl_object_t *obj, enum ucl_emitter emit_type,
pkg_object* pkg_emit_object(struct pkg *pkg, short flags);

int pkg_checksum_generate(struct pkg *pkg, char *dest, size_t destlen,
pkg_checksum_type_t type, bool inc_scripts);
pkg_checksum_type_t type, bool inc_scripts, bool inc_version);

/*
* Calculates checksum for any data.
Expand All @@ -818,7 +818,8 @@ pkg_checksum_type_t pkg_checksum_file_get_type(const char *cksum, size_t clen);
pkg_checksum_type_t pkg_checksum_type_from_string(const char *name);
const char* pkg_checksum_type_to_string(pkg_checksum_type_t type);
size_t pkg_checksum_type_size(pkg_checksum_type_t type);
int pkg_checksum_calculate(struct pkg *pkg, struct pkgdb *db, bool inc_scripts);
int pkg_checksum_calculate(struct pkg *pkg, struct pkgdb *db, bool inc_scripts,
bool inc_version);
char *pkg_checksum_generate_file(const char *path, pkg_checksum_type_t type);
char *pkg_checksum_generate_fileat(int fd, const char *path,
pkg_checksum_type_t type);
Expand Down
2 changes: 1 addition & 1 deletion libpkg/repo/binary/update.c
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ pkg_repo_binary_add_from_manifest(const char *buf, sqlite3 *sqlite, size_t len,
}

if (pkg->digest == NULL || !pkg_checksum_is_valid(pkg->digest, strlen(pkg->digest)))
pkg_checksum_calculate(pkg, NULL, false);
pkg_checksum_calculate(pkg, NULL, false, true);
abi = pkg->abi != NULL ? pkg->abi : pkg->arch;
if (abi == NULL || !is_valid_abi(abi, true)) {
rc = EPKG_FATAL;
Expand Down
35 changes: 35 additions & 0 deletions tests/frontend/query.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ files: {
"${TMPDIR}/plop": ""
"${TMPDIR}/bla": ""
}
EOF
atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg test2 test 2
cat >> test2.ucl << EOF
options: {
"OPT1": "on"
"OPT2": "off"
}
files: {
"${TMPDIR}/plop": ""
"${TMPDIR}/bla": ""
}
EOF
atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg plop plop 1
cat >> plop.ucl << EOF
Expand Down Expand Up @@ -101,4 +112,28 @@ EOF
-e empty \
-s exit:0 \
pkg query -e "%#O == 0" "%n"

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

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

atf_check \
-o inline:'2$2$iwohwqpqjwb1uq7mbg489458jatsea8ste78hm9kck6mdwq1z5u7r8n8anjdrea8dx6dt7mszoswxe3k6j13o1iepgwdxi4ecw9kupy\n' \
-e empty \
-s exit:0 \
pkg query -F ./test-1.txz '%X'

atf_check \
-o inline:'2$2$iwohwqpqjwb1uq7mbg489458jatsea8ste78hm9kck6mdwq1z5u7r8n8anjdrea8dx6dt7mszoswxe3k6j13o1iepgwdxi4ecw9kupy\n' \
-e empty \
-s exit:0 \
pkg query -F ./test-2.txz '%X'
}

0 comments on commit ed15479

Please sign in to comment.