The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CURLFTPTransport Class Reference

#include <curlftpt.h>

+ Inheritance diagram for CURLFTPTransport:
+ Collaboration diagram for CURLFTPTransport:

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)
 
long getTimeoutMillis ()
 
virtual char getURL (const char *destPath, const char *sourceURL, SWBuf *destBuf=0)
 
bool isPassive ()
 
bool isUnverifiedPeerAllowed ()
 
virtual char putURL (const char *destURL, const char *sourcePath, SWBuf *sourceBuf=0)
 
void setPassive (bool passive)
 
void setPasswd (const char *passwd)
 
void setTimeoutMillis (long timeoutMillis)
 
void setUnverifiedPeerAllowed (bool val)
 
void setUser (const char *user)
 
void terminate ()
 
 ~CURLFTPTransport ()
 

Protected Attributes

SWBuf host
 
SWBuf p
 
bool passive
 
StatusReporterstatusReporter
 
bool term
 
long timeoutMillis
 
SWBuf u
 
bool unverifiedPeerAllowed
 

Private Attributes

CURL * session
 

Detailed Description

Definition at line 34 of file curlftpt.h.

Constructor & Destructor Documentation

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

Definition at line 126 of file curlftpt.cpp.

126  : RemoteTransport(host, sr) {
127  session = (CURL *)curl_easy_init();
128 }
RemoteTransport(const char *host, StatusReporter *statusReporter=0)
Definition: remotetrans.cpp:62
CURL * session
Definition: curlftpt.h:35
CURLFTPTransport::~CURLFTPTransport ( )

Definition at line 131 of file curlftpt.cpp.

131  {
132  curl_easy_cleanup(session);
133 }
CURL * session
Definition: curlftpt.h:35

Member Function Documentation

int RemoteTransport::copyDirectory ( const char *  urlPrefix,
const char *  dir,
const char *  dest,
const char *  suffix 
)
inherited

network copy recursively a remote directly

Returns
error status 0: OK; -1: operation error, -2: connection error; -3: user requested termination

Definition at line 140 of file remotetrans.cpp.

