Skip to content

Commit

Permalink
Add 2 new macros: pkg_errno and pkg_fatal_errno
Browse files Browse the repository at this point in the history
  • Loading branch information
bapt committed Dec 17, 2016
1 parent b31cda9 commit 305ccf2
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 83 deletions.
12 changes: 4 additions & 8 deletions libpkg/backup.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand All @@ -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);
Expand Down
13 changes: 4 additions & 9 deletions libpkg/metalog.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
83 changes: 28 additions & 55 deletions libpkg/pkg_add.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
}
Expand All @@ -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);
}
}
}
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -395,9 +383,7 @@ create_symlinks(struct pkg *pkg, struct pkg_file *f, const char *target)
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,
Expand Down Expand Up @@ -466,9 +452,7 @@ create_hardlink(struct pkg *pkg, struct pkg_file *f, const char *path)
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);
Expand Down Expand Up @@ -524,9 +508,8 @@ create_regfile(struct pkg *pkg, struct pkg_file *f, struct archive *a,
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) {
Expand Down Expand Up @@ -559,7 +542,7 @@ create_regfile(struct pkg *pkg, struct pkg_file *f, struct archive *a,
} 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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 3 additions & 4 deletions libpkg/pkg_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions libpkg/pkg_ports.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 2 additions & 3 deletions libpkg/pkg_repo_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions libpkg/private/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions libpkg/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 305ccf2

Please sign in to comment.