Skip to content

Commit b75e151

Browse files
committed
lib: use xbps_pkg_path
1 parent 503084a commit b75e151

File tree

3 files changed

+33
-37
lines changed

3 files changed

+33
-37
lines changed

lib/package_unpack.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@
2424
*/
2525

2626
#include <sys/stat.h>
27-
#include <stdio.h>
27+
28+
#include <errno.h>
29+
#include <fcntl.h>
30+
#include <libgen.h>
31+
#include <limits.h>
2832
#include <stdbool.h>
33+
#include <stdio.h>
2934
#include <stdlib.h>
3035
#include <string.h>
31-
#include <errno.h>
32-
#include <fcntl.h>
3336
#include <unistd.h>
34-
#include <libgen.h>
3537

3638
#include "xbps_api_impl.h"
3739

@@ -467,10 +469,11 @@ unpack_archive(struct xbps_handle *xhp,
467469
int HIDDEN
468470
xbps_unpack_binary_pkg(struct xbps_handle *xhp, xbps_dictionary_t pkg_repod)
469471
{
472+
char bpkg[PATH_MAX];
470473
struct archive *ar = NULL;
471474
struct stat st;
472475
const char *pkgver;
473-
char *bpkg = NULL;
476+
ssize_t l;
474477
int pkg_fd = -1, rv = 0;
475478
mode_t myumask;
476479

@@ -479,19 +482,17 @@ xbps_unpack_binary_pkg(struct xbps_handle *xhp, xbps_dictionary_t pkg_repod)
479482
xbps_dictionary_get_cstring_nocopy(pkg_repod, "pkgver", &pkgver);
480483
xbps_set_cb_state(xhp, XBPS_STATE_UNPACK, 0, pkgver, NULL);
481484

482-
bpkg = xbps_repository_pkg_path(xhp, pkg_repod);
483-
if (bpkg == NULL) {
485+
l = xbps_pkg_path(xhp, bpkg, sizeof(bpkg), pkg_repod);
486+
if (l < 0) {
484487
xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL,
485488
errno, pkgver,
486489
"%s: [unpack] cannot determine binary package "
487-
"file for `%s': %s", pkgver, bpkg, strerror(errno));
488-
return errno;
490+
"file: %s", pkgver, strerror(errno));
491+
return -l;
489492
}
490493

491-
if ((ar = archive_read_new()) == NULL) {
492-
free(bpkg);
494+
if ((ar = archive_read_new()) == NULL)
493495
return ENOMEM;
494-
}
495496
/*
496497
* Enable support for tar format and some compression methods.
497498
*/
@@ -574,8 +575,6 @@ xbps_unpack_binary_pkg(struct xbps_handle *xhp, xbps_dictionary_t pkg_repod)
574575
close(pkg_fd);
575576
if (ar != NULL)
576577
archive_read_free(ar);
577-
if (bpkg)
578-
free(bpkg);
579578

580579
/* restore */
581580
umask(myumask);

lib/transaction_fetch.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,19 @@
3636
static int
3737
verify_binpkg(struct xbps_handle *xhp, xbps_dictionary_t pkgd)
3838
{
39+
char binfile[PATH_MAX];
3940
struct xbps_repo *repo;
4041
const char *pkgver, *repoloc, *sha256;
41-
char *binfile;
42+
ssize_t l;
4243
int rv = 0;
4344

4445
xbps_dictionary_get_cstring_nocopy(pkgd, "repository", &repoloc);
4546
xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);
4647

47-
binfile = xbps_repository_pkg_path(xhp, pkgd);
48-
if (binfile == NULL) {
49-
return ENOMEM;
50-
}
48+
l = xbps_pkg_path(xhp, binfile, sizeof(binfile), pkgd);
49+
if (l < 0)
50+
return -l;
51+
5152
/*
5253
* For pkgs in local repos check the sha256 hash.
5354
* For pkgs in remote repos check the RSA signature.
@@ -56,25 +57,23 @@ verify_binpkg(struct xbps_handle *xhp, xbps_dictionary_t pkgd)
5657
rv = errno;
5758
xbps_dbg_printf("%s: failed to get repository "
5859
"%s: %s\n", pkgver, repoloc, strerror(errno));
59-
goto out;
60+
return rv;
6061
}
6162
if (repo->is_remote) {
6263
/* remote repo */
6364
xbps_set_cb_state(xhp, XBPS_STATE_VERIFY, 0, pkgver,
6465
"%s: verifying RSA signature...", pkgver);
6566

6667
if (!xbps_verify_file_signature(repo, binfile)) {
67-
char *sigfile;
6868
rv = EPERM;
6969
xbps_set_cb_state(xhp, XBPS_STATE_VERIFY_FAIL, rv, pkgver,
7070
"%s: the RSA signature is not valid!", pkgver);
7171
xbps_set_cb_state(xhp, XBPS_STATE_VERIFY_FAIL, rv, pkgver,
7272
"%s: removed pkg archive and its signature.", pkgver);
7373
(void)remove(binfile);
74-
sigfile = xbps_xasprintf("%s.sig2", binfile);
75-
(void)remove(sigfile);
76-
free(sigfile);
77-
goto out;
74+
if (xbps_strlcat(binfile, ".sig2", sizeof(binfile)) < sizeof(binfile))
75+
(void)remove(binfile);
76+
return rv;
7877
}
7978
} else {
8079
/* local repo */
@@ -84,13 +83,12 @@ verify_binpkg(struct xbps_handle *xhp, xbps_dictionary_t pkgd)
8483
if ((rv = xbps_file_sha256_check(binfile, sha256)) != 0) {
8584
xbps_set_cb_state(xhp, XBPS_STATE_VERIFY_FAIL, rv, pkgver,
8685
"%s: SHA256 hash is not valid: %s", pkgver, strerror(rv));
87-
goto out;
86+
return rv;
8887
}
8988

9089
}
91-
out:
92-
free(binfile);
93-
return rv;
90+
91+
return 0;
9492
}
9593

