X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=blob.c;h=8438e73fb3e68c091e93120b6f66e4290a99e65c;hp=031f6ae3a76c2e29cd429135f3b73e60c8bc38d8;hb=b90c61663c32cdc6cfc0e78070e2a94a14cb2f8e;hpb=0496a19e8ac1afecde726c2292720d415bdb7500 diff --git a/blob.c b/blob.c index 031f6ae3..8438e73f 100644 --- a/blob.c +++ b/blob.c @@ -53,109 +53,62 @@ enum blob_ls_flags { }; /** Structure passed to the \p print_blob function. */ -struct lsblob_data { +struct lsblob_action_data { uint32_t flags; struct para_buffer pb; }; -static int print_blob(struct osl_table *table, struct osl_row *row, const char *name, void *data) +static int print_blob(struct osl_table *table, struct osl_row *row, + const char *name, void *data) { + struct lsblob_action_data *lbad = data; struct osl_object obj; uint32_t id; int ret; - struct lsblob_data *lbd = data; - if (!(lbd->flags & BLOB_LS_FLAG_LONG)) { - para_printf(&lbd->pb, "%s\n", name); + if (!(lbad->flags & BLOB_LS_FLAG_LONG)) { + para_printf(&lbad->pb, "%s\n", name); return 1; } ret = osl_get_object(table, row, BLOBCOL_ID, &obj); - if (ret < 0) - return ret; - id = *(uint32_t *)obj.data; - para_printf(&lbd->pb, "%u\t%s\n", id, name); - return 1; -} - -enum blob_match_loop_flags { - BM_NAME_LOOP = 1, - BM_REVERSE_LOOP = 2 -}; - -struct blob_match_data { - struct osl_table *table; - const char *patterns; - size_t patterns_size; - int fnmatch_flags; - unsigned loop_flags; - void *data; - int (*action)(struct osl_table *table, struct osl_row *row, const char *name, void *data); -}; - -static int action_if_blob_matches(struct osl_row *row, void *data) -{ - struct blob_match_data *bmd = data; - struct osl_object name_obj; - const char *p, *name; - int ret = osl_get_object(bmd->table, row, BLOBCOL_NAME, &name_obj); - if (ret < 0) { - PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret)); + para_printf(&lbad->pb, "%s: %s\n", name, PARA_STRERROR(-ret)); return ret; } - name = (char *)name_obj.data; - if (!*name) /* ignore dummy row */ - return 1; - if (!bmd->patterns_size) /* match everything if no pattern was given */ - return bmd->action(bmd->table, row, name, bmd->data); - for (p = bmd->patterns; p < bmd->patterns + bmd->patterns_size; - p += strlen(p) + 1) { - ret = fnmatch(p, name, bmd->fnmatch_flags); - if (ret == FNM_NOMATCH) - continue; - if (ret) - return -E_FNMATCH; - return bmd->action(bmd->table, row, name, bmd->data); - } + id = *(uint32_t *)obj.data; + para_printf(&lbad->pb, "%u\t%s\n", id, name); return 1; } -static int for_each_matching_blob(struct blob_match_data *bmd) -{ - unsigned col = (bmd->loop_flags & BM_NAME_LOOP)? - BLOBCOL_NAME : BLOBCOL_ID; - - if (bmd->loop_flags & BM_REVERSE_LOOP) - return osl_rbtree_loop_reverse(bmd->table, col, bmd, action_if_blob_matches); - return osl_rbtree_loop(bmd->table, col, bmd, action_if_blob_matches); -} - -int com_lsblob_callback(struct osl_table *table, - const struct osl_object *query, struct osl_object *ls_output) +static int com_lsblob_callback(struct osl_table *table, + const struct osl_object *query, struct osl_object *result) { - struct lsblob_data lbd = {.flags = *(uint32_t *)query}; - struct blob_match_data bmd = { + struct lsblob_action_data lbad = {.flags = *(uint32_t *)query}; + struct pattern_match_data pmd = { .table = table, - .patterns = (char *)query->data + sizeof(uint32_t), - .patterns_size = query->size - sizeof(uint32_t), - .data = &lbd, - .action = print_blob + .patterns = {.data = (char *)query->data + sizeof(uint32_t), + .size = query->size - sizeof(uint32_t)}, + .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING | PM_SKIP_EMPTY_NAME, + .match_col_num = BLOBCOL_NAME, + .data = &lbad, + .action = print_blob, }; int ret; - if (lbd.flags & BLOB_LS_FLAG_REVERSE) - bmd.loop_flags |= BM_REVERSE_LOOP; - if (!(lbd.flags & BLOB_LS_FLAG_SORT_BY_ID)) - bmd.loop_flags |= BM_NAME_LOOP; - ret = for_each_matching_blob(&bmd); - if (lbd.pb.buf) { - ls_output->data = lbd.pb.buf; - ls_output->size = lbd.pb.size; - return 1; - } - if (ret > 0) - ret = 0; - return ret; + if (lbad.flags & BLOB_LS_FLAG_REVERSE) + pmd.pm_flags |= PM_REVERSE_LOOP; + if (!(lbad.flags & BLOB_LS_FLAG_SORT_BY_ID)) + pmd.loop_col_num = BLOBCOL_ID; + else + pmd.loop_col_num = BLOBCOL_NAME; + ret = for_each_matching_row(&pmd); + if (ret < 0) + para_printf(&lbad.pb, "%s\n", PARA_STRERROR(-ret)); + if (!lbad.pb.buf) + return 0; + result->data = lbad.pb.buf; + result->size = lbad.pb.size; + return 1; } static int com_lsblob(callback_function *f, int fd, int argc, char * const * const argv) @@ -219,15 +172,17 @@ static int com_catblob_callback(struct osl_table *table, const struct osl_object *query, struct osl_object *result) { int ret; - struct blob_match_data bmd = { + struct pattern_match_data pmd = { .table = table, - .patterns = (char *)query->data, - .patterns_size = query->size, + .patterns = *query, + .loop_col_num = BLOBCOL_NAME, + .match_col_num = BLOBCOL_NAME, + .pm_flags = PM_SKIP_EMPTY_NAME, .data = result, .action = cat_blob }; result->data = NULL; - ret = for_each_matching_blob(&bmd); + ret = for_each_matching_row(&pmd); if (result->data) return 1; return (ret > 0)? 0 : ret; @@ -273,15 +228,17 @@ static int com_rmblob_callback(struct osl_table *table, { int ret; struct rmblob_data rmbd = {.num_removed = 0}; - struct blob_match_data bmd = { + struct pattern_match_data pmd = { .table = table, - .patterns = (char *)query->data, - .patterns_size = query->size, + .patterns = *query, + .loop_col_num = BLOBCOL_NAME, + .match_col_num = BLOBCOL_NAME, + .pm_flags = PM_SKIP_EMPTY_NAME, .data = &rmbd, .action = remove_blob }; result->data = NULL; - ret = for_each_matching_blob(&bmd); + ret = for_each_matching_row(&pmd); if (ret < 0) para_printf(&rmbd.pb, "%s\n", PARA_STRERROR(-ret)); if (!rmbd.num_removed)