summaryrefslogtreecommitdiff
path: root/src/mgr/remotetrans.cpp
blob: 03ad3e443622eccbe1a7279aa27e4a837e3d731b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*****************************************************************************
 *
 *  remotetrans.cpp -	
 *
 * $Id: remotetrans.cpp 2980 2013-09-14 21:51:47Z scribe $
 *
 * Copyright 2004-2013 CrossWire Bible Society (http://www.crosswire.org)
 *	CrossWire Bible Society
 *	P. O. Box 2528
 *	Tempe, AZ  85280-2528
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation version 2.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 */

#include <remotetrans.h>
#include <filemgr.h>

#include <fcntl.h>
#include <dirent.h>
#include <swlog.h>


extern "C" {
#include <ftpparse.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) {
}


RemoteTransport::RemoteTransport(const char *host, StatusReporter *statusReporter) {
	this->statusReporter = statusReporter;
	this->host = host;
	u = "ftp";
	p = "installmgr@user.com";
	term = false;
}


RemoteTransport::~RemoteTransport() {
}


// override this method in your real transport class
char RemoteTransport::getURL(const char *destPath, const char *sourceURL, SWBuf *destBuf) {
	char retVal = 0;
	return retVal;
}


vector<struct DirEntry> RemoteTransport::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("getDirList: parsing item %s(%d)\n", start, end-start);
			int status = ftpparse(&item, start, end - start);
			// in ftpparse.h, there is a warning that name is not necessarily null terminated
			SWBuf name;
			name.append(item.name, item.namelen);
			SWLog::getSystemLog()->logWarning("getDirList: got item %s\n", name.c_str());
			if (status && name != "." && name != "..") {
				struct DirEntry i;
				i.name = name;
				i.size = item.size;
				i.isDirectory = (item.flagtrycwd == 1);
				dirList.push_back(i);
			}
			start = end;
		}
	}
	else {
		SWLog::getSystemLog()->logWarning("getDirList: failed to get dir %s\n", dirURL);
	}
	return dirList;
}


int RemoteTransport::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("NetTransport: getting dir %s\n", url.c_str());
	vector<struct DirEntry> dirList = getDirList(url.c_str());

	if (!dirList.size()) {
		SWLog::getSystemLog()->logWarning("NetTransport: 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("copyDirectory: 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("copyDirectory: failed to get file %s\n", subdir.c_str());
						return -2;
					}
				}
			}
			SWCATCH (...) {}
			if (term) {
				retVal = -3;
				break;
			}
		}
	}
	return retVal;
}


#if defined(__GNUC__)
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
void StatusReporter::update(unsigned long totalBytes, unsigned long completedBytes) {
	statusUpdate(totalBytes, completedBytes);
}


SWORD_NAMESPACE_END