CURLFTPTransport Class Reference

#include <curlftpt.h>

Inheritance diagram for CURLFTPTransport:
Inheritance graph
[legend]
Collaboration diagram for CURLFTPTransport:
Collaboration graph
[legend]

List of all members.

Public Member Functions

int copyDirectory (const char *urlPrefix, const char *dir, const char *dest, const char *suffix)
 CURLFTPTransport (const char *host, StatusReporter *statusReporter=0)
virtual std::vector< struct
DirEntry
getDirList (const char *dirURL)
virtual char getURL (const char *destPath, const char *sourceURL, SWBuf *destBuf=0)
void setPassive (bool passive)
void setPasswd (const char *passwd)
void setUser (const char *user)
void terminate ()
 ~CURLFTPTransport ()

Protected Attributes

SWBuf host
SWBuf p
bool passive
StatusReporterstatusReporter
bool term
SWBuf u

Private Attributes

CURL * session

Detailed Description

Definition at line 39 of file curlftpt.h.


Constructor & Destructor Documentation

CURLFTPTransport::CURLFTPTransport ( const char *  host,
StatusReporter statusReporter = 0 
)

Definition at line 120 of file curlftpt.cpp.

00120                                                                        : FTPTransport(host, sr) {
00121     session = (CURL *)curl_easy_init();
00122 }

CURLFTPTransport::~CURLFTPTransport (  ) 

Definition at line 125 of file curlftpt.cpp.

00125                                     {
00126     curl_easy_cleanup(session);
00127 }


Member Function Documentation

int FTPTransport::copyDirectory ( const char *  urlPrefix,
const char *  dir,
const char *  dest,
const char *  suffix 
) [inherited]

Definition at line 125 of file ftptrans.cpp.

00125                                                                                                             {
00126     unsigned int i;
00127     int retVal = 0;
00128     
00129     SWBuf url = SWBuf(urlPrefix) + SWBuf(dir);
00130     removeTrailingSlash(url);
00131     url += '/';
00132     
00133     SWLog::getSystemLog()->logWarning("NetTransport: getting dir %s\n", url.c_str());
00134     vector<struct DirEntry> dirList = getDirList(url.c_str());
00135 
00136     if (!dirList.size()) {
00137         SWLog::getSystemLog()->logWarning("NetTransport: failed to read dir %s\n", url.c_str());
00138         return -1;
00139     }
00140                 
00141     long totalBytes = 0;
00142     for (i = 0; i < dirList.size(); i++)
00143         totalBytes += dirList[i].size;
00144     long completedBytes = 0;
00145     for (i = 0; i < dirList.size(); i++) {
00146         struct DirEntry &dirEntry = dirList[i];
00147         SWBuf buffer = (SWBuf)dest;
00148         removeTrailingSlash(buffer);
00149         buffer += "/";
00150         buffer += dirEntry.name;
00151         if (!strcmp(&buffer.c_str()[buffer.length()-strlen(suffix)], suffix)) {
00152             SWBuf buffer2 = "Downloading (";
00153             buffer2.appendFormatted("%d", i+1);
00154             buffer2 += " of ";
00155             buffer2.appendFormatted("%d", dirList.size());
00156             buffer2 += "): ";
00157             buffer2 += dirEntry.name;
00158             if (statusReporter)
00159                 statusReporter->preStatus(totalBytes, completedBytes, buffer2.c_str());
00160             FileMgr::createParent(buffer.c_str());  // make sure parent directory exists
00161             SWTRY {
00162                 SWBuf url = (SWBuf)urlPrefix + (SWBuf)dir;
00163                 removeTrailingSlash(url);
00164                 url += "/";
00165                 url += dirEntry.name; //dont forget the final slash
00166                 if (!dirEntry.isDirectory) {
00167                     if (getURL(buffer.c_str(), url.c_str())) {
00168                         SWLog::getSystemLog()->logWarning("FTPCopy: failed to get file %s\n", url.c_str());
00169                         return -2;
00170                     }
00171                     completedBytes += dirEntry.size;
00172                 }
00173                 else {
00174                     SWBuf subdir = (SWBuf)dir;
00175                     removeTrailingSlash(subdir);
00176                     subdir += (SWBuf)"/" + dirEntry.name;
00177                     if (copyDirectory(urlPrefix, subdir, buffer.c_str(), suffix)) {
00178                         SWLog::getSystemLog()->logWarning("FTPCopy: failed to get file %s\n", subdir.c_str());
00179                         return -2;
00180                     }
00181                 }
00182             }
00183             SWCATCH (...) {}
00184             if (term) {
00185                 retVal = -3;
00186                 break;
00187             }
00188         }
00189     }
00190     return retVal;
00191 }

vector< struct DirEntry > FTPTransport::getDirList ( const char *  dirURL  )  [virtual, inherited]

Reimplemented in CURLHTTPTransport.

Definition at line 81 of file ftptrans.cpp.

