pm

barely a pack manager
git clone git://z3bra.org/pm
Log | Files | Refs | README | LICENSE

commit e75a29dc260ce4d1a54c83eb9ec5de73c958875d
parent 5f251565fd50d808e101a1dcbce3ed48fb9502bb
Author: z3bra <willyatmailoodotorg>
Date:   Sat Jun 18 01:19:03 +0200

Rework information logging

There are now 3 logging levels: INFO, VERBOSE, DEBUG

INFO: write out which action has been done
VERBOSE: explain HOW an action is being done
DEBUG: report every change made to the filesystem

The logging level can be increased by passing the verbose flags (-v) again.
Each level includes the messages of the previous levels.

Diffstat:
pm.c | 79+++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------
1 file changed, 51 insertions(+), 28 deletions(-)
diff --git a/pm.c b/pm.c @@ -20,6 +20,8 @@ #define PACK_SEPARATOR '#' #define PACK_EXTENSION ".tar.bz2" +#define log(l,...) if(verbose>=l){printf(__VA_ARGS__);} + struct pack { char *path; char *name; @@ -46,6 +48,13 @@ enum { ERR_UNPACK = 6, }; +enum { + LOG_NONE = 0, + LOG_INFO = 1, + LOG_VERBOSE = 2, + LOG_DEBUG = 3 +}; + void usage(char *name); int is_empty(char *dir); int mkdir_parents(char *dir, mode_t mode); @@ -75,7 +84,7 @@ int install(const char *rootfs, const char *datadir, char *path); int update(const char *rootfs, const char *datadir, char *path); int delete(const char *rootfs, const char *datadir, char *name); -int verbose = 0; +int verbose = LOG_NONE; int overwrite = 0; void @@ -187,8 +196,10 @@ inspect_collision(const char *rootfs, struct pack *p) archive_read_support_format_tar(a); r = archive_read_open_filename(a, p->path, 0); - if (r != ARCHIVE_OK) + if (r != ARCHIVE_OK) { + fprintf(stderr, "%s: %s\n", p->path, archive_error_string(a)); return -1; + } getcwd(cwd, PATH_MAX); chdir(rootfs); @@ -287,12 +298,16 @@ write_metadata(const char *datadir, struct pack *p) char tmp[PATH_MAX]; snprintf(tmp, PATH_MAX, "%s/%s", datadir, p->name); + if (stat(tmp, &st) < 0 && errno == ENOENT) { + log(LOG_DEBUG, "creating metadata directory %s\n", tmp); if ((r = mkdir_parents(tmp, 0755)) < 0) return r; } snprintf(tmp, PATH_MAX, "%s/%s/version", datadir, p->name); + + log(LOG_DEBUG, "openning %s for writing\n", tmp); if ((fd = open(tmp, O_CREAT|O_WRONLY|O_TRUNC, 0644)) < 0) { perror(tmp); return -1; @@ -355,6 +370,8 @@ unpack(const char *rootfs, const char *datadir, struct pack *p) |ARCHIVE_EXTRACT_SECURE_NODOTDOT; snprintf(tmp, PATH_MAX, "%s/%s/files", datadir, p->name); + + log(LOG_DEBUG, "openning %s for writing\n", tmp); if ((fd = open(tmp, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) { perror(tmp); return -1; @@ -366,7 +383,7 @@ unpack(const char *rootfs, const char *datadir, struct pack *p) /* try and open the tarball of our pack */ if ((r = archive_read_open_filename(a, p->path, 0)) != ARCHIVE_OK) { - fprintf(stderr, "%s: %s\n", p->name, archive_error_string(a)); + fprintf(stderr, "%s: %s\n", p->path, archive_error_string(a)); archive_read_free(a); return r; } @@ -389,6 +406,7 @@ unpack(const char *rootfs, const char *datadir, struct pack *p) break; } + log(LOG_DEBUG, "+ %s\n", archive_entry_pathname(e)); if ((r = write_entry(a, w)) != ARCHIVE_OK) { fprintf(stderr, "%s: %s\n", archive_entry_pathname(e), archive_error_string(w)); @@ -418,36 +436,27 @@ pack_install(const char *rootfs, const char *datadir, struct pack *p) char tmp[PATH_MAX] = ""; struct stat st; - if (verbose == 1) - printf("%s: installing to %s\n", p->name, rootfs); - if (overwrite == 0) { snprintf(tmp, PATH_MAX, "%s/%s", datadir, p->name); + log(LOG_VERBOSE, "checking installation status\n"); if (stat(tmp, &st) == 0 && S_ISDIR(st.st_mode)) { fprintf(stderr, "%s: already installed\n", p->name); return -1; } - - if (verbose == 1) - printf("%s: checking collisions\n", p->name); + log(LOG_VERBOSE, "checking for collisions\n"); if (inspect_collision(rootfs, p) != 0) return -1; } - if (verbose == 1) - printf("%s: writing metadata\n", p->name); + log(LOG_VERBOSE, "writing metadata to %s\n", datadir); if (write_metadata(datadir, p) < 0) { perror(datadir); return -1; } - if (verbose == 1) - printf("%s: extracting %s\n", p->name, p->path); + log(LOG_VERBOSE, "extracting pack to %s\n", rootfs); r = unpack(rootfs, datadir, p); - if (verbose == 1) - printf("%s: installation %s\n", p->name, r != 0 ? "failed" : "complete"); - return r; } @@ -468,6 +477,8 @@ delete_node(char *path) if (path[len - 1] == '/') path[--len] = 0; + log(LOG_DEBUG, "- %s\n", path); + /* * if path doesn't exist anymore, it's all good :) * we use lstat here so that dangling links can be delt with too @@ -475,9 +486,6 @@ delete_node(char *path) if (lstat(path, &st) < 0 && errno == ENOENT) return 0; - if (verbose == 1) - printf("- %s\n", path); - if (S_ISDIR(st.st_mode) && is_empty(path) == 0) { if ((r = rmdir(path)) < 0) { perror(path); @@ -557,43 +565,44 @@ pack_delete(const char *rootfs, const char *datadir, struct pack *p) perror(tmp); return ERR_DELETE; } + addr = mmap(0, st.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); if (addr == MAP_FAILED) { perror(tmp); close(fd); } - if (verbose == 1) - printf("%s: deleting from %s\n", packname, rootfs); if (chdir(rootfs) < 0) { perror(rootfs); close(fd); return ERR_DELETE; } + log(LOG_VERBOSE, "deleting installed files\n"); /* ignore errors so everything gets deleted */ if (delete_content(addr, st.st_size) < 0) { - fprintf(stderr, "%s: cannot remove pack\n", p->name); + fprintf(stderr, "%s: pack not removed\n", p->name); close(fd); return ERR_DELETE; } close(fd); - if (verbose == 1) - printf("%s: removing metadata\n", p->name); + log(LOG_VERBOSE, "removing %s\n", tmp); if (unlink(tmp) < 0) { perror(tmp); - fprintf(stderr, "%s: cannot remove file list\n", p->name); return ERR_DELETE; } - snprintf(tmp, PATH_MAX, "%s/version", p->path); + snprintf(tmp, PATH_MAX, "%s/%s/version", datadir, p->name); + + log(LOG_VERBOSE, "removing %s\n", tmp); if (unlink(tmp) < 0) { perror(tmp); - fprintf(stderr, "%s: cannot clean version\n", p->name); return ERR_DELETE; } + /* remove metadata directory, no matter what */ + log(LOG_DEBUG, "removing %s\n", p->path); return rmdir(p->path); } @@ -610,7 +619,12 @@ install(const char *rootfs, const char *datadir, char *path) if ((p = pack_load(datadir, path)) == NULL) return ERR_PACK_LOAD; + r += pack_install(rootfs, datadir, p); + + if (r == 0) + log(LOG_INFO, "installed %s (%s)\n", p->name, p->version); + pack_free(p); return r; @@ -634,6 +648,10 @@ update(const char *rootfs, const char *datadir, char *path) return ERR_DELETE; r += pack_install(rootfs, datadir, p); + + if (r == 0) + log(LOG_INFO, "updated %s to %s\n", p->name, p->version); + pack_free(p); return r; @@ -651,7 +669,12 @@ delete(const char *rootfs, const char *datadir, char *name) if ((p = pack_load(datadir, tmp)) == NULL) return ERR_PACK_LOAD; + r += pack_delete(rootfs, datadir, p); + + if (r == 0) + log(LOG_INFO, "deleted %s\n", p->name); + pack_free(p); return r; @@ -809,7 +832,7 @@ main (int argc, char **argv) action = ACTION_UPDATE; break; case 'v': - verbose = 1; + verbose++; break; default: action = ACTION_INVALID;