当前位置: 首页 > 工具软件 > TFS > 使用案例 >

TFS 代码 API

夏侯涵映
2023-12-01

获取TFS C++客户端

TFS C++ 客户端库包含一些头文件和一个libtfsclient.so的共享库文件。 可以直接通过编译源码获取。

TFS C++客户端使用说明

Tfs客户端提供了TfsClient类作为TFS文件操作的接口。类TfsClient在使用前需要初始化,初始化完成之后就可以多次打开不同的Tfs文件,对它们进行读写。Tfs客户端提供了打开/关闭、读/写、删除/反删除等主要的操作,其主要的接口如下:

接口描述
int initialize(const char* ns_addr = NULL, const int32_t cache_time = common::DEFAULT_BLOCK_CACHE_TIME,                                                
                     const int32_t cache_items = common::DEFAULT_BLOCK_CACHE_ITEMS,
                     const bool start_bg = true);
   接口说明: 初始化客户端
   参数说明: ns_addr       集群的nameserver的地址(包括ip和端口),例:10.1.1.1:3100,若传入NULL,则后面的open中必须传入地址
             cache_time    客户端缓存的有效时间
             cache_items   客户端缓存的大小
             start_bg      是否启动后台任务(stat与gc)
   返回值: TFS_SUCCESS: 成功, 其它失败

int open(const char* file_name, const char* suffix, const int flags, const char* key = NULL)
int open(const char* file_name, const char* suffix, const char* ns_addr, const int flags, const char* key = NULL)
   接口说明: 打开文件
   参数说明: file_name  TFS文件名
             suffix     文件后缀,如果没有传NULL
             flags      打开文件的模式
                  T_READ    读
                  T_WRITE   写
                  T_STAT    获取文件状态信息
                  T_LARGE   大文件
                  T_UNLINK  删除
             ns_addr     集群的nameserver的地址(包括ip和端口),例:10.1.1.1:3100
             key         大文件的key 
   返回值: 大于0的整型: 打开的文件描述符(fd),其他失败

int write(const int fd, const void* data, int64_t count)
   接口说明: 写数据
   参数说明: fd    打开的文件描述符
              data  要写入数据所在的地址
              count 要写入数据的长度
   返回值: 大于0的整型:成功写入的字节数, 其它失败

int read(const int fd, void* buf, const int64_t count);
   接口说明: 读取数据
   参数说明:fd    打开的文件描述符
             buf   读取数据的缓冲区
             count 读取数据的长度
   返回值: 大于0的整型:成功读到的字节数, 其它失败

int readv2(const int fd, void* buf, const int64_t count, common::TfsFileStat* file_info);
   接口说明: 读取数据
   参数说明:fd        打开的文件描述符
             buf       读取数据的缓冲区
             count     读取数据的长度
             file_info 文件的元信息
   返回值: 大于0的整型:成功读到的字节数, 其它失败
           此调用与read的区别在于当文件的offset为0时,会返回了文件的状态信息

int fstat(const int fd, common::TfsFileStat* buf, const common::TfsStatType mode = common::NORMAL_STAT)
   接口说明: 获取文件的状态信息
   参数说明: fd        打开的文件描述符
              buf       文件状态信息
              mode      stat模式
                 NORMAL_STAT   正常模式
                 FORCE_STAT    强制模式,用于stat一些非正常状态的文件(删除、隐藏等)
   返回值: TFS_SUCCESS: 成功, 其它失败

int close(const int fd, char* ret_tfs_name = NULL, const int32_t ret_tfs_name_len = 0)
   接口说明: 关闭一个文件
   参数说明:fd                打开的文件描述符
             ret_tfs_name      写入返回的文件名
             ret_tfs_name_len  写入返回的文件名buffer的长度,必须至少为TFS_FILE_LEN(19)。
             open成功之后,必须调用close,释放fd。写的时候,调用close成功以后,数据才会真正写成功。
  返回值: TFS_SUCCESS: 成功, 其它失败

int unlink(int64_t& file_size, const char* file_name, const char* suffix,
           const common::TfsUnlinkType action = common::DELETE,
           const common::OptionFlag option_flag = common::TFS_FILE_DEFAULT_OPTION)
