Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Add 2 new macros: pkg_errno and pkg_fatal_errno
Baptiste Daroussin committed 9 years ago
commit 305ccf2d840b0204fb14c26881476caa0f321676
parent b31cda9
8 files changed +52 -83
modified libpkg/backup.c
@@ -125,16 +125,13 @@ pkgdb_dump(struct pkgdb *db, const char *dest)

	if (eaccess(dest, W_OK)) {
		if (errno != ENOENT) {
-
			pkg_emit_error("eaccess(%s) -- %s", dest,
-
			    strerror(errno));
-
			return (EPKG_FATAL);
+
			pkg_fatal_errno("Unable to access '%s'", dest);
		}

		/* Could we create the Sqlite DB file? */
		if (eaccess(bsd_dirname(dest), W_OK)) {
-
			pkg_emit_error("eaccess(%s) -- %s", bsd_dirname(dest),
-
			    strerror(errno));
-
			return (EPKG_FATAL);
+
			pkg_fatal_errno("Unable to access '%s'",
+
			    bsd_dirname(dest));
		}
	}

@@ -161,8 +158,7 @@ pkgdb_load(struct pkgdb *db, const char *src)
	int	 ret;

	if (eaccess(src, R_OK)) {
-
		pkg_emit_error("eaccess(%s) -- %s", src, strerror(errno));
-
		return (EPKG_FATAL);
+
		pkg_fatal_errno("Unable to access '%s'", src);
	}

	ret = sqlite3_open(src, &restore);
