Example #1
0
/* directory callback */
static int dircb(wagon_t * id_list, attr_set_t * attr_list,
                 unsigned int entry_count, void * dummy )
{
    /* retrieve child entries for all directories */
    int i, rc;

    for (i = 0; i < entry_count; i++)
    {
        wagon_t * chids = NULL;
        attr_set_t * chattrs = NULL;
        unsigned int chcount = 0;
        int j;

        /* match condition on dirs parent */
        if (!is_expr || (EntryMatches(&id_list[i].id, &attr_list[i],
                                      &match_expr, NULL) == POLICY_MATCH))
        {
            /* don't display dirs if no_dir is specified */
            if (! (prog_options.no_dir && ATTR_MASK_TEST(&attr_list[i], type)
                   && !strcasecmp(ATTR(&attr_list[i], type), STR_TYPE_DIR)) )
                print_entry(&id_list[i], &attr_list[i]);
        }

        if (!prog_options.dir_only)
        {
            rc = ListMgr_GetChild( &lmgr, &entry_filter, id_list+i, 1,
                                   disp_mask | query_mask,
                                   &chids, &chattrs, &chcount);
            if (rc)
            {
                DisplayLog(LVL_MAJOR, FIND_TAG, "ListMgr_GetChild() failed with error %d", rc);
                return rc;
            }

            for (j = 0; j < chcount; j++)
            {
                if (!is_expr || (EntryMatches(&chids[j].id, &chattrs[j],
                                 &match_expr, NULL) == POLICY_MATCH))
                    print_entry(&chids[j], &chattrs[j]);

                ListMgr_FreeAttrs(&chattrs[j]);
            }

            free_wagon(chids, 0, chcount);
            MemFree(chids);
            MemFree(chattrs);
        }
    }
    return 0;
}
Example #2
0
/** scan sets of directories
 * \param cb_func, callback function for each set of directory
 */
int rbh_scrub(lmgr_t *p_mgr, const wagon_t *id_list,
              unsigned int id_count, attr_mask_t dir_attr_mask,
              scrub_callback_t cb_func, void *arg)
{
    wagon_t *curr_array;
    unsigned int count;
    lmgr_filter_t filter;
    filter_value_t fv;
    int i, rc;
    int last_err = 0;

    rc = add_id_list(id_list, id_count);
    if (rc)
        return rc;

    /* only get subdirs (for scanning) */
    fv.value.val_str = STR_TYPE_DIR;
    lmgr_simple_filter_init(&filter);
    lmgr_simple_filter_add(&filter, ATTR_INDEX_type, EQUAL, fv, 0);

    /* while the array is not empty */
    while (array_used > 0) {
        unsigned int res_count = 0;
        wagon_t *child_ids;
        attr_set_t *child_attrs;

        /* get a set of entry_ids */
        curr_array = &dir_array[array_first];
        if (array_used < LS_CHUNK) {
            /* get all available dirs */
            count = array_used;
        } else {
            /* get a constant chunk */
            count = LS_CHUNK;
        }

#ifdef _DEBUG_ID_LIST
        printf("processing %u-%u\n", array_first, array_first + count - 1);
#endif

        /* read childs */
        res_count = 0;
        child_ids = NULL;
        child_attrs = NULL;

        rc = ListMgr_GetChild(p_mgr, &filter, curr_array, count, dir_attr_mask,
                              &child_ids, &child_attrs, &res_count);

        if (rc) {
            DisplayLog(LVL_CRIT, SCRUB_TAG,
                       "ListMgr_GetChild() terminated with error %d", rc);
            /* @TODO free allocated resources */
            break;
        }

        /* Call the callback func for each listed dir */
        rc = cb_func(child_ids, child_attrs, res_count, arg);
        if (rc)
            /* XXX break the scan? */
            last_err = rc;

        /* can release the list of input ids */
        rbh_scrub_release_list(array_first, count);

        /* copy entry ids before freeing them */
        /* TODO: we could transfer the pathname instead of strdup() them. */
        add_id_list(child_ids, res_count);

        /* attributes no longer needed */
        /* release attrs */
        if (child_attrs) {
            for (i = 0; i < res_count; i++)
                ListMgr_FreeAttrs(&child_attrs[i]);
            MemFree(child_attrs);
            child_attrs = NULL;
        }

        /* free the returned id array */
        if (child_ids) {
            free_wagon(child_ids, 0, res_count);
            MemFree(child_ids);
            child_ids = NULL;
        }
    }
    lmgr_simple_filter_free(&filter);

    return last_err;
}