]> pd.if.org Git - zpackage/blobdiff - lib/zpm.c
add better error reporting for invalid import file types
[zpackage] / lib / zpm.c
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;
        }