modified libpkg/metalog.c
@@ -38,9 +38,7 @@ metalog_open(const char *metalog)
{
	metalogfp = fopen(metalog, "a");
	if (metalogfp == NULL) {
-
		pkg_emit_error("Unable to open metalog '%s': %s", metalog,
-
		    strerror(errno));
-
		return EPKG_FATAL;
+
		pkg_fatal_errno("Unable to open metalog '%s'", metalog);
	} 

	return EPKG_OK;
@@ -60,24 +58,21 @@ metalog_add(int type, const char *path, const char *uname, const char *gname,
		if (fprintf(metalogfp,
		    "./%s type=dir uname=%s gname=%s mode=%3o\n",
		    path, uname, gname, mode) < 0) {
-
			pkg_emit_error("Unable to write to the metalog: %s",
-
			    strerror(errno));
+
			pkg_errno("%s", "Unable to write to the metalog");
		}
		break;
	case PKG_METALOG_FILE:
		if (fprintf(metalogfp,
		    "./%s type=file uname=%s gname=%s mode=%3o\n",
		    path, uname, gname, mode) < 0) {
-
			pkg_emit_error("Unable to write to the metalog: %s",
-
			    strerror(errno));
+
			pkg_errno("%s", "Unable to write to the metalog");
		}
		break;
	case PKG_METALOG_LINK:
		if (fprintf(metalogfp,
		    "./%s type=link uname=%s gname=%s mode=%3o link=%s\n",
		    path, uname, gname, mode, link) < 0) {
-
			pkg_emit_error("Unable to write to the metalog: %s",
-
			    strerror(errno));
+
			pkg_errno("%s", "Unable to write to the metalog");
		}
		break;
	}
modified libpkg/pkg_add.c
@@ -222,9 +222,7 @@ set_attrs(int fd, char *path, mode_t perm, uid_t uid, gid_t gid,
	times[1] = *mts;
	if (utimensat(fd, RELATIVE_PATH(path), times,
	    AT_SYMLINK_NOFOLLOW) == -1 && errno != EOPNOTSUPP){
-
		pkg_emit_error("Fail to set time on %s: %s", path,
-
		    strerror(errno));
-
		return (EPKG_FATAL);
+
		pkg_fatal_errno("Fail to set time on %s", path);
	}
	if (errno == EOPNOTSUPP) {
#endif
@@ -240,16 +238,13 @@ set_attrs(int fd, char *path, mode_t perm, uid_t uid, gid_t gid,
	if (lutimes(RELATIVE_PATH(path), tv) == -1) {

		if (errno != ENOSYS) {
-
			pkg_emit_error("Fail to set time on %s: %s", path,
-
					strerror(errno));
-
			return (EPKG_FATAL);
+
			pkg_fatal_errno("Fail to set time on %s", path);
		}
		else {
			/* Fallback to utimes */
			if (utimes(RELATIVE_PATH(path), tv) == -1) {
-
				pkg_emit_error("Fail to set time(fallback) on %s: %s", path,
-
						strerror(errno));
-
				return (EPKG_FATAL);
+
				pkg_fatal_errno("Fail to set time(fallback) on "
+
				    "%s", path);
			}
		}
	}
@@ -264,13 +259,11 @@ set_attrs(int fd, char *path, mode_t perm, uid_t uid, gid_t gid,
				AT_SYMLINK_NOFOLLOW) == -1) {
			if (errno == ENOTSUP) {
				if (fchownat(fd, RELATIVE_PATH(path), uid, gid, 0) == -1) {
-
					pkg_emit_error("Fail to chown(fallback) %s: %s", path, strerror(errno));
-
					return (EPKG_FATAL);
+
					pkg_fatal_errno("Fail to chown(fallback) %s", path);
				}
			}
			else {
-
				pkg_emit_error("Fail to chown %s: %s", path, strerror(errno));
-
				return (EPKG_FATAL);
+
				pkg_fatal_errno("Fail to chown %s", path);
			}
		}
	}
@@ -279,13 +272,12 @@ set_attrs(int fd, char *path, mode_t perm, uid_t uid, gid_t gid,
	if (fchmodat(fd, RELATIVE_PATH(path), perm, AT_SYMLINK_NOFOLLOW) == -1) {
		if (errno == ENOTSUP) {
			if (fchmodat(fd, RELATIVE_PATH(path), perm, 0) == -1) {
-
				pkg_emit_error("Fail to chmod(fallback) %s: %s", path, strerror(errno));
-
				return (EPKG_FATAL);
+
				pkg_fatal_errno("Fail to chmod(fallback) %s",
+
				    path);
			}
		}
		else {
-
			pkg_emit_error("Fail to chmod %s: %s", path, strerror(errno));
-
			return (EPKG_FATAL);
+
			pkg_fatal_errno("Fail to chmod %s", path);
		}
	}

@@ -326,17 +318,13 @@ create_dir(struct pkg *pkg, struct pkg_dir *d)
			return (EPKG_FATAL);
	if (fstatat(pkg->rootfd, RELATIVE_PATH(d->path), &st, 0) == -1) {
		if (errno != ENOENT) {
-
			pkg_emit_error("Fail to stat directory %s: %s", d->path,
-
			    strerror(errno));
-
			return (EPKG_FATAL);
+
			pkg_fatal_errno("Fail to stat directory %s", d->path);
		}
		if (fstatat(pkg->rootfd, RELATIVE_PATH(d->path), &st, AT_SYMLINK_NOFOLLOW) == 0) {
			unlinkat(pkg->rootfd, RELATIVE_PATH(d->path), 0);
		}
		if (mkdirat(pkg->rootfd, RELATIVE_PATH(d->path), 0755) == -1) {
-
			pkg_emit_error("Fail to create directory %s: %s", d->path,
-
			    strerror(errno));
-
			return (EPKG_FATAL);
+
			pkg_fatal_errno("Fail to create directory %s", d->path);
		}
	}

@@ -395,9 +383,7 @@ retry:
			goto retry;
		}

-
		pkg_emit_error("Fail to create symlink: %s: %s\n", f->temppath,
-
		    strerror(errno));
-
		return (EPKG_FATAL);
+
		pkg_fatal_errno("Fail to create symlink: %s", f->temppath);
	}

	if (set_attrs(pkg->rootfd, f->temppath, f->perm, f->uid, f->gid,
@@ -466,9 +452,7 @@ retry:
			goto retry;
		}

-
		pkg_emit_error("Fail to create hardlink: %s: %s\n", f->temppath,
-
		    strerror(errno));
-
		return (EPKG_FATAL);
+
		pkg_fatal_errno("Fail to create hardlink: %s", f->temppath);
	}

	return (EPKG_OK);
@@ -524,9 +508,8 @@ retry:
			tried_mkdir = true;
			goto retry;
		}
