Skip to content

Commit c337c10

Browse files
committed
pkg query: Include the files and sum in '%X'
Otherwise two package with the same file but different content in them will have the same hash.
1 parent 200f8b3 commit c337c10

File tree

10 files changed

+22
-20
lines changed

10 files changed

+22
-20
lines changed

libpkg/pkg.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1490,7 +1490,7 @@ pkg_validate(struct pkg *pkg, struct pkgdb *db)
14901490
strlen(pkg->digest))) {
14911491
/* Calculate new digest */
14921492
if (pkgdb_ensure_loaded(db, pkg, flags)) {
1493-
return (pkg_checksum_calculate(pkg, db, false, true));
1493+
return (pkg_checksum_calculate(pkg, db, false, true, false));
14941494
}
14951495
return (EPKG_FATAL);
14961496
}

libpkg/pkg_checksum.c

+8-3
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,15 @@ pkg_checksum_entry_cmp(struct pkg_checksum_entry *e1,
211211

212212
int
213213
pkg_checksum_generate(struct pkg *pkg, char *dest, size_t destlen,
214-
pkg_checksum_type_t type, bool inc_scripts, bool inc_version)
214+
pkg_checksum_type_t type, bool inc_scripts, bool inc_version, bool inc_files)
215215
{
216216
unsigned char *bdigest;
217217
char *olduid, *buf;
218218
size_t blen;
219219
struct pkg_checksum_entry *entries = NULL;
220220
struct pkg_option *option = NULL;
221221
struct pkg_dep *dep = NULL;
222+
struct pkg_file *f = NULL;
222223
int i;
223224

224225
if (pkg == NULL || type >= PKG_HASH_TYPE_UNKNOWN ||
@@ -286,6 +287,10 @@ pkg_checksum_generate(struct pkg *pkg, char *dest, size_t destlen,
286287
}
287288
}
288289

290+
while (pkg_files(pkg, &f) == EPKG_OK) {
291+
pkg_checksum_add_entry(f->path, f->sum, &entries);
292+
}
293+
289294
/* Sort before hashing */
290295
DL_SORT(entries, pkg_checksum_entry_cmp);
291296

@@ -619,7 +624,7 @@ pkg_checksum_type_size(pkg_checksum_type_t type)
619624

620625
int
621626
pkg_checksum_calculate(struct pkg *pkg, struct pkgdb *db, bool inc_scripts,
622-
bool inc_version)
627+
bool inc_version, bool inc_files)
623628
{
624629
char *new_digest;
625630
struct pkg_repo *repo;
@@ -640,7 +645,7 @@ pkg_checksum_calculate(struct pkg *pkg, struct pkgdb *db, bool inc_scripts,
640645

641646
new_digest = xmalloc(pkg_checksum_type_size(type));
642647
if (pkg_checksum_generate(pkg, new_digest, pkg_checksum_type_size(type),
643-
type, inc_scripts, inc_version)
648+
type, inc_scripts, inc_version, inc_files)
644649
!= EPKG_OK) {
645650
free(new_digest);
646651
return (EPKG_FATAL);

libpkg/pkg_jobs.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ pkg_jobs_process_remote_pkg(struct pkg_jobs *j, struct pkg *rp,
739739
struct pkg_dep *rdep = NULL;
740740

741741
if (rp->digest == NULL) {
742-
if (pkg_checksum_calculate(rp, j->db, false, true) != EPKG_OK) {
742+
if (pkg_checksum_calculate(rp, j->db, false, true, false) != EPKG_OK) {
743743
return (EPKG_FATAL);
744744
}
745745
}

libpkg/pkg_jobs_universe.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ pkg_jobs_universe_add_pkg(struct pkg_jobs_universe *universe, struct pkg *pkg,
163163
if (pkg->digest == NULL) {
164164
pkg_debug(3, "no digest found for package %s (%s-%s)",
165165
pkg->uid, pkg->name, pkg->version);
166-
if (pkg_checksum_calculate(pkg, universe->j->db, false, true) != EPKG_OK) {
166+
if (pkg_checksum_calculate(pkg, universe->j->db, false, true, false) != EPKG_OK) {
167167
*found = NULL;
168168
return (EPKG_FATAL);
169169
}
@@ -421,7 +421,7 @@ pkg_jobs_universe_handle_provide(struct pkg_jobs_universe *universe,
421421
if (rpkg->digest == NULL) {
422422
pkg_debug(3, "no digest found for package %s", rpkg->uid);
423423
if (pkg_checksum_calculate(rpkg,
424-
universe->j->db, false, true) != EPKG_OK) {
424+
universe->j->db, false, true, false) != EPKG_OK) {
425425
return (EPKG_FATAL);
426426
}
427427
}

libpkg/pkg_printf.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,7 @@ format_int_checksum(UT_string *buf, const void *data, struct percent_esc *p)
14961496
{
14971497
struct pkg *pkg = (struct pkg *)data;
14981498

1499-
pkg_checksum_calculate(pkg, NULL, true, false);
1499+
pkg_checksum_calculate(pkg, NULL, true, false, true);
15001500
return (string_val(buf, pkg->digest, p));
15011501
}
15021502

libpkg/pkg_repo_create.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ pkg_create_repo_worker(struct pkg_fts_item *start, size_t nelts,
396396
if (meta->version == 1) {
397397
if (pkg_checksum_generate(pkg, mdigest,
398398
pkg_checksum_type_size(meta->digest_format),
399-
meta->digest_format, false, true) != EPKG_OK) {
399+
meta->digest_format, false, true, false) != EPKG_OK) {
400400
pkg_emit_error("Cannot generate digest for a package");
401401
ret = EPKG_FATAL;
402402

libpkg/pkgdb.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3167,7 +3167,7 @@ pkgdb_begin_solver(struct pkgdb *db)
31673167
if (it != NULL) {
31683168
kv_init(pkglist);
31693169
while (pkgdb_it_next(it, &p, PKG_LOAD_BASIC|PKG_LOAD_OPTIONS) == EPKG_OK) {
3170-
pkg_checksum_calculate(p, NULL, false, true);
3170+
pkg_checksum_calculate(p, NULL, false, true, false);
31713171
kv_prepend(typeof(p), pkglist, p);
31723172
p = NULL;
31733173
cnt ++;

libpkg/private/pkg.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ bool ucl_object_emit_file(const ucl_object_t *obj, enum ucl_emitter emit_type,
793793
pkg_object* pkg_emit_object(struct pkg *pkg, short flags);
794794

795795
int pkg_checksum_generate(struct pkg *pkg, char *dest, size_t destlen,
796-
pkg_checksum_type_t type, bool inc_scripts, bool inc_version);
796+
pkg_checksum_type_t type, bool inc_scripts, bool inc_version, bool inc_files);
797797

798798
/*
799799
* Calculates checksum for any data.
@@ -819,7 +819,7 @@ pkg_checksum_type_t pkg_checksum_type_from_string(const char *name);
819819
const char* pkg_checksum_type_to_string(pkg_checksum_type_t type);
820820
size_t pkg_checksum_type_size(pkg_checksum_type_t type);
821821
int pkg_checksum_calculate(struct pkg *pkg, struct pkgdb *db, bool inc_scripts,
822-
bool inc_version);
822+
bool inc_version, bool inc_files);
823823
char *pkg_checksum_generate_file(const char *path, pkg_checksum_type_t type);
824824
char *pkg_checksum_generate_fileat(int fd, const char *path,
825825
pkg_checksum_type_t type);

libpkg/repo/binary/update.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ pkg_repo_binary_add_from_manifest(const char *buf, sqlite3 *sqlite, size_t len,
387387
}
388388

389389
if (pkg->digest == NULL || !pkg_checksum_is_valid(pkg->digest, strlen(pkg->digest)))
390-
pkg_checksum_calculate(pkg, NULL, false, true);
390+
pkg_checksum_calculate(pkg, NULL, false, true, false);
391391
abi = pkg->abi != NULL ? pkg->abi : pkg->arch;
392392
if (abi == NULL || !is_valid_abi(abi, true)) {
393393
rc = EPKG_FATAL;

tests/frontend/query.sh

+4-7
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,12 @@ EOF
125125
-s exit:0 \
126126
pkg create -M test2.ucl
127127

128-
atf_check \
129-
-o inline:'2$2$iwohwqpqjwb1uq7mbg489458jatsea8ste78hm9kck6mdwq1z5u7r8n8anjdrea8dx6dt7mszoswxe3k6j13o1iepgwdxi4ecw9kupy\n' \
130-
-e empty \
131-
-s exit:0 \
132-
pkg query -F ./test-1.txz '%X'
128+
sum1=$(pkg query -F ./test-1.txz '%X')
129+
sum2=$(pkg query -F ./test-2.txz '%X')
133130

134131
atf_check \
135-
-o inline:'2$2$iwohwqpqjwb1uq7mbg489458jatsea8ste78hm9kck6mdwq1z5u7r8n8anjdrea8dx6dt7mszoswxe3k6j13o1iepgwdxi4ecw9kupy\n' \
132+
-o empty \
136133
-e empty \
137134
-s exit:0 \
138-
pkg query -F ./test-2.txz '%X'
135+
test "${sum1}" = "${sum2}"
139136
}

0 commit comments

Comments
 (0)