pm

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

commit 767955d994347cf26faf168ebffc73bd3f3584f8
parent fb63ff08cb9d292b1b689ade10fad57a4d485a0d
Author: z3bra <willyatmailoodotorg>
Date:   Mon Jan  4 14:08:32 2016

Fixed memory leaks in pack loading

Diffstat:
 pm.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 48 insertions(+), 19 deletions(-)

diff --git a/pm.c b/pm.c @@ -39,6 +39,9 @@ enum { ERR_INVALID_ACTION = 1, ERR_METADATA = 2, ERR_PACKAGE_LOAD = 3, + ERR_DELETE = 4, + ERR_INSPECT = 5, + ERR_UNPACK = 6, }; void usage(char *name); @@ -56,6 +59,7 @@ int delete_content(FILE *metafile); int delete(const char *datadir, const char *rootfs, const char *name); struct pack *pack_load_file(char *file); struct pack *pack_load(char *name); +void pack_unload(struct pack *p); char *argv0; @@ -101,17 +105,17 @@ mkdir_parents(char *dir, mode_t mode) char *p = NULL; size_t len; - snprintf(tmp, sizeof(tmp),"%s",dir); - len = strlen(tmp); - if(tmp[len - 1] == '/') - tmp[len - 1] = 0; - for(p = tmp + 1; *p; p++) - if(*p == '/') { - *p = 0; - mkdir(tmp, mode); - *p = '/'; - } - return mkdir(tmp, mode); + snprintf(tmp, sizeof(tmp),"%s",dir); + len = strlen(tmp); + if(tmp[len - 1] == '/') + tmp[len - 1] = 0; + for(p = tmp + 1; *p; p++) + if(*p == '/') { + *p = 0; + mkdir(tmp, mode); + *p = '/'; + } + return mkdir(tmp, mode); } @@ -275,8 +279,14 @@ metadata(char *datadir, struct pack *pack) perror("open"); return -1; } - write(fd, pack->version, strlen(pack->version)); - write(fd, "\n", 1); + + r = write(fd, pack->version, strlen(pack->version)); + r += write(fd, "\n", 1); + + if (r < 1) { + perror("write"); + return -1; + } close(fd); return 0; @@ -535,6 +545,18 @@ pack_load(char *name) return p; } +void +pack_unload(struct pack *p) +{ + if (!p) + return; + + free(p->path); + free(p->name); + free(p->version); + free(p); +} + int main (int argc, char **argv) { @@ -570,16 +592,21 @@ main (int argc, char **argv) case ACTION_INSTALL: if (!p) return ERR_PACKAGE_LOAD; - if (metadata(PACKAGE_DATA, p) == 0) - return unpack(PACKAGE_ROOT, p->path); - fprintf(stderr, "could not write metadata for %s\n", p->name); - return ERR_METADATA; + if (metadata(PACKAGE_DATA, p) != 0) + return ERR_METADATA; + if (unpack(PACKAGE_ROOT, p->path) != 0) + return ERR_UNPACK; + break; case ACTION_DELETE: - return delete(PACKAGE_DATA, PACKAGE_ROOT, n); + if (delete(PACKAGE_DATA, PACKAGE_ROOT, n) != 0) + return ERR_DELETE; + break; case ACTION_INSPECT: - return inspect(n); + if (inspect(n) != 0) + return ERR_INSPECT; + break; /* handle me, Octave */ case ACTION_UPDATE: @@ -590,5 +617,7 @@ main (int argc, char **argv) return ERR_INVALID_ACTION; } + pack_unload(p); + return 0; }