安装淘宝TFS记录
- #include <stdio.h>
- #include <string>
- #include <func.h>
- #include <tfs_client_api.h>
- #include "image_data_block.h"
- #include "tfs_data_fetcher.h"
- using namespace std;
- using namespace tfs::client;
- using namespace tfs::common;
- const char* nsip = "10.168.100.240:8108";
- TfsDataFetcher::TfsDataFetcher()
- {}
- TfsDataFetcher::~TfsDataFetcher()
- {}
- static TfsClient* tfsclient = TfsClient::Instance();
- image_data_t TfsDataFetcher::fetch_image_data(const char* tfs_file_name) {
- image_data_t rtn;
- int ret = 0;
- int fd = -1;
- tfsclient->initialize(nsip);
- // 打开待读写的文件
- fd = tfsclient->open(tfs_file_name, NULL, T_READ);
- if (ret != TFS_SUCCESS)
- {
- printf("open remote file %s error", tfs_file_name);
- rtn.data = NULL;
- rtn.len = 0;
- return rtn;
- }
- // 获得文件属性
- TfsFileStat fstat;
- ret = tfsclient->fstat(fd, &fstat);
- if (ret != TFS_SUCCESS || fstat.size_ <= 0)
- {
- printf("get remote file info error");
- rtn.data = NULL;
- rtn.len = 0;
- return rtn;
- }
- char* buffer = new char[fstat.size_];
- int read = 0;
- uint32_t crc = 0;
- // 读取文件
- while (read < fstat.size_)
- {
- ret = tfsclient->read(fd, buffer + read, fstat.size_ - read);
- if (ret < 0)
- {
- break;
- }
- else
- {
- crc = Func::crc(crc, buffer + read, ret); // 对读取的文件计算crc值
- read += ret;
- }
- }
- if (ret < 0 || crc != fstat.crc_)
- {
- printf("read remote file error!\n");
- delete []buffer;
- rtn.data = NULL;
- rtn.len = 0;
- return rtn;
- }
- ret = tfsclient->close(fd);
- if (ret < 0)
- {
- printf("close remote file error!");
- delete []buffer;
- rtn.data = NULL;
- rtn.len = 0;
- return rtn;
- }
- rtn.data = (u_char *)buffer;
- rtn.len = (int)fstat.size_;
- //delete []buffer;
- return rtn;
- }
- extern "C"
- image_data_t invoke_tfsFetcher_fetchImageData(empty_struct * p, const char * tfs_file_name)
- {
- TfsDataFetcher * fetcher = (TfsDataFetcher *)p;
- return fetcher->fetch_image_data(tfs_file_name);
- }
#main.c
- //main.c
- #include <stdio.h>
- #include "image_data_block.h"
- #include <stdio.h>
- #include "fcgi_stdio.h"
- #include <unistd.h>
- extern image_data_t invoke_tfsFetcher_fetchImageData(empty_struct * p, const char * tfs_file_name);
- extern image_data_t invoke_ImageMagick_fetchImageData(empty_struct * p, image_data_t config);
- empty_struct p;
- empty_struct m;
- int main(int argc, char**argv)
- {
- while (FCGI_Accept() >= 0) {
- const char* tfs_file_name = "T1rEbTB_b_1RCvBVdK";
- image_data_t config = invoke_tfsFetcher_fetchImageData(&p, tfs_file_name);
- if(config.len == 0 || config.data == NULL){
- return -1;
- }
- fwrite(config.data,config.len,1,stdout);
- fflush(stdout);
- }
- return 0;
- }