1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399 |
- #include "CurlFtp.h"
- #include <regex>
- #include <iosfwd>
- #include <filesystem>
- #include <fstream>
- #include "spdlog/spdlog.h"
- #ifndef SPDLOG_ACTIVE_LEVEL
- #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
- #endif
- #if defined(_WIN32)
- #endif
- static bool hasInstace = false;
- static thread_local std::chrono::system_clock::time_point lastTime;
- static thread_local uint64_t dCount;
- static thread_local uint64_t uCount;
- static thread_local uint64_t dSpeed;
- static thread_local uint64_t uSpeed;
- static size_t writeStringCallback(void *contents, size_t size, size_t nmemb, std::string *userStr)
- {
-
-
-
-
-
-
-
-
-
-
-
-
- userStr->append((char*)contents, size * nmemb);
- return size * nmemb;
- }
- static int writeStringListCallback(void* buffer, size_t size, size_t nmemb, void* userp)
- {
- std::vector<std::string>* fileList = static_cast<std::vector<std::string>*>(userp);
- std::string line(static_cast<char*>(buffer), size * nmemb);
-
- fileList->push_back(line);
- return size * nmemb;
- }
- static size_t writeFileCallBack(void* contents, size_t size, size_t nmemb, std::ostream* pFile)
- {
- pFile->write(reinterpret_cast<char*>(contents), size * nmemb);
- return size * nmemb;
- }
- static size_t writeDataCallBack(void* contents, size_t size, size_t nmemb, std::vector<char>* vecData)
- {
- size_t copySize = size * nmemb;
- vecData->insert(vecData->end(), (char*)contents, (char*)contents + copySize);
- return copySize;
- }
- static size_t readFileCallBack(void* contents, size_t size, size_t nmemb, std::istream* pFile)
- {
- pFile->read(reinterpret_cast<char*>(contents), size * nmemb);
-
- size_t readSize = pFile->gcount();
- return readSize;
-
- }
- static size_t readDataCallBack(void* contents, size_t size, size_t nmemb, CF_ArrayInfo* pData)
- {
- if(pData == nullptr)
- {
- return 0;
- }
-
- size_t copySize = size * nmemb;
- if(pData->size - pData->pos < copySize)
- {
- copySize = pData->size - pData->pos;
- }
- memcpy(contents, pData->data + pData->pos, copySize);
- pData->pos += copySize;
- return copySize;
- }
- void computeSpeed(uint64_t speed, double& retSpeed, std::string& unit)
- {
- double KB = speed / 1024.0;
- double MB = KB / 1024.0;
- double GB = MB / 1024.0;
- if(GB > 1)
- {
- unit = "GB/S";
- retSpeed = GB;
- }
- else if(MB > 1)
- {
- unit = "MB/S";
- retSpeed = MB;
- }
- else if(KB > 1)
- {
- unit = "KB/S";
- retSpeed = KB;
- }
- else {
- unit = "B/S";
- retSpeed = speed;
- }
- }
- void printProgress(CF_TransType type, curl_off_t total, curl_off_t now)
- {
- std::string transType;
- uint64_t count = 0;
- if(type == CF_TransType::DOWNLOAD)
- {
- transType = "Download";
- count = now - dCount;
- dCount = now;
- }
- else if (type == CF_TransType::UPLOAD)
- {
- transType = "Upload";
- count = now - uCount;
- uCount = now;
- }
-
- double percent = (double)now / (double)total * 100;
-
- double tKB = total / 1024.0;
- double tMB = tKB / 1024.0;
- double tGB = tMB / 1024.0;
-
- double speed = 0.0;
- std::string unit;
- computeSpeed(count, speed, unit);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if(tGB > 1)
- {
- double dGB = now / 1024.0 / 1024.0 / 1024.0;
- double speed = count / 1024.0 / 1024.0;
- printf("%s Total / now : %.2fGB / %.2fGB, %.2f%%, Speed:%.2f %s\r", transType.c_str(), tGB, dGB, percent, speed, unit.c_str());
-
- }
- else if(tMB > 1)
- {
- double dMB = now / 1024.0 / 1024.0;
- printf("%s Total / now : %.2fMB / %.2fMB, %.2f%%, Speed:%.2f %s\r", transType.c_str(), tMB, dMB, percent, speed, unit.c_str());
-
- }
- else if(tKB > 1)
- {
- double dKB = now / 1024.0;
- printf("%s Total / now : %.2fKB / %.2fKB, %.2f%%, Speed:%.2f %s\r", transType.c_str(), tKB, dKB, percent, speed, unit.c_str());
- }
- else
- {
- printf("%s Total / now : %ldB / %ldB, %.2f%%, Speed:%.2f %s\r", transType.c_str(), total, now, percent, speed, unit.c_str());
- }
- fflush(stdout);
- }
- static int progress_callback(void *clientp,
- curl_off_t dltotal,
- curl_off_t dlnow,
- curl_off_t ultotal,
- curl_off_t ulnow)
- {
-
- if(dltotal == 0 && ultotal == 0)
- {
- dCount = 0;
- uCount = 0;
- dSpeed = 0;
- uSpeed = 0;
- return 0;
- }
- std::chrono::system_clock::time_point nowTime = std::chrono::system_clock::now();
- auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(nowTime - lastTime).count();
-
- if((duration < 1000) && ((dltotal != dlnow) || (ultotal != ulnow)))
- {
- return 0;
- }
- lastTime = nowTime;
-
- if(dltotal > 0)
- {
- printProgress(CF_TransType::DOWNLOAD, dltotal, dlnow);
-
- }
-
- else if(ultotal > 0)
- {
- printProgress(CF_TransType::UPLOAD, ultotal, ulnow);
-
-
- }
- return 0;
- }
- #if defined(_WIN32)
- static char* GBToUTF8(const char* gb2312)
- {
- int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
- wchar_t* wstr = new wchar_t[len+1];
- memset(wstr, 0, len+1);
- MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
- len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
- char* str = new char[len+1];
- memset(str, 0, len+1);
- WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
- if(wstr) delete[] wstr;
- return str;
- }
- #endif
- const std::regex parseRegSpace(R"(( )+)");
- const std::regex parseReg1(R"(^([^ ]+) (\d*) (\w*) (\w*) (\d*) (.*) ([^ ]+)$)");
- const std::regex parseReg2(R"(\n)");
- const std::regex parseReg3(R"(^d.*)");
- const std::regex parseReg4(R"(^-.*$)");
- static bool parseFileInfo(const std::string& strSrc, std::vector<CF_FileInfo>& fileInfo)
- {
- #if defined(_WIN32)
-
- std::string str2 = strSrc;
- #else
- std::string str2 = strSrc;
- #endif
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- std::sregex_token_iterator it2(str2.begin(), str2.end(), parseReg2, -1);
-
- for(; it2 != std::sregex_token_iterator(); ++it2)
- {
-
- auto line = std::regex_replace(it2->str(), parseRegSpace, " ");
-
- CF_FileInfo fi;
-
- std::string strFileType = std::regex_replace(line, parseReg1, "$1");
- if(std::regex_match(strFileType, parseReg3))
- {
- fi.type = CF_FileType::DIR;
- }else if (std::regex_match(strFileType, parseReg4))
- {
- fi.type = CF_FileType::FILE;
- }
-
- std::string strFileSize = std::regex_replace(line, parseReg1, "$5");
- fi.size = std::stoull(strFileSize);
-
- std::string strFileName = std::regex_replace(line, parseReg1, "$7");
- fi.name = strFileName;
-
- fileInfo.push_back(fi);
- }
- return true;
- }
- CurlFtp::CurlFtp()
- {
-
- curl_global_init(CURL_GLOBAL_DEFAULT);
-
- hasInstace = true;
- }
- CurlFtp::~CurlFtp()
- {
-
- curl_easy_cleanup(m_curl);
- curl_global_cleanup();
- hasInstace = false;
- }
- bool CurlFtp::setUsernameAndPassword(const std::string& username, const std::string& password)
- {
- m_username = username;
- m_password = password;
- return true;
- }
- bool CurlFtp::setFtpIPAndPort(const std::string& IP, const int port)
- {
-
- m_IP = IP;
- m_port = port;
- m_ftpUrl = "ftp://" + m_IP + ":" + std::to_string(m_port);
- SPDLOG_INFO("Set ftpUrl = {}", m_ftpUrl);
- m_isSftp = false;
- return true;
- }
- bool CurlFtp::setSftpIPAndPort(const std::string& IP, const int port)
- {
- m_IP = IP;
- m_port = port;
- m_ftpUrl = "sftp://" + m_IP + ":" + std::to_string(m_port);
- SPDLOG_INFO("Set sftpUrl = {}", m_ftpUrl);
- m_isSftp = true;
- return true;
- }
- void CurlFtp::setIgnoreSSLCert(bool isIgnore)
- {
- m_isIgnoreSSLCert = isIgnore;
- }
- void CurlFtp::setCaCertFile(const std::string& caCertFile)
- {
- m_caCertFile = caCertFile;
- }
- void CurlFtp::enableCurlDebug(bool isPrint)
- {
- m_enableCurlDebug = isPrint;
- }
- bool CurlFtp::getFileList(std::string dir, std::vector<std::string>& fileList)
- {
- if(m_IP.empty())
- {
- SPDLOG_WARN("IP or port is empty");
- return false;
- }
-
- auto dirTmp = checkDirPath(dir);
-
-
-
-
-
-
-
- resetCurl(m_curl);
- std::vector<CF_FileInfo> listInfo;
-
- listAll(m_curl, dirTmp, listInfo);
- for(const CF_FileInfo& fi : listInfo)
- {
-
- if(fi.type == CF_FileType::FILE)
- {
- fileList.push_back(fi.name);
- }
- }
-
-
- return true;
- }
- bool CurlFtp::getDirList(std::string dir, std::vector<std::string>& dirList)
- {
- if(m_IP.empty())
- {
- SPDLOG_WARN("IP or port is empty");
- return false;
- }
-
- auto dirTmp = checkDirPath(dir);
-
-
-
-
-
-
-
- resetCurl(m_curl);
- std::vector<CF_FileInfo> listInfo;
-
- listAll(m_curl, dirTmp, listInfo);
- for(const CF_FileInfo& fi : listInfo)
- {
-
- if(fi.type == CF_FileType::DIR)
- {
- dirList.push_back(fi.name);
- }
- }
-
- return true;
- }
- bool CurlFtp::isDirExist(const std::string& dir)
- {
- SPDLOG_DEBUG("Check remote dir: {}", dir);
- if(m_ftpUrl.empty())
- {
- SPDLOG_ERROR("ftpUrl is empty");
- return false;
- }
- if(dir == "/")
- {
- return true;
- }
- bool result = false;
- if(m_isSftp)
- {
- result = checkSftpDirExist(dir);
- }else {
- result = checkFtpDirExist(dir);
- }
-
- return result;
- }
- bool CurlFtp::createDirectory(const std::string& ftpDir)
- {
- if(m_ftpUrl.empty())
- {
- SPDLOG_ERROR("ftpUrl is empty");
- return false;
- }
-
- if(isDirExist(ftpDir))
- {
- return true;
- }
-
- auto dirTmp = checkDirPath(ftpDir);
-
-
-
-
-
-
- resetCurl(m_curl);
- curl_easy_setopt(m_curl, CURLOPT_URL, m_ftpUrl.c_str());
- curl_easy_setopt(m_curl, CURLOPT_USERNAME, m_username.c_str());
- curl_easy_setopt(m_curl, CURLOPT_PASSWORD, m_password.c_str());
-
- struct curl_slist *headerlist = NULL;
- std::string mkdir;
- if(m_isSftp)
- {
- mkdir = "MKDIR " + dirTmp;
- }else {
- mkdir = "MKD " + dirTmp;
- }
- headerlist = curl_slist_append(headerlist, mkdir.c_str());
-
- curl_easy_setopt(m_curl, CURLOPT_QUOTE, headerlist);
-
- curl_easy_setopt(m_curl, CURLOPT_NOBODY, 1L);
-
- curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, 1L);
-
- curl_easy_setopt(m_curl, CURLOPT_TIMEOUT, 30L);
- curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, 30L);
-
- setSftp(m_curl);
- if(m_enableCurlDebug)
- {
-
- curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
- }
-
-
- SPDLOG_DEBUG("Create remote dir: {}", dirTmp);
-
- bool ret = performCurl(m_curl);
- if(!ret)
- {
- SPDLOG_ERROR("Failed to create remote Dir");
- }
-
- curl_slist_free_all(headerlist);
- return ret;
- }
- bool CurlFtp::createDirectories(const std::string& ftpDir)
- {
-
- std::string ftpDirTmp = checkDirPath(ftpDir);
- std::string ftpDir2 = ftpDirTmp.substr(1, ftpDirTmp.size() -1);
-
- std::vector<std::string> strList;
- std::regex reg(R"(/)");
- std::sregex_token_iterator it(ftpDir2.begin(), ftpDir2.end(), reg, -1);
- for( ; it != std::sregex_token_iterator(); ++it)
- {
- strList.push_back(it->str());
- }
-
- std::vector<std::string> dirList;
- for(const std::string& dir : strList)
- {
- std::string dirTmp = "/";
- if(!dirList.empty())
- {
- dirTmp = dirList.back();
- dirTmp += "/";
- }
- dirTmp += dir;
- dirList.push_back(dirTmp);
- }
-
- for(const std::string& dir : dirList)
- {
-
- if(!createDirectory(dir))
- {
- SPDLOG_ERROR("Failed to create dir: {}", dir);
- return false;
- }
- }
-
- return true;
- }
- bool CurlFtp::downloadFile(const std::string& remoteFile, const std::string& localFile, size_t timeout)
- {
- if(m_ftpUrl.empty())
- {
- SPDLOG_ERROR("ftpUrl is empty");
- return false;
- }
-
-
- std::string remoteFileTmp = checkFilePath(remoteFile);
- std::string ftpUrl = m_ftpUrl + remoteFileTmp;
-
-
- std::string localDirTmp = localFile.substr(0, localFile.find_last_of("/"));
- if(!checkLocalDir(localDirTmp))
- {
- SPDLOG_ERROR("Failed to create local dir: {}", localDirTmp);
- return false;
- }
-
-
-
-
-
-
-
- resetCurl(m_curl);
-
- std::ofstream ofs;
- ofs.open(localFile, std::ios::out | std::ios::binary | std::ios::trunc);
-
- curl_easy_setopt(m_curl, CURLOPT_URL, ftpUrl.c_str());
- curl_easy_setopt(m_curl, CURLOPT_PORT, m_port);
-
- curl_easy_setopt(m_curl, CURLOPT_USERNAME, m_username.c_str());
- curl_easy_setopt(m_curl, CURLOPT_PASSWORD, m_password.c_str());
- curl_easy_setopt(m_curl, CURLOPT_USERPWD, (m_username + ":" + m_password).c_str());
-
- curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, 1L);
-
- curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, writeFileCallBack);
- curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &ofs);
-
-
- curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, timeout);
-
- curl_easy_setopt(m_curl, CURLOPT_XFERINFOFUNCTION, progress_callback);
- curl_easy_setopt(m_curl, CURLOPT_XFERINFODATA, nullptr);
-
- curl_easy_setopt(m_curl, CURLOPT_NOPROGRESS, 0L);
-
- setSftp(m_curl);
- if(m_enableCurlDebug)
- {
-
- curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
- }
-
- curl_easy_setopt(m_curl, CURLOPT_TCP_KEEPALIVE, 1L);
-
- bool ret = performCurl(m_curl);
- if(!ret)
- {
- SPDLOG_ERROR("Failed to get file list, Url = {}", ftpUrl);
-
- ofs.close();
- std::remove(localFile.c_str());
- return false;
- }
-
- ofs.close();
-
-
-
- printf("\n");
- return true;
- }
- bool CurlFtp::downloadToArray(const std::string& remoteFile, std::vector<char>& arrayInfo, size_t timeout)
- {
- if(m_ftpUrl.empty())
- {
- SPDLOG_ERROR("ftpUrl is empty");
- return false;
- }
-
-
- std::string remoteFileTmp = checkFilePath(remoteFile);
- std::string ftpUrl = m_ftpUrl + remoteFileTmp;
-
-
-
-
-
-
-
- resetCurl(m_curl);
-
- curl_easy_setopt(m_curl, CURLOPT_URL, ftpUrl.c_str());
- curl_easy_setopt(m_curl, CURLOPT_PORT, m_port);
-
- curl_easy_setopt(m_curl, CURLOPT_USERNAME, m_username.c_str());
- curl_easy_setopt(m_curl, CURLOPT_PASSWORD, m_password.c_str());
- curl_easy_setopt(m_curl, CURLOPT_USERPWD, (m_username + ":" + m_password).c_str());
-
- curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, 1L);
-
- curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, writeDataCallBack);
- curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &arrayInfo);
-
-
- curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, timeout);
-
- curl_easy_setopt(m_curl, CURLOPT_XFERINFOFUNCTION, progress_callback);
- curl_easy_setopt(m_curl, CURLOPT_XFERINFODATA, nullptr);
-
- curl_easy_setopt(m_curl, CURLOPT_NOPROGRESS, 0L);
-
- setSftp(m_curl);
- if(m_enableCurlDebug)
- {
-
- curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
- }
-
- curl_easy_setopt(m_curl, CURLOPT_TCP_KEEPALIVE, 1L);
-
- bool ret = performCurl(m_curl);
- if(!ret)
- {
- SPDLOG_ERROR("Failed to get file list, Url = {}", ftpUrl);
- }
-
-
-
-
- printf("\n");
- return ret;
- }
- bool CurlFtp::uploadFile(const std::string& localFile, const std::string& remoteFile, size_t timeout, bool isCreateDir)
- {
- if(m_ftpUrl.empty())
- {
- SPDLOG_ERROR("Url is empty");
- return false;
- }
-
- if(!std::filesystem::exists(localFile))
- {
- SPDLOG_ERROR("Local file is not exist: {}", localFile);
- return false;
- }
-
- std::string remoteFileTmp = checkFilePath(remoteFile);
- std::string remoteDirTmp = remoteFileTmp.substr(0, remoteFileTmp.find_last_of("/"));
-
- if(!isDirExist(remoteDirTmp))
- {
- if(isCreateDir)
- {
- if(!createDirectories(remoteDirTmp))
- {
-
- return false;
- }
- }else {
- SPDLOG_ERROR("Remote dir is not exist: {}", remoteDirTmp);
- return false;
- }
- }
-
-
- std::string ftpUrl = m_ftpUrl + remoteFileTmp;
-
-
- std::ifstream ifs;
- ifs.open(localFile, std::ios::in | std::ios::binary);
- if(!ifs.is_open())
- {
- SPDLOG_ERROR("Failed to open local file: {}", localFile);
- return false;
- }
-
-
- ifs.seekg(0, std::ios::end);
- auto fileSize = ifs.tellg();
-
- ifs.seekg(0, std::ios::beg);
- SPDLOG_DEBUG("File size: {}", (long)fileSize);
-
-
-
-
-
-
-
-
- if(!resetCurl(m_curl))
- {
- ifs.close();
- return false;
- }
-
- curl_easy_setopt(m_curl, CURLOPT_URL, ftpUrl.c_str());
- curl_easy_setopt(m_curl, CURLOPT_PORT, m_port);
-
- curl_easy_setopt(m_curl, CURLOPT_USERNAME, m_username.c_str());
- curl_easy_setopt(m_curl, CURLOPT_PASSWORD, m_password.c_str());
-
- curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, 1L);
-
- curl_easy_setopt(m_curl, CURLOPT_UPLOAD, 1L);
-
- curl_easy_setopt(m_curl, CURLOPT_READFUNCTION, readFileCallBack);
- curl_easy_setopt(m_curl, CURLOPT_READDATA, &ifs);
-
- curl_easy_setopt(m_curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)fileSize);
-
-
- curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, timeout);
-
- curl_easy_setopt(m_curl, CURLOPT_XFERINFOFUNCTION, progress_callback);
- curl_easy_setopt(m_curl, CURLOPT_XFERINFODATA, nullptr);
-
- curl_easy_setopt(m_curl, CURLOPT_NOPROGRESS, 0L);
-
- setSftp(m_curl);
-
- if(m_enableCurlDebug)
- {
- curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
- }
-
- curl_easy_setopt(m_curl, CURLOPT_TCP_KEEPALIVE, 1L);
-
- bool ret = performCurl(m_curl);
- if(!ret)
- {
- SPDLOG_ERROR("Upload file failed, Url = {}", ftpUrl);
- }
-
- ifs.close();
-
-
- printf("\n");
- return ret;
- }
- bool CurlFtp::uploadData(char* srcData, size_t size, const std::string& remoteFile, size_t timeout, bool isCreateDir)
- {
- if(m_ftpUrl.empty())
- {
- SPDLOG_ERROR("Url is empty");
- return false;
- }
-
- CF_ArrayInfo arrayInfo;
- arrayInfo.data = srcData;
- arrayInfo.size = size;
- arrayInfo.pos = 0;
-
- std::string remoteFileTmp = checkFilePath(remoteFile);
- std::string remoteDirTmp = remoteFileTmp.substr(0, remoteFileTmp.find_last_of("/"));
-
- if(!isDirExist(remoteDirTmp))
- {
- if(isCreateDir)
- {
- if(!createDirectories(remoteDirTmp))
- {
-
- return false;
- }
- }else {
- SPDLOG_ERROR("Remote dir is not exist: {}", remoteDirTmp);
- return false;
- }
- }
-
- std::string ftpUrl = m_ftpUrl + remoteFileTmp;
- SPDLOG_DEBUG("Data size: {}", arrayInfo.size);
-
-
-
-
-
-
-
- if(!resetCurl(m_curl))
- {
- return false;
- }
-
- curl_easy_setopt(m_curl, CURLOPT_URL, ftpUrl.c_str());
- curl_easy_setopt(m_curl, CURLOPT_PORT, m_port);
-
-
-
- curl_easy_setopt(m_curl, CURLOPT_USERPWD, (m_username + ":" + m_password).c_str());
-
- curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, 1L);
-
- curl_easy_setopt(m_curl, CURLOPT_UPLOAD, 1L);
-
- curl_easy_setopt(m_curl, CURLOPT_READFUNCTION, readDataCallBack);
- curl_easy_setopt(m_curl, CURLOPT_READDATA, &arrayInfo);
-
- curl_easy_setopt(m_curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)arrayInfo.size);
-
-
- curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, timeout);
-
- curl_easy_setopt(m_curl, CURLOPT_XFERINFOFUNCTION, progress_callback);
- curl_easy_setopt(m_curl, CURLOPT_XFERINFODATA, nullptr);
-
- curl_easy_setopt(m_curl, CURLOPT_NOPROGRESS, 0L);
-
- curl_easy_setopt(m_curl, CURLOPT_FTP_USE_EPSV, 1L);
- if(m_enableCurlDebug)
- {
-
- curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
- }
-
- curl_easy_setopt(m_curl, CURLOPT_TCP_KEEPALIVE, 1L);
-
- bool ret = performCurl(m_curl);
- if(!ret)
- {
- SPDLOG_ERROR("Upload file failed, Url = {}", ftpUrl);
- }
-
-
-
- printf("\n");
- return ret;
- }
- bool CurlFtp::listAll(CURL* curl, std::string dir, std::vector<CF_FileInfo>& fileInfoList)
- {
- if(m_IP.empty())
- {
- SPDLOG_ERROR("IP is empty");
- return false;
- }
- bool result = false;
- std::string strSrc;
-
- std::string ftpUrl = m_ftpUrl + dir;
- curl_easy_setopt(curl, CURLOPT_URL, ftpUrl.c_str());
- curl_easy_setopt(curl, CURLOPT_PORT, m_port);
-
- curl_easy_setopt(curl, CURLOPT_USERNAME, m_username.c_str());
- curl_easy_setopt(curl, CURLOPT_PASSWORD, m_password.c_str());
-
-
-
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeStringCallback);
-
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strSrc);
-
- curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L);
- curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L);
-
-
- if(m_enableCurlDebug)
- {
-
- curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
- }
-
-
- bool ret = performCurl(curl);
- if(!ret)
- {
- SPDLOG_ERROR("Failed to get file listUrl = {}", ftpUrl);
- return false;
- }
-
- parseFileInfo(strSrc, fileInfoList);
- return true;
- }
- std::string CurlFtp::checkDirPath(const std::string& dir)
- {
- std::string dirTmp = dir;
-
- std::regex reg(R"(^/[.]*)");
- if(!std::regex_match(dirTmp, reg))
- {
- dirTmp = "/" + dirTmp;
- }
-
- std::regex reg1(R"(//)");
- dirTmp = std::regex_replace(dirTmp, reg1, "/");
-
- std::regex reg2(R"([.]*/$)");
- if(!std::regex_match(dirTmp, reg2))
- {
- dirTmp = dirTmp + "/";
- }
- return dirTmp;
- }
- std::string CurlFtp::checkFilePath(const std::string& file)
- {
- std::string dirTmp = file;
-
- std::regex reg2(R"([.]*/$)");
- if(std::regex_match(dirTmp, reg2))
- {
- SPDLOG_ERROR("File path is not correct, end with '/'");
- return std::string();
- }
-
-
- std::regex reg(R"(^/[.]*)");
- if(!std::regex_match(dirTmp, reg))
- {
- dirTmp = "/" + dirTmp;
- }
-
- std::regex reg1(R"(//)");
- dirTmp = std::regex_replace(dirTmp, reg1, "/");
-
- return dirTmp;
- }
- bool CurlFtp::checkLocalDir(const std::string& localDir)
- {
-
- std::regex reg(R"([.]*/$)");
- std::string localDirTmp = std::regex_replace(localDir, reg, "");
-
- if(std::filesystem::exists(localDirTmp))
- {
- return true;
- }
-
- if(!std::filesystem::create_directories(localDirTmp))
- {
- SPDLOG_ERROR("Failed to create local dir: {}", localDirTmp);
- return false;
- }
- return true;
- }
- bool CurlFtp::performCurl(CURL* curl)
- {
- int retry = 3;
- while(retry > 0)
- {
- CURLcode res = curl_easy_perform(curl);
- if(res == CURLE_OK)
- {
- return true;
- }
- else if (res == CURLE_LOGIN_DENIED)
- {
- SPDLOG_ERROR("Login failed, error code: {} ,{}", (int)res, curl_easy_strerror(res));
-
- curl_easy_setopt(curl, CURLOPT_USERNAME, m_username.c_str());
- curl_easy_setopt(curl, CURLOPT_PASSWORD, m_password.c_str());
- }
- else
- {
- SPDLOG_ERROR("Perform curl failed, error code: {} ,{}", (int)res, curl_easy_strerror(res));
- SPDLOG_ERROR("Retry times: {}", 4 - retry);
- }
- retry--;
- }
- return false;
- }
- bool CurlFtp::resetCurl(CURL* curl)
- {
- if(m_curl == nullptr)
- {
- m_curl = curl_easy_init();
- if(m_curl == nullptr)
- {
- SPDLOG_ERROR("curl init failed !");
- return false;
- }
- }else {
- curl_easy_reset(m_curl);
- }
- return true;
- }
- bool CurlFtp::setSftp(CURL* curl)
- {
-
- if(m_isSftp)
- {
-
- if(m_isIgnoreSSLCert)
- {
-
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
-
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
- } else
- {
-
- curl_easy_setopt(curl, CURLOPT_CAINFO, m_caCertFile.c_str());
- }
- }
- return true;
- }
- bool CurlFtp::checkFtpDirExist(const std::string& dir)
- {
-
- auto dirTmp = checkDirPath(dir);
- resetCurl(m_curl);
- std::string ftpUrl = m_ftpUrl + dirTmp;
- curl_easy_setopt(m_curl, CURLOPT_URL, ftpUrl.c_str());
- curl_easy_setopt(m_curl, CURLOPT_USERNAME, m_username.c_str());
- curl_easy_setopt(m_curl, CURLOPT_PASSWORD, m_password.c_str());
-
- curl_easy_setopt(m_curl, CURLOPT_NOBODY, 1L);
-
-
- setSftp(m_curl);
-
- if(m_enableCurlDebug)
- {
- curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
- }
-
-
-
- CURLcode res = curl_easy_perform(m_curl);
- bool result = true;
- if(res == CURLE_REMOTE_FILE_NOT_FOUND)
- {
- result = false;
- }
- else if(res == CURLE_OK)
- {
- result = true;
- } else
- {
- SPDLOG_ERROR("Check remote dir error, error code: {} ,{}", (int)res, curl_easy_strerror(res));
- result = false;
- }
-
- return result;
- }
- bool CurlFtp::checkSftpDirExist(const std::string& dir)
- {
-
- std::string parentDir = std::filesystem::path(dir).parent_path().string();
-
- std::vector<std::string> vecDir;
- bool ret = getDirList(parentDir, vecDir);
- if(!ret)
- {
- SPDLOG_ERROR("Failed to check sftp dir: {}", dir);
- return false;
- }
-
- std::string dirName = std::filesystem::path(dir).filename().string();
-
-
- bool result = false;
- for(const std::string& str : vecDir)
- {
- if(str == dirName)
- {
- result = true;
- break;
- }
- }
- return result;
- }
|