140  {
141 SWLOGD("RemoteTransport::copyDirectory");
142  int retVal = 0;
143 
144  SWBuf url = SWBuf(urlPrefix) + SWBuf(dir);
145  removeTrailingSlash(url);
146  url += '/';
147 
148 SWLOGD("NetTransport: getting dir %s\n", url.c_str());
149  vector<struct DirEntry> dirList = getDirList(url.c_str());
150 
151  if (!dirList.size()) {
152  SWLog::getSystemLog()->logWarning("NetTransport: failed to read dir %s\n", url.c_str());
153  return -1;
154  }
155 
156  // append files in sub directories and calculate total download size
157  unsigned int i = 0;
158  long totalBytes = 0;
159  for (;;) {
160  if (i == dirList.size())
161  break;
162 
163  struct DirEntry &e = dirList.at(i);
164 
165  if (e.isDirectory) {
166  SWBuf name(e.name); // &e will be invalidated after first insertion
167  vector<struct DirEntry> sd = getDirList((url + name + '/').c_str());
168  for (unsigned int ii = 0; ii < sd.size(); ii++) {
169  sd[ii].name = name + '/' + sd[ii].name;
170  dirList.push_back(sd[ii]);
171  }
172  dirList.erase(dirList.begin() + i);
173  }
174  else {
175  totalBytes += e.size;
176  i++;
177  }
178  }
179 
180  long completedBytes = 0;
181  for (i = 0; i < dirList.size(); i++) {
182  struct DirEntry &dirEntry = dirList[i];
183  SWBuf buffer = (SWBuf)dest;
184  removeTrailingSlash(buffer);
185  buffer += "/";
186  buffer += dirEntry.name;
187  if (!strcmp(&buffer.c_str()[buffer.length()-strlen(suffix)], suffix)) {
188  SWBuf buffer2 = "Downloading (";
189  buffer2.appendFormatted("%d", i+1);
190  buffer2 += " of ";
191  buffer2.appendFormatted("%d", dirList.size());
192  buffer2 += "): ";
193  buffer2 += dirEntry.name;
194  if (statusReporter)
195  statusReporter->preStatus(totalBytes, completedBytes, buffer2.c_str());
196  FileMgr::createParent(buffer.c_str()); // make sure parent directory exists
197  SWTRY {
198  SWBuf url = (SWBuf)urlPrefix + (SWBuf)dir;
199  removeTrailingSlash(url);
200  url += "/";
201  url += dirEntry.name;
202  retVal = getURL(buffer.c_str(), url.c_str());
203  if (retVal) {
204  SWLog::getSystemLog()->logWarning("copyDirectory: failed to get file %s\n", url.c_str());
205  return retVal;
206  }
207  completedBytes += dirEntry.size;
208  }
209  SWCATCH (...) {}
210  if (term) {
211  retVal = -3;
212  break;
213  }
214  }
215  }
216  return retVal;
217 }
bool isDirectory
Definition: filemgr.h:43
SWBuf & appendFormatted(const char *format,...)
Definition: swbuf.cpp:81
Definition: swbuf.h:47
unsigned long length() const
Definition: swbuf.h:197
unsigned long size
Definition: filemgr.h:42
static SWLog * getSystemLog()
Definition: swlog.cpp:53
virtual char getURL(const char *destPath, const char *sourceURL, SWBuf *destBuf=0)
Definition: remotetrans.cpp:78
preg buffer
Definition: regex.c:8089
#define SWTRY
Definition: defs.h:57
#define SWCATCH(x)
Definition: defs.h:58
const char * c_str() const
Definition: swbuf.h:158
SWBuf name
Definition: filemgr.h:41
virtual void preStatus(long totalBytes, long completedBytes, const char *message)
Definition: remotetrans.cpp:54
static void removeTrailingSlash(SWBuf &buf)
Definition: installmgr.cpp:57
static int createParent(const char *pName)
Definition: filemgr.cpp:426
void logWarning(const char *fmt,...) const
Definition: swlog.cpp:74
virtual std::vector< struct DirEntry > getDirList(const char *dirURL)
Definition: remotetrans.cpp:92
#define SWLOGD(...)
Definition: defs.h:187
StatusReporter * statusReporter
Definition: remotetrans.h:59
vector< struct DirEntry > RemoteTransport::getDirList ( const char *  dirURL)
virtualinherited

Reimplemented in CURLHTTPTransport.

Definition at line 92 of file remotetrans.cpp.

92  {
93 
94 SWLOGD("RemoteTransport::getDirList(%s)", dirURL);
95  vector<struct DirEntry> dirList;
96 
97  SWBuf dirBuf;
98  if (!getURL("", dirURL, &dirBuf)) {
99  char *start = dirBuf.getRawData();
100  char *end = start;
101  while (start < (dirBuf.getRawData()+dirBuf.size())) {
102  struct ftpparse item;
103  bool looking = true;
104  for (end = start; *end; end++) {
105  if (looking) {
106  if ((*end == 10) || (*end == 13)) {
107  *end = 0;
108  looking = false;
109  }
110  }
111  else if ((*end != 10) && (*end != 13))
112  break;
113  }
114 SWLOGD("getDirList: parsing item %s(%d)\n", start, end-start);
115  int status = ftpparse(&item, start, (int)(end - start));
116  // in ftpparse.h, there is a warning that name is not necessarily null terminated
117  SWBuf name;
118  name.append(item.name, item.namelen);
119 SWLOGD("getDirList: got item %s\n", name.c_str());
120  if (status && name != "." && name != "..") {
121  struct DirEntry i;
122  i.name = name;
123  i.size = item.size;
124  i.isDirectory = (item.flagtrycwd == 1);
125  dirList.push_back(i);
126  }
127  start = end;
128  }
129  }
130  else {
131  SWLog::getSystemLog()->logWarning("getDirList: failed to get dir %s\n", dirURL);
132  }
133  return dirList;
134 }
Definition: swbuf.h:47
static SWLog * getSystemLog()
Definition: swlog.cpp:53
char * name
Definition: ftpparse.h:21
virtual char getURL(const char *destPath, const char *sourceURL, SWBuf *destBuf=0)
Definition: remotetrans.cpp:78
char * getRawData()
Definition: swbuf.h:379
const char * c_str() const
Definition: swbuf.h:158
SWBuf & append(const char *str, long max=-1)
Definition: swbuf.h:274
SWBuf name
Definition: filemgr.h:41
unsigned long size() const
Definition: swbuf.h:185
void logWarning(const char *fmt,...) const
Definition: swlog.cpp:74
int ftpparse(struct ftpparse *fp, char *buf, int len)
Definition: ftpparse.c:144
#define SWLOGD(...)
Definition: defs.h:187
long RemoteTransport::getTimeoutMillis ( )
inlineinherited

