pkgtools

morpheus pkg tools
git clone git://git.2f30.org/pkgtools
Log | Files | Refs | README | LICENSE

commit 4bb2cc57335f2ca3bab66e248982d8e6d743c24b
parent 4a6d77c4a6c8c981cbcc1babaa5900c33e9597d4
Author: sin <sin@2f30.org>
Date:   Fri, 20 Jun 2014 16:57:15 +0100

Rework pkg_load() and pkg_new()

Diffstat:
Mdb.c | 83++++++++++++++++++++++++++++++++++++++-----------------------------------------
Mdb.h | 7++++---
Mremovepkg.c | 2+-
3 files changed, 45 insertions(+), 47 deletions(-)

diff --git a/db.c b/db.c @@ -189,24 +189,14 @@ db_add(struct db *db, const char *file) /* Physically unlink the db entry for `pkg' */ int -db_rm(struct db *db, struct pkg *pkg) +db_rm(struct pkg *pkg) { - char path[PATH_MAX]; - if (pkg->deleted == 0) return -1; - estrlcpy(path, db->path, sizeof(path)); - estrlcat(path, "/", sizeof(path)); - estrlcat(path, pkg->name, sizeof(path)); - if (pkg->version) { - estrlcat(path, "#", sizeof(path)); - estrlcat(path, pkg->version, - sizeof(path)); - } if (vflag == 1) - printf("removing %s\n", path); - if (remove(path) < 0) { - weprintf("remove %s:", path); + printf("removing %s\n", pkg->path); + if (remove(pkg->path) < 0) { + weprintf("remove %s:", pkg->path); return -1; } sync(); @@ -224,11 +214,9 @@ db_load(struct db *db) if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0) continue; - pkg = pkg_new(dp->d_name); - if (pkg_load(db, pkg) < 0) { - pkg_free(pkg); + pkg = pkg_load(db, dp->d_name); + if (!pkg) return -1; - } pkg->next = db->head; db->head = pkg; } @@ -337,31 +325,42 @@ db_collisions(struct db *db, const char *file) return ok; } -/* Load the package contents for `pkg' */ -int -pkg_load(struct db *db, struct pkg *pkg) +/* Load the package contents for the given `filename' */ +struct pkg * +pkg_load(struct db *db, const char *filename) { - char path[PATH_MAX]; + char path[PATH_MAX], tmp[PATH_MAX], *p; + char *name, *version; + struct pkg *pkg; struct pkgentry *pe; FILE *fp; char *buf = NULL; size_t sz = 0; ssize_t len; - if (pkg->head) - return 0; + estrlcpy(tmp, filename, sizeof(tmp)); + p = strchr(tmp, '#'); + if (p) + *p = '\0'; + name = tmp; + version = p ? p + 1 : NULL; estrlcpy(path, db->path, sizeof(path)); estrlcat(path, "/", sizeof(path)); - estrlcat(path, pkg->name, sizeof(path)); - if (pkg->version) { + estrlcat(path, name, sizeof(path)); + if (version) { estrlcat(path, "#", sizeof(path)); - estrlcat(path, pkg->version, sizeof(path)); + estrlcat(path, version, sizeof(path)); } - if (!(fp = fopen(path, "r"))) { - weprintf("fopen %s:", pkg->name); - return -1; + pkg = pkg_new(path, name, version); + if (!pkg) + return NULL; + + if (!(fp = fopen(pkg->path, "r"))) { + weprintf("fopen %s:", pkg->path); + pkg_free(pkg); + return NULL; } while ((len = getline(&buf, &sz, fp)) != -1) { @@ -369,10 +368,11 @@ pkg_load(struct db *db, struct pkg *pkg) buf[len - 1] = '\0'; if (buf[0] == '\0') { - weprintf("%s: malformed pkg file\n", path); + weprintf("%s: malformed pkg file\n", pkg->path); free(buf); fclose(fp); - return -1; + pkg_free(pkg); + return NULL; } pe = emalloc(sizeof(*pe)); @@ -387,10 +387,11 @@ pkg_load(struct db *db, struct pkg *pkg) if (ferror(fp)) { weprintf("%s: read error:", pkg->name); fclose(fp); - return -1; + pkg_free(pkg); + return NULL; } fclose(fp); - return 0; + return pkg; } /* Install the package `file' to disk */ @@ -541,21 +542,17 @@ pkg_remove(struct db *db, struct pkg *pkg) /* Create a new package instance */ struct pkg * -pkg_new(char *filename) +pkg_new(const char *path, const char *name, const char *version) { struct pkg *pkg; - char tmp[PATH_MAX], *p; - estrlcpy(tmp, filename, sizeof(tmp)); - p = strchr(tmp, '#'); - if (p) - *p = '\0'; pkg = emalloc(sizeof(*pkg)); - pkg->name = estrdup(tmp); - if (p) - pkg->version = estrdup(p + 1); + pkg->name = estrdup(name); + if (version) + pkg->version = estrdup(version); else pkg->version = NULL; + estrlcpy(pkg->path, path, sizeof(pkg->path)); pkg->deleted = 0; pkg->head = NULL; pkg->next = NULL; diff --git a/db.h b/db.h @@ -19,6 +19,7 @@ struct rejrule { struct pkg { char *name; char *version; + char path[PATH_MAX]; int deleted; struct pkgentry *head; struct pkg *next; @@ -30,16 +31,16 @@ extern int vflag; struct db *db_new(const char *); int db_free(struct db *); int db_add(struct db *, const char *); -int db_rm(struct db *, struct pkg *); +int db_rm(struct pkg *); int db_load(struct db *); int db_walk(struct db *, int (*)(struct db *, struct pkg *, void *), void *); int db_links(struct db *, const char *); int db_collisions(struct db *, const char *); -int pkg_load(struct db *, struct pkg *); +struct pkg *pkg_load(struct db *, const char *); int pkg_install(struct db *, const char *); int pkg_remove(struct db *, struct pkg *); -struct pkg *pkg_new(char *); +struct pkg *pkg_new(const char *, const char *, const char *); void pkg_free(struct pkg *); void parse_version(const char *, char **); diff --git a/removepkg.c b/removepkg.c @@ -73,7 +73,7 @@ pkg_remove_cb(struct db *db, struct pkg *pkg, void *name) if (strcmp(pkg->name, name) == 0) { if (pkg_remove(db, pkg) < 0) return -1; - db_rm(db, pkg); + db_rm(pkg); printf("removed %s\n", pkg->name); return 1; }