int unlink(int64_t& file_size, const char* file_name, const char* suffix,
           const char*( ns_addr, const common::TfsUnlinkType action = common::DELETE,
           const common::OptionFlag option_flag = common::TFS_FILE_DEFAULT_OPTION)
   接口说明: 删除文件
   参数说明: file_size 返回删除的文件的大小
              file_name 要删除的文件名
              suffix    要删除文件的后缀
              ns_addr   集群的nameserver的地址(包括ip和端口),例:10.1.1.1:3100
              action    动作类型
                  DELETE   删除
                  UNDELETE 反删除
                  CONCEAL  隐藏
                  REVEAL   反隐藏
              option_flag 额外选项
                  TFS_FILE_DEFAULT_OPTION                默认
                  TFS_FILE_NO_SYNC_LOG                   不同步到备集群
   返回值: TFS_SUCCESS: 成功, 其它失败

int64_t save_buf(char* ret_tfs_name, const int32_t ret_tfs_name_len,
              const char* buf, const int64_t count,
              const int32_t flag, const char* suffix = NULL,
              const char* ns_addr = NULL, const char* key = NULL)
   接口说明: 保存一个本地文件到TFS
   参数说明: ret_tfs_name      返回的TFS文件名
              ret_tfs_name_len  返回的TFS文件名的长度
              buf               要保存的数据所在地址
              count             要保存的数据长度
              flag              标志位。 小文件传入T_DEFAULT,大文件传入T_LARGE。
              suffix            文件后缀
              ns_addr           集群的nameserver的地址(包括ip和端口),例:10.1.1.1:3100
              key               当传入flag为T_LARGE时,需要的key
   返回值: 成功时返回 成功save的数据长度(count), 其他失败。

int64_t save_file(char* ret_tfs_name, const int32_t ret_tfs_name_len,
              const char* local_file,
              const int32_t flag, const char* suffix = NULL,
              const char* ns_addr = NULL)
   接口说明: 保存一个本地文件到TFS
   参数说明: ret_tfs_name      返回的TFS文件名
              ret_tfs_name_len  返回的TFS文件名的长度
              local_file        要保存的本地文件(全路径)
              flag              标志位
              suffix            文件后缀
              ns_addr           集群的nameserver的地址(包括ip和端口),例:10.1.1.1:3100
   返回值: local_file的文件大小 其它失败

int fetch_file(const char* local_file,
               const char* file_name, const char* suffix = NULL, const char* ns_addr = NULL)
   接口说明: 下载一个TFS文件到本地
   参数说明: local_file        本地文件(全路径)
              file_name         要下载的TFS文件名
              suffix            文件后缀
              ns_addr           集群的nameserver的地址(包括ip和端口),例:10.1.1.1:3100
   返回值: TFS_SUCCESS: 成功, 其它失败

int fetch_file(int64_t& ret_count, char* buf, const int64_t count,
               const char* file_name, const char* suffix = NULL, const char* ns_addr = NULL)
   接口说明: 下载一个TFS文件,读取最多count大小的数据存放在到buf中
   参数说明: ret_count         下载成功的真实数据长度(ret_count <= count)
              buf               下载缓冲区的地址
              count             要下载的数据长度(文件大小)
              file_name         要下载的TFS文件名
              suffix            文件后缀
              ns_addr           集群的nameserver的地址(包括ip和端口),例:10.1.1.1:3100
   返回值: TFS_SUCCESS: 成功, 其它失败

TFS代码示例

写小文件示例

在使用TfsClient时需要包括tfs_client_api.h头文件,并打开tfs::client, tfs::common名字空间,对Tfs文件进行写操作的示例代码如下:

#include <stdio.h>
#include <string>
#include "tfs_client_api.h"

using namespace std;
using namespace tfs::client;
using namespace tfs::common;

const int length = 256;
const char* nsip = "10.232.4.12:13100";

void write_file()
{
  int ret = 0;
  int fd = -1;
  char buffer[length];
  char tfs_file_name[TFS_FILE_LEN];

  // 创建tfs客户端,并打开一个新的文件准备写入	
  TfsClient* tfsclient = TfsClient::Instance();
  ret = tfsclient->initialize(nsip);
  if (ret != TFS_SUCCESS)
  { 
    printf("init tfs client fail
");
    return ;
  }
  
  fd = tfsclient->open((char*)NULL, NULL, NULL, T_WRITE);
  if (fd <= 0)
  {
    printf("create remote file error!
");
    return ;
  }
	
  int wrote = 0;
  int left = length;
  while (left > 0)
  {
    // 将buffer中的数据写入tfs
    ret = tfsclient->write(fd, buffer + wrote, left);
    if (ret < 0 || ret >= left)  // 读写失败或完成
    {
      break;
    }
    else // 若ret>0,则ret为实际写入的数据量
    {
      wrote += ret;
      left -= ret;
    }
  }

  // 读写失败	
  if (ret < 0)
  {
    printf("write data error!
");
    return ;
  }

  // 提交写入	
  ret = tfsclient->close(fd, tfs_file_name, TFS_FILE_LEN);
	
  if (ret != TFS_SUCCESS) // 提交失败
  {
    printf("write remote file failed! ret: %s
", ret);
  } 
  else
  {
    printf("write remote file: %s successful!
", tfs_file_name);
  }
		
}

int main(int argc, char **argv)
{
	write_file();
	return 0;
}

读文件示例如下

读文件的示例代码如下:

#include <stdio.h>
#include <string>
#include "func.h"
#include "tfs_client_api.h"

using namespace std;
using namespace tfs::client;
using namespace tfs::common;

const char* tfs_file_name = "T2mRETB7AK1RCvBVdK";
const char* nsip = "10.232.35.41:13100";

void read_file()
{
  int ret = 0;
  int fd = -1;
  TfsClient* tfsclient = TfsClient::Instance();
  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);
    return ;
  }

  // 获得文件属性
  TfsFileStat fstat;
  ret = tfsclient->fstat(fd, &fstat);
  if (ret != TFS_SUCCESS || fstat.size_ <= 0)
  {
    printf("get remote file info error
");
    return ;
  }

  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!
");
    delete []buffer;
    return ;
  }

  ret = tfsclient->close(fd);
  if (ret < 0)
  {
    printf("close remote file error!");
    delete []buffer;
    return ;
  }

  printf("read file success!
");
  delete []buffer;
  return ;
}

int main(int argc, char **argv)
{
  read_file();
  return 0;
}

注意:close()必须和open()成对使用。


转载:http://code.taobao.org/p/tfs/wiki/cclient/

 类似资料: