diff options
author | Mike Frysinger <vapier@gentoo.org> | 2014-03-10 04:11:14 -0400 |
---|---|---|
committer | Mike Frysinger <vapier@gentoo.org> | 2014-03-10 04:11:14 -0400 |
commit | bc7d4c9bf7245e9a97f504fd229f2b850358749b (patch) | |
tree | 510f81fb6c35204a41d7638afbadf23be22c2369 | |
parent | eat_file: convert API to work on dynamic buffers (diff) | |
download | portage-utils-bc7d4c9bf7245e9a97f504fd229f2b850358749b.tar.gz portage-utils-bc7d4c9bf7245e9a97f504fd229f2b850358749b.tar.bz2 portage-utils-bc7d4c9bf7245e9a97f504fd229f2b850358749b.zip |
vdb: add a q_vdb_pkg_eat helper to unify various call styles
Now that the core eat func is based on dynamic bufs, it's a lot easier
to create a core vdb helper for eating pkg files.
This has the added advantage of making the pkg dir open a delayed call.
We only opendir when we actually want to read files out of it. For many
funcs which don't do that, it saves a lot of syscall overhead.
-rw-r--r-- | libq/vdb.c | 36 | ||||
-rw-r--r-- | qcheck.c | 2 | ||||
-rw-r--r-- | qdepends.c | 8 | ||||
-rw-r--r-- | qfile.c | 4 | ||||
-rw-r--r-- | qlist.c | 15 |
5 files changed, 33 insertions, 32 deletions
@@ -170,7 +170,8 @@ _q_static void q_vdb_close_cat(q_vdb_cat_ctx *cat_ctx) typedef struct { const char *name; - const char *slot; + char *slot; + size_t slot_len; int fd; q_vdb_cat_ctx *cat_ctx; } q_vdb_pkg_ctx; @@ -192,17 +193,10 @@ _q_static int q_vdb_filter_pkg(const struct dirent *de) _q_static q_vdb_pkg_ctx *q_vdb_open_pkg(q_vdb_cat_ctx *cat_ctx, const char *name) { - q_vdb_pkg_ctx *pkg_ctx; - int fd; - - fd = openat(cat_ctx->fd, name, O_RDONLY|O_CLOEXEC|O_PATH); - if (fd == -1) - return NULL; - - pkg_ctx = xmalloc(sizeof(*pkg_ctx)); + q_vdb_pkg_ctx *pkg_ctx = xmalloc(sizeof(*pkg_ctx)); pkg_ctx->name = name; pkg_ctx->slot = NULL; - pkg_ctx->fd = fd; + pkg_ctx->fd = -1; pkg_ctx->cat_ctx = cat_ctx; return pkg_ctx; } @@ -229,8 +223,16 @@ _q_static q_vdb_pkg_ctx *q_vdb_next_pkg(q_vdb_cat_ctx *cat_ctx) return pkg_ctx; } -#define q_vdb_pkg_openat(pkg_ctx, file, flags, mode...) \ - openat((pkg_ctx)->fd, file, (flags)|O_CLOEXEC, ## mode) +_q_static int +q_vdb_pkg_openat(q_vdb_pkg_ctx *pkg_ctx, const char *file, int flags, mode_t mode) +{ + pkg_ctx->fd = openat(pkg_ctx->cat_ctx->fd, pkg_ctx->name, O_RDONLY|O_CLOEXEC|O_PATH); + if (pkg_ctx->fd == -1) + return -1; + + return openat(pkg_ctx->fd, file, flags|O_CLOEXEC, mode); +} + _q_static FILE *q_vdb_pkg_fopenat(q_vdb_pkg_ctx *pkg_ctx, const char *file, int flags, mode_t mode, const char *fmode) { @@ -250,10 +252,20 @@ _q_static FILE *q_vdb_pkg_fopenat(q_vdb_pkg_ctx *pkg_ctx, const char *file, #define q_vdb_pkg_fopenat_ro(pkg_ctx, file) q_vdb_pkg_fopenat(pkg_ctx, file, O_RDONLY, 0, "r") #define q_vdb_pkg_fopenat_rw(pkg_ctx, file) q_vdb_pkg_fopenat(pkg_ctx, file, O_RDWR|O_CREAT|O_TRUNC, 0644, "w") +_q_static bool +q_vdb_pkg_eat(q_vdb_pkg_ctx *pkg_ctx, const char *file, char **bufptr, size_t *buflen) +{ + int fd = q_vdb_pkg_openat(pkg_ctx, file, O_RDONLY, 0); + bool ret = eat_file_fd(fd, bufptr, buflen); + rmspace(*bufptr); + return ret; +} + _q_static void q_vdb_close_pkg(q_vdb_pkg_ctx *pkg_ctx) { if (pkg_ctx->fd != -1) close(pkg_ctx->fd); + free(pkg_ctx->slot); free(pkg_ctx); } @@ -69,7 +69,7 @@ static int qcheck_process_contents(q_vdb_pkg_ctx *pkg_ctx, struct qcheck_opt_sta fpx = NULL; - fd = q_vdb_pkg_openat(pkg_ctx, "CONTENTS", O_RDONLY|O_CLOEXEC); + fd = q_vdb_pkg_openat(pkg_ctx, "CONTENTS", O_RDONLY|O_CLOEXEC, 0); if (fd == -1) return EXIT_SUCCESS; if (fstat(fd, &cst)) { @@ -413,7 +413,7 @@ _q_static int qdepends_main_vdb_cb(q_vdb_pkg_ctx *pkg_ctx, void *priv) IF_DEBUG(warn("matched %s/%s", catname, pkgname)); - if (!eat_file_at(pkg_ctx->fd, state->depend_file, &depend, &depend_len)) + if (!q_vdb_pkg_eat(pkg_ctx, state->depend_file, &depend, &depend_len)) return 0; IF_DEBUG(warn("growing tree...")); @@ -434,7 +434,7 @@ _q_static int qdepends_main_vdb_cb(q_vdb_pkg_ctx *pkg_ctx, void *priv) printf("%s%s/%s%s%s: ", BOLD, catname, BLUE, pkgname, NORM); } - if (!eat_file_at(pkg_ctx->fd, "USE", &use, &use_len)) { + if (!q_vdb_pkg_eat(pkg_ctx, "USE", &use, &use_len)) { warn("Could not eat_file(%s), you'll prob have incorrect output", buf); } else { for (ptr = use; *ptr; ++ptr) @@ -473,7 +473,7 @@ _q_static int qdepends_vdb_deep_cb(q_vdb_pkg_ctx *pkg_ctx, void *priv) IF_DEBUG(warn("matched %s/%s for %s", catname, pkgname, state->depend_file)); - if (!eat_file_at(pkg_ctx->fd, state->depend_file, &depend, &depend_len)) + if (!q_vdb_pkg_eat(pkg_ctx, state->depend_file, &depend, &depend_len)) return 0; IF_DEBUG(warn("growing tree...")); @@ -483,7 +483,7 @@ _q_static int qdepends_vdb_deep_cb(q_vdb_pkg_ctx *pkg_ctx, void *priv) IF_DEBUG(puts(depend)); IF_DEBUG(dep_dump_tree(dep_tree)); - if (eat_file_at(pkg_ctx->fd, "USE", &use, &use_len)) + if (q_vdb_pkg_eat(pkg_ctx, "USE", &use, &use_len)) use[0] = ' '; for (ptr = use; *ptr; ++ptr) @@ -114,7 +114,7 @@ _q_static int qfile_cb(q_vdb_pkg_ctx *pkg_ctx, void *priv) } if (state->exclude_slot == NULL) goto qlist_done; /* "(CAT/)?(PN|PF)" matches, and no SLOT specified */ - eat_file_at(pkg_ctx->fd, "SLOT", &state->buf, &state->buflen); + q_vdb_pkg_eat(pkg_ctx, "SLOT", &state->buf, &state->buflen); rmspace(state->buf); if (strcmp(state->exclude_slot, state->buf) == 0) goto qlist_done; /* "(CAT/)?(PN|PF):SLOT" matches */ @@ -218,7 +218,7 @@ _q_static int qfile_cb(q_vdb_pkg_ctx *pkg_ctx, void *priv) /* XXX: This assumes the buf is big enough. */ char *slot_hack = slot + 1; size_t slot_len = sizeof(slot) - 1; - eat_file_at(pkg_ctx->fd, "SLOT", &slot_hack, &slot_len); + q_vdb_pkg_eat(pkg_ctx, "SLOT", &slot_hack, &slot_len); rmspace(slot_hack); slot[0] = ':'; } else @@ -61,17 +61,6 @@ _q_static queue *filter_dups(queue *sets) return dups; } -_q_static char *q_vdb_pkg_eat(q_vdb_pkg_ctx *pkg_ctx, const char *item) -{ - static char *buf; - static size_t buf_len; - - eat_file_at(pkg_ctx->fd, item, &buf, &buf_len); - rmspace(buf); - - return buf; -} - static char *grab_pkg_umap(const char *CAT, const char *PV) { static char umap[BUFSIZ]; @@ -159,7 +148,7 @@ qlist_match(q_vdb_pkg_ctx *pkg_ctx, const char *name, depend_atom **name_atom, b if (uslot) { ++uslot; if (!pkg_ctx->slot) - pkg_ctx->slot = q_vdb_pkg_eat(pkg_ctx, "SLOT"); + q_vdb_pkg_eat(pkg_ctx, "SLOT", &pkg_ctx->slot, &pkg_ctx->slot_len); } /* maybe they're using a version range */ @@ -298,7 +287,7 @@ _q_static int qlist_cb(q_vdb_pkg_ctx *pkg_ctx, void *priv) atom = (verbose ? NULL : atom_explode(pkgname)); if ((state->all + state->just_pkgname) < 2) { if (state->show_slots && !pkg_ctx->slot) - pkg_ctx->slot = q_vdb_pkg_eat(pkg_ctx, "SLOT"); + q_vdb_pkg_eat(pkg_ctx, "SLOT", &pkg_ctx->slot, &pkg_ctx->slot_len); /* display it */ printf("%s%s/%s%s%s%s%s%s%s%s%s%s\n", BOLD, catname, BLUE, (!state->columns ? (atom ? atom->PN : pkgname) : atom->PN), |