123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471 |
- #include "CurlFtp.h"
- #include <iostream>
- #include <regex>
- #include <iosfwd>
- #include "fmtlog.h"
- #include "CurlFtpInfo.h"
- #if defined(_WIN32)
- #endif /* _WIN32 */
- /* ==================================================================================
- * *********************************** 全局变量 *************************************
- * ==================================================================================
- */
- /* 是否有初始化实例,主要用于静态函数调用curl_global_cleanup()函数前判断,有实例就不调用 */
- static bool hasInstace = false;
- /* ==================================================================================
- * *********************************** 全局函数 *************************************
- * ==================================================================================
- */
- /**
- * @brief 写入回调函数
- *
- * @param contents curl的数据缓冲区
- * @param size 数据大小,单位是size_t
- * @param nmemb size_t的单位字节数
- * @param userStr 用户提供的字符串,格式可以是任意的
- * @return size_t 总共获取到的数据大小,单位是字节
- */
- static size_t writeStringCallback(void *contents, size_t size, size_t nmemb, std::string *userStr)
- {
- size_t newLength = size * nmemb;
- size_t oldLength = userStr->size();
- try
- {
- userStr->resize(oldLength + newLength);
- }
- catch(std::bad_alloc &e)
- {
- //handle memory problem
- return 0;
- }
- std::copy_n((char*)contents, newLength, userStr->begin() + oldLength);
- return size * nmemb;
- }
- /**
- * @brief 写入回调函数,listFiles需要调用
- *
- * @param buffer curl下载回来的数据
- * @param size 数据大小
- * @param nmemb 数据单位字节数
- * @param userp 用户传进来的容器
- * @return int 返回拷贝的字节数
- */
- 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);
- // printf("line = %s\n", line.c_str());
- fileList->push_back(line);
- return size * nmemb;
- }
- /* 使用Windows API进行编码转换 */
- #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 /* _WIN32 */
- /**
- * @brief 解析字符串文件信息
- *
- * @param strSrc
- * @param fileList
- * @return true
- * @return false
- */
- static bool parseFileInfo(const std::string& strSrc, std::vector<CF_FileInfo>& fileInfo)
- {
- #if defined(_WIN32)
- auto str1 = GBToUTF8(strSrc.c_str());
- std::string str2 = str1;
- #else
- std::string str2 = strSrc;
- #endif /* _WIN32 */
-
- // FMTLOG_DEBUG("\n{}", str2);
- /* 正则表达式,匹配多个空格 */
- std::regex reg(R"(( )+)");
- /* 匹配以非空格开头的字符,空格隔开,中间任意字符,空格隔开,非空格组成的结尾
- * (文件类型) ... (文件大小,$5) ... (文件名)
- */
- std::regex reg1(R"(^([^ ]+) (\d*) (\w*) (\w*) (\d*) (.*) ([^ ]+)$)");
- // FMTLOG_INFO("\n-------------------\n");
- /* 匹配换行符,分割每一行 */
- std::regex reg2(R"(\n)");
- /* 匹配文件夹 */
- std::regex reg3(R"(^d.*)");
- /* 匹配文件 */
- std::regex reg4(R"(^-.*$)");
- /* -1 表示对正则表达式之前的子序列感兴趣
- * 0 表示对正则表达式本身感兴趣,输出的就是换行符 */
- std::sregex_token_iterator it2(str2.begin(), str2.end(), reg2, -1);
- /* 这里取出每一行 */
- for(; it2 != std::sregex_token_iterator(); ++it2)
- {
- /* 去掉多余的空格 */
- auto line = std::regex_replace(it2->str(), reg, " ");
- // FMTLOG_INFO("{}", line);
- CF_FileInfo fi;
- /* 取出文件类型 */
- std::string strFileType = std::regex_replace(line, reg1, "$1");
- if(std::regex_match(strFileType, reg3))
- {
- fi.type = CF_FileType::DIR;
- }else if (std::regex_match(strFileType, reg4))
- {
- fi.type = CF_FileType::FILE;
- }
- /* 取出文件大小 */
- std::string strFileSize = std::regex_replace(line, reg1, "$5");
- fi.size = std::stoull(strFileSize);
- /* 取出文件名 */
- std::string strFileName = std::regex_replace(line, reg1, "$7");
- fi.name = strFileName;
- /* 加入队列 */
- fileInfo.push_back(fi);
- }
- return true;
- }
- /* ==================================================================================
- * *********************************** 成员函数 *************************************
- * ================================================================================== */
- CurlFtp::CurlFtp()
- {
- /* 调用初始化函数,这个函数可以重复调用 */
- curl_global_init(CURL_GLOBAL_DEFAULT);
- /* 初始化curl */
- m_curl = curl_easy_init();
- hasInstace = true;
- }
- CurlFtp::~CurlFtp()
- {
- /* 清理curl */
- curl_easy_cleanup(m_curl);
- curl_global_cleanup();
- hasInstace = false;
- }
- /* 检查文件夹是否存在,不确定好不好用 */
- // bool CurlFtp::isDirExists(const std::string &ftpUrl, const std::string &username, const std::string &password)
- // {
- // CURL *curl;
- // CURLcode res;
- // bool result = false;
- // std::string retList;
- // curl_global_init(CURL_GLOBAL_DEFAULT);
- // curl = curl_easy_init();
- // if(curl)
- // {
- // curl_easy_setopt(curl, CURLOPT_URL, ftpUrl.c_str());
- // curl_easy_setopt(curl, CURLOPT_USERNAME, username.c_str());
- // curl_easy_setopt(curl, CURLOPT_PASSWORD, password.c_str());
- // // curl_easy_setopt(curl, CURLOPT_DIRLISTONLY, 1L); // We only want the directory listing
-
- // curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
- // curl_easy_setopt(curl, CURLOPT_WRITEDATA, &retList);
- // res = curl_easy_perform(curl);
- // // printf("res = %d\n", res);
- // if(res != CURLE_OK)
- // {
- // fprintf(stderr, "getDirList() failed, error code %d, :%s\n",res, curl_easy_strerror(res));
- // }
- // else
- // {
- // // std::cout << "Directory list: \n" << retList << std::endl;
- // }
- // curl_easy_cleanup(curl);
- // }
-
- // curl_global_cleanup();
-
- // return result;
- // }
- /* 使用正则表达式提取出文件夹 */
- // bool CurlFtp::extractDirectories(const std::string& responseString)
- // {
- // std::regex directoryRegex(R"(^d.*\s(\S+)$)");
- // std::smatch match;
- // std::istringstream retStream(responseString);
- // std::string line;
- // while (std::getline(retStream, line))
- // {
- // if (std::regex_match(line, match, directoryRegex))
- // {
- // std::cout << "Directory: " << match[1] << std::endl;
- // }
- // }
- // return true;
- // }
- /**
- * @brief 列出FTP文件夹
- *
- * @param ftpUrl 需要列出文件夹的FTP地址
- * @param username
- * @param password
- * @return std::string
- */
- std::string CurlFtp::listDir(const std::string &ftpUrl, const std::string &username, const std::string &password)
- {
- CURL *curl;
- CURLcode res;
- bool result = false;
- std::string retList;
- /* 1. 初始化curl,这个函数需要在调用任何curl函数之前 */
- curl_global_init(CURL_GLOBAL_DEFAULT);
- /* 2. 获取一个curl句柄 */
- curl = curl_easy_init();
- if(curl)
- {
- /* 3. 设置curl选项 */
- curl_easy_setopt(curl, CURLOPT_URL, ftpUrl.c_str());
- curl_easy_setopt(curl, CURLOPT_USERNAME, username.c_str());
- curl_easy_setopt(curl, CURLOPT_PASSWORD, password.c_str());
- // curl_easy_setopt(curl, CURLOPT_DIRLISTONLY, 1L); // We only want the directory listing
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeStringCallback);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &retList);
- /* 4. 发送信息,一直等待知道返回 */
- res = curl_easy_perform(curl);
- // printf("res = %d\n", res);
- if(res != CURLE_OK)
- {
- fprintf(stderr, "getDirList() failed, error code %d, :%s\n",res, curl_easy_strerror(res));
- } else
- {
- // std::cout << "Directory list: \n" << retList << std::endl;
- }
- /* 5. 清理curl */
- curl_easy_cleanup(curl);
- }
- if(hasInstace == false)
- {
- curl_global_cleanup();
- }
- return retList;
- }
- /* 列出文件夹中的所有文件 */
- bool CurlFtp::listFiles(const std::string &ftpUrl, const std::string &username, const std::string &password, std::vector<std::string>& fileList)
- {
- CURL *curl;
- CURLcode res;
- bool result = false;
- curl_global_init(CURL_GLOBAL_DEFAULT);
- curl = curl_easy_init();
- if(curl)
- {
- curl_easy_setopt(curl, CURLOPT_URL, (ftpUrl).c_str());
- curl_easy_setopt(curl, CURLOPT_USERNAME, username.c_str());
- curl_easy_setopt(curl, CURLOPT_PASSWORD, password.c_str());
- curl_easy_setopt(curl, CURLOPT_DIRLISTONLY, 1L);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeStringListCallback);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &fileList);
- res = curl_easy_perform(curl);
- if(res != CURLE_OK)
- {
- fprintf(stderr, "Failed to get file list, error code :%d ,%s\n", res, curl_easy_strerror(res));
- }
- else
- {
- // for(const std::string& filename : fileList)
- // {
- // printf("%s\n", filename.c_str());
- // }
- result = true;
- }
- curl_easy_cleanup(curl);
- }
- if(hasInstace == false)
- {
- curl_global_cleanup();
- }
- return result;
- }
- /* 创建文件夹 */
- bool CurlFtp::createDir(const std::string &ftpUrl, const std::string &username, const std::string &password, const std::string &dirName)
- {
- CURL *curl;
- CURLcode res;
- bool result = false;
- curl_global_init(CURL_GLOBAL_DEFAULT);
- curl = curl_easy_init();
- if(curl)
- {
- curl_easy_setopt(curl, CURLOPT_URL, ftpUrl.c_str());
- curl_easy_setopt(curl, CURLOPT_USERNAME, username.c_str());
- curl_easy_setopt(curl, CURLOPT_PASSWORD, password.c_str());
- // Create a list of FTP commands to be executed on the server
- struct curl_slist *headerlist = NULL;
- std::string mkdir = "MKD " + dirName;
- headerlist = curl_slist_append(headerlist, mkdir.c_str());
- // Set the list of FTP commands to be executed on the server before the transfer
- curl_easy_setopt(curl, CURLOPT_QUOTE, headerlist);
- // Tell libcurl to not include the body in the output
- curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
- res = curl_easy_perform(curl);
- if(res != CURLE_OK)
- {
- fprintf(stderr, "createDir() failed, error code :%d ,%s\n", res, curl_easy_strerror(res));
- result = false;
- }else
- {
- result = true;
- }
- curl_easy_cleanup(curl);
- curl_slist_free_all(headerlist);
- }
- if(hasInstace == false)
- {
- curl_global_cleanup();
- }
- return result;
- }
- /* 设置IP和端口 */
- bool CurlFtp::setFtpIPAndPort(const std::string& IP, const std::string& port)
- {
- /*先判断是否有ftp器前缀,通过正则表达式,取出IP地址和端口号,去掉前缀和后面的'/ */
- m_IP = IP;
- m_port = port;
- m_ftpUrl = "ftp://" + m_IP + ":" + m_port;
- printf("ftpUrl = %s\n", m_ftpUrl.c_str());
- return true;
- }
- /* 设置用户名和密码 */
- bool CurlFtp::setFtpUsernameAndPassword(const std::string& username, const std::string& password)
- {
- m_username = username;
- m_password = password;
- return true;
- }
- /* 列出文件列表 */
- bool CurlFtp::getFileList(std::string dir, std::vector<std::string>& fileList)
- {
- if(m_IP.empty() || m_port.empty())
- {
- FMTLOG_WARN("IP or port is empty");
- return false;
- }
- if(m_curl == nullptr)
- {
- FMTLOG_WARN("m_curl is nullptr");
- return false;
- }
- std::vector<CF_FileInfo> listInfo;
- listAll(dir, listInfo);
- // for(const CF_FileInfo& fi : listInfo)
- // {
- // FMTLOG_INFO("type = {}, size = {}, name = {}", (int)fi.type, fi.size, fi.name);
- // }
-
- return true;
- }
- /* 列出文件列表 */
- bool CurlFtp::listAll(std::string dir, std::vector<CF_FileInfo>& fileList)
- {
- if(m_IP.empty() || m_port.empty())
- {
- printf("IP or port is empty\n");
- return false;
- }
- if(m_curl == nullptr)
- {
- printf("m_curl is nullptr\n");
- return false;
- }
- bool result = false;
- std::string strSrc;
- /* 先设置FTP地址 */
- std::string ftpUrl = m_ftpUrl + dir;
- 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_DIRLISTONLY, 1L);
- /* 设置回调函数 */
- curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, writeStringCallback);
- /* 设置需要写入的容器,回调函数的第四个参数 */
- curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &strSrc);
- /* 发送请求 */
- m_res = curl_easy_perform(m_curl);
- if(m_res != CURLE_OK)
- {
- FMTLOG_ERROR("Failed to get file list, error code: {} ,{}", (int)m_res, curl_easy_strerror(m_res));
- return false;
- }
- /* 解析字符串 */
- parseFileInfo(strSrc, fileList);
- return true;
- }
|