int cmd_del_life_cycle(const VSTRING& param) { int32_t file_type = atoi(param[0].c_str()); const char* file_name = param[1].c_str(); char appkey[257]; strcpy(appkey, app_key); RcClientImpl impl; impl.set_kv_rs_addr(krs_addr); int ret = impl.initialize(rc_addr, appkey, app_ip); if (TFS_SUCCESS != ret) { TBSYS_LOG(DEBUG, "rc client init failed, ret: %d", ret); } else { ret = impl.rm_life_cycle(file_type, file_name); } if (TFS_SUCCESS == ret) { ToolUtil::print_info(ret, "del life cycle file %s success", file_name); } return ret; }
int cmd_get_file_raw(const VSTRING& param) { const char* tfs_name = canonical_param(param[0]); const char* local_file = expand_path(const_cast<string&>(param[1])); char appkey[257]; int size = param.size(); if (size > 2) { TBSYS_LOG(DEBUG, "appkey: %s", param[2].c_str()); strncpy(appkey, param[2].c_str(), 256); appkey[256] = '\0'; } else { strcpy(appkey, app_key); } int ret = TFS_SUCCESS; RcClientImpl impl; ret = impl.initialize(rc_addr, appkey, app_ip); if (TFS_SUCCESS != ret) { TBSYS_LOG(DEBUG, "rc client init failed, ret: %d", ret); } else { ret = impl.fetch_file(local_file, tfs_name, NULL); } ToolUtil::print_info(ret, "fetch %s => %s", tfs_name, local_file); return ret; }
int cmd_del_object(const VSTRING& param) { const char* bucket_name = param[0].c_str(); const char* object_name = param[1].c_str(); char appkey[257]; int size = param.size(); if (size > 2) { strncpy(appkey, param[2].c_str(), 256); appkey[256] = '\0'; } else { strcpy(appkey, app_key); } UserInfo user_info; RcClientImpl impl; impl.set_kv_rs_addr(krs_addr); int ret = impl.initialize(rc_addr, appkey, app_ip); if (TFS_SUCCESS != ret) { TBSYS_LOG(DEBUG, "rc client init failed, ret: %d", ret); } else { ret = impl.del_object(bucket_name, object_name, user_info); } ToolUtil::print_info(ret, "del bucket: %s, object: %s", bucket_name, object_name); return ret; }
int cmd_rm_dir_meta(const VSTRING& param) { int ret = TFS_SUCCESS; const char* dir_path = expand_path(const_cast<string&>(param[0])); char appkey[257]; int size = param.size(); if (size > 1) { strncpy(appkey, param[1].c_str(), 256); appkey[256] = '\0'; } else { strcpy(appkey, app_key); } if (size > 2) { uid = strtoll(param[2].c_str(), NULL, 10); } RcClientImpl impl; ret = impl.initialize(rc_addr, appkey, app_ip); if (TFS_SUCCESS != ret) { TBSYS_LOG(DEBUG, "meta client init failed, ret: %d", ret); } else { ret = impl.rm_dir(uid, dir_path); } return ret; }
int cmd_ls_file_meta(const VSTRING& param) { int ret = TFS_SUCCESS; char appkey[257]; if (!g_use_meta) { TBSYS_LOG(WARN, "sorry, this commond is for name meta!!!"); } else { const char* file_path = expand_path(const_cast<string&>(param[0])); int size = param.size(); if (size > 1) { strncpy(appkey, param[1].c_str(), 256); appkey[256] = '\0'; } else { strcpy(appkey, app_key); } if (size > 2) { app_id = strtoll(param[2].c_str(), NULL, 10); } if (size > 3) { uid = strtoll(param[3].c_str(), NULL, 10); } RcClientImpl impl; ret = impl.initialize(rc_addr, appkey, app_ip); if (TFS_SUCCESS != ret) { TBSYS_LOG(DEBUG, "meta client init failed, ret: %d", ret); } else { FileMetaInfo file_info; ret = impl.ls_file(app_id, uid, file_path, file_info); if (TFS_SUCCESS == ret) { if (file_info.name_.size() > 0) fprintf(stdout, "name:%s\n", file_info.name_.data()); fprintf(stdout, "pid %"PRI64_PREFIX"d id %"PRI64_PREFIX "d create_time %s modify_time %s size %"PRI64_PREFIX"d ver_no %d\n", file_info.pid_, file_info.id_, Func::time_to_str(file_info.create_time_).c_str(), Func::time_to_str(file_info.modify_time_).c_str(), file_info.size_, file_info.ver_no_); } } } return ret; }
int cmd_ls_dir_meta(const VSTRING& param) { int ret = TFS_SUCCESS; const char* dir_path = expand_path(const_cast<string&>(param[0])); int size = param.size(); char appkey[257]; TBSYS_LOG(DEBUG, "size: %d", size); if (size > 1) { TBSYS_LOG(DEBUG, "appkey: %s", param[1].c_str()); strncpy(appkey, param[1].c_str(), 256); appkey[256] = '\0'; } else { strcpy(appkey, app_key); } if (size > 2) { app_id = strtoll(param[2].c_str(), NULL, 10); } if (size > 3) { uid = strtoll(param[3].c_str(), NULL, 10); } RcClientImpl impl; ret = impl.initialize(rc_addr, appkey, app_ip); if (TFS_SUCCESS != ret) { TBSYS_LOG(DEBUG, "meta client init failed, ret: %d", ret); } else { std::vector<FileMetaInfo> meta_info; std::vector<FileMetaInfo>::const_iterator it; ret = impl.ls_dir(app_id, uid, dir_path, meta_info); if (TFS_SUCCESS == ret) { for (it = meta_info.begin(); it != meta_info.end(); it++) { if (it->name_.size() > 0) fprintf(stdout, "name:%s\n", it->name_.data()); fprintf(stdout, "pid %"PRI64_PREFIX"d id %"PRI64_PREFIX "d create_time %d modify_time %d size %"PRI64_PREFIX"d ver_no %d\n", it->pid_, it->id_, it->create_time_, it->modify_time_, it->size_, it->ver_no_); } } } return ret; }
int put_file_raw_ex(const VSTRING& param, const bool is_large) { int32_t size = param.size(); const char* local_file = expand_path(const_cast<string&>(param[0])); char* tfs_name = NULL; const char* suffix = NULL; char appkey[257]; //int32_t flag = T_DEFAULT; int ret = TFS_SUCCESS; char ret_tfs_name[TFS_FILE_LEN]; ret_tfs_name[0] = '\0'; if (size > 1) { suffix = canonical_param(param[1]); } if (size > 2) { //TBSYS_LOG(DEBUG, "app_key: %s", param[2].c_str()); strncpy(appkey, canonical_param(param[2]), 256); appkey[256] = '\0'; } else { // default app_key = "tfscom" strcpy(appkey, app_key); } //login with rc and app_key RcClientImpl impl; ret = impl.initialize(rc_addr, appkey, app_ip); if (TFS_SUCCESS != ret) { TBSYS_LOG(DEBUG, "rc client init failed, ret: %d", ret); } else { ret = impl.save_file(local_file, ret_tfs_name, TFS_FILE_LEN, suffix, is_large) < 0 ? TFS_ERROR : TFS_SUCCESS; } //printf("tfs_name: %s, ret_tfs_name: %s\n", tfs_name, ret_tfs_name); ToolUtil::print_info(ret, "put %s => %s", local_file, tfs_name != NULL ? FSName(tfs_name, suffix).get_name() : ret_tfs_name); return ret; }
int cmd_is_dir_exist_meta(const VSTRING& param) { int ret = TFS_SUCCESS; const char* dir_path = expand_path(const_cast<string&>(param[0])); char appkey[257]; int size = param.size(); TBSYS_LOG(DEBUG, "size: %d", size); if (size > 1) { //TBSYS_LOG(DEBUG, "appkey: %s", param[1].c_str()); strncpy(appkey, param[1].c_str(), 256); appkey[256] = '\0'; } else { strcpy(appkey, app_key); } if (size > 2) { app_id = strtoll(param[2].c_str(), NULL, 10); } if (size > 3) { uid = strtoll(param[3].c_str(), NULL, 10); } RcClientImpl impl; ret = impl.initialize(rc_addr, appkey, app_ip); if (TFS_SUCCESS != ret) { TBSYS_LOG(DEBUG, "meta client init failed, ret: %d", ret); } else { std::vector<FileMetaInfo> meta_info; std::vector<FileMetaInfo>::const_iterator it; bool bRet = impl.is_dir_exist(app_id, uid, dir_path); fprintf(stdout, "dir: %s %s exist \n", dir_path, bRet ? "" : "not "); } return ret; }
int remove_file_raw_ex(const VSTRING& param, TfsUnlinkType type) { const char* tfs_name = canonical_param(param[0]); char appkey[257]; int ret = TFS_SUCCESS; int size = param.size(); if (size > 1) { //TBSYS_LOG(DEBUG, "app_key: %s", param[1].c_str()); strncpy(appkey, canonical_param(param[1]), 256); appkey[256] = '\0'; } else { //default app_key = "tfscom" strcpy(appkey, app_key); } RcClientImpl impl; ret = impl.initialize(rc_addr, appkey, app_ip); if (TFS_SUCCESS != ret) { TBSYS_LOG(DEBUG, "rc client init fail, ret: %d", ret); } else { ret = impl.unlink(tfs_name, NULL, type); } if (type == DELETE) { ToolUtil::print_info(ret, "del %s", tfs_name); } else if (type == UNDELETE) { ToolUtil::print_info(ret, "undel %s", tfs_name); } return ret; }
int cmd_head_bucket(const VSTRING& param) { const char* bucket_name = param[0].c_str(); char appkey[257]; int size = param.size(); if (size > 1) { strncpy(appkey, param[1].c_str(), 256); appkey[256] = '\0'; } else { strcpy(appkey, app_key); } BucketMetaInfo bucket_meta_info; UserInfo user_info; RcClientImpl impl; impl.set_kv_rs_addr(krs_addr); int ret = impl.initialize(rc_addr, appkey, app_ip); if (TFS_SUCCESS != ret) { TBSYS_LOG(DEBUG, "rc client init failed, ret: %d", ret); } else { ret = impl.head_bucket(bucket_name, &bucket_meta_info, user_info); } ToolUtil::print_info(ret, "head bucket %s", bucket_name); if (TFS_SUCCESS == ret) { printf("bucket: %s, create_time: %"PRI64_PREFIX"d, owner_id: %"PRI64_PREFIX"d\n", bucket_name, bucket_meta_info.create_time_, bucket_meta_info.owner_id_); } return ret; }
int cmd_put_object(const VSTRING& param) { const char* bucket_name = param[0].c_str(); const char* object_name = param[1].c_str(); const char* local_file = expand_path(const_cast<string&>(param[2])); int64_t owner_id = strtoll(param[3].c_str(), NULL, 10); char appkey[257]; int size = param.size(); if (size > 4) { strncpy(appkey, param[4].c_str(), 256); appkey[256] = '\0'; } else { strcpy(appkey, app_key); } UserInfo user_info; user_info.owner_id_ = owner_id; RcClientImpl impl; impl.set_kv_rs_addr(krs_addr); int ret = impl.initialize(rc_addr, appkey, app_ip); if (TFS_SUCCESS != ret) { TBSYS_LOG(DEBUG, "rc client init failed, ret: %d", ret); } else { ret = impl.put_object(bucket_name, object_name, local_file, user_info); ToolUtil::print_info(ret, "put object: %s, object: %s => %s owner_id: %"PRI64_PREFIX"d", bucket_name, object_name, local_file, owner_id); } return ret; }
int cmd_hide_file_raw(const VSTRING& param) { const char* tfs_name = canonical_param(param[0]); char appkey[257]; int size = param.size(); TfsUnlinkType unlink_type = CONCEAL; if (size > 1) { unlink_type = static_cast<TfsUnlinkType>(atoi(param[1].c_str())); } if (size > 2) { strncpy(appkey, param[2].c_str(), 256); appkey[256] = '\0'; } else { strcpy(appkey, app_key); } RcClientImpl impl; int ret = impl.initialize(rc_addr, appkey, app_ip); if (TFS_SUCCESS != ret) { TBSYS_LOG(DEBUG, "rc client login fail, ret: %d", ret); } else { ret = impl.unlink(tfs_name, NULL, unlink_type); } ToolUtil::print_info(ret, "hide %s %d", tfs_name, unlink_type); return ret; }
int cmd_put_bucket(const VSTRING& param) { const char* bucket_name = param[0].c_str(); int64_t owner_id = strtoll(param[1].c_str(), NULL, 10); char appkey[257]; int size = param.size(); if (size > 2) { strncpy(appkey, param[2].c_str(), 256); appkey[256] = '\0'; } else { strcpy(appkey, app_key); } UserInfo user_info; user_info.owner_id_ = owner_id; RcClientImpl impl; impl.set_kv_rs_addr(krs_addr); int ret = impl.initialize(rc_addr, appkey, app_ip); if (TFS_SUCCESS != ret) { TBSYS_LOG(DEBUG, "rc client init failed, ret: %d", ret); } else { ret = impl.put_bucket(bucket_name, user_info); } if (TFS_SUCCESS == ret) { ToolUtil::print_info(ret, "put bucket %s owner_id : %ld", bucket_name, owner_id); } return ret; }
int cmd_stat_file_raw(const VSTRING& param) { const char* tfs_name = canonical_param(param[0]); char appkey[257]; int size = param.size(); if (size > 1) { //TBSYS_LOG(DEBUG, "app_key: %s", param[1].c_str()); strncpy(appkey, param[1].c_str(), 256); appkey[256] = '\0'; } else { strcpy(appkey, app_key); } TfsFileStat file_stat; RcClientImpl impl; int ret = impl.initialize(rc_addr, appkey, app_ip); if (TFS_SUCCESS != ret) { TBSYS_LOG(DEBUG, "rc init fail, ret: %d", ret); } else { int fd = -1; if (tfs_name[0] == 'L') { fd = impl.open(tfs_name, NULL, RcClient::READ, true); } else { fd = impl.open(tfs_name, NULL, RcClient::READ); } if (fd < 0) { ret = TFS_ERROR; TBSYS_LOG(DEBUG, "%s open fail, return fd: %d", tfs_name, fd); } else { if ((ret = impl.fstat(fd, &file_stat, FORCE_STAT)) != TFS_SUCCESS) { TBSYS_LOG(DEBUG, "stat %s fail, return %d", tfs_name, ret); } int re = impl.close(fd, const_cast<char*>(tfs_name), TFS_FILE_LEN); if (TFS_SUCCESS != re) { TBSYS_LOG(DEBUG, "close %s fail, return %d", tfs_name, re); } } } ToolUtil::print_info(ret, "stat %s", tfs_name); if (TFS_SUCCESS == ret) { FSName fsname(tfs_name, NULL); fprintf(stdout, " FILE_NAME: %s\n" " BLOCK_ID: %u\n" " FILE_ID: %" PRI64_PREFIX "u\n" " OFFSET: %d\n" " SIZE: %"PRI64_PREFIX"d\n" " OCCUPY SIZE: %"PRI64_PREFIX"d\n" " MODIFIED_TIME: %s\n" " CREATE_TIME: %s\n" " STATUS: %d\n" " CRC: %u\n", tfs_name, fsname.get_block_id(), file_stat.file_id_, file_stat.offset_, file_stat.size_, file_stat.usize_, Func::time_to_str(file_stat.modify_time_).c_str(), Func::time_to_str(file_stat.create_time_).c_str(), file_stat.flag_, file_stat.crc_); } // uint64_t id = file_stat.size_; // id <<= 32; // id |= file_info.crc_; // uint32_t tindex = static_cast<uint32_t>(id & 0xFFFFFFFF); // printf(" TABLE: select * from t%d where id='%" PRI64_PREFIX "u'\n", tindex % TABLE_COUNT, id); return ret; }
int cmd_get_file_meta(const VSTRING& param) { int ret = TFS_SUCCESS; const char* local_file = expand_path(const_cast<string&>(param[1])); const char* file_path = expand_path(const_cast<string&>(param[0])); char appkey[257]; int size = param.size(); if (size > 2) { strncpy(appkey, param[2].c_str(), 256); appkey[256] = '\0'; } else { strcpy(appkey, app_key); } if (size > 3) { app_id = strtoll(param[3].c_str(), NULL, 10); } if (size > 4) { uid = strtoll(param[4].c_str(), NULL, 10); } RcClientImpl impl; ret = impl.initialize(rc_addr, appkey, app_ip); if (TFS_SUCCESS != ret) { TBSYS_LOG(DEBUG, "meta client init failed, ret: %d", ret); } else { int fd = impl.open(app_id, uid, file_path, RcClient::READ); if (fd < 0) { TBSYS_LOG(WARN, "open file path failed. file_path: %s, ret: %d", file_path, fd); ret = TFS_ERROR; } else { FILE* fp=fopen(local_file,"w+b"); if(fp == NULL) { TBSYS_LOG(WARN, "open local file failed. local_file: %s, ret: %p", local_file, fp); ret = TFS_ERROR; } else { int64_t offset = 0; char buffer[MAX_READ_SIZE]; while (true) { int rlen = impl.pread(fd, buffer, MAX_READ_SIZE, offset); if (rlen <= 0) { break; } offset += rlen; int wlen = fwrite(buffer,sizeof(char),rlen,fp); if (wlen != rlen) { TBSYS_LOG(ERROR, "write to local file error, expect len: %d, return len: %d", rlen, wlen); ret= TFS_ERROR; break; } if (rlen < MAX_READ_SIZE) { break; } } fclose(fp); } impl.close(fd); } } ToolUtil::print_info(ret, "get %s => %s", file_path, local_file); return ret; }
int cmd_put_file_meta(const VSTRING& param) { int ret = TFS_SUCCESS; const char* local_file = expand_path(const_cast<string&>(param[0])); const char* file_path = expand_path(const_cast<string&>(param[1])); char appkey[257]; int size = param.size(); if (size > 2) { strncpy(appkey, param[2].c_str(), 256); appkey[256] = '\0'; } else { strcpy(appkey, app_key); } if (size > 3) { app_id = strtoll(param[3].c_str(), NULL, 10); } if (size > 4) { uid = strtoll(param[4].c_str(), NULL, 10); } RcClientImpl impl; ret = impl.initialize(rc_addr, appkey, app_ip); if (TFS_SUCCESS != ret) { TBSYS_LOG(DEBUG, "meta client init failed, ret: %d", ret); } else { FILE* fp=fopen(local_file,"r"); if(fp == NULL) { TBSYS_LOG(WARN, "open local file failed. local_file: %s, ret: %p", local_file, fp); ret = TFS_ERROR; } else { // create file if not exist FileMetaInfo file_meta_info; ret = impl.ls_file(app_id, uid, file_path, file_meta_info); if (ret != TFS_SUCCESS) { ret = impl.create_file(uid, file_path); } if (ret != TFS_SUCCESS) { TBSYS_LOG(DEBUG, "create file failed. file_path: %s, ret: %d", file_path, ret); } else { int fd = impl.open(app_id, uid, file_path, RcClient::WRITE); if (fd < 0) { TBSYS_LOG(WARN, "open file path failed. file_path: %s, ret: %d", file_path, fd); ret = TFS_ERROR; } else { int64_t offset = 0; char buffer[MAX_READ_SIZE]; while (true) { int64_t rlen = fread(buffer,sizeof(char),MAX_READ_SIZE,fp); if (rlen <= 0) { break; } int64_t wlen = impl.pwrite(fd, buffer, rlen, offset); if (wlen != rlen) { TBSYS_LOG(ERROR, "write meta data failed. expect len: %"PRI64_PREFIX"d, return len: %"PRI64_PREFIX"d", rlen, wlen); ret = TFS_ERROR; break; } offset = -1; if (rlen < MAX_READ_SIZE) { break; } } impl.close(fd); } } fclose(fp); } } ToolUtil::print_info(ret, "put %s => %s", local_file, file_path); return ret; }
int cmd_get_bucket(const VSTRING& param) { int size = param.size(); int ret = TFS_SUCCESS; const char *bucket_name = NULL; const char *prefix = NULL; const char *start_key = NULL; char delimiter = KvDefine::DEFAULT_CHAR; int32_t limit = KvDefine::MAX_LIMIT; bucket_name = param[0].c_str(); char appkey[257]; if (size > 1) { prefix = canonical_param(param[1]); } if (size > 2) { start_key = canonical_param(param[2]); } if (size > 3) { delimiter = canonical_param(param[3]) == NULL ? KvDefine::DEFAULT_CHAR : (param[3].size() == 1 ? param[3][0] : KvDefine::DEFAULT_CHAR); } if (size > 4) { limit = atoi(param[4].c_str()); } if (size > 5) { strncpy(appkey, param[5].c_str(), 256); appkey[256] = '\0'; } else { strcpy(appkey, app_key); } vector<ObjectMetaInfo> v_object_meta_info; VSTRING v_object_name; set<string> s_common_prefix; int8_t is_truncated = 0; UserInfo user_info; RcClientImpl impl; impl.set_kv_rs_addr(krs_addr); ret = impl.initialize(rc_addr, appkey, app_ip); if (TFS_SUCCESS != ret) { TBSYS_LOG(DEBUG, "rc client init failed, ret: %d", ret); } else { ret = impl.get_bucket(bucket_name, prefix, start_key, delimiter, limit, &v_object_meta_info, &v_object_name, &s_common_prefix, &is_truncated, user_info); } if (TFS_SUCCESS == ret) { printf("bucket: %s has %d common_prefix\n", bucket_name, static_cast<int>(s_common_prefix.size())); set<string>::iterator iter = s_common_prefix.begin(); for (int i = 0; iter != s_common_prefix.end(); iter++, i++) { cout << i << ": " << *iter << endl; } } if (TFS_SUCCESS == ret) { printf("bucket: %s has %d objects\n", bucket_name, static_cast<int>(v_object_name.size())); for (int i = 0; i < static_cast<int>(v_object_name.size()); i++) { cout << i << ": " << v_object_name[i] << endl; } } //todo show info of objects ToolUtil::print_info(ret, "get bucket %s", bucket_name); return ret; }