123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- #include "CurlFtp.h"
- #include <iostream>
- #include <regex>
- #include <iosfwd>
- CurlFtp::CurlFtp()
- {
- }
- CurlFtp::~CurlFtp()
- {
- }
-
-
-
- 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, writeCallback);
- 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);
- }
-
- curl_global_cleanup();
- return retList;
- }
- size_t CurlFtp::writeCallback(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 filelistCallback(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;
- }
- 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, filelistCallback);
- 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);
- }
- 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);
- }
- curl_global_cleanup();
- return result;
- }
|