CurlFtp.cpp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  1. #include "CurlFtp.h"
  2. #include <regex>
  3. #include <iosfwd>
  4. #include <filesystem>
  5. #include <fstream>
  6. #include "stdlog.h"
  7. #if defined(_WIN32)
  8. #endif /* _WIN32 */
  9. /* ==================================================================================
  10. * *********************************** 全局变量 *************************************
  11. * ==================================================================================
  12. */
  13. /* 是否有初始化实例,主要用于静态函数调用curl_global_cleanup()函数前判断,有实例就不调用 */
  14. static bool hasInstace = false;
  15. /* 记录上传或者下载的时间,thread_local是C++11加入的关键字,表示在每个线程中都有自己的lastTime */
  16. static thread_local std::chrono::system_clock::time_point lastTime;
  17. static thread_local uint64_t dCount;
  18. static thread_local uint64_t uCount;
  19. static thread_local uint64_t dSpeed; /* 保存最后的下载速度 */
  20. static thread_local uint64_t uSpeed; /* 保存最后的上传速度 */
  21. /* ==================================================================================
  22. * *********************************** 全局函数 *************************************
  23. * ==================================================================================
  24. */
  25. /**
  26. * @brief 写入回调函数
  27. *
  28. * @param contents curl的数据缓冲区
  29. * @param size 数据大小,单位是size_t
  30. * @param nmemb size_t的单位字节数
  31. * @param userStr 用户提供的字符串,格式可以是任意的
  32. * @return size_t 总共获取到的数据大小,单位是字节
  33. */
  34. static size_t writeStringCallback(void *contents, size_t size, size_t nmemb, std::string *userStr)
  35. {
  36. // size_t newLength = size * nmemb;
  37. // size_t oldLength = userStr->size();
  38. // try
  39. // {
  40. // userStr->resize(oldLength + newLength);
  41. // }
  42. // catch(std::bad_alloc &e)
  43. // {
  44. // //handle memory problem
  45. // return 0;
  46. // }
  47. // std::copy_n((char*)contents, newLength, userStr->begin() + oldLength);
  48. userStr->append((char*)contents, size * nmemb);
  49. return size * nmemb;
  50. }
  51. /**
  52. * @brief 写入回调函数,listFiles需要调用
  53. *
  54. * @param buffer curl下载回来的数据
  55. * @param size 数据大小
  56. * @param nmemb 数据单位字节数
  57. * @param userp 用户传进来的容器
  58. * @return int 返回拷贝的字节数
  59. */
  60. static int writeStringListCallback(void* buffer, size_t size, size_t nmemb, void* userp)
  61. {
  62. std::vector<std::string>* fileList = static_cast<std::vector<std::string>*>(userp);
  63. std::string line(static_cast<char*>(buffer), size * nmemb);
  64. // printf("line = %s\n", line.c_str());
  65. fileList->push_back(line);
  66. return size * nmemb;
  67. }
  68. /**
  69. * @brief 写入文件回调函数
  70. *
  71. * @param contents 读取到的数据内容
  72. * @param size 数据大小
  73. * @param nmemb 数据单位
  74. * @param pFile 文件指针
  75. * @return size_t 实际读取的大小
  76. */
  77. static size_t writeFileCallBack(void* contents, size_t size, size_t nmemb, std::ostream* pFile)
  78. {
  79. pFile->write(reinterpret_cast<char*>(contents), size * nmemb);
  80. return size * nmemb;
  81. }
  82. /**
  83. * @brief 写入数据到vector中
  84. *
  85. * @param contents
  86. * @param size
  87. * @param nmemb
  88. * @param vecData
  89. * @return size_t
  90. */
  91. static size_t writeDataCallBack(void* contents, size_t size, size_t nmemb, std::vector<char>* vecData)
  92. {
  93. size_t copySize = size * nmemb;
  94. vecData->insert(vecData->end(), (char*)contents, (char*)contents + copySize);
  95. return copySize;
  96. }
  97. /**
  98. * @brief 读取文件回调函数
  99. *
  100. * @param contents 下载到的数据内容
  101. * @param size 数据大小
  102. * @param nmemb 数据单位
  103. * @param pFile 文件指针
  104. * @return size_t 写入的大小
  105. */
  106. static size_t readFileCallBack(void* contents, size_t size, size_t nmemb, std::istream* pFile)
  107. {
  108. pFile->read(reinterpret_cast<char*>(contents), size * nmemb);
  109. /* 获取读取到的字节数,可能读取到文件末尾,所以不能直接使用传入的字节数 */
  110. size_t readSize = pFile->gcount();
  111. return readSize;
  112. }
  113. /**
  114. * @brief
  115. *
  116. * @param contents ftp需要的目标内容
  117. * @param size 拷贝的数据大小
  118. * @param nmemb 单个数据的字节数
  119. * @param pData 源指针
  120. * @return size_t 已拷贝的数据大小
  121. */
  122. static size_t readDataCallBack(void* contents, size_t size, size_t nmemb, CF_ArrayInfo* pData)
  123. {
  124. if(pData == nullptr)
  125. {
  126. return 0;
  127. }
  128. /* 判断是否还够本次拷贝的字节数 */
  129. size_t copySize = size * nmemb;
  130. if(pData->size - pData->pos < copySize)
  131. {
  132. copySize = pData->size - pData->pos;
  133. }
  134. memcpy(contents, pData->data + pData->pos, copySize);
  135. pData->pos += copySize;
  136. return copySize;
  137. }
  138. /**
  139. * @brief 计算速度
  140. *
  141. * @param speed
  142. * @param retSpeed
  143. * @param unit
  144. */
  145. void computeSpeed(uint64_t speed, double& retSpeed, std::string& unit)
  146. {
  147. double KB = speed / 1024.0;
  148. double MB = KB / 1024.0;
  149. double GB = MB / 1024.0;
  150. if(GB > 1)
  151. {
  152. unit = "GB/S";
  153. retSpeed = GB;
  154. }
  155. else if(MB > 1)
  156. {
  157. unit = "MB/S";
  158. retSpeed = MB;
  159. }
  160. else if(KB > 1)
  161. {
  162. unit = "KB/S";
  163. retSpeed = KB;
  164. }
  165. else {
  166. unit = "B/S";
  167. retSpeed = speed;
  168. }
  169. }
  170. /**
  171. * @brief 打印进度条
  172. *
  173. * @param type 上传还是下载类型
  174. * @param total 总大小,单位字节
  175. * @param now 现在的进度,单位字节
  176. */
  177. void printProgress(CF_TransType type, curl_off_t total, curl_off_t now)
  178. {
  179. std::string transType;
  180. uint64_t count = 0;
  181. if(type == CF_TransType::DOWNLOAD)
  182. {
  183. transType = "Download";
  184. count = now - dCount;
  185. dCount = now;
  186. }
  187. else if (type == CF_TransType::UPLOAD)
  188. {
  189. transType = "Upload";
  190. count = now - uCount;
  191. uCount = now;
  192. }
  193. /* 计算进度,百分比 */
  194. double percent = (double)now / (double)total * 100;
  195. /* 计算单位 */
  196. double tKB = total / 1024.0;
  197. double tMB = tKB / 1024.0;
  198. double tGB = tMB / 1024.0;
  199. /* 计算速度 */
  200. double speed = 0.0;
  201. std::string unit;
  202. computeSpeed(count, speed, unit);
  203. // if(speed == 0.0)
  204. // {
  205. // if(CF_TransType::DOWNLOAD == type)
  206. // {
  207. // speed = dSpeed;
  208. // }else if (CF_TransType::UPLOAD == type) {
  209. // speed = uSpeed;
  210. // }
  211. // }else {
  212. // if(CF_TransType::DOWNLOAD == type)
  213. // {
  214. // dSpeed = speed;
  215. // }else if (CF_TransType::UPLOAD == type) {
  216. // uSpeed = speed;
  217. // }
  218. // }
  219. if(tGB > 1)
  220. {
  221. double dGB = now / 1024.0 / 1024.0 / 1024.0;
  222. double speed = count / 1024.0 / 1024.0;
  223. printf("%s Total / now : %.2fGB / %.2fGB, %.2f%%, Speed:%.2f %s\r", transType.c_str(), tGB, dGB, percent, speed, unit.c_str());
  224. // printf("Download Total / now : {:.2f}GB / {:.2f}GB, {:.2f}%\r", tGB, dGB, percent);
  225. }
  226. else if(tMB > 1)
  227. {
  228. double dMB = now / 1024.0 / 1024.0;
  229. printf("%s Total / now : %.2fMB / %.2fMB, %.2f%%, Speed:%.2f %s\r", transType.c_str(), tMB, dMB, percent, speed, unit.c_str());
  230. // printf("Download Total / now : {:.2f}MB / {:.2f}MB, {:.2f}%\r", tMB, dMB, percent);
  231. }
  232. else if(tKB > 1)
  233. {
  234. double dKB = now / 1024.0;
  235. printf("%s Total / now : %.2fKB / %.2fKB, %.2f%%, Speed:%.2f %s\r", transType.c_str(), tKB, dKB, percent, speed, unit.c_str());
  236. }
  237. else
  238. {
  239. printf("%s Total / now : %ldB / %ldB, %.2f%%, Speed:%.2f %s\r", transType.c_str(), total, now, percent, speed, unit.c_str());
  240. }
  241. fflush(stdout);
  242. }
  243. /**
  244. * @brief 上传和下载进度回调函数,这个函数kennel会被多次调用,即使是没有在下载的时候,因此需要判断传入的数据是否是0
  245. * 必须将 CURLOPT_NOPROGRESS 设为 0 才能真正调用该函数。
  246. *
  247. * @param clientp 通过CURLOPT_XFERINFODATA 设置的指针,libcurl 不会使用它,只会将其从应用程序传递给回调函数。
  248. * 可以通过这个指针将下载的进度传递给应用程序
  249. * @param dltotal 下载的总字节数,上传的时候这个为0
  250. * @param dlnow 已经下载的总字节数
  251. * @param ultotal 需要上传的总字节数,下载的时候这个为0
  252. * @param ulnow 已经上传的总字节数
  253. * @return int 返回0表示正常,非0表示异常
  254. */
  255. static int progress_callback(void *clientp,
  256. curl_off_t dltotal,
  257. curl_off_t dlnow,
  258. curl_off_t ultotal,
  259. curl_off_t ulnow)
  260. {
  261. // LOG_DEBUG("dTotal: {}, dNow: {}, uTotal: {}, uNow: {}", dltotal, dlnow, ultotal, ulnow);
  262. if(dltotal == 0 && ultotal == 0)
  263. {
  264. dCount = 0;
  265. uCount = 0;
  266. dSpeed = 0;
  267. uSpeed = 0;
  268. return 0;
  269. }
  270. std::chrono::system_clock::time_point nowTime = std::chrono::system_clock::now();
  271. auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(nowTime - lastTime).count();
  272. // LOG_DEBUG("duration:{}", duration);
  273. if((duration < 1000) && ((dltotal != dlnow) || (ultotal != ulnow)))
  274. {
  275. return 0;
  276. }
  277. lastTime = nowTime;
  278. /* 正在下载 */
  279. if(dltotal > 0)
  280. {
  281. printProgress(CF_TransType::DOWNLOAD, dltotal, dlnow);
  282. // printf("Download Total / now : {} / {}, {:.2f}%\r", dltotal, dlnow, downloadPercent);
  283. }
  284. /* 正在上传 */
  285. else if(ultotal > 0)
  286. {
  287. printProgress(CF_TransType::UPLOAD, ultotal, ulnow);
  288. // double uploadPercent = (double)ulnow / (double)ultotal * 100;
  289. // printf("Upload Total / now : {} / {}, {:.2f}%\r", ultotal, ulnow, uploadPercent);
  290. }
  291. return 0;
  292. }
  293. /* 使用Windows API进行编码转换 */
  294. #if defined(_WIN32)
  295. static char* GBToUTF8(const char* gb2312)
  296. {
  297. int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
  298. wchar_t* wstr = new wchar_t[len+1];
  299. memset(wstr, 0, len+1);
  300. MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
  301. len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
  302. char* str = new char[len+1];
  303. memset(str, 0, len+1);
  304. WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
  305. if(wstr) delete[] wstr;
  306. return str;
  307. }
  308. #endif /* _WIN32 */
  309. /* 正则表达式,匹配多个空格 */
  310. const std::regex parseRegSpace(R"(( )+)");
  311. /* 匹配以非空格开头的字符,空格隔开,中间任意字符,空格隔开,非空格组成的结尾
  312. * (文件类型) ... (文件大小,$5) ... (文件名)
  313. */
  314. const std::regex parseReg1(R"(^([^ ]+) (\d*) (\w*) (\w*) (\d*) (.*) ([^ ]+)$)");
  315. // LOG_INFO("\n-------------------\n");
  316. /* 匹配换行符,分割每一行 */
  317. const std::regex parseReg2(R"(\n)");
  318. /* 匹配文件夹 */
  319. const std::regex parseReg3(R"(^d.*)");
  320. /* 匹配文件 */
  321. const std::regex parseReg4(R"(^-.*$)");
  322. /**
  323. * @brief 解析字符串文件信息,如果FTP服务器规定好个编码,不需要进行转换
  324. *
  325. * @param strSrc 读取到的字符串
  326. * @param fileList 文件信息列表
  327. * @return true
  328. * @return false
  329. */
  330. static bool parseFileInfo(const std::string& strSrc, std::vector<CF_FileInfo>& fileInfo)
  331. {
  332. #if defined(_WIN32)
  333. // auto str1 = GBToUTF8(strSrc.c_str());
  334. std::string str2 = strSrc;
  335. #else
  336. std::string str2 = strSrc;
  337. #endif /* _WIN32 */
  338. // LOG_DEBUG("\n{}", str2);
  339. // /* 正则表达式,匹配多个空格 */
  340. // std::regex regSpace(R"(( )+)");
  341. // /* 匹配以非空格开头的字符,空格隔开,中间任意字符,空格隔开,非空格组成的结尾
  342. // * (文件类型) ... (文件大小,$5) ... (文件名)
  343. // */
  344. // std::regex reg1(R"(^([^ ]+) (\d*) (\w*) (\w*) (\d*) (.*) ([^ ]+)$)");
  345. // // LOG_INFO("\n-------------------\n");
  346. // /* 匹配换行符,分割每一行 */
  347. // std::regex reg2(R"(\n)");
  348. // /* 匹配文件夹 */
  349. // std::regex reg3(R"(^d.*)");
  350. // /* 匹配文件 */
  351. // std::regex reg4(R"(^-.*$)");
  352. /* -1 表示对正则表达式之前的子序列感兴趣
  353. * 0 表示对正则表达式本身感兴趣,输出的就是换行符 */
  354. std::sregex_token_iterator it2(str2.begin(), str2.end(), parseReg2, -1);
  355. /* 这里取出每一行 */
  356. for(; it2 != std::sregex_token_iterator(); ++it2)
  357. {
  358. /* 去掉多余的空格 */
  359. auto line = std::regex_replace(it2->str(), parseRegSpace, " ");
  360. // LOG_INFO("{}", line);
  361. CF_FileInfo fi;
  362. /* 取出文件类型 */
  363. std::string strFileType = std::regex_replace(line, parseReg1, "$1");
  364. if(std::regex_match(strFileType, parseReg3))
  365. {
  366. fi.type = CF_FileType::DIR;
  367. }else if (std::regex_match(strFileType, parseReg4))
  368. {
  369. fi.type = CF_FileType::FILE;
  370. }
  371. /* 取出文件大小 */
  372. std::string strFileSize = std::regex_replace(line, parseReg1, "$5");
  373. fi.size = std::stoull(strFileSize);
  374. /* 取出文件名 */
  375. std::string strFileName = std::regex_replace(line, parseReg1, "$7");
  376. fi.name = strFileName;
  377. /* 加入队列 */
  378. fileInfo.push_back(fi);
  379. }
  380. return true;
  381. }
  382. /* ==================================================================================
  383. * *********************************** 成员函数 *************************************
  384. * ================================================================================== */
  385. CurlFtp::CurlFtp()
  386. {
  387. /* 调用初始化函数,这个函数可以重复调用 */
  388. curl_global_init(CURL_GLOBAL_DEFAULT);
  389. /* 初始化curl */
  390. hasInstace = true;
  391. }
  392. CurlFtp::~CurlFtp()
  393. {
  394. /* 清理curl */
  395. curl_easy_cleanup(m_curl);
  396. curl_global_cleanup();
  397. hasInstace = false;
  398. }
  399. /* 设置用户名和密码 */
  400. bool CurlFtp::setUsernameAndPassword(const std::string& username, const std::string& password)
  401. {
  402. m_username = username;
  403. m_password = password;
  404. return true;
  405. }
  406. /* 设置IP和端口 */
  407. bool CurlFtp::setFtpIPAndPort(const std::string& IP, const int port)
  408. {
  409. /*先判断是否有ftp器前缀,通过正则表达式,取出IP地址和端口号,去掉前缀和后面的'/ */
  410. m_IP = IP;
  411. m_port = port;
  412. m_ftpUrl = "ftp://" + m_IP + ":" + std::to_string(m_port);
  413. LOG_INFO("Set ftpUrl = " << m_ftpUrl);
  414. m_isSftp = false;
  415. return true;
  416. }
  417. /* 设置SFTP的IP和端口 */
  418. bool CurlFtp::setSftpIPAndPort(const std::string& IP, const int port)
  419. {
  420. m_IP = IP;
  421. m_port = port;
  422. m_ftpUrl = "sftp://" + m_IP + ":" + std::to_string(m_port);
  423. LOG_INFO("Set sftpUrl = " << m_ftpUrl);
  424. m_isSftp = true;
  425. return true;
  426. }
  427. /* 设置是否忽略SSL证书,使用SFTP不建议忽略 */
  428. void CurlFtp::setIgnoreSSLCert(bool isIgnore)
  429. {
  430. m_isIgnoreSSLCert = isIgnore;
  431. }
  432. /* 设置CA证书文件 */
  433. void CurlFtp::setCaCertFile(const std::string& caCertFile)
  434. {
  435. m_caCertFile = caCertFile;
  436. }
  437. /* 设置是否启用CURL的调试信息 */
  438. void CurlFtp::enableCurlDebug(bool isPrint)
  439. {
  440. m_enableCurlDebug = isPrint;
  441. }
  442. /* 列出文件列表 */
  443. bool CurlFtp::getFileList(std::string dir, std::vector<std::string>& fileList)
  444. {
  445. if(m_IP.empty())
  446. {
  447. LOG_WARN("IP or port is empty");
  448. return false;
  449. }
  450. /* 检查dir,添加前缀“/” */
  451. auto dirTmp = checkDirPath(dir);
  452. // CURL *curl = nullptr;
  453. // curl = curl_easy_init();
  454. // if(curl == nullptr)
  455. // {
  456. // LOG_ERROR("curl init failed !");
  457. // return false;
  458. // }
  459. resetCurl(m_curl);
  460. std::vector<CF_FileInfo> listInfo;
  461. /* 获取文件信息 */
  462. listAll(m_curl, dirTmp, listInfo);
  463. for(const CF_FileInfo& fi : listInfo)
  464. {
  465. // LOG_INFO("type = {}, size = {}, name = {}", (int)fi.type, fi.size, fi.name);
  466. if(fi.type == CF_FileType::FILE)
  467. {
  468. fileList.push_back(fi.name);
  469. }
  470. }
  471. // curl_easy_cleanup(curl);
  472. return true;
  473. }
  474. /* 获取文件夹列表 */
  475. bool CurlFtp::getDirList(std::string dir, std::vector<std::string>& dirList)
  476. {
  477. if(m_IP.empty())
  478. {
  479. LOG_WARN("IP or port is empty");
  480. return false;
  481. }
  482. /* 检查dir,添加前缀“/” */
  483. auto dirTmp = checkDirPath(dir);
  484. // CURL *curl = nullptr;
  485. // curl = curl_easy_init();
  486. // if(curl == nullptr)
  487. // {
  488. // LOG_ERROR("curl init failed !");
  489. // return false;
  490. // }
  491. resetCurl(m_curl);
  492. std::vector<CF_FileInfo> listInfo;
  493. /* 获取文件信息 */
  494. listAll(m_curl, dirTmp, listInfo);
  495. for(const CF_FileInfo& fi : listInfo)
  496. {
  497. // LOG_INFO("type = {}, size = {}, name = {}", (int)fi.type, fi.size, fi.name);
  498. if(fi.type == CF_FileType::DIR)
  499. {
  500. dirList.push_back(fi.name);
  501. }
  502. }
  503. // curl_easy_cleanup(curl);
  504. return true;
  505. }
  506. /**
  507. * @brief 判断文件夹是否存在,FTP和SFTP判断方式不同
  508. * FTP使用curl检测判断,但是SFTP无论文件夹是否存在都会返回真,所以只能通过列出文件夹的方式判断
  509. *
  510. * @param dir
  511. * @return true
  512. * @return false
  513. */
  514. bool CurlFtp::isDirExist(const std::string& dir)
  515. {
  516. LOG_DEBUG("Check remote dir:" << dir);
  517. if(m_ftpUrl.empty())
  518. {
  519. LOG_ERROR("ftpUrl is empty");
  520. return false;
  521. }
  522. if(dir == "/")
  523. {
  524. return true;
  525. }
  526. bool result = false;
  527. if(m_isSftp)
  528. {
  529. result = checkSftpDirExist(dir);
  530. }else {
  531. result = checkFtpDirExist(dir);
  532. }
  533. return result;
  534. }
  535. /**
  536. * @brief 创建FTP文件夹,只能创建一层文件夹
  537. *
  538. * @param ftpDir 文件夹路径
  539. * @return true 文件夹创建成功或者文件夹存在
  540. * @return false
  541. */
  542. bool CurlFtp::createDirectory(const std::string& ftpDir)
  543. {
  544. if(m_ftpUrl.empty())
  545. {
  546. LOG_ERROR("ftpUrl is empty");
  547. return false;
  548. }
  549. /* 先检查FTP文件夹是否存在,如果存在直接返回true */
  550. if(isDirExist(ftpDir))
  551. {
  552. return true;
  553. }
  554. /* 检查传入的文件夹格式 */
  555. auto dirTmp = checkDirPath(ftpDir);
  556. // CURL *curl = curl_easy_init();
  557. // if(curl == nullptr)
  558. // {
  559. // LOG_ERROR("Create FTP DIR, curl init failed !");
  560. // return false;
  561. // }
  562. resetCurl(m_curl);
  563. curl_easy_setopt(m_curl, CURLOPT_URL, m_ftpUrl.c_str());
  564. curl_easy_setopt(m_curl, CURLOPT_USERNAME, m_username.c_str());
  565. curl_easy_setopt(m_curl, CURLOPT_PASSWORD, m_password.c_str());
  566. /* 创建FTP头信息 */
  567. struct curl_slist *headerlist = NULL;
  568. std::string mkdir;
  569. if(m_isSftp)
  570. {
  571. mkdir = "MKDIR " + dirTmp;
  572. }else {
  573. mkdir = "MKD " + dirTmp;
  574. }
  575. headerlist = curl_slist_append(headerlist, mkdir.c_str());
  576. /* 设置FTP命令行选项 */
  577. curl_easy_setopt(m_curl, CURLOPT_QUOTE, headerlist);
  578. /* 不包含实体 */
  579. curl_easy_setopt(m_curl, CURLOPT_NOBODY, 1L);
  580. /* 启用跟随重定向 */
  581. curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, 1L);
  582. /* 设置超时时间 */
  583. curl_easy_setopt(m_curl, CURLOPT_TIMEOUT, 30L);
  584. curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, 30L);
  585. /* 设置SFTP */
  586. setSftp(m_curl);
  587. if(m_enableCurlDebug)
  588. {
  589. /* 启用调试信息 */
  590. curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
  591. }
  592. // 启用持久连接
  593. // curl_easy_setopt(m_curl, CURLOPT_TCP_KEEPALIVE, 1L);
  594. LOG_DEBUG("Create remote dir: " << dirTmp);
  595. // CURLcode res = curl_easy_perform(curl);
  596. bool ret = performCurl(m_curl);
  597. if(!ret)
  598. {
  599. LOG_ERROR("Failed to create remote Dir");
  600. }
  601. // curl_easy_cleanup(curl);
  602. curl_slist_free_all(headerlist);
  603. return ret;
  604. }
  605. /* 创建FTP文件夹,递归创建 */
  606. bool CurlFtp::createDirectories(const std::string& ftpDir)
  607. {
  608. /* 检查路径格式,并去掉第一个 / */
  609. std::string ftpDirTmp = checkDirPath(ftpDir);
  610. std::string ftpDir2 = ftpDirTmp.substr(1, ftpDirTmp.size() -1);
  611. /* 将文件夹分开,取出每一个文件夹名称 */
  612. std::vector<std::string> strList;
  613. std::regex reg(R"(/)");
  614. std::sregex_token_iterator it(ftpDir2.begin(), ftpDir2.end(), reg, -1);
  615. for( ; it != std::sregex_token_iterator(); ++it)
  616. {
  617. strList.push_back(it->str());
  618. }
  619. /* 将每一层拼接起来,逐层递归 */
  620. std::vector<std::string> dirList;
  621. for(const std::string& dir : strList)
  622. {
  623. std::string dirTmp = "/";
  624. if(!dirList.empty())
  625. {
  626. dirTmp = dirList.back();
  627. dirTmp += "/";
  628. }
  629. dirTmp += dir;
  630. dirList.push_back(dirTmp);
  631. }
  632. /* 逐层创建 */
  633. for(const std::string& dir : dirList)
  634. {
  635. // LOG_DEBUG("Create dir: {}", dir);
  636. if(!createDirectory(dir))
  637. {
  638. LOG_ERROR("Failed to create dir: " << dir);
  639. return false;
  640. }
  641. }
  642. return true;
  643. }
  644. /**
  645. * @brief 下载文件
  646. *
  647. * @param remoteFile
  648. * @param localFile
  649. * @param timeout 超时时间,单位秒
  650. * @return true
  651. * @return false
  652. */
  653. bool CurlFtp::downloadFile(const std::string& remoteFile, const std::string& localFile, size_t timeout)
  654. {
  655. if(m_ftpUrl.empty())
  656. {
  657. LOG_ERROR("ftpUrl is empty");
  658. return false;
  659. }
  660. /* 检查传入的文件是否符合规范 */
  661. std::string remoteFileTmp = checkFilePath(remoteFile);
  662. std::string ftpUrl = m_ftpUrl + remoteFileTmp;
  663. /* 检查本地文件夹是否存在,不存在则创建 */
  664. // std::string localDir = localFile.substr(0, localFile.find_last_of("/")); /* 去掉最后的 / */
  665. std::string localDirTmp = localFile.substr(0, localFile.find_last_of("/"));
  666. if(!checkLocalDir(localDirTmp))
  667. {
  668. LOG_ERROR("Failed to create local dir: " << localDirTmp);
  669. return false;
  670. }
  671. // CURL* curl = nullptr;
  672. // curl = curl_easy_init();
  673. // if(curl == nullptr)
  674. // {
  675. // LOG_ERROR("curl init failed !");
  676. // return false;
  677. // }
  678. resetCurl(m_curl);
  679. /* 打开文件 */
  680. std::ofstream ofs;
  681. ofs.open(localFile, std::ios::out | std::ios::binary | std::ios::trunc);
  682. /* 设置FTP地址 */
  683. curl_easy_setopt(m_curl, CURLOPT_URL, ftpUrl.c_str());
  684. curl_easy_setopt(m_curl, CURLOPT_PORT, m_port);
  685. /* 设置用户名和密码 */
  686. curl_easy_setopt(m_curl, CURLOPT_USERNAME, m_username.c_str());
  687. curl_easy_setopt(m_curl, CURLOPT_PASSWORD, m_password.c_str());
  688. curl_easy_setopt(m_curl, CURLOPT_USERPWD, (m_username + ":" + m_password).c_str());
  689. /* 启用跟随重定向 */
  690. curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, 1L);
  691. /* 设置回调函数 */
  692. curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, writeFileCallBack);
  693. curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &ofs);
  694. /* 设置超时时间 */
  695. // curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L);
  696. curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, timeout);
  697. /* 设置进度回调函数 */
  698. curl_easy_setopt(m_curl, CURLOPT_XFERINFOFUNCTION, progress_callback);
  699. curl_easy_setopt(m_curl, CURLOPT_XFERINFODATA, nullptr);
  700. /* 启用下载进度回调函数 */
  701. curl_easy_setopt(m_curl, CURLOPT_NOPROGRESS, 0L);
  702. /* 设置SFTP */
  703. setSftp(m_curl);
  704. if(m_enableCurlDebug)
  705. {
  706. /* 启用调试信息 */
  707. curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
  708. }
  709. /* 启用持久连接 */
  710. curl_easy_setopt(m_curl, CURLOPT_TCP_KEEPALIVE, 1L);
  711. /* 发送请求 */
  712. bool ret = performCurl(m_curl);
  713. if(!ret)
  714. {
  715. std::stringstream ss;
  716. ss << "Failed to download file: " << ftpUrl;
  717. LOG_ERROR("Failed to get file list, Url = " << ftpUrl);
  718. /* 清理下载失败的文件 */
  719. ofs.close();
  720. std::remove(localFile.c_str());
  721. return false;
  722. }
  723. /* 关闭文件,清理curl */
  724. ofs.close();
  725. // curl_easy_cleanup(curl);
  726. /* 打印一个换行符 */
  727. // printf("\n");
  728. printf("\n");
  729. return true;
  730. }
  731. /* 下载文件到数组 */
  732. bool CurlFtp::downloadToArray(const std::string& remoteFile, std::vector<char>& arrayInfo, size_t timeout)
  733. {
  734. if(m_ftpUrl.empty())
  735. {
  736. LOG_ERROR("ftpUrl is empty");
  737. return false;
  738. }
  739. /* 检查传入的文件是否符合规范 */
  740. std::string remoteFileTmp = checkFilePath(remoteFile);
  741. std::string ftpUrl = m_ftpUrl + remoteFileTmp;
  742. // CURL* curl = nullptr;
  743. // curl = curl_easy_init();
  744. // if(curl == nullptr)
  745. // {
  746. // LOG_ERROR("curl init failed !");
  747. // return false;
  748. // }
  749. resetCurl(m_curl);
  750. /* 设置FTP地址 */
  751. curl_easy_setopt(m_curl, CURLOPT_URL, ftpUrl.c_str());
  752. curl_easy_setopt(m_curl, CURLOPT_PORT, m_port);
  753. /* 设置用户名和密码 */
  754. curl_easy_setopt(m_curl, CURLOPT_USERNAME, m_username.c_str());
  755. curl_easy_setopt(m_curl, CURLOPT_PASSWORD, m_password.c_str());
  756. curl_easy_setopt(m_curl, CURLOPT_USERPWD, (m_username + ":" + m_password).c_str());
  757. /* 启用跟随重定向 */
  758. curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, 1L);
  759. /* 设置回调函数 */
  760. curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, writeDataCallBack);
  761. curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &arrayInfo);
  762. /* 设置超时时间 */
  763. // curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L);
  764. curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, timeout);
  765. /* 设置进度回调函数 */
  766. curl_easy_setopt(m_curl, CURLOPT_XFERINFOFUNCTION, progress_callback);
  767. curl_easy_setopt(m_curl, CURLOPT_XFERINFODATA, nullptr);
  768. /* 启用下载进度回调函数 */
  769. curl_easy_setopt(m_curl, CURLOPT_NOPROGRESS, 0L);
  770. /* 设置SFTP */
  771. setSftp(m_curl);
  772. if(m_enableCurlDebug)
  773. {
  774. /* 启用调试信息 */
  775. curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
  776. }
  777. /* 启用持久连接 */
  778. curl_easy_setopt(m_curl, CURLOPT_TCP_KEEPALIVE, 1L);
  779. /* 发送请求 */
  780. bool ret = performCurl(m_curl);
  781. if(!ret)
  782. {
  783. LOG_ERROR("Failed to get file list, Url = " << ftpUrl);
  784. }
  785. /* 清理curl */
  786. // curl_easy_cleanup(curl);
  787. /* 打印一个换行符 */
  788. // printf("\n");
  789. printf("\n");
  790. return ret;
  791. }
  792. /**
  793. * @brief 上传文件
  794. *
  795. * @param localFile 本地文件
  796. * @param remoteFile 远程文件
  797. * @param timeout 超时时间,单位秒
  798. * @return true
  799. * @return false
  800. */
  801. bool CurlFtp::uploadFile(const std::string& localFile, const std::string& remoteFile, size_t timeout, bool isCreateDir)
  802. {
  803. if(m_ftpUrl.empty())
  804. {
  805. LOG_ERROR("Url is empty");
  806. return false;
  807. }
  808. /* 检查本地文件是否存在 */
  809. if(!std::filesystem::exists(localFile))
  810. {
  811. LOG_ERROR("Local file is not exist: " << localFile);
  812. return false;
  813. }
  814. /* 检查FTP文件名是否符合规范 */
  815. std::string remoteFileTmp = checkFilePath(remoteFile);
  816. std::string remoteDirTmp = remoteFileTmp.substr(0, remoteFileTmp.find_last_of("/"));
  817. /* 检查远程FTP上的文件夹是否存在,不存在则创建 */
  818. if(!isDirExist(remoteDirTmp))
  819. {
  820. if(isCreateDir)
  821. {
  822. if(!createDirectories(remoteDirTmp))
  823. {
  824. // LOG_ERROR("Failed to create remote dir: {}", remoteFileTmp);
  825. return false;
  826. }
  827. }else {
  828. LOG_ERROR("Remote dir is not exist: " << remoteDirTmp);
  829. return false;
  830. }
  831. }
  832. /* 拼接远程文件的url */
  833. std::string ftpUrl = m_ftpUrl + remoteFileTmp;
  834. /* 打开文件 */
  835. std::ifstream ifs;
  836. ifs.open(localFile, std::ios::in | std::ios::binary);
  837. if(!ifs.is_open())
  838. {
  839. LOG_ERROR("Failed to open local file: " << localFile);
  840. return false;
  841. }
  842. /* 获取文件大小 */
  843. // auto startPos = ifs.tellg();
  844. ifs.seekg(0, std::ios::end);
  845. auto fileSize = ifs.tellg();
  846. /* 恢复指针到文件头 */
  847. ifs.seekg(0, std::ios::beg);
  848. LOG_DEBUG("File size: " << (long)fileSize);
  849. // CURL* curl = nullptr;
  850. // curl = curl_easy_init();
  851. // if(curl == nullptr)
  852. // {
  853. // LOG_ERROR("curl init failed !");
  854. // ifs.close();
  855. // return false;
  856. // }
  857. if(!resetCurl(m_curl))
  858. {
  859. ifs.close();
  860. return false;
  861. }
  862. /* 设置FTP地址 */
  863. curl_easy_setopt(m_curl, CURLOPT_URL, ftpUrl.c_str());
  864. curl_easy_setopt(m_curl, CURLOPT_PORT, m_port);
  865. /* 设置用户名和密码 */
  866. curl_easy_setopt(m_curl, CURLOPT_USERNAME, m_username.c_str());
  867. curl_easy_setopt(m_curl, CURLOPT_PASSWORD, m_password.c_str());
  868. /* 启用跟随重定向 */
  869. curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, 1L);
  870. /* 启用上传 */
  871. curl_easy_setopt(m_curl, CURLOPT_UPLOAD, 1L);
  872. /* 设置回调函数 */
  873. curl_easy_setopt(m_curl, CURLOPT_READFUNCTION, readFileCallBack);
  874. curl_easy_setopt(m_curl, CURLOPT_READDATA, &ifs);
  875. /* 设置上传文件的大小 */
  876. curl_easy_setopt(m_curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)fileSize);
  877. /* 设置超时时间 */
  878. // curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L);
  879. curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, timeout);
  880. /* 设置进度回调函数 */
  881. curl_easy_setopt(m_curl, CURLOPT_XFERINFOFUNCTION, progress_callback);
  882. curl_easy_setopt(m_curl, CURLOPT_XFERINFODATA, nullptr);
  883. /* 启用下载进度回调函数 */
  884. curl_easy_setopt(m_curl, CURLOPT_NOPROGRESS, 0L);
  885. /* 设置SFTP */
  886. setSftp(m_curl);
  887. /* 启用调试信息 */
  888. if(m_enableCurlDebug)
  889. {
  890. curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
  891. }
  892. /* 启用持久连接 */
  893. curl_easy_setopt(m_curl, CURLOPT_TCP_KEEPALIVE, 1L);
  894. /* 发送请求 */
  895. bool ret = performCurl(m_curl);
  896. if(!ret)
  897. {
  898. LOG_ERROR("Upload file failed, Url = " << ftpUrl);
  899. }
  900. /* 关闭文件,清理curl */
  901. ifs.close();
  902. // curl_easy_cleanup(curl);
  903. /* 打印一个换行符 */
  904. printf("\n");
  905. return ret;
  906. }
  907. /**
  908. * @brief 上传文件,上传数据
  909. * 注意:函数内不会拷贝数据,因此在上传完成前需要保证该指针的有效性,拷贝完成后也不会销毁源数据
  910. * 默认超时时间是30秒,也无法设置
  911. *
  912. * @param srcData 数据指针
  913. * @param size 数据大小
  914. * @param remoteFile 远程文件名,包括地址
  915. * @param timeout 超时时间,单位秒
  916. * @return true
  917. * @return false
  918. */
  919. bool CurlFtp::uploadData(char* srcData, size_t size, const std::string& remoteFile, size_t timeout, bool isCreateDir)
  920. {
  921. if(m_ftpUrl.empty())
  922. {
  923. LOG_ERROR("Url is empty");
  924. return false;
  925. }
  926. /* 初始化本地数据 */
  927. CF_ArrayInfo arrayInfo;
  928. arrayInfo.data = srcData;
  929. arrayInfo.size = size;
  930. arrayInfo.pos = 0;
  931. /* 检查FTP文件名是否符合规范 */
  932. std::string remoteFileTmp = checkFilePath(remoteFile);
  933. std::string remoteDirTmp = remoteFileTmp.substr(0, remoteFileTmp.find_last_of("/"));
  934. /* 检查远程FTP上的文件夹是否存在,不存在则创建 */
  935. if(!isDirExist(remoteDirTmp))
  936. {
  937. if(isCreateDir)
  938. {
  939. if(!createDirectories(remoteDirTmp))
  940. {
  941. // LOG_ERROR("Failed to create remote dir: {}", remoteFileTmp);
  942. return false;
  943. }
  944. }else {
  945. LOG_ERROR("Remote dir is not exist: " << remoteDirTmp);
  946. return false;
  947. }
  948. }
  949. /* 拼接远程文件的url */
  950. std::string ftpUrl = m_ftpUrl + remoteFileTmp;
  951. LOG_DEBUG("Data size: " << arrayInfo.size);
  952. // CURL* curl = nullptr;
  953. // curl = curl_easy_init();
  954. // if(curl == nullptr)
  955. // {
  956. // LOG_ERROR("curl init failed !");
  957. // return false;
  958. // }
  959. if(!resetCurl(m_curl))
  960. {
  961. return false;
  962. }
  963. /* 设置FTP地址 */
  964. curl_easy_setopt(m_curl, CURLOPT_URL, ftpUrl.c_str());
  965. curl_easy_setopt(m_curl, CURLOPT_PORT, m_port);
  966. /* 设置用户名和密码 */
  967. // curl_easy_setopt(curl, CURLOPT_USERNAME, m_username.c_str());
  968. // curl_easy_setopt(curl, CURLOPT_PASSWORD, m_password.c_str());
  969. curl_easy_setopt(m_curl, CURLOPT_USERPWD, (m_username + ":" + m_password).c_str());
  970. /* 启用跟随重定向 */
  971. curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, 1L);
  972. /* 启用上传 */
  973. curl_easy_setopt(m_curl, CURLOPT_UPLOAD, 1L);
  974. /* 设置回调函数 */
  975. curl_easy_setopt(m_curl, CURLOPT_READFUNCTION, readDataCallBack);
  976. curl_easy_setopt(m_curl, CURLOPT_READDATA, &arrayInfo);
  977. /* 设置上传文件的大小 */
  978. curl_easy_setopt(m_curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)arrayInfo.size);
  979. /* 设置超时时间 */
  980. // curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L);
  981. curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, timeout);
  982. /* 设置进度回调函数 */
  983. curl_easy_setopt(m_curl, CURLOPT_XFERINFOFUNCTION, progress_callback);
  984. curl_easy_setopt(m_curl, CURLOPT_XFERINFODATA, nullptr);
  985. /* 启用下载进度回调函数 */
  986. curl_easy_setopt(m_curl, CURLOPT_NOPROGRESS, 0L);
  987. // 禁用被动模式(如果需要)
  988. curl_easy_setopt(m_curl, CURLOPT_FTP_USE_EPSV, 1L);
  989. if(m_enableCurlDebug)
  990. {
  991. /* 启用调试信息 */
  992. curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
  993. }
  994. /* 启用持久连接 */
  995. curl_easy_setopt(m_curl, CURLOPT_TCP_KEEPALIVE, 1L);
  996. /* 发送请求 */
  997. bool ret = performCurl(m_curl);
  998. if(!ret)
  999. {
  1000. LOG_ERROR("Upload file failed, Url = " << ftpUrl);
  1001. }
  1002. /* 关闭文件,清理curl */
  1003. // curl_easy_cleanup(curl);
  1004. /* 打印一个换行符 */
  1005. printf("\n");
  1006. return ret;
  1007. }
  1008. /**
  1009. * @brief 列出文件列表,这个需要的参数很多,无法设置成静态函数
  1010. *
  1011. * @param curl CURL句柄
  1012. * @param dir 文件夹,相对路径,不带有IP和端口号
  1013. * @param fileList 返回值,文件列表
  1014. * @return true
  1015. * @return false
  1016. */
  1017. bool CurlFtp::listAll(CURL* curl, std::string dir, std::vector<CF_FileInfo>& fileInfoList)
  1018. {
  1019. if(m_IP.empty())
  1020. {
  1021. LOG_ERROR("IP is empty");
  1022. return false;
  1023. }
  1024. bool result = false;
  1025. std::string strSrc;
  1026. /* 先设置FTP地址 */
  1027. std::string ftpUrl = m_ftpUrl + dir;
  1028. curl_easy_setopt(curl, CURLOPT_URL, ftpUrl.c_str());
  1029. curl_easy_setopt(curl, CURLOPT_PORT, m_port);
  1030. /* 设置用户名和密码 */
  1031. curl_easy_setopt(curl, CURLOPT_USERNAME, m_username.c_str());
  1032. curl_easy_setopt(curl, CURLOPT_PASSWORD, m_password.c_str());
  1033. /* 设置列出文件命令,只列出文件名称,不携带信息 */
  1034. // curl_easy_setopt(m_curl, CURLOPT_DIRLISTONLY, 1L);
  1035. /* 设置回调函数 */
  1036. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeStringCallback);
  1037. /* 设置需要写入的容器,回调函数的第四个参数 */
  1038. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strSrc);
  1039. /* 设置超时时间 */
  1040. curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L);
  1041. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L);
  1042. // 禁用被动模式,设置为0是禁用(如果需要)
  1043. // curl_easy_setopt(curl, CURLOPT_FTP_USE_EPSV, 1L);
  1044. if(m_enableCurlDebug)
  1045. {
  1046. /* 启用调试信息 */
  1047. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  1048. }
  1049. /* 发送请求 */
  1050. // CURLcode res = curl_easy_perform(curl);
  1051. bool ret = performCurl(curl);
  1052. if(!ret)
  1053. {
  1054. LOG_ERROR("Failed to get file listUrl = " << ftpUrl);
  1055. return false;
  1056. }
  1057. /* 解析字符串 */
  1058. parseFileInfo(strSrc, fileInfoList);
  1059. return true;
  1060. }
  1061. /* 检查文件夹路径是否合规,不合规就修改 */
  1062. std::string CurlFtp::checkDirPath(const std::string& dir)
  1063. {
  1064. std::string dirTmp = dir;
  1065. /* 检查是否以“/”开头 */
  1066. std::regex reg(R"(^/[.]*)");
  1067. if(!std::regex_match(dirTmp, reg))
  1068. {
  1069. dirTmp = "/" + dirTmp;
  1070. }
  1071. /* 如果有重复的“/”,替换成一个 “/” */
  1072. std::regex reg1(R"(//)");
  1073. dirTmp = std::regex_replace(dirTmp, reg1, "/");
  1074. /* 检查是否以“/”结束 */
  1075. std::regex reg2(R"([.]*/$)");
  1076. if(!std::regex_match(dirTmp, reg2))
  1077. {
  1078. dirTmp = dirTmp + "/";
  1079. }
  1080. return dirTmp;
  1081. }
  1082. /* 检查文件路径是否合规 */
  1083. std::string CurlFtp::checkFilePath(const std::string& file)
  1084. {
  1085. std::string dirTmp = file;
  1086. /* 检查是否以“/”结束,如果是以“/”结尾,返回空字符串,并报错 */
  1087. std::regex reg2(R"([.]*/$)");
  1088. if(std::regex_match(dirTmp, reg2))
  1089. {
  1090. LOG_ERROR("File path is not correct, end with '/'");
  1091. return std::string();
  1092. }
  1093. /* 检查是否以“/”开头 */
  1094. std::regex reg(R"(^/[.]*)");
  1095. if(!std::regex_match(dirTmp, reg))
  1096. {
  1097. dirTmp = "/" + dirTmp;
  1098. }
  1099. /* 如果有重复的“/”,替换成一个 “/” */
  1100. std::regex reg1(R"(//)");
  1101. dirTmp = std::regex_replace(dirTmp, reg1, "/");
  1102. return dirTmp;
  1103. }
  1104. /**
  1105. * @brief 检查本地文件夹是否存在,不存在则创建
  1106. * 这里默认传入的是文件夹路径,不是文件路径,如果最后有‘/’,会自动去掉
  1107. *
  1108. * @param localDir 文件夹路径
  1109. * @return true
  1110. * @return false
  1111. */
  1112. bool CurlFtp::checkLocalDir(const std::string& localDir)
  1113. {
  1114. /* 去掉最后的‘/’ */
  1115. std::regex reg(R"([.]*/$)");
  1116. std::string localDirTmp = std::regex_replace(localDir, reg, "");
  1117. /* 检查文件夹是否存在 */
  1118. if(std::filesystem::exists(localDirTmp))
  1119. {
  1120. return true;
  1121. }
  1122. /* 创建文件夹 */
  1123. if(!std::filesystem::create_directories(localDirTmp))
  1124. {
  1125. LOG_ERROR("Failed to create local dir: " << localDirTmp);
  1126. return false;
  1127. }
  1128. return true;
  1129. }
  1130. /* 执行curl,添加重试机制 */
  1131. bool CurlFtp::performCurl(CURL* curl)
  1132. {
  1133. int retry = 3;
  1134. while(retry > 0)
  1135. {
  1136. CURLcode res = curl_easy_perform(curl);
  1137. if(res == CURLE_OK)
  1138. {
  1139. return true;
  1140. }
  1141. else if (res == CURLE_LOGIN_DENIED)
  1142. {
  1143. LOG_ERROR("Login failed, error code: " << (int)res << ", " << curl_easy_strerror(res));
  1144. /* 设置用户名和密码 */
  1145. curl_easy_setopt(curl, CURLOPT_USERNAME, m_username.c_str());
  1146. curl_easy_setopt(curl, CURLOPT_PASSWORD, m_password.c_str());
  1147. }
  1148. else
  1149. {
  1150. LOG_ERROR("Perform curl failed, error code: " << (int)res << ", " << curl_easy_strerror(res));
  1151. LOG_ERROR("Retry times: " << (4 - retry));
  1152. }
  1153. retry--;
  1154. }
  1155. return false;
  1156. }
  1157. /* 重置curl设置 */
  1158. bool CurlFtp::resetCurl(CURL* curl)
  1159. {
  1160. if(m_curl == nullptr)
  1161. {
  1162. m_curl = curl_easy_init();
  1163. if(m_curl == nullptr)
  1164. {
  1165. LOG_ERROR("curl init failed !");
  1166. return false;
  1167. }
  1168. }else {
  1169. curl_easy_reset(m_curl);
  1170. }
  1171. return true;
  1172. }
  1173. /* 设置sftp,如果是sftp就设置 */
  1174. bool CurlFtp::setSftp(CURL* curl)
  1175. {
  1176. /* 判断是否是SFTP */
  1177. if(m_isSftp)
  1178. {
  1179. /* 判断是否需要设置CA证书 */
  1180. if(m_isIgnoreSSLCert)
  1181. {
  1182. /* 忽略证书验证 */
  1183. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
  1184. /* 忽略主机名验证 */
  1185. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
  1186. } else
  1187. {
  1188. /* 设置CA证书 */
  1189. curl_easy_setopt(curl, CURLOPT_CAINFO, m_caCertFile.c_str());
  1190. }
  1191. }
  1192. return true;
  1193. }
  1194. /**
  1195. * @brief 检查FTP文件夹是否存在,注意,传入的文件夹最后一定要带/,否则会检查的是文件
  1196. *
  1197. * @param dir
  1198. * @return true
  1199. * @return false
  1200. */
  1201. bool CurlFtp::checkFtpDirExist(const std::string& dir)
  1202. {
  1203. /* 检查传入的文件夹 */
  1204. auto dirTmp = checkDirPath(dir);
  1205. resetCurl(m_curl);
  1206. std::string ftpUrl = m_ftpUrl + dirTmp;
  1207. curl_easy_setopt(m_curl, CURLOPT_URL, ftpUrl.c_str());
  1208. curl_easy_setopt(m_curl, CURLOPT_USERNAME, m_username.c_str());
  1209. curl_easy_setopt(m_curl, CURLOPT_PASSWORD, m_password.c_str());
  1210. /* 获取文件夹是否存在,不需要接收文件 */
  1211. curl_easy_setopt(m_curl, CURLOPT_NOBODY, 1L);
  1212. // curl_easy_setopt(m_curl, CURLOPT_HEADER, 1L);
  1213. /* 设置SFTP */
  1214. setSftp(m_curl);
  1215. /* 启用调试信息 */
  1216. if(m_enableCurlDebug)
  1217. {
  1218. curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
  1219. }
  1220. /* 启用持久连接 */
  1221. // curl_easy_setopt(m_curl, CURLOPT_TCP_KEEPALIVE, 1L);
  1222. /* 这里只需要执行一次 */
  1223. CURLcode res = curl_easy_perform(m_curl);
  1224. bool result = true;
  1225. if(res == CURLE_REMOTE_FILE_NOT_FOUND)
  1226. {
  1227. result = false;
  1228. }
  1229. else if(res == CURLE_OK)
  1230. {
  1231. result = true;
  1232. } else
  1233. {
  1234. LOG_ERROR("Check remote dir error, error code: " << (int)res << ", " << curl_easy_strerror(res));
  1235. result = false;
  1236. }
  1237. // LOG_DEBUG("Check remote dir: {}, res: {}", dir, (int)res);
  1238. return result;
  1239. }
  1240. /* 检查SFTP文件夹是否存在,这里通过列出上一层的文件夹中的内容,然后进行对比判断的 */
  1241. bool CurlFtp::checkSftpDirExist(const std::string& dir)
  1242. {
  1243. /* 取出上一层文件夹路径 */
  1244. std::string parentDir = std::filesystem::path(dir).parent_path().string();
  1245. // LOG_DEBUG("Parent dir: {}", parentDir);
  1246. std::vector<std::string> vecDir;
  1247. bool ret = getDirList(parentDir, vecDir);
  1248. if(!ret)
  1249. {
  1250. LOG_ERROR("Failed to check sftp dir: " << dir);
  1251. return false;
  1252. }
  1253. /* 取出本层文件夹名称 */
  1254. std::string dirName = std::filesystem::path(dir).filename().string();
  1255. // LOG_DEBUG("Dir name: {}", dirName);
  1256. /* 判断是否存在 */
  1257. bool result = false;
  1258. for(const std::string& str : vecDir)
  1259. {
  1260. if(str == dirName)
  1261. {
  1262. result = true;
  1263. break;
  1264. }
  1265. }
  1266. return result;
  1267. }