Beispiel #1
0
//自猜
int baiduapi_mkdir(const char *path, mode_t mode)
{
    (void) mode;
    char buff[2048];
    char fullpath[PATHLEN];
    snprintf(fullpath, sizeof(fullpath) - 1, "%s%s", basepath, path);
    snprintf(buff, sizeof(buff) - 1,
             "https://pcs.baidu.com/rest/2.0/pcs/file?"
             "method=mkdir&"
             "access_token=%s&"
             "path=%s"
             , Access_Token, URLEncode(fullpath));
    FILE *tpfile = tmpfile();

    if (!tpfile) {
        int lasterrno = errno;
        errorlog("create temp file error:%s\n", strerror(errno));
        return -lasterrno;
    }

    Http *r = Httpinit(buff);

    if (r == NULL) {
        int lasterrno = errno;
        errorlog("can't resolve domain:%s\n", strerror(errno));
        fclose(tpfile);
        return -lasterrno;
    }

    r->method = get;
    r->writefunc = savetofile;
    r->writeprame = tpfile;

    if ((errno = request(r)) != CURLE_OK) {
        errorlog("network error:%d\n", errno);
        fclose(tpfile);
        Httpdestroy(r);
        return -EPROTO;
    }

    Httpdestroy(r);
    json_object *json_get = json_object_from_FILE(tpfile);
    fclose(tpfile);

    if (json_get == NULL) {
        errorlog("json_object_from_FILE filed!\n");
        return -EPROTO;
    }

    json_object *jerror_code;
    if (json_object_object_get_ex(json_get, "error_code",&jerror_code)) {
        int errorno = json_object_get_int(jerror_code) ;
        json_object_put(json_get);
        return handleerror(errorno);
    }

    json_object_put(json_get);
    return 0;
}
Beispiel #2
0
PyMODINIT_FUNC initWt(void) {
	PyObject* m = init();

	PyObject_SetAttrString(m, "Http", Httpinit());
	PyObject_SetAttrString(m, "Chart", Chartinit());

	PyObject_SetAttrString(m, "__hook__", PyCObject_FromVoidPtr(&PythonApplication::hook, 0));

	PythonApplication::hook.createApplication = &PythonApplication::createApplication;
	PythonApplication::hook.setAppCreatorFunc = &PythonApplication::setAppCreatorFunc;
	PythonApplication::hook.initThreading = InitThreading;
	PythonApplication::hook.finalizeThreading = FinalizeThreading;

}
Beispiel #3
0
//从服务器读一个block
void readblock(block *tb)
{
    block b = *tb;
    free(tb);
    size_t startp = b.bno * RBS;
    char buff[2048];
    char fullpath[PATHLEN];
    snprintf(fullpath, sizeof(fullpath) - 1, "%s%s", basepath, b.fd->path);
    snprintf(buff, sizeof(buff) - 1,
             "https://pcs.baidu.com/rest/2.0/pcs/file?"
             "method=download&"
             "access_token=%s&"
             "path=%s"
             , Access_Token, URLEncode(fullpath));
    char range[100] = {0};
    snprintf(range, sizeof(range) - 1, "%lu-%lu", startp, startp + RBS - 1);
    char buf[RBS];
    buffstruct bs = {0, RBS, buf};
    Http *r = Httpinit(buff);

    if (r == NULL) {
        errorlog("can't resolve domain:%s\n", strerror(errno));
        goto ret;
    }

    r->method = get;
    r->writefunc = savetobuff;
    r->writeprame = &bs;
    r->range = range;

    if ((errno = request(r)) != CURLE_OK) {
        errorlog("network error:%d\n", errno);
        Httpdestroy(r);
        goto ret;
    }

    Httpdestroy(r);
    pthread_mutex_lock(&b.fd->lock);
    lseek(b.fd->file, startp, SEEK_SET);
    write(b.fd->file,bs.buf, bs.offset);
    b.fd->cache.r.mask[b.bno / 32] |= 1 << (b.bno % 32);
    pthread_mutex_unlock(&b.fd->lock);
    return;
ret:
    pthread_mutex_lock(&b.fd->lock);
    b.fd->cache.r.taskid[b.bno] = 0;
    pthread_mutex_unlock(&b.fd->lock);
}
Beispiel #4
0
//删除文件(不是文件夹)
int baiduapi_unlink(const char *path)
{
    rmscache(path);
    filedec *f = getfcache(path);

    if (f) {
        f->flags |= DELETE;
        pthread_mutex_unlock(&f->lock);
    }

    char buff[2048];
    char fullpath[PATHLEN];
    snprintf(fullpath, sizeof(fullpath) - 1, "%s%s", basepath, path);
    
    snprintf(buff, sizeof(buff) - 1,
             "https://pcs.baidu.com/rest/2.0/pcs/file?"
             "method=delete&"
             "access_token=%s&"
             "path=%s"
             , Access_Token, URLEncode(fullpath));
    FILE *tpfile = tmpfile();

    if (!tpfile) {
        int lasterrno = errno;
        errorlog("create temp file error:%s\n", strerror(errno));
        return -lasterrno;
    }

    Http *r = Httpinit(buff);

    if (r == NULL) {
        int lasterrno = errno;
        errorlog("can't resolve domain:%s\n", strerror(errno));
        fclose(tpfile);
        return -lasterrno;
    }

    r->method = get;
    r->writefunc = savetofile;
    r->writeprame = tpfile;

    if ((errno = request(r)) != CURLE_OK) {
        errorlog("network error:%d\n", errno);
        fclose(tpfile);
        Httpdestroy(r);
        return -EPROTO;
    }

    Httpdestroy(r);
    json_object *json_get = json_object_from_FILE(tpfile);
    fclose(tpfile);

    if (json_get == NULL) {
        errorlog("json_object_from_FILE filed!\n");
        return -EPROTO;
    }

    json_object *jerror_code;
    if (json_object_object_get_ex(json_get, "error_code",&jerror_code)) {
        int errorno = json_object_get_int(jerror_code) ;
        json_object_put(json_get);
        errno = handleerror(errorno);
        if (f && errno == -ENOENT) {
            return 0;
        } else {
            return errno;
        }
    }

    json_object_put(json_get);
    return 0;
}
Beispiel #5
0
//获得文件系统信息,对于百度网盘来说,只有容量是有用的……
int baiduapi_statfs(const char *path, struct statvfs *sf)
{
    char buff[1025];
    sprintf(buff,
            "https://pcs.baidu.com/rest/2.0/pcs/quota?"
            "method=info&"
            "access_token=%s", Access_Token);
    FILE *tpfile = tmpfile();

    if (!tpfile) {
        int lasterrno = errno;
        errorlog("create temp file error:%s\n", strerror(errno));
        return -lasterrno;
    }

    Http *r = Httpinit(buff);

    if (r == NULL) {
        int lasterrno = errno;
        errorlog("can't resolve domain:%s\n", strerror(errno));
        fclose(tpfile);
        return -lasterrno;
    }

    r->method = get;
    r->writefunc = savetofile;
    r->writeprame = tpfile;

    if ((errno = request(r)) != CURLE_OK) {
        errorlog("network error:%d\n",  errno);
        fclose(tpfile);
        Httpdestroy(r);
        return -EPROTO;
    }

    Httpdestroy(r);
    json_object *json_get = json_object_from_FILE(tpfile);
    fclose(tpfile);

    if (json_get == NULL) {
        errorlog("json_object_from_FILE filed!\n");
        return -EPROTO;
    }

    json_object *jerror_code;
    if (json_object_object_get_ex(json_get, "error_code",&jerror_code)) {
        int errorno = json_object_get_int(jerror_code) ;
        json_object_put(json_get);
        return handleerror(errorno);
    }

    sf->f_bsize = 1;
    sf->f_frsize = 1;
    
    json_object *jquota;
    json_object_object_get_ex(json_get, "quota",&jquota);
    sf->f_blocks = json_object_get_int64(jquota);
    
    json_object *jused;
    json_object_object_get_ex(json_get, "used",&jused);
    sf->f_bavail = sf->f_blocks - json_object_get_int64(jused);
    sf->f_bfree = sf->f_bavail;
    json_object_put(json_get);

    return 0;
}
Beispiel #6
0
//读取目录下面有什么文件
int baiduapi_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi)
{
    (void) offset;
    (void) fi;

    char buff[2048];
    char fullpath[PATHLEN];
    sprintf(fullpath, "%s%s", basepath, path);
    int pathlen = strlen(fullpath);

    if (fullpath[pathlen - 1] != '/') {         //如果路径不是以‘/’结尾的,加一个‘/’
        fullpath[pathlen] = '/';
        fullpath[++pathlen] = '\0';
    };

    

    struct stat st;
    memset(&st, 0, sizeof(struct stat));
    st.st_nlink = 1;

    snprintf(buff, sizeof(buff) - 1,
             "https://pcs.baidu.com/rest/2.0/pcs/file?"
             "method=list&"
             "access_token=%s&"
             "path=%s"
             , Access_Token, URLEncode(fullpath));

    FILE *tpfile = tmpfile();

    if (!tpfile) {
        int lasterrno = errno;
        errorlog("create temp file error:%s\n", strerror(errno));
        return -lasterrno;
    }

    Http *r = Httpinit(buff);

    if (r == NULL) {
        int lasterrno = errno;
        errorlog("can't resolve domain:%s\n", strerror(errno));
        fclose(tpfile);
        return -lasterrno;
    }

    r->method = get;
    r->writefunc = savetofile;
    r->writeprame = tpfile;

    if ((errno = request(r)) != CURLE_OK) {
        errorlog("network error:%d\n", errno);
        fclose(tpfile);
        Httpdestroy(r);
        return -EPROTO;
    }

    Httpdestroy(r);
    json_object *json_get = json_object_from_FILE(tpfile);
    fclose(tpfile);

    if (json_get == NULL) {
        errorlog("json_object_from_FILE filed!\n");
        return -EPROTO;
    }

    json_object *jerror_code;
    if (json_object_object_get_ex(json_get, "error_code",&jerror_code)) {
        int errorno = json_object_get_int(jerror_code) ;
        json_object_put(json_get);
        return handleerror(errorno);
    }

    json_object *jlist;
    json_object_object_get_ex(json_get, "list",&jlist);
    filler(buf, ".", NULL, 0);
    filler(buf, "..", NULL, 0);
    
    int i;
    for (i = 0; i < json_object_array_length(jlist); ++i) {
        json_object *filenode = json_object_array_get_idx(jlist, i);
        
        json_object *jmtime;
        json_object_object_get_ex(filenode, "mtime",&jmtime);
        st.st_mtim.tv_sec = json_object_get_int64(jmtime);
        
        json_object *jctime;
        json_object_object_get_ex(filenode, "ctime",&jctime);
        st.st_ctim.tv_sec = json_object_get_int64(jctime);
        
        json_object *jfs_id;
        json_object_object_get_ex(filenode, "fs_id",&jfs_id);
        st.st_ino = json_object_get_int64(jfs_id);
        
        json_object *jsize;
        json_object_object_get_ex(filenode, "size",&jsize);
        st.st_size = json_object_get_int64(jsize);

        json_object *jisdir;
        json_object_object_get_ex(filenode, "isdir",&jisdir);
        if (json_object_get_boolean(jisdir)) {
            st.st_mode = S_IFDIR | 0755;
        } else {
            st.st_mode = S_IFREG | 0444;
        }
        
        json_object *jpath;
        json_object_object_get_ex(filenode, "path",&jpath);
        filler(buf, json_object_get_string(jpath) + pathlen, &st, 0);
        addscache(json_object_get_string(jpath) + strlen(basepath), st);
    }

    json_object_put(json_get);

    filldir(path, buf, filler);
    return 0;
}
Beispiel #7
0
//获得文件属性……
int baiduapi_getattr(const char *path, struct stat *st)
{
    st->st_nlink = 1;
    if (strcmp(path, "/") == 0) {                   //根目录,直接返回,都根目录了,你还查什么查啊,还一直查,说你呢,fuse
        st->st_mode = S_IFDIR | 0755;
        return 0;
    }
    
    filedec *f = getfcache(path);

    if (f) {
        if (f->flags & DELETE) {
            pthread_mutex_unlock(&f->lock);
            return -ENOENT;
        } else {
            st->st_size = f->lengh;
            st->st_mode = S_IFREG | 0444;
            st->st_ctim.tv_sec = f->ctime;
            st->st_mtim.tv_sec = f->mtime;
            pthread_mutex_unlock(&f->lock);
            return 0;
        }
    }

    struct stat *tst = getscache(path);
    if (tst) {
        *st = *tst;
        return 0;
    }
    char buff[2048];
    json_object *json_get = 0, *filenode;
    char fullpath[PATHLEN];
    memset(st, 0, sizeof(struct stat));
    snprintf(fullpath, sizeof(fullpath) - 1, "%s%s", basepath, path);

    snprintf(buff, sizeof(buff) - 1,
             "https://pcs.baidu.com/rest/2.0/pcs/file?"
             "method=meta&"
             "access_token=%s&"
             "path=%s",
             Access_Token, URLEncode(fullpath));
    FILE *tpfile = tmpfile();

    if (!tpfile) {
        int lasterrno = errno;
        errorlog("create temp file error:%s\n", strerror(errno));
        return -lasterrno;
    }

    Http *r = Httpinit(buff);

    if (r == NULL) {
        int lasterrno = errno;
        errorlog("can't resolve domain:%s\n", strerror(errno));
        fclose(tpfile);
        return -lasterrno;
    }

    r->method = get;
    r->writefunc = savetofile;
    r->writeprame = tpfile;

    if ((errno = request(r)) != CURLE_OK) {
        errorlog("network error:%d\n", errno);
        fclose(tpfile);
        Httpdestroy(r);
        return -EPROTO;
    }

    Httpdestroy(r);
    json_get = json_object_from_FILE(tpfile);
    fclose(tpfile);

    if (json_get == NULL) {
        errorlog("json_object_from_FILE filed!\n");
        return -EPROTO;
    }

    json_object *jerror_code;
    if (json_object_object_get_ex(json_get, "error_code",&jerror_code)) {
        int errorno = json_object_get_int(jerror_code) ;
        json_object_put(json_get);
        return handleerror(errorno);
    }

    json_object *jlist;
    json_object_object_get_ex(json_get, "list",&jlist);
    filenode = json_object_array_get_idx(jlist, 0);
    
    json_object *jmtime;
    json_object_object_get_ex(filenode, "mtime",&jmtime);
    st->st_mtim.tv_sec = json_object_get_int64(jmtime);
    
    json_object *jctime;
    json_object_object_get_ex(filenode, "ctime",&jctime);
    st->st_ctim.tv_sec = json_object_get_int64(jctime);
    
    json_object *jfs_id;
    json_object_object_get_ex(filenode, "fs_id",&jfs_id);
    st->st_ino = json_object_get_int64(jfs_id);
    
    json_object *jsize;
    json_object_object_get_ex(filenode, "size",&jsize);
    st->st_size = json_object_get_int64(jsize);

    json_object *jisdir;
    json_object_object_get_ex(filenode, "isdir",&jisdir);
    if (json_object_get_boolean(jisdir)) {
        st->st_mode = S_IFDIR | 0755;                        //文件:只读,想要写,对不起,先拷贝一份下来,然后覆盖
    } else {
        st->st_mode = S_IFREG | 0444;
    }
    addscache(path, *st);
    json_object_put(json_get);
    return 0;
}
Beispiel #8
0
int refreshtoken()
{
    char buff[1024];
    char ATfile[1024];
    char Refresh_Token[100];
    json_object *json_get;

    sprintf(ATfile,"%s/Access_Token",confpath);
    if (!(json_get = json_object_from_file(ATfile))) {            //如果当前目录下面没有.Access_Token文件,那么直接调用gettoken
        return gettoken();
    } else {
        
        json_object *jrefresh_token;
        json_object_object_get_ex(json_get, "refresh_token",&jrefresh_token);
        strcpy(Refresh_Token, json_object_get_string(jrefresh_token));
        json_object_put(json_get);
        snprintf(buff, sizeof(buff) - 1,
                 "https://openapi.baidu.com/oauth/2.0/token?"
                 "grant_type=refresh_token&"
                 "refresh_token=%s&"
                 "client_id=%s&"
                 "client_secret=%s", Refresh_Token, ak, sk);
        FILE *tpfile = tmpfile();

        if (!tpfile) {
            int lasterrno = errno;
            errorlog("create temp file error:%s\n", strerror(errno));
            return -lasterrno;
        }

        Http *r = Httpinit(buff);

        if (r == NULL) {
            int lasterrno = errno;
            errorlog("can't resolve domain:%s\n", strerror(errno));
            fclose(tpfile);
            return -lasterrno;
        }

        r->method = get;
        r->writefunc = savetofile;
        r->writeprame = tpfile;

        if ((errno = request(r)) != CURLE_OK) {
            errorlog("network error:%d\n", errno);
            fclose(tpfile);
            Httpdestroy(r);
            return -EPROTO;
        }

        Httpdestroy(r);
        json_get = json_object_from_FILE(tpfile);
        fclose(tpfile);

        if (json_get == NULL) {
            errorlog("json_object_from_FILE filed!\n");
            return -EPROTO;
        }
        
        json_object *jerror_code;
        if (json_object_object_get_ex(json_get, "error_code",&jerror_code)) {
            int errorno = json_object_get_int(jerror_code) ;
            json_object_put(json_get);
            return handleerror(errorno);
        }

        json_object *jaccess_token;
        if (json_object_object_get_ex(json_get, "access_token",&jaccess_token)) {              
            //找到access_token了,存到文件里面
            strcpy(Access_Token, json_object_get_string(jaccess_token));
            json_object_to_file(ATfile, json_get);
            json_object_put(json_get);
        } else {
            puts("Authorization error!");
            json_object_put(json_get);
            errno = EPERM;
            return -errno;
        }
    }
    return 0;
}
Beispiel #9
0
/*获得Access_Token
 * 我曾经把自己模拟成浏览器手动post用户名密码,开始挺好使的
 * 后来不行了……因为登录次数多了居然要输验证码了!!!
 */