00081                                                                    {
00082 
00083     vector<struct DirEntry> dirList;
00084     
00085     SWBuf dirBuf;
00086     if (!getURL("", dirURL, &dirBuf)) {
00087         char *start = dirBuf.getRawData();
00088         char *end = start;
00089         while (start < (dirBuf.getRawData()+dirBuf.size())) {
00090             struct ftpparse item;
00091             bool looking = true;
00092             for (end = start; *end; end++) {
00093                 if (looking) {
00094                     if ((*end == 10) || (*end == 13)) {
00095                         *end = 0;
00096                         looking = false;
00097                     }
00098                 }
00099                 else if ((*end != 10) && (*end != 13))
00100                     break;
00101             }
00102             SWLog::getSystemLog()->logWarning("FTPURLGetDir: parsing item %s(%d)\n", start, end-start);
00103             int status = ftpparse(&item, start, end - start);
00104             // in ftpparse.h, there is a warning that name is not necessarily null terminated
00105             SWBuf name;
00106             name.append(item.name, item.namelen);
00107             SWLog::getSystemLog()->logWarning("FTPURLGetDir: got item %s\n", name.c_str());
00108             if (status && name != "." && name != "..") {
00109                 struct DirEntry i;
00110                 i.name = name;
00111                 i.size = item.size;
00112                 i.isDirectory = (item.flagtrycwd == 1);
00113                 dirList.push_back(i);
00114             }
00115             start = end;
00116         }
00117     }
00118     else {
00119         SWLog::getSystemLog()->logWarning("FTPURLGetDir: failed to get dir %s\n", dirURL);
00120     }
00121     return dirList;
00122 }

char CURLFTPTransport::getURL ( const char *  destPath,
const char *  sourceURL,
SWBuf destBuf = 0 
) [virtual]

Reimplemented from FTPTransport.

Definition at line 130 of file curlftpt.cpp.

00130                                                                                          {
00131     signed char retVal = 0;
00132     struct FtpFile ftpfile = {destPath, 0, destBuf};
00133 
00134     CURLcode res;
00135     
00136     if (session) {
00137 
00138         struct MyProgressData pd;
00139         pd.sr = statusReporter;
00140         pd.term = &term;
00141 
00142         curl_easy_setopt(session, CURLOPT_URL, sourceURL);
00143     
00144         SWBuf credentials = u + ":" + p;
00145         curl_easy_setopt(session, CURLOPT_USERPWD, credentials.c_str());
00146         curl_easy_setopt(session, CURLOPT_WRITEFUNCTION, my_fwrite);
00147         if (!passive)
00148             curl_easy_setopt(session, CURLOPT_FTPPORT, "-");
00149         curl_easy_setopt(session, CURLOPT_NOPROGRESS, 0);
00150         curl_easy_setopt(session, CURLOPT_PROGRESSDATA, &pd);
00151         curl_easy_setopt(session, CURLOPT_PROGRESSFUNCTION, my_fprogress);
00152         curl_easy_setopt(session, CURLOPT_DEBUGFUNCTION, my_trace);
00153         /* Set a pointer to our struct to pass to the callback */
00154         curl_easy_setopt(session, CURLOPT_FILE, &ftpfile);
00155 
00156         /* Switch on full protocol/debug output */
00157         curl_easy_setopt(session, CURLOPT_VERBOSE, true);
00158         
00159         /* FTP connection settings */
00160 
00161 #if (LIBCURL_VERSION_MAJOR > 7) || \
00162    ((LIBCURL_VERSION_MAJOR == 7) && (LIBCURL_VERSION_MINOR > 10)) || \
00163    ((LIBCURL_VERSION_MAJOR == 7) && (LIBCURL_VERSION_MINOR == 10) && (LIBCURL_VERSION_PATCH >= 5))
00164 #      define EPRT_AVAILABLE 1
00165 #endif
00166 
00167 #ifdef EPRT_AVAILABLE
00168         curl_easy_setopt(session, CURLOPT_FTP_USE_EPRT, 0);
00169         SWLog::getSystemLog()->logDebug("***** using CURLOPT_FTP_USE_EPRT\n");
00170 #endif
00171 
00172         
00173         SWLog::getSystemLog()->logDebug("***** About to perform curl easy action. \n");
00174         SWLog::getSystemLog()->logDebug("***** destPath: %s \n", destPath);
00175         SWLog::getSystemLog()->logDebug("***** sourceURL: %s \n", sourceURL);
00176         res = curl_easy_perform(session);
00177         SWLog::getSystemLog()->logDebug("***** Finished performing curl easy action. \n");
00178 
00179         // it seems CURL tries to use this option data later for some reason, so we unset here
00180         curl_easy_setopt(session, CURLOPT_PROGRESSDATA, (void*)NULL);
00181 
00182         if(CURLE_OK != res) {
00183             retVal = -1;
00184         }
00185     }
00186 
00187     if (ftpfile.stream)
00188         fclose(ftpfile.stream); /* close the local file */
00189 
00190     return retVal;
00191 }

void FTPTransport::setPassive ( bool  passive  )  [inline, inherited]

Definition at line 75 of file ftptrans.h.

00075 { this->passive = passive; }

void FTPTransport::setPasswd ( const char *  passwd  )  [inline, inherited]

Definition at line 77 of file ftptrans.h.

00077 { p = passwd; }

void FTPTransport::setUser ( const char *  user  )  [inline, inherited]

Definition at line 76 of file ftptrans.h.

00076 { u = user; }

void FTPTransport::terminate (  )  [inline, inherited]

Definition at line 78 of file ftptrans.h.

00078 { term = true; }


Member Data Documentation

SWBuf FTPTransport::host [protected, inherited]

Definition at line 56 of file ftptrans.h.

SWBuf FTPTransport::p [protected, inherited]

Definition at line 58 of file ftptrans.h.

bool FTPTransport::passive [protected, inherited]

Definition at line 54 of file ftptrans.h.

CURL* CURLFTPTransport::session [private]

Definition at line 40 of file curlftpt.h.

Definition at line 53 of file ftptrans.h.

bool FTPTransport::term [protected, inherited]

Definition at line 55 of file ftptrans.h.

SWBuf FTPTransport::u [protected, inherited]

Definition at line 57 of file ftptrans.h.


The documentation for this class was generated from the following files:

Generated on 18 Mar 2013 for The SWORD Project by  doxygen 1.6.1