Definition at line 94 of file remotetrans.h.

94 { return timeoutMillis; }
char CURLFTPTransport::getURL ( const char *  destPath,
const char *  sourceURL,
SWBuf destBuf = 0 
)
virtual

Reimplemented from RemoteTransport.

Definition at line 136 of file curlftpt.cpp.

136  {
137  signed char retVal = 0;
138  struct FtpFile ftpfile = {destPath, 0, destBuf};
139 
140  CURLcode res;
141 
142  if (session) {
143 
144  struct MyProgressData pd;
145  pd.sr = statusReporter;
146  pd.term = &term;
147 
148  curl_easy_setopt(session, CURLOPT_URL, sourceURL);
149 
150  SWBuf credentials = u + ":" + p;
151  curl_easy_setopt(session, CURLOPT_USERPWD, credentials.c_str());
152  curl_easy_setopt(session, CURLOPT_WRITEFUNCTION, my_fwrite);
153  if (!passive)
154  curl_easy_setopt(session, CURLOPT_FTPPORT, "-");
155  curl_easy_setopt(session, CURLOPT_NOPROGRESS, 0);
156  curl_easy_setopt(session, CURLOPT_PROGRESSDATA, &pd);
157  curl_easy_setopt(session, CURLOPT_PROGRESSFUNCTION, my_fprogress);
158 
159 
160  curl_easy_setopt(session, CURLOPT_DEBUGFUNCTION, my_trace);
161  /* Set a pointer to our struct to pass to the callback */
162  curl_easy_setopt(session, CURLOPT_FILE, &ftpfile);
163 
164  /* Switch on full protocol/debug output */
165  curl_easy_setopt(session, CURLOPT_VERBOSE, true);
166 #ifndef OLDCURL
167  curl_easy_setopt(session, CURLOPT_CONNECTTIMEOUT_MS, timeoutMillis);
168  curl_easy_setopt(session, CURLOPT_TIMEOUT_MS, timeoutMillis);
169 #else
170  curl_easy_setopt(session, CURLOPT_CONNECTTIMEOUT, timeoutMillis/1000);
171  curl_easy_setopt(session, CURLOPT_TIMEOUT, timeoutMillis/1000);
172 #endif
173  /* FTP connection settings */
174 
175 #if (LIBCURL_VERSION_MAJOR > 7) || \
176  ((LIBCURL_VERSION_MAJOR == 7) && (LIBCURL_VERSION_MINOR > 10)) || \
177  ((LIBCURL_VERSION_MAJOR == 7) && (LIBCURL_VERSION_MINOR == 10) && (LIBCURL_VERSION_PATCH >= 5))
178 # define EPRT_AVAILABLE 1
179 #endif
180 
181 #ifdef EPRT_AVAILABLE
182  curl_easy_setopt(session, CURLOPT_FTP_USE_EPRT, 0);
183 SWLOGD("***** using CURLOPT_FTP_USE_EPRT\n");
184 #endif
185 
186 
187 SWLOGD("***** About to perform curl easy action. \n");
188 SWLOGD("***** destPath: %s \n", destPath);
189 SWLOGD("***** sourceURL: %s \n", sourceURL);
190  res = curl_easy_perform(session);
191 SWLOGD("***** Finished performing curl easy action. \n");
192 
193  // it seems CURL tries to use this option data later for some reason, so we unset here
194  curl_easy_setopt(session, CURLOPT_PROGRESSDATA, (void*)NULL);
195 
196  if (CURLE_OK != res) {
197  if (CURLE_OPERATION_TIMEDOUT == res
198 // older CURL doesn't define this
199 #ifdef CURLE_FTP_ACCEPT_TIMEOUT
200  || CURLE_FTP_ACCEPT_TIMEOUT == res
201 #endif
202  ) {
203  retVal = -2;
204  }
205  else {
206  retVal = -1;
207  }
208  }
209  }
210 
211  if (ftpfile.fd > 0)
212  FileMgr::closeFile(ftpfile.fd); /* close the local file */
213 
214  return retVal;
215 }
Definition: swbuf.h:47
static int my_trace(CURL *handle, curl_infotype type, unsigned char *data, size_t size, void *userp)
Definition: curlftpt.cpp:97
static void closeFile(int fd)
Definition: filemgr.cpp:491
static int my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
Definition: curlftpt.cpp:57
static int my_fprogress(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
Definition: curlftpt.cpp:81
return NULL
Definition: regex.c:7953
const char * c_str() const
Definition: swbuf.h:158
#define SWLOGD(...)
Definition: defs.h:187
StatusReporter * statusReporter
Definition: remotetrans.h:59
CURL * session
Definition: curlftpt.h:35
bool RemoteTransport::isPassive ( )
inlineinherited

Definition at line 95 of file remotetrans.h.

95 { return passive; }
bool RemoteTransport::isUnverifiedPeerAllowed ( )
inlineinherited

Definition at line 99 of file remotetrans.h.

99 { return unverifiedPeerAllowed; }
bool unverifiedPeerAllowed
Definition: remotetrans.h:63
char RemoteTransport::putURL ( const char *  destURL,
const char *  sourcePath,
SWBuf sourceBuf = 0 
)
virtualinherited

Reimplemented in RTransportGDrive.

Definition at line 85 of file remotetrans.cpp.

85  {
86  SWLog::getSystemLog()->logWarning("RemoteTransport::putURL called but unsupported");
87  char retVal = -1;
88  return retVal;
89 }
static SWLog * getSystemLog()
Definition: swlog.cpp:53
void logWarning(const char *fmt,...) const
Definition: swlog.cpp:74
void RemoteTransport::setPassive ( bool  passive)
inlineinherited

Definition at line 92 of file remotetrans.h.

92 { this->passive = passive; }
void RemoteTransport::setPasswd ( const char *  passwd)
inlineinherited

Definition at line 97 of file remotetrans.h.

97 { p = passwd; }
void RemoteTransport::setTimeoutMillis ( long  timeoutMillis)
inlineinherited

Definition at line 93 of file remotetrans.h.

93 { this->timeoutMillis = timeoutMillis; }
void RemoteTransport::setUnverifiedPeerAllowed ( bool  val)
inlineinherited

Definition at line 98 of file remotetrans.h.

98 { this->unverifiedPeerAllowed = val; }
bool unverifiedPeerAllowed
Definition: remotetrans.h:63
void RemoteTransport::setUser ( const char *  user)
inlineinherited

Definition at line 96 of file remotetrans.h.

96 { u = user; }
void RemoteTransport::terminate ( )
inlineinherited

Definition at line 100 of file remotetrans.h.

100 { term = true; }

Member Data Documentation

SWBuf RemoteTransport::host
protectedinherited

Definition at line 64 of file remotetrans.h.

SWBuf RemoteTransport::p
protectedinherited

Definition at line 66 of file remotetrans.h.

bool RemoteTransport::passive
protectedinherited

Definition at line 60 of file remotetrans.h.

CURL* CURLFTPTransport::session
private

Definition at line 35 of file curlftpt.h.

StatusReporter* RemoteTransport::statusReporter
protectedinherited

Definition at line 59 of file remotetrans.h.

bool RemoteTransport::term
protectedinherited

Definition at line 62 of file remotetrans.h.

long RemoteTransport::timeoutMillis
protectedinherited

Definition at line 61 of file remotetrans.h.

SWBuf RemoteTransport::u
protectedinherited

Definition at line 65 of file remotetrans.h.

bool RemoteTransport::unverifiedPeerAllowed
protectedinherited

Definition at line 63 of file remotetrans.h.


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