Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Get rid of the dirname and basename which implement differs depending on OSes
Baptiste Daroussin committed 5 years ago
commit 3c5dde3e6e88398671a2b20ed5c458a05095fdbf
parent 9ba7144
13 files changed +67 -222
modified compat/Makefile.autosetup
@@ -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 \
deleted compat/basename.c
@@ -1,96 +0,0 @@
-
/*	$OpenBSD: basename.c,v 1.14 2005/08/08 08:05:33 espie Exp $	*/
-

-
/*
-
 * Copyright (c) 1997, 2004 Todd C. Miller <Todd.Miller@courtesan.com>
-
 *
-
 * Permission to use, copy, modify, and distribute this software for any
-
 * purpose with or without fee is hereby granted, provided that the above
-
 * copyright notice and this permission notice appear in all copies.
-
 *
-
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-
 */
-

-
#include <errno.h>
-
#ifndef __GLIBC__
-
/*
-
 * GLIBC provides basename in string.h, and defines basename to
-
 * __xpg_basename (which can modify its input argument) in libgen.h.
-
 */
-
#include <libgen.h>
-
#endif
-
#include <stdlib.h>
-
#include <string.h>
-
#include <sys/param.h>
-

-
#include "bsd_compat.h"
-

-
#if !HAVE_BASENAME_R
-
char *
-
basename_r(const char *path, char *bname)
-
{
-
	const char *endp, *startp;
-
	size_t len;
-

-
	/* Empty or NULL string gets treated as "." */
-
	if (path == NULL || *path == '\0') {
-
		bname[0] = '.';
-
		bname[1] = '\0';
-
		return (bname);
-
	}
-

-
	/* Strip any trailing slashes */
-
	endp = path + strlen(path) - 1;
-
	while (endp > path && *endp == '/')
-
		endp--;
-

-
	/* All slashes becomes "/" */
-
	if (endp == path && *endp == '/') {
-
		bname[0] = '/';
-
		bname[1] = '\0';
-
		return (bname);
-
	}
-

-
	/* Find the start of the base */
-
	startp = endp;
-
	while (startp > path && *(startp - 1) != '/')
-
		startp--;
-

-
	len = endp - startp + 1;
-
	if (len >= MAXPATHLEN) {
-
		errno = ENAMETOOLONG;
-
		return (NULL);
-
	}
-
	memcpy(bname, startp, len);
-
	bname[len] = '\0';
-
	return (bname);
-
}
-
#endif
-

-
#if HAVE_BSD_BASENAME
-
char *
-
bsd_basename(const char *path)
-
{
-
    return basename(path);
-
}
-

-
#else
-

-
char *
-
bsd_basename(const char *path)
-
{
-
	static char *bname = NULL;
-

-
	if (bname == NULL) {
-
		bname = (char *)malloc(MAXPATHLEN);
-
		if (bname == NULL)
-
			return (NULL);
-
	}
-
	return (basename_r(path, bname));
-
}
-
#endif
modified compat/bsd_compat.h
@@ -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
deleted compat/dirname.c
@@ -1,89 +0,0 @@
-
/*	$OpenBSD: dirname.c,v 1.13 2005/08/08 08:05:33 espie Exp $	*/
-

-
/*
-
 * Copyright (c) 1997, 2004 Todd C. Miller <Todd.Miller@courtesan.com>
-
 *
-
 * Permission to use, copy, modify, and distribute this software for any
-
 * purpose with or without fee is hereby granted, provided that the above
-
 * copyright notice and this permission notice appear in all copies.
-
 *
-
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-
 */
-

-
#include "bsd_compat.h"
-

-
#if HAVE_BSD_DIRNAME
-

-
#include <libgen.h>
-

-
char *
-
bsd_dirname(const char *path)
-
{
-
	return dirname (path);
-
}
-

-
#else
-

-
#include <errno.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <sys/param.h>
-

-
char *
-
bsd_dirname(const char *path)
-
{
-
	static char *dname = NULL;
-
	size_t len;
-
	const char *endp;
-

-
	if (dname == NULL) {
-
		dname = (char *)malloc(MAXPATHLEN);
-
		if (dname == NULL)
-
			return(NULL);
-
	}
-

-
	/* Empty or NULL string gets treated as "." */
-
	if (path == NULL || *path == '\0') {
-
		dname[0] = '.';
-
		dname[1] = '\0';
-
		return (dname);
-
	}
-

-
	/* Strip any trailing slashes */
-
	endp = path + strlen(path) - 1;
-
	while (endp > path && *endp == '/')
-
		endp--;
-

-
	/* Find the start of the dir */
-
	while (endp > path && *endp != '/')
-
		endp--;
-

-
	/* Either the dir is "/" or there are no slashes */
-
	if (endp == path) {
-
		dname[0] = *endp == '/' ? '/' : '.';
-
		dname[1] = '\0';
-
		return (dname);
-
	} else {
-
		/* Move forward past the separating slashes */
-
		do {
-
			endp--;
-
		} while (endp > path && *endp == '/');
-
	}
-

-
	len = endp - path + 1;
-
	if (len >= MAXPATHLEN) {
-
		errno = ENAMETOOLONG;
-
		return (NULL);
-
	}
-
	memcpy(dname, path, len);
-
	dname[len] = '\0';
-
	return (dname);
-
}
-

-
#endif
modified libpkg/backup.c
@@ -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");
modified libpkg/pkg_add.c
@@ -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)
{
@@ -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;
@@ -488,8 +502,7 @@ retry:
	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;
@@ -544,10 +557,8 @@ retry:
	    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;
		}
@@ -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);
modified libpkg/pkg_elf.c
@@ -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. */

modified libpkg/pkg_repo_create.c
@@ -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;
@@ -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;
modified libpkg/private/utils.h
@@ -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
modified libpkg/repo/binary/fetch.c
@@ -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;

@@ -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;

	/*
@@ -244,8 +237,8 @@ cleanup:

	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 */
modified libpkg/repo/binary/init.c
@@ -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) {
@@ -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
modified libpkg/utils.c
@@ -883,3 +883,22 @@ get_socketpair(int *pipe)

	return (r);
}
+

+
char *
+
get_dirname(char *d)
+
{
+
	char *walk;
+

+
	if (d == NULL)
+
		return (__DECONST(char *, "."));
+

+
	walk = strrchr(d, '/');
+
	if (walk == NULL) {
+
		d[0] = '.';
+
		d[1] = '\0';
+
	} else {
+
		*walk = '\0';
+
	}
+

+
	return (d);
+
}
modified src/add.c
@@ -144,8 +144,13 @@ exec_add(int argc, char **argv)
	pkg_manifest_keys_new(&keys);
	for (i = 0; i < argc; i++) {
		if (is_url(argv[i]) == EPKG_OK) {
+
			const char *name = strrchr(argv[i], '/');
+
			if (name == NULL)
+
				name = argv[i];
+
			else
+
				name++;
			snprintf(path, sizeof(path), "%s/%s.XXXXX",
-
			    getenv("TMPDIR") != NULL ? getenv("TMPDIR") : "/tmp", basename(argv[i]));
+
			    getenv("TMPDIR") != NULL ? getenv("TMPDIR") : "/tmp", name);
			if ((retcode = pkg_fetch_file(NULL, argv[i], path, 0, 0, 0)) != EPKG_OK)
				break;