123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- #include "CurlFtp.h"
- #include <iostream>
- #include <regex>
- #include <iosfwd>
- static bool hasInstace = false;
- static size_t writeStringCallback(void *contents, size_t size, size_t nmemb, std::string *s)
- {
- size_t newLength = size*nmemb;
- size_t oldLength = s->size();
- try
- {
- s->resize(oldLength + newLength);
- }
- catch(std::bad_alloc &e)
- {
-
- return 0;
- }
- std::copy((char*)contents,(char*)contents+newLength,s->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;
- }
- 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::listAll(std::string dir, std::vector<std::string>& 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 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, writeStringListCallback);
-
- curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &fileList);
-
- m_res = curl_easy_perform(m_curl);
- if(m_res != CURLE_OK)
- {
- fprintf(stderr, "Failed to get file list, error code :%d ,%s\n", m_res, curl_easy_strerror(m_res));
- result = false;
- } else
- {
- result = true;
- }
-
- return result;
- }
|