123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471 |
- #include "CurlFtp.h"
- #include <iostream>
- #include <regex>
- #include <iosfwd>
- #include "fmtlog.h"
- #include "CurlFtpInfo.h"
- #if defined(_WIN32)
- #endif
- static bool hasInstace = false;
- 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)
- {
-
- return 0;
- }
- std::copy_n((char*)contents, newLength, userStr->begin() + oldLength);
- 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;
- }
- #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
- 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
-
-
-
- std::regex reg(R"(( )+)");
-
- std::regex reg1(R"(^([^ ]+) (\d*) (\w*) (\w*) (\d*) (.*) ([^ ]+)$)");
-
-
- std::regex reg2(R"(\n)");
-
- std::regex reg3(R"(^d.*)");
-
- std::regex reg4(R"(^-.*$)");
-
- 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, " ");
-
- 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);
-
- m_curl = curl_easy_init();
- hasInstace = true;
- }
- CurlFtp::~CurlFtp()
- {
-
- curl_easy_cleanup(m_curl);
- curl_global_cleanup();
- hasInstace = false;
- }
-
-
-
- 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;
-
- 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_WRITEFUNCTION, writeStringCallback);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &retList);
-
- res = curl_easy_perform(curl);
-
- if(res != CURLE_OK)
- {
- fprintf(stderr, "getDirList() failed, error code %d, :%s\n",res, curl_easy_strerror(res));
- } else
- {
-
- }
-
- 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
- {
-
-
-
-
- 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());
-
- struct curl_slist *headerlist = NULL;
- std::string mkdir = "MKD " + dirName;
- headerlist = curl_slist_append(headerlist, mkdir.c_str());
-
- curl_easy_setopt(curl, CURLOPT_QUOTE, headerlist);
-
- 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;
- }
- bool CurlFtp::setFtpIPAndPort(const std::string& IP, const std::string& port)
- {
-
- 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);
-
-
-
-
-
- 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;
-
- 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_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;
- }
|