]> pd.if.org Git - zpackage/commitdiff
add better error reporting for invalid import file types
authorNathan Wagner <nw@hydaspes.if.org>
Thu, 29 Sep 2016 00:24:26 +0000 (00:24 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Thu, 29 Sep 2016 00:24:26 +0000 (00:24 +0000)
lib/zpm.c
zpm.h

index 1cbf44e6d1a9840f75f32464dc935d6de80e11b7..d97bebee27ebef848efc8eea40e1070fba9646c6 100644 (file)
--- a/lib/zpm.c
+++ b/lib/zpm.c
@@ -1,3 +1,5 @@
+#define _POSIX_C_SOURCE 200809L
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -520,22 +522,36 @@ int zpm_import(struct zpm *pkg, char *path, uint32_t flags, char *hash) {
        /* mmap the file */
        fd = open(path, O_RDONLY);
        if (fd == -1) {
+               pkg->error = errno;
                fprintf(stderr, "%s can't open %s: %s\n", __FUNCTION__, path,strerror(errno));
                return 0;
        }
        if (fstat(fd, &sbuf) == -1) {
+               pkg->error = errno;
                fprintf(stderr, "%s can't fstat %s: %s\n", __FUNCTION__, path,strerror(errno));
                return 0;
        }
        /* not a regular file? */
        if (!S_ISREG(sbuf.st_mode)) {
+               char *ftype;
+               switch (sbuf.st_mode & S_IFMT) {
+                       case S_IFSOCK: ftype = "socket"; break;
+                       case S_IFLNK : ftype = "symlink"; break;
+                       case S_IFBLK : ftype = "block device"; break;
+                       case S_IFDIR : ftype = "directory"; break;
+                       case S_IFCHR : ftype = "character device"; break;
+                       case S_IFIFO : ftype = "fifo"; break;
+                       default: ftype = "unknown file type"; break;
+               }
                /* TODO this is ok, just stored differently */
-               fprintf(stderr, "%s non-regular files unsupported %s\n", __FUNCTION__, path);
+               fprintf(stderr, "%s can't import %s file: %s\n", __FUNCTION__, ftype, path);
+               pkg->error = EINVAL;
                return 0;
        }
 
        content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
        if (!content) {
+               pkg->error = errno;
                fprintf(stderr, "%s can't mmap %s: %s\n", __FUNCTION__, path,strerror(errno));
                return 0;
        }
diff --git a/zpm.h b/zpm.h
index 756d7dbf4b7723f4d83bce826e7dfe0740f23fdc..7ed6cfefdcb900784abda2a794f9b6ae937b5a2f 100644 (file)
--- a/zpm.h
+++ b/zpm.h
@@ -11,6 +11,7 @@ struct zpm {
        char *version;
        int release;
        char *pkgname;
+       int error; /* internal error number */
        time_t installed; /* install time, 0 for not installed */
 };