diff --git a/client/client.cpp b/client/client.cpp index 4f5e0f2..c52388e 100644 --- a/client/client.cpp +++ b/client/client.cpp @@ -363,9 +363,10 @@ bool CClient::request_update_list(const std::string& param) } // 读取list文件 - std::ifstream in(COfPath::to_full(list_file)); + std::string list_file_full = COfPath::to_full(list_file); + std::ifstream in(list_file_full); if (!in.is_open()) { - mperror("Can't Open File:{}", COfPath::to_full(list_file)); + mperror("Can't Open File:{}", list_file_full); return false; } std::istreambuf_iterator iterf(in); @@ -389,12 +390,14 @@ bool CClient::request_update_list(const std::string& param) mpinfo("---> check {}", hitem); auto v = COfStr::split(hitem, "|"); if (v.size() >= 2) { - if (!fs::exists(v[0])) { - mperror("file {} not exist.", v[0]); + auto pr = variable_handle(list_file_full, v[0], true); + if (!fs::exists(pr)) { + mperror("file {} not exist.", pr); valid = false; break; } - handled_content.append(hitem + "\n"); + mpinfo("---> check pass {}", pr); + handled_content.append(pr + "|" + v[1] + "\n"); continue; } valid = false; @@ -402,7 +405,7 @@ bool CClient::request_update_list(const std::string& param) } if (!valid) { - mperror("Judge List File {} Format Not Passed.", list_file); + mperror("Judge List File {} Format Not Passed.", list_file_full); return false; } @@ -410,7 +413,7 @@ bool CClient::request_update_list(const std::string& param) handled_content = CCodec::ansi_to_u8(handled_content); #endif - list_file_ = list_file; + list_file_ = list_file_full; std::shared_ptr buf = std::make_shared(); buf->type_ = TYPE_REQUEST_UPDATE_LIST; buf->data_ = new char[handled_content.size() + 1](); @@ -442,15 +445,17 @@ bool CClient::check_update_list(const std::string& content, std::map check pass {}", pr); #ifdef _WIN32 - files[CCodec::ansi_to_u8(vi[0])] = CCodec::ansi_to_u8(vi[1]); + files[CCodec::ansi_to_u8(vi[0])] = CCodec::ansi_to_u8(pr); #else - files[vi[0]] = vi[1]; + files[vi[0]] = pr; #endif } return valid; @@ -766,6 +771,24 @@ void CClient::judget_down_active() } } +std::string CClient::variable_handle(const std::string& task_list_path, const std::string& source, + bool is_send) +{ + std::string result(source); + // 支持的变量如下: + // ${HOME} 用户目录(发送端接收端均支持) + // ${CURRENT} 任务文件所在目录(该变量仅支持发送端,因为接收端没有任务文件所在路径) + if (source.find("${HOME}") != std::string::npos) { + result = COfStr::replace(result, "${HOME}", COfPath::get_home()); + } + if (is_send && source.find("${CURRENT}") != std::string::npos) { + fs::path p(task_list_path); + std::string list_dir = p.parent_path().string(); + result = COfStr::replace(result, "${CURRENT}", list_dir); + } + return result; +} + CFileOpr::CFileOpr() = default; CFileOpr::~CFileOpr() = default; diff --git a/client/client.h b/client/client.h index f51926d..b486862 100644 --- a/client/client.h +++ b/client/client.h @@ -50,7 +50,7 @@ public: bool down_one_file(const std::string& id, const std::string& file, const std::string& local_dir = ""); void report_trans_ret(TransState state, const std::string& key = ""); bool request_update_list(const std::string& param); - static bool check_update_list(const std::string& content, std::map& files); + bool check_update_list(const std::string& content, std::map& files); bool down_update_file(const std::map& files); private: @@ -61,6 +61,7 @@ private: void send_file_data_th(const char* keys); void hearts(); void judget_down_active(); + std::string variable_handle(const std::string& task_list_path, const std::string& source, bool is_send); private: std::mutex mutex_;