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_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_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; }