-
		pkg_emit_error("Fail to create temporary file: %s: %s",
-
		    f->temppath, strerror(errno));
-
		return (EPKG_FATAL);
+
		pkg_fatal_errno("Fail to create temporary file: %s",
+
		    f->temppath);
	}

	if (fromfd == -1) {
@@ -559,7 +542,7 @@ retry:
	} else {
		while ((len = read(fromfd, buf, sizeof(buf))) > 0)
			if (write(fd, buf, len) == -1) {
-
				pkg_emit_error("Fail to write file: %s", strerror(errno));
+
				pkg_errno("%s", "Fail to write file: %s");
			}
	}
	if (fd != -1) {
@@ -731,18 +714,15 @@ pkg_extract_finalize(struct pkg *pkg)
		}
		if (renameat(pkg->rootfd, RELATIVE_PATH(f->temppath),
		    pkg->rootfd, RELATIVE_PATH(fto)) == -1) {
-
			pkg_emit_error("Fail to rename %s -> %s: %s",
-
			    f->temppath, fto, strerror(errno));
-
			return (EPKG_FATAL);
+
			pkg_fatal_errno("Fail to rename %s -> %s",
+
			    f->temppath, fto);
		}

#ifdef HAVE_CHFLAGSAT
		if (f->fflags != 0) {
			if (chflagsat(pkg->rootfd, RELATIVE_PATH(fto),
			    f->fflags, AT_SYMLINK_NOFOLLOW) == -1) {
-
				pkg_emit_error("Fail to chflags %s: %s",
-
				    fto, strerror(errno));
-
				return (EPKG_FATAL);
+
				pkg_fatal_errno("Fail to chflags %s", fto);
			}
		}
