pm

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

commit 1be3eff999d9f349dbc430701429058d30152778
parent b701a8feda3f99c087b21d82c87429919b056e67
Author: z3bra <willyatmailoodotorg>
Date:   Thu Jun 16 13:02:13 +0200

Add function to retrieve version from an installed pack

Diffstat:
pm.c | 39++++++++++++++++++++++++++++-----------
1 file changed, 28 insertions(+), 11 deletions(-)
diff --git a/pm.c b/pm.c @@ -55,9 +55,11 @@ struct pack *pack_load(char *path); void pack_free(struct pack *p); int pack_install(const char *rootfs, const char *datadir, struct pack *p); + int inspect_collision(const char *rootfs, struct pack *p); int inspect_system(int fd, const char *datadir); int inspect_files(int fd, const char *datadir, const char *packname); +int inspect_version(const char *datadir, const char *name, char version[]); int inspect(const char *datadir, const char *packname); int write_metadata(const char *datadir, struct pack *pack); @@ -142,6 +144,29 @@ base_name(char *path) return b ? b + 1 : path; } +int +inspect_version(const char *datadir, const char *name, char version[LINE_MAX]) +{ + FILE *stream; + size_t len = 0; + char tmp[PATH_MAX] = "", *lf = NULL; + + snprintf(tmp, PATH_MAX, "%s/%s/version", datadir, name); + + if ((stream = fopen(tmp, "r")) == NULL) { + sprintf(version, "(unknown)"); + return 1; + } else { + fgets(version, LINE_MAX, stream); + if ((lf = strchr(version, '\n')) != NULL) + *lf = 0; + } + + if (stream) + fclose(stream); + + return 0; +} /* * Check for collisions between the filesystem and the tarball @@ -214,7 +239,7 @@ inspect_system(int fd, const char *datadir) { DIR *d; struct dirent *p; - char tmp[PATH_MAX] = ""; + char tmp[PATH_MAX] = "", ver[LINE_MAX]; int meta; size_t len; @@ -225,16 +250,8 @@ inspect_system(int fd, const char *datadir) while ((p = readdir(d))) if (strcmp(p->d_name, ".") && strcmp(p->d_name, "..")) { - snprintf(tmp, PATH_MAX, "%s/%s/version", - datadir, p->d_name); - - if ((meta = open(tmp, O_RDONLY)) < 0) { - sprintf(tmp, "(unknown)"); - } else { - len = read(meta, tmp, 32); - tmp[len - 1] = 0; - } - dprintf(fd, "%s\t%s\n", p->d_name, tmp); + inspect_version(datadir, p->d_name, ver); + dprintf(fd, "%s\t%s\n", p->d_name, ver); } closedir(d);