9694
static int

lib/transaction_internalize.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
#include <errno.h>
2626
#include <fcntl.h>
27+
#include <limits.h>
2728
#include <stdlib.h>
2829
#include <string.h>
2930

@@ -69,28 +70,27 @@ internalize_script(xbps_dictionary_t pkg_repod, const char *script,
6970
static int
7071
internalize_binpkg(struct xbps_handle *xhp, xbps_dictionary_t pkg_repod)
7172
{
73+
char pkgfile[PATH_MAX];
7274
xbps_dictionary_t filesd = NULL, propsd = NULL;
7375
struct stat st;
7476
struct archive *ar = NULL;
7577
struct archive_entry *entry;
7678
const char *pkgver, *pkgname, *binpkg_pkgver;
79+
ssize_t l;
7780
int pkg_fd = -1;
78-
char *pkgfile;
7981
int rv = 0;
8082

8183
xbps_dictionary_get_cstring_nocopy(pkg_repod, "pkgver", &pkgver);
8284
assert(pkgver);
8385
xbps_dictionary_get_cstring_nocopy(pkg_repod, "pkgname", &pkgname);
8486
assert(pkgname);
8587

86-
pkgfile = xbps_repository_pkg_path(xhp, pkg_repod);
87-
if (pkgfile == NULL)
88-
return -errno;
88+
l = xbps_pkg_path(xhp, pkgfile, sizeof(pkgfile), pkg_repod);
89+
if (l < 0)
90+
return l;
8991

90-
if ((ar = archive_read_new()) == NULL) {
91-
free(pkgfile);
92+
if ((ar = archive_read_new()) == NULL)
9293
return -errno;
93-
}
9494

9595
/*
9696
* Enable support for tar format and gzip/bzip2/lzma compression methods.
@@ -193,7 +193,6 @@ internalize_binpkg(struct xbps_handle *xhp, xbps_dictionary_t pkg_repod)
193193
close(pkg_fd);
194194
if (ar != NULL)
195195
archive_read_free(ar);
196-
free(pkgfile);
197196
return rv;
198197
}
199198

0 commit comments

Comments
 (0)