int gettoken()
{
    char code[200];
    char buff[1024];
    char ATfile[1024];
    json_object *json_get;

    sprintf(ATfile,"%s/Access_Token",confpath);
    if (!(json_get = json_object_from_file(ATfile))) {                     //如果当前目录下面没有.Access_Token文件,那么重新获取
        fprintf(stderr, "You have to login first!\n");

        if (fork() == 0) {
            snprintf(buff, sizeof(buff) - 1,
                     "x-www-browser "                                               //什么?你说没有x-www-browser?那可不关我的事,你自己搞定吧
                     "-new-tab "
                     "\"http://openapi.baidu.com/oauth/2.0/authorize?"
                     "response_type=code&"
                     "client_id=%s&"
                     "redirect_uri=oob&"
                     "scope=netdisk&"
                     "display=page\""
                     , ak);
            system(buff);
            exit(errno);
        } else {
            puts("please copy the authorization code from the browser:");
            scanf("%199s", code);
            sprintf(buff,
                    "https://openapi.baidu.com/oauth/2.0/token?"
                    "grant_type=authorization_code&"
                    "code=%s&"
                    "client_id=%s&"
                    "client_secret=%s&"
                    "redirect_uri=oob"
                    , code, ak, sk);
            FILE *tpfile = tmpfile();

            if (!tpfile) {
                int lasterrno = errno;
                errorlog("create temp file error:%s\n", strerror(errno));
                return -lasterrno;
            }

            Http *r = Httpinit(buff);

            if (r == NULL) {
                int lasterrno = errno;
                errorlog("can't resolve domain:%s\n", strerror(errno));
                fclose(tpfile);
                return -lasterrno;
            }

            r->method = get;
            r->writefunc = savetofile;
            r->writeprame = tpfile;

            if ((errno = request(r)) != CURLE_OK) {
                errorlog("network error:%d\n", errno);
                fclose(tpfile);
                Httpdestroy(r);
                return -EPROTO;
            }

            Httpdestroy(r);
            json_get = json_object_from_FILE(tpfile);
            fclose(tpfile);

            if (json_get == NULL) {
                errorlog("json_object_from_FILE filed!\n");
                return -EPROTO;
            }

            json_object *jerror_code;
            if (json_object_object_get_ex(json_get, "error_code",&jerror_code)) {
                int errorno = json_object_get_int(jerror_code) ;
                json_object_put(json_get);
                return handleerror(errorno);
            }

            json_object *jaccess_token;
            if (json_object_object_get_ex(json_get, "access_token",&jaccess_token)) {        //找到access_token了,存到文件里面
                strcpy(Access_Token, json_object_get_string(jaccess_token));
                json_object_to_file(ATfile, json_get);
                json_object_put(json_get);
            } else {
                puts("Authorization error!");
                remove(ATfile);
                json_object_put(json_get);
                errno = EPERM;
                return -errno;
            }
        }

    } else {            //如果文件里面存在,直接读取文件,当然,这里没有考虑有不怀好意的人修改我的文件的情况
        json_object *jaccess_token;
        json_object_object_get_ex(json_get, "access_token",&jaccess_token);
        strcpy(Access_Token, json_object_get_string(jaccess_token));
        json_object_put(json_get);
    }

    return 0;
}
Beispiel #10
0
//上传一个block作为tmpfile
void uploadblock(block *tb)
{
    block b = *tb;
    free(tb);
    char buff[1024];
    snprintf(buff, sizeof(buff) - 1,
             "https://c.pcs.baidu.com/rest/2.0/pcs/file?"
             "method=upload&"
             "access_token=%s&"
             "type=tmpfile"
             , Access_Token);

    Http *r = Httpinit(buff);

    if (r == NULL) {
        errorlog("can't resolve domain:%s\n", strerror(errno));
        goto ret;
    }

    FILE *tpfile = tmpfile();
    if (!tpfile) {
        errorlog("create temp file error:%s\n", strerror(errno));
        goto ret;
    }

    char *buf = (char *)malloc(GetWriteBlkSize(b.bno));
    buffstruct bs = {0, GetWriteBlkSize(b.bno), buf};
    pthread_mutex_lock(&b.fd->lock);
    lseek(b.fd->file, GetWriteBlkStartPoint(b.bno), SEEK_SET);
    bs.len = read(b.fd->file, bs.buf, bs.len);
    CLRR(b.fd->cache.w.flags, b.bno);
    pthread_mutex_unlock(&b.fd->lock);

    r->method = post_formdata;
    r->readfunc = readfrombuff;
    r->readprame = &bs;
    r->length = bs.len;
    r->writefunc = savetofile;
    r->writeprame = tpfile;

    if ((errno = request(r)) != CURLE_OK) {
        errorlog("network error:%d\n", errno);
        free(buf);
        Httpdestroy(r);
        fclose(tpfile);
        goto ret;
    }
    free(buf);
    Httpdestroy(r);

    json_object * json_get = json_object_from_FILE(tpfile);
    fclose(tpfile);

    if (json_get == NULL) {
        errorlog("json_object_from_FILE filed!\n");
        goto ret;
    }

    json_object *jerror_code;
    if (json_object_object_get_ex(json_get, "error_code", &jerror_code)) {
        json_object *jerror_msg;
        json_object_object_get_ex(json_get, "error_msg", &jerror_msg);
        errorlog("api error:%s\n", json_object_get_string(jerror_msg));
        json_object_put(json_get);
        goto ret;
    }

    json_object *jmd5;
    if (json_object_object_get_ex(json_get, "md5",&jmd5)) {
        pthread_mutex_lock(&b.fd->lock);
        if (GETR(b.fd->cache.w.flags, b.bno)) {
            CLRR(b.fd->cache.w.flags, b.bno);
        } else {
            strcpy(b.fd->cache.w.md5[b.bno], json_object_get_string(jmd5));
            CLRD(b.fd->cache.w.flags, b.bno);
            CLRZ(b.fd->cache.w.flags, b.bno);
            if (b.bno)
                sem_post(&wcache);
        }
        b.fd->cache.w.taskid[b.bno] = 0;
        pthread_mutex_unlock(&b.fd->lock);
        json_object_put(json_get);
        return;
    } else {
        errorlog("Did not get MD5:%s\n", json_object_to_json_string(json_get));
    }
ret:
    pthread_mutex_lock(&b.fd->lock);
    b.fd->cache.w.taskid[b.bno] = 0;
    pthread_mutex_unlock(&b.fd->lock);
}
Beispiel #11
0
//把上传的临时文件合并成一个文件
int baiduapi_mergertmpfile(const char *path, filedec *f)
{
    char buff[2048];
    char fullpath[PATHLEN];
    snprintf(fullpath, sizeof(fullpath) - 1, "%s%s", basepath, path);
    snprintf(buff, sizeof(buff) - 1,
             "https://pcs.baidu.com/rest/2.0/pcs/file?"
             "method=createsuperfile&"
             "access_token=%s&"
             "path=%s&"
             "ondup=overwrite"
             , Access_Token, URLEncode(fullpath));
    FILE *tpfile = tmpfile();

    if (!tpfile) {
        int lasterrno = errno;
        errorlog("create temp file error:%s\n", strerror(errno));
        return -lasterrno;
    }

    json_object *jobj = json_object_new_object();
    json_object *jarray = json_object_new_array();

    int i;
    for (i = 0; i <= GetWriteBlkNo(f->lengh); ++i) {
        json_object_array_add(jarray, json_object_new_string(f->cache.w.md5[i]));
    };

    json_object_object_add(jobj, "block_list", jarray);
    char param[35000];
    snprintf(param, sizeof(param) - 1, "param=%s", json_object_to_json_string(jobj));
    json_object_put(jobj);
    buffstruct bs = {0, strlen(param), param};
    bs.len = strlen(bs.buf);
    Http *r = Httpinit(buff);

    if (r == NULL) {
        int lasterrno = errno;
        errorlog("can't resolve domain:%s\n", strerror(errno));
        fclose(tpfile);
        return -lasterrno;
    }

    r->method = post_x_www_form_urlencoded;
    r->readfunc = readfrombuff;
    r->readprame = &bs;
    r->writefunc = savetofile;
    r->writeprame = tpfile;
    r->length = bs.len;

    if ((errno = request(r)) != CURLE_OK) {
        errorlog("network error:%d\n", errno);
        Httpdestroy(r);
        fclose(tpfile);
        return -EPROTO;
    }

    Httpdestroy(r);
    json_object *json_get = json_object_from_FILE(tpfile);
    fclose(tpfile);

    if (json_get == NULL) {
        errorlog("json_object_from_FILE filed!\n");
        return -EPROTO;
    }

    json_object *jerror_code;
    if (json_object_object_get_ex(json_get, "error_code",&jerror_code)) {
        int errorno = json_object_get_int(jerror_code) ;
        errorlog("body:\n%s\nget:\n%s\n", param, json_object_to_json_string(json_get));
        json_object_put(json_get);
        return handleerror(errorno);
    }

    json_object_put(json_get);
    return 0;
}
Beispiel #12
0
//上传一个临时文件,md5是服务器返回的md5值,用来合并成一个文件
int baiduapi_uploadtmpfile(int file, char *md5)
{
    char buff[1024];
    snprintf(buff, sizeof(buff) - 1,
             "https://c.pcs.baidu.com/rest/2.0/pcs/file?"
             "method=upload&"
             "access_token=%s&"
             "type=tmpfile"
             , Access_Token);
    FILE *tpfile = tmpfile();

    if (!tpfile) {
        int lasterrno = errno;
        errorlog("create temp file error:%s\n", strerror(errno));
        return -lasterrno;
    }

    Http *r = Httpinit(buff);

    if (r == NULL) {
        int lasterrno = errno;
        errorlog("can't resolve domain:%s\n", strerror(errno));
        fclose(tpfile);
        return -lasterrno;
    }

    r->method = post_formdata;
    r->readfunc = readfromfd;
    r->readprame = (void *)(long)file;
    r->length = lseek(file, 0, SEEK_END);
    lseek(file, 0, SEEK_SET);
    r->writefunc = savetofile;
    r->writeprame = tpfile;

    if ((errno = request(r)) != CURLE_OK) {
        errorlog("network error:%d\n", errno);
        Httpdestroy(r);
        fclose(tpfile);
        return -EPROTO;
    }

    Httpdestroy(r);
    json_object *json_get = json_object_from_FILE(tpfile);
    fclose(tpfile);

    if (json_get == NULL) {
        errorlog("json_object_from_FILE filed!\n");
        return -EPROTO;
    }

    json_object *jerror_code;
    if (json_object_object_get_ex(json_get, "error_code",&jerror_code)) {
        int errorno = json_object_get_int(jerror_code) ;
        json_object_put(json_get);
        return handleerror(errorno);
    }

    json_object *jmd5;
    if (json_object_object_get_ex(json_get, "md5",&jmd5)) {
        strcpy(md5, json_object_get_string(jmd5));
        json_object_put(json_get);
        return 0;
    } else {
        errorlog("Did not get MD5:%s\n", json_object_to_json_string(json_get));
        return -EPROTO;
    }
}
Beispiel #13
0
/* 想猜你就继续猜吧
 */
