From: Nathan Wagner Date: Thu, 29 Sep 2016 00:24:26 +0000 (+0000) Subject: add better error reporting for invalid import file types X-Git-Tag: v0.1.6~159 X-Git-Url: https://pd.if.org/git/?p=zpackage;a=commitdiff_plain;h=df8771ff12a79e070f78895fa0ff3f8c2cc1a224 add better error reporting for invalid import file types --- diff --git a/lib/zpm.c b/lib/zpm.c index 1cbf44e..d97bebe 100644 --- a/lib/zpm.c +++ b/lib/zpm.c @@ -1,3 +1,5 @@ +#define _POSIX_C_SOURCE 200809L + #include #include #include @@ -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 756d7db..7ed6cfe 100644 --- 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 */ };