#endif
@@ -1243,17 +1223,13 @@ pkg_add_fromdir(struct pkg *pkg, const char *src)

	fromfd = open(src, O_DIRECTORY);
	if (fromfd == -1) {
-
		pkg_emit_error("Unable to open source directory '%s': %s'",
-
		    src, strerror(errno));
-
		return (EPKG_FATAL);
+
		pkg_fatal_errno("Unable to open source directory '%s'", src);
	}
	pkg_open_root_fd(pkg);

	while (pkg_dirs(pkg, &d) == EPKG_OK) {
		if (fstatat(fromfd, RELATIVE_PATH(d->path), &st, 0) == -1) {
-
			pkg_emit_error("%s%s: %s", src, d->path,
-
			    strerror(errno));
-
			return (EPKG_FATAL);
+
			pkg_fatal_errno("%s%s", src, d->path);
		}
		if (d->perm == 0)
			d->perm = st.st_mode & ~S_IFMT;
@@ -1288,9 +1264,7 @@ pkg_add_fromdir(struct pkg *pkg, const char *src)
	while (pkg_files(pkg, &f) == EPKG_OK) {
		if (fstatat(fromfd, RELATIVE_PATH(f->path), &st,
		    AT_SYMLINK_NOFOLLOW) == -1) {
-
			pkg_emit_error("%s%s: %s", src, f->path,
-
			    strerror(errno));
-
			return (EPKG_FATAL);
+
			pkg_fatal_errno("%s%s", src, f->path);
		}
		if (f->uname[0] != '\0') {
			if (getpwnam_r(f->uname, &pwent, buffer, sizeof(buffer),
@@ -1325,19 +1299,18 @@ pkg_add_fromdir(struct pkg *pkg, const char *src)
			if ((link_len = readlinkat(fromfd,
			    RELATIVE_PATH(f->path), target,
			    sizeof(target))) == -1) {
-
				pkg_emit_error("Impossible to read symlinks "
-
				    "'%s': %s", f->path, strerror(errno));
-
				return (EPKG_FATAL);
+
				pkg_fatal_errno("Impossible to read symlinks "
+
				    "'%s'", f->path);
			}
			target[link_len] = '\0';
			if (create_symlinks(pkg, f, target) == EPKG_FATAL) {
				return (EPKG_FATAL);
			}
		} else if (S_ISREG(st.st_mode)) {
-
			if ((fd = openat(fromfd, RELATIVE_PATH(f->path), O_RDONLY)) == -1) {
-
				pkg_emit_error("Impossible to open source file '%s': %s",
-
				    RELATIVE_PATH(f->path), strerror(errno));
-
				return (EPKG_FATAL);
+
			if ((fd = openat(fromfd, RELATIVE_PATH(f->path),
+
			    O_RDONLY)) == -1) {
+
				pkg_fatal_errno("Impossible to open source file"
+
				    " '%s'", RELATIVE_PATH(f->path));
			}
			kh_find(hls, hardlinks, st.st_ino, path);
			if (path != NULL) {
modified libpkg/pkg_config.c
@@ -726,8 +726,7 @@ load_repo_file(int dfd, const char *repodir, const char *repofile,
	pkg_debug(1, "PKgConfig: loading %s/%s", repodir, repofile);
	fd = openat(dfd, repofile, O_RDONLY);
	if (fd == -1) {
-
		pkg_emit_error("Unable to open '%s/%s': %s\n", repodir,
-
		    repofile, strerror(errno));
+
		pkg_errno("Unable to open '%s/%s'", repodir, repofile);
		return;
	}
	if (!ucl_parser_add_fd(p, fd)) {
@@ -956,9 +955,9 @@ pkg_ini(const char *path, const char *reposdir, pkg_init_flags flags)
	else
		conffd = open(path, O_RDONLY);
	if (conffd == -1 && errno != ENOENT) {
-
		pkg_emit_error("Cannot open %s/%s: %s",
+
		pkg_errno("Cannot open %s/%s",
		    pkg_rootdir != NULL ? pkg_rootdir : "",
-
		    path, strerror(errno));
+
		    path);
	}

	p = ucl_parser_new(0);
modified libpkg/pkg_ports.c
@@ -317,8 +317,7 @@ meta_file(struct plist *p, char *line, struct file_attr *a, bool is_config)
	}

	if (lstat(testpath, &st) == -1) {
-
		pkg_emit_error("Unable to access file %s: %s", testpath,
-
		    strerror(errno));
+
		pkg_errno("Unable to access file %s", testpath);
		if (p->stage != NULL)
			ret = EPKG_FATAL;
		if (developer_mode) {
modified libpkg/pkg_repo_create.c
@@ -535,9 +535,8 @@ pkg_create_repo(char *path, const char *output_dir, bool filelist,
		/* Try to create dir */
		if (errno == ENOENT) {
			if (mkdir(output_dir, 00755) == -1) {
-
				pkg_emit_error("cannot create output directory %s: %s",
-
					output_dir, strerror(errno));
-
				return (EPKG_FATAL);
+
				pkg_fatal_errno("cannot create output directory %s",
+
					output_dir);
			}
		}
		else {
modified libpkg/private/event.h
@@ -33,6 +33,15 @@
void pkg_emit_error(const char *fmt, ...);
void pkg_emit_notice(const char *fmt, ...);
void pkg_emit_errno(const char *func, const char *arg);
+

+
#define pkg_errno(fmt, ...) \
+
	pkg_emit_error(fmt":%s", __VA_ARGS__, strerror(errno))
+

+
#define pkg_fatal_errno(fmt, ...) do { \
+
	pkg_errno(fmt, __VA_ARGS__);   \
+
	return (EPKG_FATAL);           \
+
} while (0);
+

void pkg_emit_already_installed(struct pkg *p);
void pkg_emit_fetch_begin(const char *url);
void pkg_emit_fetch_finished(const char *url);
modified libpkg/utils.c
@@ -748,8 +748,7 @@ mkdirat_p(int fd, const char *path)
				strlcat(pathdone, "/", sizeof(pathdone));
				continue;
			}
-
			pkg_emit_error("Fail to create /%s: %s",
-
			    pathdone, strerror(errno));
+
			pkg_errno("Fail to create /%s", pathdone);
			free(walk);
			return (false);
		}