int baiduapi_rename(const char *oldname, const char *newname)
{
    rmscache(oldname);
    filedec *f = getfcache(oldname);

    if (f) {
        renamecache(oldname, newname);
        if(f->type == forwrite && (f->flags & SYNCED) == 0) {
            pthread_mutex_unlock(&f->lock);
            return 0;
        }
        pthread_mutex_unlock(&f->lock);
    }


    char buff[3096];
    char oldfullpath[PATHLEN];
    char newfullpath[PATHLEN];
    snprintf(oldfullpath, sizeof(oldfullpath) - 1, "%s%s", basepath, oldname);
    snprintf(newfullpath, sizeof(newfullpath) - 1, "%s%s", basepath, newname);

    snprintf(buff, sizeof(buff) - 1,
             "https://pcs.baidu.com/rest/2.0/pcs/file?"
             "method=move&"
             "access_token=%s&"
             "from=%s&"
             "to=%s"
             , Access_Token, URLEncode(oldfullpath), URLEncode(newfullpath));
    FILE *tpfile = tmpfile();

    if (!tpfile) {
        int lasterrno = errno;
        errorlog("create temp file error:%s\n", strerror(errno));
        return -lasterrno;
    }

    Http *r = Httpinit(buff);

    if (r == NULL) {
        int lasterrno = errno;
        errorlog("can't resolve domain:%s\n", strerror(errno));
        fclose(tpfile);
        return -lasterrno;
    }

    r->method = get;
    r->writefunc = savetofile;
    r->writeprame = tpfile;

    if ((errno = request(r)) != CURLE_OK) {
        errorlog("network error:%d\n", errno);
        fclose(tpfile);
        Httpdestroy(r);
        return -EPROTO;
    }

    Httpdestroy(r);
    json_object *json_get = json_object_from_FILE(tpfile);
    fclose(tpfile);

    if (json_get == NULL) {
        errorlog("json_object_from_FILE filed!\n");
        return -EPROTO;
    }

    json_object *jerror_code;
    if (json_object_object_get_ex(json_get, "error_code",&jerror_code)) {
        int errorno = json_object_get_int(jerror_code) ;
        json_object_put(json_get);
        return handleerror(errorno);
    }

    json_object_put(json_get);
    return 0;
}
Beispiel #14
0
int baiduapi_link(const char *target, const char *lnname)
{
    filedec *f = getfcache(target);

    if (f) {
        if ((f->flags & SYNCED) == 0) {                              //如果没有同步则不能操作,其实该文件服务器上还不存在……
            pthread_mutex_unlock(&f->lock);
            return -EBUSY;
        }
        pthread_mutex_unlock(&f->lock);
    }

    char buff[3096];
    char targetfullpath[PATHLEN];
    char lnfullpath[PATHLEN];
    snprintf(targetfullpath, sizeof(targetfullpath) - 1, "%s%s", basepath, target);
    snprintf(lnfullpath, sizeof(lnfullpath) - 1, "%s%s", basepath, lnname);

    snprintf(buff, sizeof(buff) - 1,
             "https://pcs.baidu.com/rest/2.0/pcs/file?"
             "method=copy&"
             "access_token=%s&"
             "from=%s&"
             "to=%s"
             , Access_Token, URLEncode(targetfullpath), URLEncode(lnfullpath));
    FILE *tpfile = tmpfile();

    if (!tpfile) {
        int lasterrno = errno;
        errorlog("create temp file error:%s\n", strerror(errno));
        return -lasterrno;
    }

    Http *r = Httpinit(buff);

    if (r == NULL) {
        int lasterrno = errno;
        errorlog("can't resolve domain:%s\n", strerror(errno));
        fclose(tpfile);
        return -lasterrno;
    }

    r->method = get;
    r->writefunc = savetofile;
    r->writeprame = tpfile;

    if ((errno = request(r)) != CURLE_OK) {
        errorlog("network error:%d\n", errno);
        fclose(tpfile);
        Httpdestroy(r);
        return -EPROTO;
    }

    Httpdestroy(r);
    json_object *json_get = json_object_from_FILE(tpfile);
    fclose(tpfile);

    if (json_get == NULL) {
        errorlog("json_object_from_FILE filed!\n");
        return -EPROTO;
    }

    json_object *jerror_code;
    if (json_object_object_get_ex(json_get, "error_code",&jerror_code)) {
        int errorno = json_object_get_int(jerror_code) ;
        json_object_put(json_get);
        return handleerror(errorno);
    }

    json_object_put(json_get);
    return 0;
}
Beispiel #15
0
//删除文件夹,会失效所有状态缓存
int baiduapi_rmdir(const char *path)
{
    /*    if(getscache(path))
            return -ENOTEMPTY;
        filedec *f = getcache(path);
        if (f) {
            pthread_mutex_unlock(&f->lock);
            return -ENOTEMPTY;
        }*/


    char buff[2048];
    char fullpath[PATHLEN];
    snprintf(fullpath, sizeof(fullpath) - 1, "%s%s", basepath, path);

    snprintf(buff, sizeof(buff) - 1,
             "https://pcs.baidu.com/rest/2.0/pcs/file?"
             "method=list&"
             "access_token=%s&"
             "path=%s&limit=0-1"
             , Access_Token, URLEncode(fullpath));

    FILE *tpfile = tmpfile();

    if (!tpfile) {
        int lasterrno = errno;
        errorlog("create temp file error:%s\n", strerror(errno));
        return -lasterrno;
    }

    Http *r = Httpinit(buff);

    if (r == NULL) {
        int lasterrno = errno;
        errorlog("can't resolve domain:%s\n", strerror(errno));
        fclose(tpfile);
        return -lasterrno;
    }

    r->method = get;
    r->writefunc = savetofile;
    r->writeprame = tpfile;

    if ((errno = request(r)) != CURLE_OK) {
        errorlog("network error:%d\n", errno);
        fclose(tpfile);
        Httpdestroy(r);
        return -EPROTO;
    }

    Httpdestroy(r);
    json_object *json_get = json_object_from_FILE(tpfile);
    fclose(tpfile);

    if (json_get == NULL) {
        errorlog("json_object_from_FILE filed!\n");
        return -EPROTO;
    }

    json_object *jerror_code;
    if (json_object_object_get_ex(json_get, "error_code",&jerror_code)) {
        int errorno = json_object_get_int(jerror_code) ;
        json_object_put(json_get);
        return handleerror(errorno);
    }

    json_object *jlist;
    json_object_object_get_ex(json_get, "list",&jlist);
    if (json_object_array_length(jlist) != 0) {
        json_object_put(json_get);
        return -ENOTEMPTY;
    }
    json_object_put(json_get);

    return baiduapi_unlink(path);
}