// // Created by taynpg on 24-8-6. // #include "FunctionImp.h" #include #include #include #include #include #include namespace fs = std::filesystem; CFunPack::CFunPack() = default; bool CFunPack::gen(const wxString& bin, const wxString& out_dir, const wxArrayString& dirs) { auto ret_source = get_depend_on(bin, dirs); auto result = parse_result(ret_source); return handle_copy(result, out_dir.ToStdString()); } wxArrayString CFunPack::get_depend_on(const wxString& bin, const wxArrayString& dirs) { filename_ = fs::path(bin).filename().wstring(); wxArrayString array; wxString cmd; if (!dirs.empty()) { cmd.append("export LD_LIBRARY_PATH=$LD_LIBRARY_PATH"); for (const auto& data : dirs) { cmd.append(":" + data); } } cmd.append(" && ldd " + bin); FILE* pf = nullptr; if ((pf = popen(cmd.c_str(), "r")) == nullptr) { return array; } char buffer[1024]{}; wxString result{}; while (std::fgets(buffer, sizeof(buffer), pf)) { result.append(buffer); } array = wxStringTokenize(result, wxT("\t"), wxTOKEN_DEFAULT); return array; } std::list CFunPack::parse_result(const wxArrayString& array) { std::list ret; wxArrayString simlog; wxArrayString bk_arry = array; for (auto& data : bk_arry) { if (data.empty()) { continue; } if (data.Contains("not found")) { simlog.push_back(wxT("未找到依赖:" + data)); continue; } data.Replace("=>", ""); wxArrayString tokens = wxStringTokenize(data, " "); wxString dy; if (tokens.size() == 4) { dy = tokens[2]; } if (tokens.size() == 3) { dy = tokens[1]; } if (dy.starts_with("/lib")) { continue; } if (!dy.empty()) { ret.push_back(dy); } } return ret; } bool CFunPack::handle_copy(const std::list& rets, const wxString& dest_dir) { std::vector need_copy; for (const auto& item : rets) { // fs::path item_path(item); // if (fs::is_symlink(item.ToStdString())) { // auto real = fs::read_symlink(item.ToStdWstring()); // auto read_full = fs::path(item).parent_path().append(real.wstring()); // need_copy.push_back(read_full); // wxMessageBox(wxString(read_full.wstring())); // } need_copy.push_back(item.ToStdWstring()); } dest_dir_ = fs::path(dest_dir).append(filename_.ToStdWstring()).wstring(); fs::create_directories(dest_dir_.ToStdWstring()); for (const auto& item : need_copy) { std::wstring new_path = fs::path(dest_dir_).append(item.filename().wstring()).wstring(); fs::copy_file(item, new_path, fs::copy_options::overwrite_existing); } return true; } CFunInstall::CFunInstall() = default; bool CFunInstall::install(const wxString& file, const wxString& category, const wxString& ico) { fs::path full_path(file.ToStdWstring()); wxString file_name = full_path.filename().wstring(); wxString script_name = file_name + ".sh"; wxString script_full_path = fs::path(file).parent_path().append(script_name.ToStdWstring()).wstring(); wxString script_content = "IyEvYmluL2Jhc2gKU0NSSVBUX0RJUj0kKGNkICIkKGRpcm5hbWUgIiQwIikiICYmIHB3ZCkKY2QgIiRTQ1JJUFRfRElSIiB8fCBleGl0CmV4cG9ydCBMRF9MSUJSQVJZX1BBVEg9JExE" "X0xJQlJBUllfUEFUSDoiJFNDUklQVF9ESVIiClJFUExBQ0VfU1RSSU5HX1NDUklQVD0iJFNDUklQVF9ESVIvcmVwbGFjZV9zdHJpbmciCiIkUkVQTEFDRV9TVFJJTkdfU0NSSVBUIgo" "K"; wxMemoryBuffer decodeData = wxBase64Decode(script_content); script_content = wxString::FromUTF8(static_cast(decodeData.GetData()), decodeData.GetBufSize()); wxString desktop_content = "W0Rlc2t0b3AgRW50cnldCk5hbWU9cmVfbmFtZQpDb21tZW50PXJlX2Rlc2NyaWJlCkV4ZWM9cmVfcGF0aApJY29uPXJlX2ljb24KVGVybWluYWw9ZmFsc2UKVHlwZT1BcHBsaWNhdGlv" "bgpDYXRlZ29yaWVzPXJlX2NhdGVnb3J5Owo="; wxMemoryBuffer decodeData2 = wxBase64Decode(script_content); desktop_content = wxString::FromUTF8(static_cast(decodeData2.GetData()), decodeData2.GetBufSize()); script_content.Replace("replace_string", script_name); desktop_content.Replace("Name", file_name); desktop_content.Replace("Comment", ""); desktop_content.Replace("Exec", ""); desktop_content.Replace("Icon", ""); desktop_content.Replace("Categories", ""); return true; }