Skip to content

Commit

Permalink
pkg query: Include the files and sum in '%X'
Browse files Browse the repository at this point in the history
Otherwise two package with the same file but different content in them
will have the same hash.
  • Loading branch information
evadot committed Sep 22, 2020
1 parent 200f8b3 commit c337c10
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 20 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, true));
return (pkg_checksum_calculate(pkg, db, false, true, false));
}
return (EPKG_FATAL);
}
Expand Down
11 changes: 8 additions & 3 deletions libpkg/pkg_checksum.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,15 @@ 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, bool inc_version)
pkg_checksum_type_t type, bool inc_scripts, bool inc_version, bool inc_files)
{
unsigned char *bdigest;
char *olduid, *buf;
size_t blen;
struct pkg_checksum_entry *entries = NULL;
struct pkg_option *option = NULL;
struct pkg_dep *dep = NULL;
struct pkg_file *f = NULL;
int i;

if (pkg == NULL || type >= PKG_HASH_TYPE_UNKNOWN ||
Expand Down Expand Up @@ -286,6 +287,10 @@ pkg_checksum_generate(struct pkg *pkg, char *dest, size_t destlen,
}
}

while (pkg_files(pkg, &f) == EPKG_OK) {
pkg_checksum_add_entry(f->path, f->sum, &entries);
}

/* Sort before hashing */
DL_SORT(entries, pkg_checksum_entry_cmp);

Expand Down Expand Up @@ -619,7 +624,7 @@ pkg_checksum_type_size(pkg_checksum_type_t type)

int
pkg_checksum_calculate(struct pkg *pkg, struct pkgdb *db, bool inc_scripts,
bool inc_version)
bool inc_version, bool inc_files)
{
char *new_digest;
struct pkg_repo *repo;
Expand All @@ -640,7 +645,7 @@ 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, inc_version)
type, inc_scripts, inc_version, inc_files)
!= 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, true) != EPKG_OK) {
if (pkg_checksum_calculate(rp, j->db, false, true, false) != EPKG_OK) {
return (EPKG_FATAL);
}
}
Expand Down
4 changes: 2 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, true) != EPKG_OK) {
if (pkg_checksum_calculate(pkg, universe->j->db, false, true, false) != EPKG_OK) {
*found = NULL;
return (EPKG_FATAL);
}
Expand Down Expand Up @@ -421,7 +421,7 @@ pkg_jobs_universe_handle_provide(struct pkg_jobs_universe *universe,
if (rpkg->digest == NULL) {
pkg_debug(3, "no digest found for package %s", rpkg->uid);
if (pkg_checksum_calculate(rpkg,
universe->j->db, false, true) != EPKG_OK) {
universe->j->db, false, true, false) != 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, false);
pkg_checksum_calculate(pkg, NULL, true, false, true);
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, true) != EPKG_OK) {
meta->digest_format, false, true, false) != 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, true);
pkg_checksum_calculate(p, NULL, false, true, false);
kv_prepend(typeof(p), pkglist, p);
p = NULL;
cnt ++;
Expand Down
4 changes: 2 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, bool inc_version);
pkg_checksum_type_t type, bool inc_scripts, bool inc_version, bool inc_files);

/*
* Calculates checksum for any data.
Expand All @@ -819,7 +819,7 @@ 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,
bool inc_version);
bool inc_version, bool inc_files);
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, true);
pkg_checksum_calculate(pkg, NULL, false, true, false);
abi = pkg->abi != NULL ? pkg->abi : pkg->arch;
if (abi == NULL || !is_valid_abi(abi, true)) {
rc = EPKG_FATAL;
Expand Down
11 changes: 4 additions & 7 deletions tests/frontend/query.sh
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,12 @@ EOF
-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'
sum1=$(pkg query -F ./test-1.txz '%X')
sum2=$(pkg query -F ./test-2.txz '%X')

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

0 comments on commit c337c10

Please sign in to comment.