Skip to content

Commit aa670a2

Browse files
committed
lib: add xbps_transaction_file_get
1 parent e82437f commit aa670a2

3 files changed

Lines changed: 48 additions & 25 deletions

File tree

include/xbps_api_impl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454

5555
struct archive_entry;
5656

57+
struct xbps_file {
58+
char *sha256;
59+
};
60+
5761
/**
5862
* @private
5963
*/
@@ -94,6 +98,8 @@ bool HIDDEN xbps_transaction_store(struct xbps_handle *, xbps_array_t, xbps_dict
9498
int HIDDEN xbps_transaction_init(struct xbps_handle *);
9599
int HIDDEN xbps_transaction_files(struct xbps_handle *,
96100
xbps_object_iterator_t);
101+
void HIDDEN xbps_transaction_files_free(struct xbps_handle *);
102+
const struct xbps_file HIDDEN *xbps_transaction_file_get(struct xbps_handle *xhp, const char *path);
97103
int HIDDEN xbps_transaction_fetch(struct xbps_handle *,
98104
xbps_object_iterator_t);
99105
int HIDDEN xbps_transaction_pkg_deps(struct xbps_handle *, xbps_array_t, xbps_dictionary_t);

lib/transaction_commit.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ xbps_transaction_commit(struct xbps_handle *xhp)
436436
out:
437437
xbps_object_release(remove_scripts);
438438
xbps_object_iterator_release(iter);
439+
xbps_transaction_files_free(xhp);
439440
if (rv == 0) {
440441
/* Force a pkgdb write for all unpacked pkgs in transaction */
441442
rv = xbps_pkgdb_update(xhp, true, true);

lib/transaction_files.c

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ struct item {
4848
char *file;
4949
size_t len;
5050
struct {
51+
struct xbps_file file;
5152
const char *pkgname;
5253
const char *pkgver;
53-
char *sha256;
5454
const char *target;
5555
uint64_t size;
5656
enum type type;
@@ -311,8 +311,8 @@ collect_obsoletes(struct xbps_handle *xhp)
311311
/*
312312
* Skip unexisting files and keep files with hash mismatch.
313313
*/
314-
if (item->old.sha256 != NULL) {
315-
rv = xbps_file_sha256_check(item->file, item->old.sha256);
314+
if (item->old.file.sha256 != NULL) {
315+
rv = xbps_file_sha256_check(item->file, item->old.file.sha256);
316316
switch (rv) {
317317
case 0:
318318
/* hash matches, we can safely delete and/or overwrite it */
@@ -529,8 +529,11 @@ collect_file(struct xbps_handle *xhp, const char *file, size_t size,
529529
item->old.update = update;
530530
item->old.removepkg = removepkg;
531531
item->old.target = target;
532-
if (sha256)
533-
item->old.sha256 = strdup(sha256);
532+
if (sha256) {
533+
item->old.file.sha256 = strdup(sha256);
534+
if (!item->old.file.sha256)
535+
return errno;
536+
}
534537
} else {
535538
item->new.pkgname = pkgname;
536539
item->new.pkgver = pkgver;
@@ -541,6 +544,11 @@ collect_file(struct xbps_handle *xhp, const char *file, size_t size,
541544
item->new.update = update;
542545
item->new.removepkg = removepkg;
543546
item->new.target = target;
547+
if (sha256) {
548+
item->new.file.sha256 = strdup(sha256);
549+
if (!item->new.file.sha256)
550+
return errno;
551+
}
544552
}
545553
if (item->old.type && item->new.type) {
546554
/*
@@ -580,8 +588,7 @@ collect_files(struct xbps_handle *xhp, xbps_dictionary_t d,
580588
for (i = 0; i < xbps_array_count(a); i++) {
581589
filed = xbps_array_get(a, i);
582590
xbps_dictionary_get_cstring_nocopy(filed, "file", &file);
583-
if (removefile)
584-
xbps_dictionary_get_cstring_nocopy(filed, "sha256", &sha256);
591+
xbps_dictionary_get_cstring_nocopy(filed, "sha256", &sha256);
585592
size = 0;
586593
xbps_dictionary_get_uint64(filed, "size", &size);
587594
rv = collect_file(xhp, file, size, pkgname, pkgver, idx, sha256,
@@ -600,8 +607,7 @@ collect_files(struct xbps_handle *xhp, xbps_dictionary_t d,
600607
xbps_dictionary_get_cstring_nocopy(filed, "file", &file);
601608
size = 0;
602609
xbps_dictionary_get_uint64(filed, "size", &size);
603-
if (removefile)
604-
xbps_dictionary_get_cstring_nocopy(filed, "sha256", &sha256);
610+
xbps_dictionary_get_cstring_nocopy(filed, "sha256", &sha256);
605611
#if 0
606612
/* XXX: how to handle conf_file size */
607613
if (removefile && stat(file, &st) != -1 && size != (uint64_t)st.st_size)
@@ -761,21 +767,6 @@ pathcmp(const void *l1, const void *l2)
761767
return (a->len < b->len) - (b->len < a->len);
762768
}
763769

764-
static void
765-
cleanup(void)
766-
{
767-
struct item *item, *itmp;
768-
769-
HASH_ITER(hh, hashtab, item, itmp) {
770-
HASH_DEL(hashtab, item);
771-
free(item->file);
772-
free(item->old.sha256);
773-
free(item->new.sha256);
774-
free(item);
775-
}
776-
free(items);
777-
}
778-
779770
/*
780771
* xbps_transaction_files:
781772
*
@@ -890,6 +881,31 @@ xbps_transaction_files(struct xbps_handle *xhp, xbps_object_iterator_t iter)
890881
return rv;
891882

892883
rv = collect_obsoletes(xhp);
893-
cleanup();
894884
return rv;
895885
}
886+
887+
void HIDDEN
888+
xbps_transaction_files_free(struct xbps_handle *xhp UNUSED)
889+
{
890+
struct item *item, *itmp;
891+
892+
HASH_ITER(hh, hashtab, item, itmp) {
893+
HASH_DEL(hashtab, item);
894+
free(item->file);
895+
free(item->old.file.sha256);
896+
free(item->new.file.sha256);
897+
free(item);
898+
}
899+
free(items);
900+
}
901+
902+
const struct xbps_file HIDDEN *
903+
xbps_transaction_file_get(struct xbps_handle *xhp UNUSED, const char *path)
904+
{
905+
struct item *item;
906+
907+
item = lookupItem(path);
908+
if (!item)
909+
return NULL;
910+
return &item->new.file;
911+
}

0 commit comments

Comments
 (0)