Esempio n. 1
0
/*
 * get fileset's info
 * inp: urls, url list you want to get
 * out: files, file list with file_t elements. don't forget free
 */
int file_get_infos_by_list(mdb_conn *conn, ULIST *urls, ULIST **files, int *noksn)
{
    int listlen;
    char *url;
    file_t *file;
    NEOERR *err;
    int ret;

    listlen = uListLength(urls);
    if (listlen <= 0 || urls == NULL || files == NULL) {
        return RET_RBTOP_INPUTE;
    }

    err = uListInit(files, 0, 0);
    RETURN_V_NOK(err, RET_RBTOP_MEMALLOCE);

    int pid = 1;
    int i;
    for (i = 0; i < listlen; i++) {
        err = uListGet(urls, i, (void**)&url);
        RETURN_V_NOK(err, RET_RBTOP_GETLISTE);
        ret = file_get_info_by_id(conn, 0, url, pid, &file);
        if (ret != RET_RBTOP_OK) {
            mtc_warn("can't get file info for %s", url);
            *noksn = i;
            return RET_RBTOP_GETLISTE;
        } else {
            pid = file->id;
            uListAppend(*files, file);
        }
    }
    *noksn = -1;
    return RET_RBTOP_OK;
}
Esempio n. 2
0
File: ltpl.c Progetto: adderly/cmoon
NEOERR* ltpl_parse_dir(char *dir, HASH *outhash)
{
    struct dirent **eps = NULL;
    int n;
    NEOERR *err;

    if (!dir) return nerr_raise(NERR_ASSERT, "can't read null directory");

    HASH *dbh, *evth;
    void *lib = dlopen(NULL, RTLD_NOW|RTLD_GLOBAL);
    if (!lib) return nerr_raise(NERR_SYSTEM, "dlopen %s", dlerror());
    
    err = ldb_init(&dbh);
    if (err != STATUS_OK) return nerr_pass(err);

    err = levt_init(&evth);
    if (err != STATUS_OK) return nerr_pass(err);
    
    n = scandir(dir, &eps, ltpl_config, alphasort);
    for (int i = 0; i < n; i++) {
        mtc_dbg("parse file %s", eps[i]->d_name);
        err = ltpl_parse_file(dbh, evth, lib, dir, eps[i]->d_name, outhash);
        TRACE_NOK(err);
        free(eps[i]);
    }

    ldb_destroy(dbh);
    levt_destroy(evth);
    dlclose(lib);
    
    if (n > 0) free(eps);
    else mtc_warn("no .hdf file found in %s", dir);

    return STATUS_OK;
}
Esempio n. 3
0
int file_get_info_by_id(mdb_conn *conn, int id, char *url, int pid, file_t **file)
{
    char mmckey[LEN_MMC_KEY];
    file_t *fl;
    size_t datalen;
    char *buf;
    int ret;
    
    if (id < 0 && (url == NULL || pid < 0))    return RET_RBTOP_INPUTE;

    if (id <= 0)
        snprintf(mmckey, LEN_MMC_KEY, "%s.%d.%s", PRE_MMC_FILE, pid, url);
    else
        snprintf(mmckey, LEN_MMC_KEY, "%s.%d", PRE_MMC_FILE, id);
    buf = mmc_get(mmckey, &datalen, 0);
    if (buf == NULL || datalen < sizeof(file_t)) {
        if (buf != NULL && datalen < sizeof(file_t)) {
            mtc_warn("get %d %d.%s info error from mmc %d",
                     id, pid, url, datalen);
        }
        if (mdb_get_errcode(conn) != MDB_ERR_NONE) {
            mtc_err("conn err %s", mdb_get_errmsg(conn));
            return RET_RBTOP_INPUTE;
        }
        fl = file_new();
        if (fl == NULL) return RET_RBTOP_MEMALLOCE;
        if (id <= 0) {
            LDB_QUERY_RAW(conn, "fileinfo", FILE_QUERY_COL,
                          "pid=%d AND name=$1", "s", pid, url);
        } else {
            LDB_QUERY_RAW(conn, "fileinfo", FILE_QUERY_COL,
                          "id=%d", NULL, id);
        }
        ret = FILE_GET_RAW(conn, fl);
        if (ret != MDB_ERR_NONE) {
            mtc_err("get %d %d.%s info failure from db %s",
                    id, pid, url, mdb_get_errmsg(conn));
            if (ret == MDB_ERR_NORESULT)
                return RET_RBTOP_NEXIST;
            return RET_RBTOP_SELECTE;
        } else {
            file_pack(fl, &buf, &datalen);
            mmc_store(MMC_OP_SET, mmckey, (void*)buf, datalen, ONE_HOUR, 0);
        }
    } else {
        ret = file_unpack(buf, datalen, &fl, NULL);
        if (ret != RET_RBTOP_OK) {
            mtc_err("assembly file from mmc error");
            return RET_RBTOP_MMCERR;
        }
    }
    free(buf);
    *file = fl;
    return RET_RBTOP_OK;
}
Esempio n. 4
0
int file_get_files(HDF *hdf, mdb_conn *conn, session_t *ses)
{
    PRE_DBOP(hdf, conn);

    int ret, cnt = 0;
    char tok[LEN_MD];
    int count, offset, uin, pid;
    ULIST *gnode;
    gnode_t *node;
    member_t *mb;
    file_t *fl;

    pid = hdf_get_int_value(hdf, PRE_QUERY".pid", 1);

    //sprintf(tok, "pid=%d", pid);
    //mmisc_set_count(hdf, conn, "fileinfo", tok);
    mmisc_get_offset(hdf, &count, &offset);

    if (member_has_login(hdf, conn, ses) != RET_RBTOP_OK) {
        return RET_RBTOP_NOTLOGIN;
    }

    if (!member_has_gmode(ses->member, GROUP_MODE_SENIOR, GROUP_STAT_OK)) {
        mtc_warn("%d attemped to list sys-file", ses->member->uin);
        return RET_RBTOP_LIMITE;
    }

    ret = member_get_group(ses->member, GROUP_MODE_SENIOR, GROUP_STAT_OK, &gnode);
    if (ret != RET_RBTOP_OK) {
        mtc_err("get %d %d failure", ses->member->uin, GROUP_MODE_SENIOR);
        return ret;
    }

    MLIST_ITERATE(gnode, node) {
        ret = file_get_info_by_id(conn, node->gid, NULL, -1, &fl);
        if (ret != RET_RBTOP_OK) {
            mtc_err("get %d info failure", node->gid);
            continue;
        }
        sprintf(tok, "%s.files.%d", PRE_OUTPUT, cnt++);
        file_item2hdf(fl, tok, hdf);
        file_del(fl);
    }
Esempio n. 5
0
int file_get_info_by_uri(mdb_conn *conn, char *uri, file_t **file)
{
    file_t *fl;
    size_t datalen;
    char *buf;
    int ret;
    
    if (uri == NULL) return RET_RBTOP_INPUTE;

    buf = mmc_getf(&datalen, 0, PRE_MMC_FILE".%s", uri);
    if (buf == NULL || datalen < sizeof(file_t)) {
        if (buf != NULL && datalen < sizeof(file_t)) {
            mtc_warn("get %s info error from mmc %d", uri, datalen);
        }
        if (mdb_get_errcode(conn) != MDB_ERR_NONE) {
            mtc_err("conn err %s", mdb_get_errmsg(conn));
            return RET_RBTOP_INPUTE;
        }
        fl = file_new();
        if (fl == NULL) return RET_RBTOP_MEMALLOCE;
        LDB_QUERY_RAW(conn, "fileinfo", FILE_QUERY_COL, "uri=$1", "s", uri);
        ret = FILE_GET_RAW(conn, fl);
        if (ret != MDB_ERR_NONE) {
            mtc_err("get %s info failure from db %s", uri, mdb_get_errmsg(conn));
            if (ret == MDB_ERR_NORESULT)
                return RET_RBTOP_NEXIST;
            return RET_RBTOP_SELECTE;
        } else {
            file_pack(fl, &buf, &datalen);
            mmc_storef(MMC_OP_SET, (void*)buf, datalen, ONE_HOUR, 0,
                       PRE_MMC_FILE".%s", uri);
        }
    } else {
        ret = file_unpack(buf, datalen, &fl, NULL);
        if (ret != RET_RBTOP_OK) {
            mtc_err("assembly file from mmc error");
            return RET_RBTOP_MMCERR;
        }
    }
    free(buf);
    *file = fl;
    return RET_RBTOP_OK;
}
Esempio n. 6
0
int file_get_infos_by_uri(mdb_conn *conn, char *uri, ULIST **files, int *noksn)
{
    ULIST *urls;
    int listlen;
    NEOERR *err;
    int ret;
    
    err = string_array_split(&urls, uri, URI_SPLITER, MAX_URI_ITEM);
    RETURN_V_NOK(err, RET_RBTOP_INPUTE);
    listlen = uListLength(urls);
    if (listlen < 1) {
        mtc_warn("%s not a valid request", uri);
        return RET_RBTOP_INPUTE;
    }

    ret = file_get_infos_by_list(conn, urls, files, noksn);
    if (urls != NULL)
        uListDestroy(&urls, ULIST_FREE);
    return ret;
}
Esempio n. 7
0
File: ldml.c Progetto: bigml/mgate
NEOERR* ldml_parse_dir(char *dir, HASH *outhash)
{
    struct dirent **eps = NULL;
    int n;
    NEOERR *err;

    if (!dir) return nerr_raise(NERR_ASSERT, "can't read null directory");

    n = scandir(dir, &eps, ldml_config, alphasort);
    for (int i = 0; i < n; i++) {
        mtc_dbg("parse file %s", eps[i]->d_name);
        err = ldml_parse_file(dir, eps[i]->d_name, outhash);
        TRACE_NOK(err);
        free(eps[i]);
    }

    if (n > 0) free(eps);
    else mtc_warn("no .hdf file found in %s", dir);

    return STATUS_OK;
}
Esempio n. 8
0
static int mevent_start_driver(struct mevent *evt, struct event_driver *d,
                               void *lib, int num_thread, HDF *matenode)
{
    if (evt == NULL || evt->table == NULL || d == NULL) return 0;

    struct event_entry *e = d->init_driver();
    if (e == NULL) return 0;

    num_thread = num_thread < 0 ? 1 : num_thread;
    if (num_thread > MAX_THREAD_NUM) {
        wlog("plugin %s thread number %d too large, set to default max number %d",
             e->name, num_thread, MAX_THREAD_NUM);
        mtc_warn("plugin %s thread number %d too large, set to default max number %d",
                 e->name, num_thread, MAX_THREAD_NUM);
        num_thread = MAX_THREAD_NUM;
    }

    e->num_thread = num_thread;
    e->cur_thread = 0;

    for (int i = 0; i < num_thread; i++) {
        e->op_queue[i] = queue_create();
        e->op_thread[i] = malloc(sizeof(pthread_t));
        pthread_create(e->op_thread[i], NULL, mevent_start_base_entry, (void*)e);
        sleep(1);
        e->cur_thread++;
    }

    e->cur_thread = 0;

    HDF *cnode = hdf_obj_child(matenode);
    char *dle;
    for (int i = 0; cnode && i < MAX_THREAD_NUM; cnode = hdf_obj_next(cnode)) {
        void* (*mate_func)(void *arg);

        mate_func = dlsym(lib, hdf_obj_value(cnode));
        if ((dle = dlerror()) != NULL) {
            wlog("unable to find %s %s", hdf_obj_value(cnode), dle);
            mtc_err("unable to find %s %s", hdf_obj_value(cnode), dle);
            continue;
        }

        e->mt_thread[i] = malloc(sizeof(pthread_t));
        pthread_create(e->mt_thread[i], NULL, mate_func, (void*)e);
        i++;
    }

    uint32_t h;
    struct event_chain *c;
    h = hash(e->name, e->ksize) % evt->hashlen;
    c = evt->table + h;

    if (c->len == 0) {
        c->first = e;
        c->last = e;
        c->len = 1;
    } else if (c->len <= evt->chainlen) {
        e->next = c->first;
        c->first->prev = e;
        c->first = e;
        c->len += 1;
    } else {
        wlog("unbelieveable, you have more than %d plugins, stop it %s",
             evt->chainlen, e->name);
        mtc_warn("unbelieveable, you have more than %ld plugins, stop it %s",
                 evt->chainlen, e->name);

        mevent_stop_driver(e);
        return 0;
    }

    return 1;
}
Esempio n. 9
0
File: ooms.c Progetto: kingiol/cmoon
static void ips2places(HDF *hdf, HASH *evth)
{
    if (!hdf || !evth) return;

    mevent_t *evt = (mevent_t*)hash_lookup(evth, "place");
    mevent_t *evta = (mevent_t*)hash_lookup(evth, "aic");
    if (!evt || !evta) {
        mtc_err("can't find event");
        return;
    }

    char *p, *q;
    HDF *node, *rnode;
    STRING ip;

    string_init(&ip);
    node = hdf_obj_child(hdf);
    while (node) {
        p = hdf_get_value(node, "ip", NULL);
        q = hdf_get_value(node, "addr", NULL);
        if (p && (!q || !*q)) string_appendf(&ip, "%s,", p);

        node = hdf_obj_next(node);
    }

    if (ip.len <= 0) return;

    hdf_set_value(evt->hdfsnd, "ip", ip.buf);
    MEVENT_TRIGGER_VOID(evt, ip.buf, REQ_CMD_PLACEGET, FLAGS_SYNC);

    if (evt->hdfrcv) {
        node = hdf_obj_child(hdf);
        while (node) {
            rnode = hdf_obj_child(evt->hdfrcv);
            p = hdf_get_value(node, "ip", NULL);
            q = hdf_get_value(node, "addr", NULL);
            if (p && (!q || !*q)) {
                while (rnode) {
                    q = hdf_get_value(rnode, "ip", NULL);
                    if (q && !strcmp(p, q)) break;
                    else q = NULL;

                    rnode = hdf_obj_next(rnode);
                }

                if (q) {
                    hdf_set_value(node, "addr", hdf_get_value(rnode, "c", NULL));
                    hdf_set_value(node, "area", hdf_get_value(rnode, "a", NULL));

                    hdf_set_value(evta->hdfsnd, "uid", hdf_get_value(node, "uid", 0));
                    hdf_set_value(evta->hdfsnd, "aid", hdf_get_value(node, "aid", 0));
                    hdf_set_value(evta->hdfsnd, "addr", hdf_get_value(rnode, "c", "Mars"));
                    MEVENT_TRIGGER_NRET(evta, NULL, REQ_CMD_APPUSERUP, FLAGS_NONE);
                } else {
                    mtc_warn("can't find addr for %s", p);
                }
            }

            node = hdf_obj_next(node);
        }
    }

    string_clear(&ip);
}
Esempio n. 10
0
NEOERR* mast_dds_load(char *dir, char *name, RendAsset **a)
{
    char fname[PATH_MAX], *buf, *pos;
    NEOERR *err;

    DdsLoadInfo loadInfoDXT1 = {
        1, 0, 0, 4, 8, GL_COMPRESSED_RGBA_S3TC_DXT1
    };
    DdsLoadInfo loadInfoDXT3 = {
        1, 0, 0, 4, 16, GL_COMPRESSED_RGBA_S3TC_DXT3
    };
    DdsLoadInfo loadInfoDXT5 = {
        1, 0, 0, 4, 16, GL_COMPRESSED_RGBA_S3TC_DXT5
    };
    DdsLoadInfo loadInfoBGRA8 = {
        0, 0, 0, 1, 4, GL_RGBA8, GL_BGRA, GL_UNSIGNED_BYTE
    };
    DdsLoadInfo loadInfoBGR8 = {
        0, 0, 0, 1, 3, GL_RGB8, GL_BGR, GL_UNSIGNED_BYTE
    };
    DdsLoadInfo loadInfoBGR5A1 = {
        0, 1, 0, 1, 2, GL_RGB5_A1, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV
    };
    DdsLoadInfo loadInfoBGR565 = {
        0, 1, 0, 1, 2, GL_RGB5, GL_RGB, GL_UNSIGNED_SHORT_5_6_5
    };
    DdsLoadInfo loadInfoIndex8 = {
        0, 0, 1, 1, 1, GL_RGB8, GL_BGRA, GL_UNSIGNED_BYTE
    };

    if (dir) snprintf(fname, sizeof(fname), "%s%s", dir, name);
    else strncpy(fname, name, sizeof(fname));

    TexAsset *tnode = mtex_node_new();
    if (!tnode) return nerr_raise(NERR_NOMEM, "alloc texture");

    DDS_header hdr;
    int x = 0;
    int y = 0;
    int mipMapCount = 0;
    int totallen = 0;

    err = ne_load_file_len(fname, &buf, &totallen);
    if (err != STATUS_OK) return nerr_pass(err);

    glEnable(GL_TEXTURE_2D);
    glGenTextures(1, &tnode->tex);
    glBindTexture(GL_TEXTURE_2D, tnode->tex);

    pos = buf;
    memcpy(&hdr, buf, sizeof(hdr));
    pos += sizeof(hdr);
    if (pos - buf > totallen) return nerr_raise(NERR_ASSERT, "file too short");

    if (hdr.dwMagic != DDS_MAGIC || hdr.dwSize != 124 ||
        !(hdr.dwFlags & DDSD_PIXELFORMAT) || !(hdr.dwFlags & DDSD_CAPS) )
        return nerr_raise(NERR_ASSERT, "%s Does not appear to be a .dds file", fname);

    x = hdr.dwWidth;
    y = hdr.dwHeight;

    if (!is_power_of_two(x)) {
        mtc_warn("Texture %s with is %i pixels which is not a power of two!", fname, x);
    }

    if (!is_power_of_two(y)) {
        mtc_warn("Texture %s height is %i pixels which is not a power of two!", fname, y);
    }

    DdsLoadInfo* li = &loadInfoDXT1;

    if (PF_IS_DXT1(hdr.sPixelFormat)) {
        li = &loadInfoDXT1;
    } else if (PF_IS_DXT3(hdr.sPixelFormat)) {
        li = &loadInfoDXT3;
    } else if (PF_IS_DXT5(hdr.sPixelFormat)) {
        li = &loadInfoDXT5;
    } else if (PF_IS_BGRA8(hdr.sPixelFormat)) {
        li = &loadInfoBGRA8;
    } else if (PF_IS_BGR8(hdr.sPixelFormat)) {
        li = &loadInfoBGR8;
    } else if (PF_IS_BGR5A1(hdr.sPixelFormat)) {
        li = &loadInfoBGR5A1;
    } else if (PF_IS_BGR565(hdr.sPixelFormat)) {
        li = &loadInfoBGR565;
    } else if (PF_IS_INDEX8(hdr.sPixelFormat)) {
        li = &loadInfoIndex8;
    } else {
        return nerr_raise(NERR_ASSERT, "%s: Unknown DDS File format type.", fname);
    }

    glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE);
    mipMapCount = (hdr.dwFlags & DDSD_MIPMAPCOUNT) ? hdr.dwMipMapCount : 1;

    int ix, zz;
    GLenum cFormat, format;

    if (li->compressed) {
        size_t size = max(li->divSize, x) / li->divSize *
                      max(li->divSize, y) / li->divSize * li->blockBytes;
        char *data = malloc(size);
        if (!data ) {
            return nerr_raise(NERR_ASSERT, "%s: not contain any data.", fname);
        }

        cFormat = li->internalFormat;
        format = li->internalFormat;

        for( ix = 0; ix < mipMapCount; ++ix ) {
            memcpy(data, pos, size);
            pos += size;
            if (pos - buf > totallen) return nerr_raise(NERR_ASSERT, "file too short");

            glCompressedTexImage2D(GL_TEXTURE_2D, ix, li->internalFormat,
                                   x, y, 0, size, data);
            x = (x+1)>>1;
            y = (y+1)>>1;
            size = max(li->divSize, x) / li->divSize *
                   max(li->divSize, y) / li->divSize * li->blockBytes;
        }
        free(data);
    } else if (li->palette) {