Ejemplo n.º 1
0
struct spx_msg *spx_header_to_msg(struct spx_msg_header *header,size_t len,err_t *err){/*{{{*/
    if(NULL == header){
        *err = EINVAL;
        return NULL;
    }
    struct spx_msg *ctx = spx_msg_new(len,err);
    if(NULL == ctx){
        return NULL;
    }
    spx_msg_pack_u32(ctx,header->version);
    spx_msg_pack_u32(ctx,header->protocol);
    spx_msg_pack_u64(ctx,header->bodylen);
    spx_msg_pack_u64(ctx,header->offset);
    if(header->is_keepalive){
        spx_msg_pack_true(ctx);
    } else {
        spx_msg_pack_false(ctx);
    }
    spx_msg_pack_u32(ctx,header->err);
    return ctx;
}/*}}}*/
Ejemplo n.º 2
0
err_t ydb_storage_dio_delete_context_from_chunkfile(
        struct ydb_storage_configurtion *c,
        string_t fname,
        u64_t cbegin,
        u64_t ctotalsize,
        u32_t copver,
        u32_t cver,
        u64_t clastmodifytime,
        u64_t crealsize,
        u64_t lastmodifytime){/*{{{*/
    err_t err = 0;
    struct spx_msg *ioctx = NULL;
    int fd = 0;
    char *mptr = NULL;
    string_t io_suffix = NULL;
    string_t io_hashcode = NULL;

    bool_t io_isdelete = false;
    u32_t io_opver = 0;
    u32_t io_ver = 0;
    u64_t io_createtime = 0;
    u64_t io_lastmodifytime = 0;
    u64_t io_totalsize = 0;
    u64_t io_realsize = 0;


    u32_t unit = (int) cbegin / c->pagesize;
    u64_t begin = unit * c->pagesize;
    u64_t offset = cbegin - begin;
    u64_t len = offset + ctotalsize;

    fd = open(fname,O_RDWR,SpxFileMode);
    if(0 == fd){
        err = errno;
        SpxLogFmt2(c->log,SpxLogError,err,\
                "open chunkfile:%s is fail.",
                fname);
        goto r1;
    }

    mptr = mmap(NULL,\
            len,PROT_READ | PROT_WRITE ,\
            MAP_SHARED,fd,begin);
    if(MAP_FAILED == mptr){
        err = errno;
        SpxLogFmt2(c->log,SpxLogError,err,\
                "mmap chunkfile:%s with begin:%lld,length:%lld is fail.",
                fname,begin,len);
        goto r1;
    }

    ioctx = spx_msg_new(YDB_CHUNKFILE_MEMADATA_SIZE,&err);
    if(NULL == ioctx){
        SpxLog2(c->log,SpxLogError,err,\
                "alloc metedata ioctx is fail.");
        goto r1;
    }

    if(0 != (err = spx_msg_pack_ubytes(ioctx,
                    ((ubyte_t *) (mptr+ offset)),
                    YDB_CHUNKFILE_MEMADATA_SIZE))){
        SpxLog2(c->log,SpxLogError,err,\
                "read metedata to ioctx is fail.");
        goto r1;
    }

    spx_msg_seek(ioctx,0,SpxMsgSeekSet);

    err = ydb_storage_dio_parser_metadata(c->log,ioctx,
            &io_isdelete,&io_opver,
            &io_ver,&io_createtime,
            &io_lastmodifytime,&io_totalsize,&io_realsize,
            &io_suffix,&io_hashcode);
    if(0 != err){
        SpxLog2(c->log,SpxLogError,err,\
                "unpack metedate ioctx is fail.");
        goto r1;
    }

    if(io_isdelete){
        SpxLogFmt1(c->log,SpxLogError,
                "the file in the chunkfile:%s "
                "with begin:%lld totalsize:%lld is deleted.",
                fname,cbegin,ctotalsize);
        err = ENOENT;
        goto r1;
    }
    if(copver != io_opver || cver != io_ver
            || clastmodifytime != io_lastmodifytime
            || ctotalsize != io_totalsize
            || crealsize != io_realsize){
        SpxLogFmt1(c->log,SpxLogError,
                "the file in the chunkfile:%s "
                "with begin:%lld totalsize:%lld is not the deleting-file.",
                fname,cbegin,ctotalsize);
        err = ENOENT;
        goto r1;
    }

    spx_msg_seek(ioctx,0,SpxMsgSeekSet);
    spx_msg_pack_true(ioctx);//isdelete
    spx_msg_pack_u32(ioctx,copver + 1);
    spx_msg_pack_u32(ioctx,YDB_VERSION);
    spx_msg_seek(ioctx,sizeof(u64_t),SpxMsgSeekCurrent);//jump createtime
    spx_msg_pack_u64(ioctx,lastmodifytime);

    memcpy(mptr + offset,ioctx->buf,YDB_CHUNKFILE_MEMADATA_SIZE);

r1:
    if(NULL != ioctx){
        SpxMsgFree(ioctx);
    }
    if(NULL != mptr){
        munmap(mptr,len);
    }
    if(0 < fd){
        SpxClose(fd);
    }
    if(NULL != io_hashcode){
        SpxStringFree(io_hashcode);
    }
    if(NULL != io_suffix){
        SpxStringFree(io_suffix);
    }
    return err;
}/*}}}*/