安裝淘寶TFS記錄mysql
- #include <stdio.h>
- #include <string>
- #include <func.h>
- #include <tfs_client_api.h>
- #include "p_w_picpath_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();
- p_w_picpath_data_t TfsDataFetcher::fetch_p_w_picpath_data(const char* tfs_file_name) {
- p_w_picpath_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"
- p_w_picpath_data_t invoke_tfsFetcher_fetchImageData(empty_struct * p, const char * tfs_file_name)
- {
- TfsDataFetcher * fetcher = (TfsDataFetcher *)p;
- return fetcher->fetch_p_w_picpath_data(tfs_file_name);
- }
#main.cc++
- //main.c
- #include <stdio.h>
- #include "p_w_picpath_data_block.h"
- #include <stdio.h>
- #include "fcgi_stdio.h"
- #include <unistd.h>
- extern p_w_picpath_data_t invoke_tfsFetcher_fetchImageData(empty_struct * p, const char * tfs_file_name);
- extern p_w_picpath_data_t invoke_ImageMagick_fetchImageData(empty_struct * p, p_w_picpath_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";
- p_w_picpath_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;
- }