58
83
char FTPLibFTPTransport::assureLoggedIn() {
60
85
if (ftpConnection == 0) {
61
SWLog::getSystemLog()->logDebug("connecting to host %s\n", host.c_str());
62
if (FtpConnect(host, &ftpConnection))
86
SWLog::getSystemLog()->logDebug("connecting to host: %s...\n", host.c_str());
87
if (FtpConnect(host, &ftpConnection)) {
88
SWLog::getSystemLog()->logDebug("connected. logging in...\n");
63
89
if (FtpLogin(u.c_str(), p.c_str(), ftpConnection)) {
90
SWLog::getSystemLog()->logDebug("logged in.\n");
67
94
SWLog::getSystemLog()->logError("Failed to login to %s\n", host.c_str());
71
99
SWLog::getSystemLog()->logError("Failed to connect to %s\n", host.c_str());
106
// yeah yeah, I know I know. Compile with curl support if you don't like it
107
#pragma GCC diagnostic ignored "-Wall"
108
void my_tmpnam(char *tmpName) {
109
tmpName = tmpnam(tmpName);
111
#pragma GCC diagnostic warning "-Wall"
79
115
char FTPLibFTPTransport::getURL(const char *destPath, const char *sourceURL, SWBuf *destBuf) {
119
SWLog::getSystemLog()->logDebug("FTPLibFTPTransport::getURL(%s, %s, ...);\n", (destPath)?destPath:"(null)", sourceURL);
83
120
// assert we can login
84
121
retVal = assureLoggedIn();
85
122
if (retVal) return retVal;
123
SWLog::getSystemLog()->logDebug("FTPLibFTPTransport - logged in.\n");
87
125
SWBuf sourcePath = sourceURL;
88
SWBuf outFile = (!destBuf) ? destPath : "swftplib.tmp";
133
outFile = "/sdcard/sword/InstallMgr/swtmpbuf.out";
89
141
sourcePath << (6 + host.length()); // shift << "ftp://hostname";
90
142
SWLog::getSystemLog()->logDebug("getting file %s to %s\n", sourcePath.c_str(), outFile.c_str());
92
144
FtpOptions(FTPLIB_CONNMODE, FTPLIB_PASSIVE, ftpConnection);
94
146
FtpOptions(FTPLIB_CONNMODE, FTPLIB_PORT, ftpConnection);
148
struct MyProgressData pd;
149
pd.sr = statusReporter;
95
153
// !!!WDG also want to set callback options
154
FtpOptions(FTPLIB_CALLBACK, (long)&my_fprogress, ftpConnection);
155
FtpOptions(FTPLIB_CALLBACKARG, (long)&pd, ftpConnection);
156
FtpOptions(FTPLIB_CALLBACKBYTES, (long)2048, ftpConnection);
96
158
if (sourcePath.endsWith("/") || sourcePath.endsWith("\\")) {
97
SWLog::getSystemLog()->logDebug("getting test directory %s\n", sourcePath.c_str());
98
FtpDir(NULL, sourcePath, ftpConnection);
159
// SWLog::getSystemLog()->logDebug("getting test directory %s\n", sourcePath.c_str());
160
// FtpDir(NULL, sourcePath, ftpConnection);
99
161
SWLog::getSystemLog()->logDebug("getting real directory %s\n", sourcePath.c_str());
100
162
retVal = FtpDir(outFile.c_str(), sourcePath, ftpConnection) - 1;
163
SWLog::getSystemLog()->logDebug("got real directory %s to %s\n", sourcePath.c_str(), outFile.c_str());
103
166
SWLog::getSystemLog()->logDebug("getting file %s\n", sourcePath.c_str());
168
FtpSize(sourcePath, &size, FTPLIB_IMAGE, ftpConnection);
104
170
retVal = FtpGet(outFile.c_str(), sourcePath, FTPLIB_IMAGE, ftpConnection) - 1;
107
173
// Is there a way to FTPGet directly to a buffer?
108
174
// If not, we probably want to add x-platform way to open a tmp file with FileMgr
109
// This wreaks and will easily fail if a user's CWD is not writable.
175
// Currently outFile is set to tmpFile above sortof unsafe
111
FileDesc *fd = FileMgr::getSystemFileMgr()->open("swftplib.tmp", FileMgr::RDONLY);
177
SWLog::getSystemLog()->logDebug("filling destBuf\n");
178
FileDesc *fd = FileMgr::getSystemFileMgr()->open(outFile.c_str(), FileMgr::RDONLY);
112
179
long size = fd->seek(0, SEEK_END);
113
180
fd->seek(0, SEEK_SET);
114
181
destBuf->size(size);
115
182
fd->read(destBuf->getRawData(), size);
116
183
FileMgr::getSystemFileMgr()->close(fd);
117
FileMgr::removeFile("swftplib.tmp");
184
FileMgr::removeFile(outFile.c_str());
187
SWLog::getSystemLog()->logDebug("FTPLibFTPTransport - returning: %d\n", retVal);