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

#include <rtranspgdrive.h>

+ Inheritance diagram for RTransportGDrive:
+ Collaboration diagram for RTransportGDrive:

Public Member Functions

int copyDirectory (const char *urlPrefix, const char *dir, const char *dest, const char *suffix)
 
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)
 
 RTransportGDrive (const char *host, StatusReporter *statusReporter=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 ()
 
 ~RTransportGDrive ()
 

Protected Attributes

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

Detailed Description

Definition at line 32 of file rtranspgdrive.h.

Constructor & Destructor Documentation

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

Definition at line 93 of file rtranspgdrive.cpp.

93  : RemoteTransport(host, sr) {
94  // session open
95 }
RemoteTransport(const char *host, StatusReporter *statusReporter=0)
Definition: remotetrans.cpp:62
RTransportGDrive::~RTransportGDrive ( )

Definition at line 98 of file rtranspgdrive.cpp.

98  {
99  // session cleanup
100 }

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 RTransportGDrive::getURL ( const char *  destPath,
const char *  sourceURL,
SWBuf destBuf = 0 
)
virtual

Reimplemented from RemoteTransport.

Definition at line 106 of file rtranspgdrive.cpp.

106  {
107  signed char retVal = 0;
108  struct FtpFile ftpfile = {destPath, 0, destBuf};
109 #if 0
110 
111  if (session) {
112 
113  CURLcode res;
114 
115  struct MyProgressData pd;
116  pd.sr = statusReporter;
117  pd.term = &term;
118 
119  curl_easy_setopt(session, CURLOPT_URL, sourceURL);
120 
121  SWBuf credentials = u + ":" + p;
122  curl_easy_setopt(session, CURLOPT_USERPWD, credentials.c_str());
123  curl_easy_setopt(session, CURLOPT_WRITEFUNCTION, my_fwrite);
124  if (!passive)
125  curl_easy_setopt(session, CURLOPT_FTPPORT, "-");
126  curl_easy_setopt(session, CURLOPT_NOPROGRESS, 0);
127  curl_easy_setopt(session, CURLOPT_PROGRESSDATA, &pd);
128  curl_easy_setopt(session, CURLOPT_PROGRESSFUNCTION, my_fprogress);
129  curl_easy_setopt(session, CURLOPT_DEBUGFUNCTION, my_trace);
130  /* Set a pointer to our struct to pass to the callback */
131  curl_easy_setopt(session, CURLOPT_FILE, &ftpfile);
132 
133  /* Switch on full protocol/debug output */
134  curl_easy_setopt(session, CURLOPT_VERBOSE, true);
135  curl_easy_setopt(session, CURLOPT_CONNECTTIMEOUT, 45);
136 
137  /* FTP connection settings */
138 
139 #if (LIBCURL_VERSION_MAJOR > 7) || \
140  ((LIBCURL_VERSION_MAJOR == 7) && (LIBCURL_VERSION_MINOR > 10)) || \
141  ((LIBCURL_VERSION_MAJOR == 7) && (LIBCURL_VERSION_MINOR == 10) && (LIBCURL_VERSION_PATCH >= 5))
142 # define EPRT_AVAILABLE 1
143 #endif
144 
145 #ifdef EPRT_AVAILABLE
146  curl_easy_setopt(session, CURLOPT_FTP_USE_EPRT, 0);
147 SWLOGD("***** using CURLOPT_FTP_USE_EPRT\n");
148 #endif
149 
150 
151 SWLOGD("***** About to perform curl easy action. \n");
152 SWLOGD("***** destPath: %s \n", destPath);
153 SWLOGD("***** sourceURL: %s \n", sourceURL);
154  res = curl_easy_perform(session);
155 SWLOGD("***** Finished performing curl easy action. \n");
156 
157  // it seems CURL tries to use this option data later for some reason, so we unset here
158  curl_easy_setopt(session, CURLOPT_PROGRESSDATA, (void*)NULL);
159 
160  if(CURLE_OK != res) {
161  retVal = -1;
162  }
163  }
164 #endif
165 
166  if (ftpfile.stream)
167  fclose(ftpfile.stream); /* close the local file */
168 
169  return retVal;
170 }
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 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
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 RTransportGDrive::putURL ( const char *  destURL,
const char *  sourcePath,
SWBuf sourceBuf = 0 
)
virtual

Reimplemented from RemoteTransport.

Definition at line 103 of file rtranspgdrive.cpp.

103  {
104  return RemoteTransport::putURL(destURL, sourcePath, sourceBuf);
105 }
virtual char putURL(const char *destURL, const char *sourcePath, SWBuf *sourceBuf=0)
Definition: remotetrans.cpp:85
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.

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: