Skip to content

Commit

Permalink
pkg_add: replace stringlist with c_charv_t
Browse files Browse the repository at this point in the history
  • Loading branch information
bapt committed Dec 30, 2024
1 parent 513e1b1 commit 6485306
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
36 changes: 19 additions & 17 deletions libpkg/pkg_add.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ reopen_tempdir(int rootfd, struct tempdir *t)
}

static struct tempdir *
get_tempdir(int rootfd, const char *path, tempdirs_t *tempdirs, stringlist_t *symlinks_allowed)
get_tempdir(int rootfd, const char *path, tempdirs_t *tempdirs, c_charv_t *symlinks_allowed)
{
struct tempdir *tmpdir = NULL;

Expand Down Expand Up @@ -451,7 +451,7 @@ close_tempdir(struct tempdir *t)
}

static int
create_dir(struct pkg *pkg, struct pkg_dir *d, tempdirs_t *tempdirs, stringlist_t *symlinks_allowed)
create_dir(struct pkg *pkg, struct pkg_dir *d, tempdirs_t *tempdirs, c_charv_t *symlinks_allowed)
{
struct stat st;
struct tempdir *tmpdir = NULL;
Expand Down Expand Up @@ -501,7 +501,7 @@ create_dir(struct pkg *pkg, struct pkg_dir *d, tempdirs_t *tempdirs, stringlist_
/* In case of directories create the dir and extract the creds */
static int
do_extract_dir(struct pkg* pkg, struct archive *a __unused, struct archive_entry *ae,
const char *path, struct pkg *local __unused, tempdirs_t *tempdirs, stringlist_t *symlinks_allowed)
const char *path, struct pkg *local __unused, tempdirs_t *tempdirs, c_charv_t *symlinks_allowed)
{
struct pkg_dir *d;
const struct stat *aest;
Expand Down Expand Up @@ -547,7 +547,7 @@ try_mkdir(int fd, const char *path)

static int
create_symlinks(struct pkg *pkg, struct pkg_file *f, const char *target, tempdirs_t *tempdirs,
stringlist_t *symlinks_allowed)
c_charv_t *symlinks_allowed)
{
struct tempdir *tmpdir = NULL;
int fd;
Expand Down Expand Up @@ -594,7 +594,7 @@ create_symlinks(struct pkg *pkg, struct pkg_file *f, const char *target, tempdir
static int
do_extract_symlink(struct pkg *pkg, struct archive *a __unused, struct archive_entry *ae,
const char *path, struct pkg *local __unused, tempdirs_t *tempdirs,
stringlist_t *symlinks_allowed)
c_charv_t *symlinks_allowed)
{
struct pkg_file *f;
const struct stat *aest;
Expand Down Expand Up @@ -626,7 +626,7 @@ do_extract_symlink(struct pkg *pkg, struct archive *a __unused, struct archive_e

static int
create_hardlink(struct pkg *pkg, struct pkg_file *f, const char *path, tempdirs_t *tempdirs,
stringlist_t *symlinks_allowed)
c_charv_t *symlinks_allowed)
{
bool tried_mkdir = false;
struct pkg_file *fh;
Expand Down Expand Up @@ -702,7 +702,7 @@ create_hardlink(struct pkg *pkg, struct pkg_file *f, const char *path, tempdirs_
static int
do_extract_hardlink(struct pkg *pkg, struct archive *a __unused, struct archive_entry *ae,
const char *path, struct pkg *local __unused, tempdirs_t *tempdirs,
stringlist_t *symlinks_allowed)
c_charv_t *symlinks_allowed)
{
struct pkg_file *f;
const struct stat *aest;
Expand Down Expand Up @@ -749,7 +749,7 @@ open_tempfile(int rootfd, const char *path, int perm)
static int
create_regfile(struct pkg *pkg, struct pkg_file *f, struct archive *a,
struct archive_entry *ae, int fromfd, struct pkg *local, tempdirs_t *tempdirs,
stringlist_t *symlinks_allowed)
c_charv_t *symlinks_allowed)
{
int fd = -1;
size_t len;
Expand Down Expand Up @@ -842,7 +842,7 @@ create_regfile(struct pkg *pkg, struct pkg_file *f, struct archive *a,

static int
do_extract_regfile(struct pkg *pkg, struct archive *a, struct archive_entry *ae,
const char *path, struct pkg *local, tempdirs_t *tempdirs, stringlist_t *symlinks_allowed)
const char *path, struct pkg *local, tempdirs_t *tempdirs, c_charv_t *symlinks_allowed)
{
struct pkg_file *f;
const struct stat *aest;
Expand Down Expand Up @@ -875,14 +875,14 @@ do_extract_regfile(struct pkg *pkg, struct archive *a, struct archive_entry *ae,
static int
do_extract(struct archive *a, struct archive_entry *ae,
int nfiles, struct pkg *pkg, struct pkg *local, tempdirs_t *tempdirs,
stringlist_t *symlinks_allowed)
c_charv_t *symlinks_allowed)
{
int retcode = EPKG_OK;
int ret = 0, cur_file = 0;
char path[MAXPATHLEN];
int (*extract_cb)(struct pkg *pkg, struct archive *a,
struct archive_entry *ae, const char *path, struct pkg *local,
tempdirs_t *tempdirs, stringlist_t *sa);
tempdirs_t *tempdirs, c_charv_t *sa);

#ifndef HAVE_ARC4RANDOM
srand(time(NULL));
Expand Down Expand Up @@ -1357,11 +1357,12 @@ pkg_add_common(struct pkgdb *db, const char *path, unsigned flags,
int ret;
int nfiles;
tempdirs_t tempdirs;
stringlist_t symlinks_allowed = tll_init();
c_charv_t symlinks_allowed;

assert(path != NULL);

pkgvec_init(&tempdirs);
pkgvec_init(&symlinks_allowed);

/*
* Open the package archive file, read all the meta files and set the
Expand Down Expand Up @@ -1464,7 +1465,7 @@ pkg_add_common(struct pkgdb *db, const char *path, unsigned flags,
*/
if (extract) {
pkg_register_cleanup_callback(pkg_rollback_cb, pkg);
tll_push_back(symlinks_allowed, pkg->prefix);
pkgvec_push(&symlinks_allowed, pkg->prefix);
retcode = do_extract(a, ae, nfiles, pkg, local, &tempdirs, &symlinks_allowed);
pkg_unregister_cleanup_callback(pkg_rollback_cb, pkg);
if (retcode != EPKG_OK) {
Expand Down Expand Up @@ -1563,7 +1564,7 @@ pkg_add_common(struct pkgdb *db, const char *path, unsigned flags,
}

cleanup:
tll_free(symlinks_allowed);
pkgvec_free(&symlinks_allowed);

if (openxact)
pkgdb_register_finale(db, retcode, NULL);
Expand Down Expand Up @@ -1662,9 +1663,10 @@ pkg_add_fromdir(struct pkg *pkg, const char *src)
size_t link_len;
bool install_as_user;
tempdirs_t tempdirs;
stringlist_t symlinks_allowed = tll_init();
tll_push_back(symlinks_allowed, pkg->prefix);
c_charv_t symlinks_allowed;

pkgvec_init(&symlinks_allowed);
pkgvec_push(&symlinks_allowed, pkg->prefix);
pkgvec_init(&hardlinks);
pkgvec_init(&tempdirs);
install_as_user = (getenv("INSTALL_AS_USER") != NULL);
Expand Down Expand Up @@ -1843,7 +1845,7 @@ pkg_add_fromdir(struct pkg *pkg, const char *src)
retcode = pkg_extract_finalize(pkg, &tempdirs);

cleanup:
tll_free(symlinks_allowed);
pkgvec_free(&symlinks_allowed);
pkgvec_free_and_free(&hardlinks, free);
close(fromfd);
return (retcode);
Expand Down
2 changes: 1 addition & 1 deletion libpkg/private/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ char *rtrimspace(char *buf);
void hidden_tempfile(char *buf, int buflen, const char *path);
void append_random_suffix(char *buf, int buflen, int suffixlen);
char *json_escape(const char *str);
struct tempdir *open_tempdir(int rootfd, const char *path, stringlist_t *strlist);
struct tempdir *open_tempdir(int rootfd, const char *path, c_charv_t *strlist);
const char *get_http_auth(void);
bool c_charv_contains(c_charv_t *, const char *, bool);

Expand Down
6 changes: 3 additions & 3 deletions libpkg/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ json_escape(const char *str)
}

struct tempdir *
open_tempdir(int rootfd, const char *path, stringlist_t *symlinks_allowed)
open_tempdir(int rootfd, const char *path, c_charv_t *symlinks_allowed)
{
struct stat st;
char walk[MAXPATHLEN];
Expand All @@ -1003,8 +1003,8 @@ open_tempdir(int rootfd, const char *path, stringlist_t *symlinks_allowed)
if (len > 0) {
int flag = AT_SYMLINK_NOFOLLOW;
if (symlinks_allowed != NULL) {
tll_foreach(*symlinks_allowed, t) {
if (STREQ(RELATIVE_PATH(walk), RELATIVE_PATH(t->item)))
for (size_t i = 0; i < symlinks_allowed->len; i++) {
if (STREQ(RELATIVE_PATH(walk), RELATIVE_PATH(symlinks_allowed->d[i])))
flag = 0;
}
}
Expand Down

0 comments on commit 6485306

Please sign in to comment.