Skip to content

Commit

Permalink
Get rid of the dirname and basename which implement differs depending…
Browse files Browse the repository at this point in the history
… on OSes
  • Loading branch information
bapt committed Dec 3, 2020
1 parent 9ba7144 commit 3c5dde3
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 222 deletions.
4 changes: 1 addition & 3 deletions compat/Makefile.autosetup
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
include @builddir@/mk/defs.mk
LIB= bsd_compat
SRCS= basename.c \
closefrom.c \
dirname.c \
SRCS= closefrom.c \
file_at.c \
humanize_number.c \
strtonum.c \
Expand Down
96 changes: 0 additions & 96 deletions compat/basename.c

This file was deleted.

3 changes: 0 additions & 3 deletions compat/bsd_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@
#include <sys/stat.h>
#include "endian_util.h"

char *bsd_dirname(const char *);
char *bsd_basename(const char *);

#if !HAVE_EACCESS
#define eaccess(_p, _m) access(_p, _m)
#endif
Expand Down
89 changes: 0 additions & 89 deletions compat/dirname.c

This file was deleted.

12 changes: 7 additions & 5 deletions libpkg/backup.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,18 @@ pkgdb_dump(struct pkgdb *db, const char *dest)
int ret;
int destdbfd;
int savedfd;
char *d;

destdbfd = open(bsd_dirname(dest), O_DIRECTORY|O_CLOEXEC);
if (destdbfd == -1) {
pkg_fatal_errno("Unable to access '%s'",
bsd_dirname(dest));
}
d = xstrdup(dest);
d = get_dirname(d);
destdbfd = open(d, O_DIRECTORY|O_CLOEXEC);
if (destdbfd == -1)
pkg_fatal_errno("Unable to access '%s'", d);

savedfd = pkg_get_dbdirfd();
ctx.pkg_dbdirfd = destdbfd;
ret = sqlite3_open(dest, &backup);
free(d);

