CurlFtp.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. #include "CurlFtp.h"
  2. #include <iostream>
  3. #include <regex>
  4. #include <iosfwd>
  5. #include "fmtlog.h"
  6. #include "CurlFtpInfo.h"
  7. #if defined(_WIN32)
  8. #endif /* _WIN32 */
  9. /* ==================================================================================
  10. * *********************************** 全局变量 *************************************
  11. * ==================================================================================
  12. */
  13. /* 是否有初始化实例,主要用于静态函数调用curl_global_cleanup()函数前判断,有实例就不调用 */
  14. static bool hasInstace = false;
  15. /* ==================================================================================
  16. * *********************************** 全局函数 *************************************
  17. * ==================================================================================
  18. */
  19. /**
  20. * @brief 写入回调函数
  21. *
  22. * @param contents curl的数据缓冲区
  23. * @param size 数据大小,单位是size_t
  24. * @param nmemb size_t的单位字节数
  25. * @param userStr 用户提供的字符串,格式可以是任意的
  26. * @return size_t 总共获取到的数据大小,单位是字节
  27. */
  28. static size_t writeStringCallback(void *contents, size_t size, size_t nmemb, std::string *userStr)
  29. {
  30. size_t newLength = size * nmemb;
  31. size_t oldLength = userStr->size();
  32. try
  33. {
  34. userStr->resize(oldLength + newLength);
  35. }
  36. catch(std::bad_alloc &e)
  37. {
  38. //handle memory problem
  39. return 0;
  40. }
  41. std::copy_n((char*)contents, newLength, userStr->begin() + oldLength);
  42. return size * nmemb;
  43. }
  44. /**
  45. * @brief 写入回调函数,listFiles需要调用
  46. *
  47. * @param buffer curl下载回来的数据
  48. * @param size 数据大小
  49. * @param nmemb 数据单位字节数
  50. * @param userp 用户传进来的容器
  51. * @return int 返回拷贝的字节数
  52. */
  53. static int writeStringListCallback(void* buffer, size_t size, size_t nmemb, void* userp)
  54. {
  55. std::vector<std::string>* fileList = static_cast<std::vector<std::string>*>(userp);
  56. std::string line(static_cast<char*>(buffer), size * nmemb);
  57. // printf("line = %s\n", line.c_str());
  58. fileList->push_back(line);
  59. return size * nmemb;
  60. }
  61. /* 使用Windows API进行编码转换 */
  62. #if defined(_WIN32)
  63. static char* GBToUTF8(const char* gb2312)
  64. {
  65. int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
  66. wchar_t* wstr = new wchar_t[len+1];
  67. memset(wstr, 0, len+1);
  68. MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
  69. len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
  70. char* str = new char[len+1];
  71. memset(str, 0, len+1);
  72. WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
  73. if(wstr) delete[] wstr;
  74. return str;
  75. }
  76. #endif /* _WIN32 */
  77. /**
  78. * @brief 解析字符串文件信息
  79. *
  80. * @param strSrc
  81. * @param fileList
  82. * @return true
  83. * @return false
  84. */
  85. static bool parseFileInfo(const std::string& strSrc, std::vector<CF_FileInfo>& fileInfo)
  86. {
  87. #if defined(_WIN32)
  88. auto str1 = GBToUTF8(strSrc.c_str());
  89. std::string str2 = str1;
  90. #else
  91. std::string str2 = strSrc;
  92. #endif /* _WIN32 */
  93. // FMTLOG_DEBUG("\n{}", str2);
  94. /* 正则表达式,匹配多个空格 */
  95. std::regex reg(R"(( )+)");
  96. /* 匹配以非空格开头的字符,空格隔开,中间任意字符,空格隔开,非空格组成的结尾
  97. * (文件类型) ... (文件大小,$5) ... (文件名)
  98. */
  99. std::regex reg1(R"(^([^ ]+) (\d*) (\w*) (\w*) (\d*) (.*) ([^ ]+)$)");
  100. // FMTLOG_INFO("\n-------------------\n");
  101. /* 匹配换行符,分割每一行 */
  102. std::regex reg2(R"(\n)");
  103. /* 匹配文件夹 */
  104. std::regex reg3(R"(^d.*)");
  105. /* 匹配文件 */
  106. std::regex reg4(R"(^-.*$)");
  107. /* -1 表示对正则表达式之前的子序列感兴趣
  108. * 0 表示对正则表达式本身感兴趣,输出的就是换行符 */
  109. std::sregex_token_iterator it2(str2.begin(), str2.end(), reg2, -1);
  110. /* 这里取出每一行 */
  111. for(; it2 != std::sregex_token_iterator(); ++it2)
  112. {
  113. /* 去掉多余的空格 */
  114. auto line = std::regex_replace(it2->str(), reg, " ");
  115. // FMTLOG_INFO("{}", line);
  116. CF_FileInfo fi;
  117. /* 取出文件类型 */
  118. std::string strFileType = std::regex_replace(line, reg1, "$1");
  119. if(std::regex_match(strFileType, reg3))
  120. {
  121. fi.type = CF_FileType::DIR;
  122. }else if (std::regex_match(strFileType, reg4))
  123. {
  124. fi.type = CF_FileType::FILE;
  125. }
  126. /* 取出文件大小 */
  127. std::string strFileSize = std::regex_replace(line, reg1, "$5");
  128. fi.size = std::stoull(strFileSize);
  129. /* 取出文件名 */
  130. std::string strFileName = std::regex_replace(line, reg1, "$7");
  131. fi.name = strFileName;
  132. /* 加入队列 */
  133. fileInfo.push_back(fi);
  134. }
  135. return true;
  136. }
  137. /* ==================================================================================
  138. * *********************************** 成员函数 *************************************
  139. * ================================================================================== */
  140. CurlFtp::CurlFtp()
  141. {
  142. /* 调用初始化函数,这个函数可以重复调用 */
  143. curl_global_init(CURL_GLOBAL_DEFAULT);
  144. /* 初始化curl */
  145. m_curl = curl_easy_init();
  146. hasInstace = true;
  147. }
  148. CurlFtp::~CurlFtp()
  149. {
  150. /* 清理curl */
  151. curl_easy_cleanup(m_curl);
  152. curl_global_cleanup();
  153. hasInstace = false;
  154. }
  155. /* 检查文件夹是否存在,不确定好不好用 */
  156. // bool CurlFtp::isDirExists(const std::string &ftpUrl, const std::string &username, const std::string &password)
  157. // {
  158. // CURL *curl;
  159. // CURLcode res;
  160. // bool result = false;
  161. // std::string retList;
  162. // curl_global_init(CURL_GLOBAL_DEFAULT);
  163. // curl = curl_easy_init();
  164. // if(curl)
  165. // {
  166. // curl_easy_setopt(curl, CURLOPT_URL, ftpUrl.c_str());
  167. // curl_easy_setopt(curl, CURLOPT_USERNAME, username.c_str());
  168. // curl_easy_setopt(curl, CURLOPT_PASSWORD, password.c_str());
  169. // // curl_easy_setopt(curl, CURLOPT_DIRLISTONLY, 1L); // We only want the directory listing
  170. // curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
  171. // curl_easy_setopt(curl, CURLOPT_WRITEDATA, &retList);
  172. // res = curl_easy_perform(curl);
  173. // // printf("res = %d\n", res);
  174. // if(res != CURLE_OK)
  175. // {
  176. // fprintf(stderr, "getDirList() failed, error code %d, :%s\n",res, curl_easy_strerror(res));
  177. // }
  178. // else
  179. // {
  180. // // std::cout << "Directory list: \n" << retList << std::endl;
  181. // }
  182. // curl_easy_cleanup(curl);
  183. // }
  184. // curl_global_cleanup();
  185. // return result;
  186. // }
  187. /* 使用正则表达式提取出文件夹 */
  188. // bool CurlFtp::extractDirectories(const std::string& responseString)
  189. // {
  190. // std::regex directoryRegex(R"(^d.*\s(\S+)$)");
  191. // std::smatch match;
  192. // std::istringstream retStream(responseString);
  193. // std::string line;
  194. // while (std::getline(retStream, line))
  195. // {
  196. // if (std::regex_match(line, match, directoryRegex))
  197. // {
  198. // std::cout << "Directory: " << match[1] << std::endl;
  199. // }
  200. // }
  201. // return true;
  202. // }
  203. /**
  204. * @brief 列出FTP文件夹
  205. *
  206. * @param ftpUrl 需要列出文件夹的FTP地址
  207. * @param username
  208. * @param password
  209. * @return std::string
  210. */
  211. std::string CurlFtp::listDir(const std::string &ftpUrl, const std::string &username, const std::string &password)
  212. {
  213. CURL *curl;
  214. CURLcode res;
  215. bool result = false;
  216. std::string retList;
  217. /* 1. 初始化curl,这个函数需要在调用任何curl函数之前 */
  218. curl_global_init(CURL_GLOBAL_DEFAULT);
  219. /* 2. 获取一个curl句柄 */
  220. curl = curl_easy_init();
  221. if(curl)
  222. {
  223. /* 3. 设置curl选项 */
  224. curl_easy_setopt(curl, CURLOPT_URL, ftpUrl.c_str());
  225. curl_easy_setopt(curl, CURLOPT_USERNAME, username.c_str());
  226. curl_easy_setopt(curl, CURLOPT_PASSWORD, password.c_str());
  227. // curl_easy_setopt(curl, CURLOPT_DIRLISTONLY, 1L); // We only want the directory listing
  228. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeStringCallback);
  229. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &retList);
  230. /* 4. 发送信息,一直等待知道返回 */
  231. res = curl_easy_perform(curl);
  232. // printf("res = %d\n", res);
  233. if(res != CURLE_OK)
  234. {
  235. fprintf(stderr, "getDirList() failed, error code %d, :%s\n",res, curl_easy_strerror(res));
  236. } else
  237. {
  238. // std::cout << "Directory list: \n" << retList << std::endl;
  239. }
  240. /* 5. 清理curl */
  241. curl_easy_cleanup(curl);
  242. }
  243. if(hasInstace == false)
  244. {
  245. curl_global_cleanup();
  246. }
  247. return retList;
  248. }
  249. /* 列出文件夹中的所有文件 */
  250. bool CurlFtp::listFiles(const std::string &ftpUrl, const std::string &username, const std::string &password, std::vector<std::string>& fileList)
  251. {
  252. CURL *curl;
  253. CURLcode res;
  254. bool result = false;
  255. curl_global_init(CURL_GLOBAL_DEFAULT);
  256. curl = curl_easy_init();
  257. if(curl)
  258. {
  259. curl_easy_setopt(curl, CURLOPT_URL, (ftpUrl).c_str());
  260. curl_easy_setopt(curl, CURLOPT_USERNAME, username.c_str());
  261. curl_easy_setopt(curl, CURLOPT_PASSWORD, password.c_str());
  262. curl_easy_setopt(curl, CURLOPT_DIRLISTONLY, 1L);
  263. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeStringListCallback);
  264. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &fileList);
  265. res = curl_easy_perform(curl);
  266. if(res != CURLE_OK)
  267. {
  268. fprintf(stderr, "Failed to get file list, error code :%d ,%s\n", res, curl_easy_strerror(res));
  269. }
  270. else
  271. {
  272. // for(const std::string& filename : fileList)
  273. // {
  274. // printf("%s\n", filename.c_str());
  275. // }
  276. result = true;
  277. }
  278. curl_easy_cleanup(curl);
  279. }
  280. if(hasInstace == false)
  281. {
  282. curl_global_cleanup();
  283. }
  284. return result;
  285. }
  286. /* 创建文件夹 */
  287. bool CurlFtp::createDir(const std::string &ftpUrl, const std::string &username, const std::string &password, const std::string &dirName)
  288. {
  289. CURL *curl;
  290. CURLcode res;
  291. bool result = false;
  292. curl_global_init(CURL_GLOBAL_DEFAULT);
  293. curl = curl_easy_init();
  294. if(curl)
  295. {
  296. curl_easy_setopt(curl, CURLOPT_URL, ftpUrl.c_str());
  297. curl_easy_setopt(curl, CURLOPT_USERNAME, username.c_str());
  298. curl_easy_setopt(curl, CURLOPT_PASSWORD, password.c_str());
  299. // Create a list of FTP commands to be executed on the server
  300. struct curl_slist *headerlist = NULL;
  301. std::string mkdir = "MKD " + dirName;
  302. headerlist = curl_slist_append(headerlist, mkdir.c_str());
  303. // Set the list of FTP commands to be executed on the server before the transfer
  304. curl_easy_setopt(curl, CURLOPT_QUOTE, headerlist);
  305. // Tell libcurl to not include the body in the output
  306. curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
  307. res = curl_easy_perform(curl);
  308. if(res != CURLE_OK)
  309. {
  310. fprintf(stderr, "createDir() failed, error code :%d ,%s\n", res, curl_easy_strerror(res));
  311. result = false;
  312. }else
  313. {
  314. result = true;
  315. }
  316. curl_easy_cleanup(curl);
  317. curl_slist_free_all(headerlist);
  318. }
  319. if(hasInstace == false)
  320. {
  321. curl_global_cleanup();
  322. }
  323. return result;
  324. }
  325. /* 设置IP和端口 */
  326. bool CurlFtp::setFtpIPAndPort(const std::string& IP, const std::string& port)
  327. {
  328. /*先判断是否有ftp器前缀,通过正则表达式,取出IP地址和端口号,去掉前缀和后面的'/ */
  329. m_IP = IP;
  330. m_port = port;
  331. m_ftpUrl = "ftp://" + m_IP + ":" + m_port;
  332. printf("ftpUrl = %s\n", m_ftpUrl.c_str());
  333. return true;
  334. }
  335. /* 设置用户名和密码 */
  336. bool CurlFtp::setFtpUsernameAndPassword(const std::string& username, const std::string& password)
  337. {
  338. m_username = username;
  339. m_password = password;
  340. return true;
  341. }
  342. /* 列出文件列表 */
  343. bool CurlFtp::getFileList(std::string dir, std::vector<std::string>& fileList)
  344. {
  345. if(m_IP.empty() || m_port.empty())
  346. {
  347. FMTLOG_WARN("IP or port is empty");
  348. return false;
  349. }
  350. if(m_curl == nullptr)
  351. {
  352. FMTLOG_WARN("m_curl is nullptr");
  353. return false;
  354. }
  355. std::vector<CF_FileInfo> listInfo;
  356. listAll(dir, listInfo);
  357. // for(const CF_FileInfo& fi : listInfo)
  358. // {
  359. // FMTLOG_INFO("type = {}, size = {}, name = {}", (int)fi.type, fi.size, fi.name);
  360. // }
  361. return true;
  362. }
  363. /* 列出文件列表 */
  364. bool CurlFtp::listAll(std::string dir, std::vector<CF_FileInfo>& fileList)
  365. {
  366. if(m_IP.empty() || m_port.empty())
  367. {
  368. printf("IP or port is empty\n");
  369. return false;
  370. }
  371. if(m_curl == nullptr)
  372. {
  373. printf("m_curl is nullptr\n");
  374. return false;
  375. }
  376. bool result = false;
  377. std::string strSrc;
  378. /* 先设置FTP地址 */
  379. std::string ftpUrl = m_ftpUrl + dir;
  380. curl_easy_setopt(m_curl, CURLOPT_URL, ftpUrl.c_str());
  381. /* 设置用户名和密码 */
  382. curl_easy_setopt(m_curl, CURLOPT_USERNAME, m_username.c_str());
  383. curl_easy_setopt(m_curl, CURLOPT_PASSWORD, m_password.c_str());
  384. /* 设置列出文件命令,只列出文件名称,不携带信息 */
  385. // curl_easy_setopt(m_curl, CURLOPT_DIRLISTONLY, 1L);
  386. /* 设置回调函数 */
  387. curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, writeStringCallback);
  388. /* 设置需要写入的容器,回调函数的第四个参数 */
  389. curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &strSrc);
  390. /* 发送请求 */
  391. m_res = curl_easy_perform(m_curl);
  392. if(m_res != CURLE_OK)
  393. {
  394. FMTLOG_ERROR("Failed to get file list, error code: {} ,{}", (int)m_res, curl_easy_strerror(m_res));
  395. return false;
  396. }
  397. /* 解析字符串 */
  398. parseFileInfo(strSrc, fileList);
  399. return true;
  400. }