-
-static size_t
-ftp_ls_data (void* buffer, size_t size, size_t nmemb, void* data)
-{
- string* s = reinterpret_cast<string *> (data);
- uint8_t* b = reinterpret_cast<uint8_t *> (buffer);
- for (size_t i = 0; i < (size * nmemb); ++i) {
- *s += b[i];
- }
- return size * nmemb;
-}
-
-list<string>
-ftp_ls (string url, bool pasv)
-{
- CURL* curl = curl_easy_init ();
- if (!curl) {
- throw NetworkError ("could not set up curl");
- }
-
- if (url.substr (url.length() - 1, 1) != "/") {
- url += "/";
- }
- curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
- /* 20s timeout */
- curl_easy_setopt (curl, CURLOPT_TIMEOUT, 20);
-
- string ls_raw;
- struct curl_slist* commands = 0;
- commands = curl_slist_append (commands, "NLST");
- curl_easy_setopt (curl, CURLOPT_POSTQUOTE, commands);
- curl_easy_setopt (curl, CURLOPT_WRITEDATA, &ls_raw);
- curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ftp_ls_data);
- curl_easy_setopt (curl, CURLOPT_FTP_USE_EPSV, 0);
- curl_easy_setopt (curl, CURLOPT_FTP_USE_EPRT, 0);
- curl_easy_setopt (curl, CURLOPT_VERBOSE, 1);
- if (!pasv) {
- curl_easy_setopt (curl, CURLOPT_FTPPORT, "-");
- }
- CURLcode const r = curl_easy_perform (curl);
- if (r != CURLE_OK) {
- curl_easy_cleanup (curl);
- throw NetworkError (curl_easy_strerror (r));
- }
-
- list<string> ls;
- string line;
- for (size_t i = 0; i < ls_raw.length(); ++i) {
- line += ls_raw[i];
- if (ls_raw[i] == '\n') {
- trim (line);
- if (line.length() > 55) {
- string const file = line.substr (55);
- if (file != "." && file != "..") {
- ls.push_back (file);
- }
- }
- line = "";
- }
- }
-
- curl_easy_cleanup (curl);
-
- return ls;
-}