if (ret != SQLITE_OK) {
ERROR_SQLITE(backup, "sqlite3_open");
Expand Down
25 changes: 18 additions & 7 deletions libpkg/pkg_add.c
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,20 @@ do_extract_dir(struct pkg* pkg, struct archive *a __unused, struct archive_entry
return (EPKG_OK);
}


static bool
try_mkdir(int fd, const char *path)
{
char *p = get_dirname(xstrdup(path));

if (!mkdirat_p(fd, RELATIVE_PATH(p))) {
free(p);
return (false);
}
free(p);
return (true);
}

static int
create_symlinks(struct pkg *pkg, struct pkg_file *f, const char *target)
{
Expand All @@ -420,7 +434,7 @@ create_symlinks(struct pkg *pkg, struct pkg_file *f, const char *target)
retry:
if (symlinkat(target, pkg->rootfd, RELATIVE_PATH(f->temppath)) == -1) {
if (!tried_mkdir) {
if (!mkdirat_p(pkg->rootfd, RELATIVE_PATH(bsd_dirname(f->path))))
if (!try_mkdir(pkg->rootfd, f->path))
return (EPKG_FATAL);
tried_mkdir = true;
goto retry;
Expand Down Expand Up @@ -488,8 +502,7 @@ create_hardlink(struct pkg *pkg, struct pkg_file *f, const char *path)
if (linkat(pkg->rootfd, RELATIVE_PATH(fh->temppath),
pkg->rootfd, RELATIVE_PATH(f->temppath), 0) == -1) {
if (!tried_mkdir) {
if (!mkdirat_p(pkg->rootfd,
RELATIVE_PATH(bsd_dirname(f->path))))
if (!try_mkdir(pkg->rootfd, f->path))
return (EPKG_FATAL);
tried_mkdir = true;
goto retry;
Expand Down Expand Up @@ -544,10 +557,8 @@ create_regfile(struct pkg *pkg, struct pkg_file *f, struct archive *a,
O_CREAT|O_WRONLY|O_EXCL, f->perm);
if (fd == -1) {
if (!tried_mkdir) {
if (!mkdirat_p(pkg->rootfd,
RELATIVE_PATH(bsd_dirname(f->path)))) {
if (!try_mkdir(pkg->rootfd, f->path))
return (EPKG_FATAL);
}
tried_mkdir = true;
goto retry;
}
Expand Down Expand Up @@ -902,7 +913,7 @@ pkg_add_check_pkg_archive(struct pkgdb *db, struct pkg *pkg,
fromstdin = (strcmp(path, "-") == 0);
strlcpy(bd, path, sizeof(bd));
if (!fromstdin) {
basedir = bsd_dirname(bd);
basedir = get_dirname(bd);
strlcpy(bd, basedir, sizeof(bd));
if ((ext = strrchr(path, '.')) == NULL) {
pkg_emit_error("%s has no extension", path);
Expand Down
7 changes: 5 additions & 2 deletions libpkg/pkg_elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,11 @@ analyse_elf(struct pkg *pkg, const char *fpath)
rpath == NULL)
rpath = elf_strptr(e, sh_link, dyn->d_un.d_val);
}
if (rpath != NULL)
shlib_list_from_rpath(rpath, bsd_dirname(fpath));
if (rpath != NULL) {
char *p = xstrdup(fpath);
shlib_list_from_rpath(rpath, get_dirname(p));
free(p);
}

/* Now find all of the NEEDED shared libraries. */

Expand Down
8 changes: 4 additions & 4 deletions libpkg/pkg_repo_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ hash_file(struct pkg_repo_meta *meta, struct pkg *pkg, char *path)
ext = strrchr(path, '.');

strlcpy(tmp_name, path, sizeof(tmp_name));
rel_dir = dirname(tmp_name);
rel_dir = get_dirname(tmp_name);
while (strstr(rel_dir, "/Hashed") != NULL) {
rel_dir = dirname(rel_dir);
rel_dir = get_dirname(rel_dir);
}
strlcpy(tmp_name, rel_dir, sizeof(tmp_name));
rel_dir = (char *)&tmp_name;
Expand All @@ -115,9 +115,9 @@ hash_file(struct pkg_repo_meta *meta, struct pkg *pkg, char *path)
rel_repo++;
}
strlcpy(tmp_repo, rel_repo, sizeof(tmp_repo));
rel_repo = dirname(tmp_repo);
rel_repo = get_dirname(tmp_repo);
while (strstr(rel_repo, "/Hashed") != NULL) {
rel_repo = dirname(rel_repo);
rel_repo = get_dirname(rel_repo);
}
strlcpy(tmp_repo, rel_repo, sizeof(tmp_repo));
rel_repo = (char *)&tmp_repo;
Expand Down
1 change: 1 addition & 0 deletions libpkg/private/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,6 @@ bool mkdirat_p(int fd, const char *path);
int get_socketpair(int *);
int checkflags(const char *mode, int *optr);
bool match_ucl_lists(const char *buffer, const ucl_object_t *globs, const ucl_object_t *regexes);
char *get_dirname(char *dir);

#endif
15 changes: 4 additions & 11 deletions libpkg/repo/binary/fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ pkg_repo_binary_try_fetch(struct pkg_repo *repo, struct pkg *pkg,
char *dir = NULL;
bool fetched = false;
struct stat st;
char *path = NULL;
const char *packagesite = NULL;
ssize_t offset = -1;

Expand Down Expand Up @@ -165,14 +164,8 @@ pkg_repo_binary_try_fetch(struct pkg_repo *repo, struct pkg *pkg,
}

/* Create the dirs in cachedir */
dir = xstrdup(dest);
if ((path = dirname(dir)) == NULL) {
pkg_emit_errno("dirname", dest);
retcode = EPKG_FATAL;
goto cleanup;
}

if ((retcode = mkdirs(path)) != EPKG_OK)
dir = get_dirname(xstrdup(dest));
if ((retcode = mkdirs(dir)) != EPKG_OK)
goto cleanup;

/*
Expand Down Expand Up @@ -244,8 +237,8 @@ pkg_repo_binary_try_fetch(struct pkg_repo *repo, struct pkg *pkg,

if (retcode != EPKG_OK)
unlink(dest);
else if (!mirror && path != NULL) {
(void)pkg_repo_binary_create_symlink(pkg, dest, path);
else if (!mirror && dir != NULL) {
(void)pkg_repo_binary_create_symlink(pkg, dest, dir);
}

/* allowed even if dir is NULL */
Expand Down
3 changes: 2 additions & 1 deletion libpkg/repo/binary/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ sqlite_file_exists(sqlite3_context *ctx, int argc, sqlite3_value **argv)
{
char fpath[MAXPATHLEN];
sqlite3 *db = sqlite3_context_db_handle(ctx);
char *path = bsd_dirname(sqlite3_db_filename(db, "main"));
char *path = get_dirname(xstrdup(sqlite3_db_filename(db, "main")));
char *cksum;

if (argc != 2) {
Expand All @@ -75,6 +75,7 @@ sqlite_file_exists(sqlite3_context *ctx, int argc, sqlite3_value **argv)
} else {
sqlite3_result_int(ctx, 0);
}
free(path);
}

static int
Expand Down
Loading

0 comments on commit 3c5dde3

Please sign in to comment.