pkgtools

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

commit 621edd1167bae72ab8f47ab894ca3b46ab5cd622
parent 832411a406c946cd0b6014efaea09af19bec04f6
Author: sin <sin@2f30.org>
Date:   Fri, 20 Jun 2014 15:57:07 +0100

Re-organize functions in db.c

Diffstat:
Mdb.c | 246++++++++++++++++++++++++++++++++++++++++----------------------------------------
Mdb.h | 13++++++++-----
Minfopkg.c | 8++++----
Minstallpkg.c | 8++++----
Mremovepkg.c | 8++++----
5 files changed, 143 insertions(+), 140 deletions(-)

diff --git a/db.c b/db.c @@ -35,7 +35,7 @@ int vflag = 0; /* Request access to the db and initialize the context */ struct db * -db_attach(const char *prefix) +db_new(const char *prefix) { struct db *db; struct sigaction sa; @@ -79,90 +79,26 @@ db_attach(const char *prefix) return db; } -/* Load the entire db in memory */ -int -db_load(struct db *db) -{ - struct pkg *pkg; - struct dirent *dp; - - while ((dp = readdir(db->pkgdir))) { - 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); - return -1; - } - pkg->next = db->head; - db->head = pkg; - } - - return 0; -} - -/* Check if the contents of package `file' - * collide with corresponding entries in the filesystem */ +/* Free the db context and release resources */ int -db_collisions(struct db *db, const char *file) +db_free(struct db *db) { - char pkgpath[PATH_MAX]; - char path[PATH_MAX]; - char resolvedpath[PATH_MAX]; - struct archive *ar; - struct archive_entry *entry; - struct stat sb; - int ok = 0, r; + struct pkg *pkg, *tmp; - if (!realpath(file, pkgpath)) { - weprintf("realpath %s:", file); - return -1; + pkg = db->head; + while (pkg) { + tmp = pkg->next; + pkg_free(pkg); + pkg = tmp; } - - ar = archive_read_new(); - - archive_read_support_filter_gzip(ar); - archive_read_support_filter_bzip2(ar); - archive_read_support_filter_xz(ar); - archive_read_support_format_tar(ar); - - if (archive_read_open_filename(ar, pkgpath, ARCHIVEBUFSIZ) < 0) { - weprintf("archive_read_open_filename %s: %s\n", pkgpath, - archive_error_string(ar)); + if (flock(dirfd(db->pkgdir), LOCK_UN) < 0) { + weprintf("flock %s:", db->path); return -1; } - - while (1) { - r = archive_read_next_header(ar, &entry); - if (r == ARCHIVE_EOF) - break; - if (r != ARCHIVE_OK) { - weprintf("archive_read_next_header: %s\n", - archive_error_string(ar)); - return -1; - } - estrlcpy(path, db->prefix, sizeof(path)); - estrlcat(path, "/", sizeof(path)); - estrlcat(path, archive_entry_pathname(entry), sizeof(path)); - if (access(path, F_OK) == 0) { - if (stat(path, &sb) < 0) { - weprintf("lstat %s:", archive_entry_pathname(entry)); - return -1; - } - if (S_ISDIR(sb.st_mode) == 0) { - if (realpath(path, resolvedpath)) - weprintf("%s exists\n", resolvedpath); - else - weprintf("%s exists\n", path); - ok = -1; - } - } - } - - archive_read_free(ar); - - return ok; + closedir(db->pkgdir); + rej_free(db->rejrules); + free(db); + return 0; } /* Update the db entry on disk for package `file' */ @@ -251,6 +187,59 @@ db_add(struct db *db, const char *file) return 0; } +/* Physically unlink the db entry for `file' */ +int +db_rm(struct db *db, const char *name) +{ + struct pkg *pkg; + char path[PATH_MAX]; + + for (pkg = db->head; pkg; pkg = pkg->next) { + if (pkg->deleted == 1 && strcmp(pkg->name, name) == 0) { + 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); + return -1; + } + sync(); + break; + } + } + return 0; +} + +/* Load the entire db in memory */ +int +db_load(struct db *db) +{ + struct pkg *pkg; + struct dirent *dp; + + while ((dp = readdir(db->pkgdir))) { + 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); + return -1; + } + pkg->next = db->head; + db->head = pkg; + } + + return 0; +} + /* Walk through all the db entries and call `cb' for each one */ int db_walk(struct db *db, int (*cb)(struct db *, struct pkg *, void *), void *data) @@ -289,26 +278,67 @@ db_links(struct db *db, const char *path) return links; } -/* Free the db context and release resources */ +/* Check if the contents of package `file' + * collide with corresponding entries in the filesystem */ int -db_detach(struct db *db) +db_collisions(struct db *db, const char *file) { - struct pkg *pkg, *tmp; + char pkgpath[PATH_MAX]; + char path[PATH_MAX]; + char resolvedpath[PATH_MAX]; + struct archive *ar; + struct archive_entry *entry; + struct stat sb; + int ok = 0, r; - pkg = db->head; - while (pkg) { - tmp = pkg->next; - pkg_free(pkg); - pkg = tmp; + if (!realpath(file, pkgpath)) { + weprintf("realpath %s:", file); + return -1; } - if (flock(dirfd(db->pkgdir), LOCK_UN) < 0) { - weprintf("flock %s:", db->path); + + ar = archive_read_new(); + + archive_read_support_filter_gzip(ar); + archive_read_support_filter_bzip2(ar); + archive_read_support_filter_xz(ar); + archive_read_support_format_tar(ar); + + if (archive_read_open_filename(ar, pkgpath, ARCHIVEBUFSIZ) < 0) { + weprintf("archive_read_open_filename %s: %s\n", pkgpath, + archive_error_string(ar)); return -1; } - closedir(db->pkgdir); - rej_free(db->rejrules); - free(db); - return 0; + + while (1) { + r = archive_read_next_header(ar, &entry); + if (r == ARCHIVE_EOF) + break; + if (r != ARCHIVE_OK) { + weprintf("archive_read_next_header: %s\n", + archive_error_string(ar)); + return -1; + } + estrlcpy(path, db->prefix, sizeof(path)); + estrlcat(path, "/", sizeof(path)); + estrlcat(path, archive_entry_pathname(entry), sizeof(path)); + if (access(path, F_OK) == 0) { + if (stat(path, &sb) < 0) { + weprintf("lstat %s:", archive_entry_pathname(entry)); + return -1; + } + if (S_ISDIR(sb.st_mode) == 0) { + if (realpath(path, resolvedpath)) + weprintf("%s exists\n", resolvedpath); + else + weprintf("%s exists\n", path); + ok = -1; + } + } + } + + archive_read_free(ar); + + return ok; } /* Load the package contents for `pkg' */ @@ -522,36 +552,6 @@ pkg_remove(struct db *db, const char *name) return 0; } -/* Physically unlink the db entry for `file' */ -int -db_rm(struct db *db, const char *name) -{ - struct pkg *pkg; - char path[PATH_MAX]; - - for (pkg = db->head; pkg; pkg = pkg->next) { - if (pkg->deleted == 1 && strcmp(pkg->name, name) == 0) { - 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); - return -1; - } - sync(); - break; - } - } - return 0; -} - /* Create a new package instance */ struct pkg * pkg_new(char *filename) diff --git a/db.h b/db.h @@ -27,21 +27,24 @@ struct pkg { extern int fflag; extern int vflag; -struct db *db_attach(const char *); -int db_load(struct db *); -int db_collisions(struct db *, const char *); +struct db *db_new(const char *); +int db_free(struct db *); int db_add(struct db *, const char *); +int db_rm(struct db *, const char *); +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_detach(struct db *); +int db_collisions(struct db *, const char *); + int pkg_load(struct db *, struct pkg *); int pkg_install(struct db *, const char *); int pkg_remove(struct db *, const char *); -int db_rm(struct db *, const char *); struct pkg *pkg_new(char *); void pkg_free(struct pkg *); + void parse_version(const char *, char **); void parse_name(const char *, char **); + void rej_free(struct rejrule *); struct rejrule * rej_load(const char *); int rej_match(struct db *, const char *); diff --git a/infopkg.c b/infopkg.c @@ -45,12 +45,12 @@ main(int argc, char *argv[]) if (oflag == 0 || argc < 1) usage(); - db = db_attach(prefix); + db = db_new(prefix); if (!db) exit(EXIT_FAILURE); r = db_load(db); if (r < 0) { - db_detach(db); + db_free(db); exit(EXIT_FAILURE); } @@ -61,12 +61,12 @@ main(int argc, char *argv[]) } r = db_walk(db, own_pkg_cb, path); if (r < 0) { - db_detach(db); + db_free(db); exit(EXIT_FAILURE); } } - db_detach(db); + db_free(db); return EXIT_SUCCESS; } diff --git a/installpkg.c b/installpkg.c @@ -43,12 +43,12 @@ main(int argc, char *argv[]) if (argc < 1) usage(); - db = db_attach(prefix); + db = db_new(prefix); if (!db) exit(EXIT_FAILURE); r = db_load(db); if (r < 0) { - db_detach(db); + db_free(db); exit(EXIT_FAILURE); } @@ -62,7 +62,7 @@ main(int argc, char *argv[]) if (fflag == 0) { r = db_walk(db, collisions_cb, path); if (r < 0) { - db_detach(db); + db_free(db); printf("not installed %s\n", path); exit(EXIT_FAILURE); } @@ -72,7 +72,7 @@ main(int argc, char *argv[]) printf("installed %s\n", path); } - db_detach(db); + db_free(db); return EXIT_SUCCESS; } diff --git a/removepkg.c b/removepkg.c @@ -43,19 +43,19 @@ main(int argc, char *argv[]) if (argc < 1) usage(); - db = db_attach(prefix); + db = db_new(prefix); if (!db) exit(EXIT_FAILURE); r = db_load(db); if (r < 0) { - db_detach(db); + db_free(db); exit(EXIT_FAILURE); } for (i = 0; i < argc; i++) { r = db_walk(db, pkg_remove_cb, argv[i]); if (r < 0) { - db_detach(db); + db_free(db); exit(EXIT_FAILURE); } else if (r > 0) { db_rm(db, argv[i]); @@ -65,7 +65,7 @@ main(int argc, char *argv[]) } } - db_detach(db); + db_free(db); return EXIT_SUCCESS; }