summaryrefslogtreecommitdiff
path: root/src/mgr/ftptrans.cpp
diff options
context:
space:
mode:
authorRoberto C. Sanchez <roberto@connexer.com>2014-03-29 10:53:59 -0400
committerRoberto C. Sanchez <roberto@connexer.com>2014-03-29 10:53:59 -0400
commit03134fa5f6f25d92724ce4c183f9bbe12a9e37dc (patch)
tree847326a4de82f0241ac87cbbc427a1b92a696a02 /src/mgr/ftptrans.cpp
parentd7469385b05b9510338407fa123e9ad090f80af6 (diff)
Imported Upstream version 1.5.11
Diffstat (limited to 'src/mgr/ftptrans.cpp')
-rw-r--r--src/mgr/ftptrans.cpp170
1 files changed, 170 insertions, 0 deletions
diff --git a/src/mgr/ftptrans.cpp b/src/mgr/ftptrans.cpp
new file mode 100644
index 0000000..ab0a605
--- /dev/null
+++ b/src/mgr/ftptrans.cpp
@@ -0,0 +1,170 @@
+ /*****************************************************************************
+ * FTPTransport functions
+ *
+ */
+
+
+#include <ftptrans.h>
+#include <filemgr.h>
+
+#include <fcntl.h>
+#include <dirent.h>
+#include <swlog.h>
+
+
+using std::vector;
+
+
+SWORD_NAMESPACE_START
+
+
+namespace {
+
+void removeTrailingSlash(SWBuf &buf) {
+ int len = buf.size();
+ if ((buf[len-1] == '/')
+ || (buf[len-1] == '\\'))
+ buf.size(len-1);
+}
+
+};
+
+
+void StatusReporter::preStatus(long totalBytes, long completedBytes, const char *message) {
+}
+
+void StatusReporter::statusUpdate(double dtTotal, double dlNow) {
+}
+
+
+FTPTransport::FTPTransport(const char *host, StatusReporter *statusReporter) {
+ this->statusReporter = statusReporter;
+ this->host = host;
+ term = false;
+}
+
+
+FTPTransport::~FTPTransport() {
+}
+
+
+// override this method in your real transport class
+char FTPTransport::getURL(const char *destPath, const char *sourceURL, SWBuf *destBuf) {
+ char retVal = 0;
+ return retVal;
+}
+
+
+vector<struct DirEntry> FTPTransport::getDirList(const char *dirURL) {
+
+ vector<struct DirEntry> dirList;
+
+ SWBuf dirBuf;
+ if (!getURL("", dirURL, &dirBuf)) {
+ char *start = dirBuf.getRawData();
+ char *end = start;
+ while (start < (dirBuf.getRawData()+dirBuf.size())) {
+ struct ftpparse item;
+ bool looking = true;
+ for (end = start; *end; end++) {
+ if (looking) {
+ if ((*end == 10) || (*end == 13)) {
+ *end = 0;
+ looking = false;
+ }
+ }
+ else if ((*end != 10) && (*end != 13))
+ break;
+ }
+ SWLog::getSystemLog()->logWarning("FTPURLGetDir: parsing item %s(%d)\n", start, end-start);
+ int status = ftpparse(&item, start, end - start);
+ SWLog::getSystemLog()->logWarning("FTPURLGetDir: got item %s\n", item.name);
+ if (status) {
+ struct DirEntry i;
+ i.name = item.name;
+ i.size = item.size;
+ i.isDirectory = (item.flagtrycwd == 1);
+ dirList.push_back(i);
+ }
+ start = end;
+ }
+ }
+ else
+ {
+ SWLog::getSystemLog()->logWarning("FTPURLGetDir: failed to get dir %s\n", dirURL);
+ }
+ return dirList;
+}
+
+
+int FTPTransport::copyDirectory(const char *urlPrefix, const char *dir, const char *dest, const char *suffix) {
+ unsigned int i;
+ int retVal = 0;
+
+ SWBuf url = SWBuf(urlPrefix) + SWBuf(dir);
+ removeTrailingSlash(url);
+ url += '/';
+
+ SWLog::getSystemLog()->logWarning("FTPCopy: getting dir %s\n", url.c_str());
+ vector<struct DirEntry> dirList = getDirList(url.c_str());
+
+ if (!dirList.size()) {
+ SWLog::getSystemLog()->logWarning("FTPCopy: failed to read dir %s\n", url.c_str());
+ return -1;
+ }
+
+ long totalBytes = 0;
+ for (i = 0; i < dirList.size(); i++)
+ totalBytes += dirList[i].size;
+ long completedBytes = 0;
+ for (i = 0; i < dirList.size(); i++) {
+ struct DirEntry &dirEntry = dirList[i];
+ SWBuf buffer = (SWBuf)dest;
+ removeTrailingSlash(buffer);
+ buffer += "/";
+ buffer += dirEntry.name;
+ if (!strcmp(&buffer.c_str()[buffer.length()-strlen(suffix)], suffix)) {
+ SWBuf buffer2 = "Downloading (";
+ buffer2.appendFormatted("%d", i+1);
+ buffer2 += " of ";
+ buffer2.appendFormatted("%d", dirList.size());
+ buffer2 += "): ";
+ buffer2 += dirEntry.name;
+ if (statusReporter)
+ statusReporter->preStatus(totalBytes, completedBytes, buffer2.c_str());
+ FileMgr::createParent(buffer.c_str()); // make sure parent directory exists
+ SWTRY {
+ SWBuf url = (SWBuf)urlPrefix + (SWBuf)dir;
+ removeTrailingSlash(url);
+ url += "/";
+ url += dirEntry.name; //dont forget the final slash
+ if (!dirEntry.isDirectory) {
+ if (getURL(buffer.c_str(), url.c_str())) {
+ SWLog::getSystemLog()->logWarning("FTPCopy: failed to get file %s\n", url.c_str());
+ return -2;
+ }
+ completedBytes += dirEntry.size;
+ }
+ else {
+ SWBuf subdir = (SWBuf)dir;
+ removeTrailingSlash(subdir);
+ subdir += (SWBuf)"/" + dirEntry.name;
+ if (copyDirectory(urlPrefix, subdir, buffer.c_str(), suffix)) {
+ SWLog::getSystemLog()->logWarning("FTPCopy: failed to get file %s\n", subdir.c_str());
+ return -2;
+ }
+ }
+ }
+ SWCATCH (...) {}
+ if (term) {
+ retVal = -3;
+ break;
+ }
+ }
+ }
+ return retVal;
+}
+
+
+SWORD_NAMESPACE_END
+