Logo Search packages:      
Sourcecode: jabberd2 version File versions  Download package

st_ret_t storage_add_type ( storage_t  st,
const char *  driver,
const char *  type 
)

associate this data type with this driver

Definition at line 155 of file storage.c.

References st_driver_st::add_type, storage_st::default_drv, storage_st::drivers, sm_st::log, st_driver_st::name, storage_st::sm, st_driver_st::st, st_FAILED, st_SUCCESS, and storage_st::types.

Referenced by storage_delete(), storage_get(), storage_new(), storage_put(), and storage_replace().

                                                                              {
    st_driver_t drv;
    st_driver_init_fn init = NULL;
    int i;
    st_ret_t ret;

    /* startup, see if we've already registered this type */
    if(type == NULL) {
        log_debug(ZONE, "adding arbitrary types to driver '%s'", driver);

        /* see if we already have one */
        if(st->default_drv != NULL) {
            log_debug(ZONE, "we already have a default handler, ignoring this one");
            return st_FAILED;
        }
    } else {
        log_debug(ZONE, "adding type '%s' to driver '%s'", type, driver);

        /* see if we already have one */
        if(xhash_get(st->types, type) != NULL) {
            log_debug(ZONE, "we already have a handler for type '%s', ignoring this one");
            return st_FAILED;
        }
    }

    /* get the driver */
    drv = xhash_get(st->drivers, driver);
    if(drv == NULL) {
        log_debug(ZONE, "driver not loaded, trying to init");

        /* find the init function */
        for(i = 0; st_driver_names[i] != NULL; i++) {
            if(strcmp(driver, st_driver_names[i]) == 0) {
                init = st_driver_inits[i];
                break;
            }
        }

        /* d'oh */
        if(init == NULL) {
            log_debug(ZONE, "no init function for driver '%s'", driver);

            return st_FAILED;
        }

        /* make a new driver structure */
        drv = (st_driver_t) malloc(sizeof(struct st_driver_st));
        memset(drv, 0, sizeof(struct st_driver_st));

        drv->st = st;

        log_debug(ZONE, "calling driver initializer");

        /* init */
        if((init)(drv) == st_FAILED) {
            log_write(st->sm->log, LOG_NOTICE, "initialisation of storage driver '%s' failed", driver);
            free(drv);
            return st_FAILED;
        }

        /* add it to the drivers hash so we can find it later */
        drv->name = pstrdup(xhash_pool(st->drivers), driver);
        xhash_put(st->drivers, drv->name, (void *) drv);

        log_write(st->sm->log, LOG_NOTICE, "initialised storage driver '%s'", driver);
    }

    /* if its a default, set it up as such */
    if(type == NULL) {
        st->default_drv = drv;
        return st_SUCCESS;
    }

    /* its a real type, so let the driver know */
    if(type != NULL && (ret = (drv->add_type)(drv, type)) != st_SUCCESS) {
        log_debug(ZONE, "driver '%s' can't handle '%s' data", driver, type);
        return ret;
    }

    /* register the type */
    xhash_put(st->types, pstrdup(xhash_pool(st->types), type), (void *) drv);

    return st_SUCCESS;
}


Generated by  Doxygen 1.6.0   Back to index