~ubuntu-branches/ubuntu/lucid/sword/lucid

« back to all changes in this revision

Viewing changes to src/mgr/ftplibftpt.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Dmitrijs Ledkovs, Jonathan Marsden, Dmitrijs Ledkovs
  • Date: 2010-01-21 00:10:17 UTC
  • mfrom: (1.1.4 upstream) (5.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100121001017-5ea19163rgisywbl
Tags: 1.6.1+dfsg-1
[ Jonathan Marsden ]
* debian/libsword8.examples: Add missing tcl eggdrop bot script.

[ Dmitrijs Ledkovs ]
* Drop shipping .la file in favour of pkg-config (Debian release goal)
  - More info http://lists.debian.org/debian-devel/2009/08/msg00783.html
* Drop backported patches, refreshed libver & compiler warnings.
* Compiling with -Werror enabled. 
* Bumped standards version to 3.8.3, no changes needed.
* Added debian/README.source documentation quilt usage
* Added configure option --without-internalregex

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
 
33
33
SWORD_NAMESPACE_START
34
34
 
 
35
namespace {
 
36
 
 
37
struct MyProgressData {
 
38
        StatusReporter *sr;
 
39
        long totalSize;
 
40
        bool *term;
 
41
};
 
42
 
 
43
int my_fprogress(netbuf *nControl, int xfered, void *arg) {
 
44
        if (arg) {
 
45
                MyProgressData *pd = (MyProgressData *)arg;
 
46
SWLog::getSystemLog()->logDebug("FTPLibFTPTransport report progress: totalSize: %ld; xfered: %d\n", pd->totalSize, xfered);
 
47
                if (pd->sr) {
 
48
                        pd->sr->statusUpdate(pd->totalSize, xfered);
 
49
                }
 
50
                if (*(pd->term)) return 0;
 
51
        }
 
52
        return 1;
 
53
}
 
54
 
 
55
}
 
56
 
 
57
 
35
58
 
36
59
static FTPLibFTPTransport_init _FTPLibFTPTransport_init;
37
60
 
 
61
 
 
62
 
38
63
FTPLibFTPTransport_init::FTPLibFTPTransport_init() {
39
64
        FtpInit();
40
65
}
58
83
char FTPLibFTPTransport::assureLoggedIn() {
59
84
        char retVal = 0;
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");
64
91
                                retVal = 0;
65
92
                        }
66
93
                        else {
67
94
                                SWLog::getSystemLog()->logError("Failed to login to %s\n", host.c_str());
68
95
                                retVal = -2;
69
96
                        }
 
97
                }
70
98
                else {
71
99
                        SWLog::getSystemLog()->logError("Failed to connect to %s\n", host.c_str());
72
100
                        retVal = -1;
75
103
        return retVal;
76
104
}
77
105
 
 
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);
 
110
}
 
111
#pragma GCC diagnostic warning "-Wall"
 
112
 
 
113
 
78
114
 
79
115
char FTPLibFTPTransport::getURL(const char *destPath, const char *sourceURL, SWBuf *destBuf) {
80
116
 
81
117
        char retVal = 0;
82
118
 
 
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");
86
124
 
87
125
        SWBuf sourcePath = sourceURL;
88
 
        SWBuf outFile = (!destBuf) ? destPath : "swftplib.tmp";
 
126
 
 
127
        SWBuf outFile;
 
128
        if (!destBuf) {
 
129
                outFile = destPath;
 
130
        }
 
131
        else {
 
132
#ifdef ANDROID
 
133
                outFile = "/sdcard/sword/InstallMgr/swtmpbuf.out";
 
134
#else
 
135
                char tmpName[128];
 
136
                my_tmpnam(tmpName);
 
137
                outFile = tmpName;
 
138
#endif
 
139
        }
 
140
 
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());
91
143
        if (passive)
92
144
                FtpOptions(FTPLIB_CONNMODE, FTPLIB_PASSIVE, ftpConnection);
93
145
        else
94
146
                FtpOptions(FTPLIB_CONNMODE, FTPLIB_PORT, ftpConnection);
 
147
 
 
148
        struct MyProgressData pd;
 
149
        pd.sr = statusReporter;
 
150
        pd.term = &term;
 
151
        pd.totalSize = 0;
 
152
 
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);
 
157
 
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());
101
164
        }
102
165
        else {
103
166
                SWLog::getSystemLog()->logDebug("getting file %s\n", sourcePath.c_str());
 
167
                int size;
 
168
                FtpSize(sourcePath, &size, FTPLIB_IMAGE, ftpConnection);
 
169
                pd.totalSize = size;
104
170
                retVal = FtpGet(outFile.c_str(), sourcePath, FTPLIB_IMAGE, ftpConnection) - 1;
105
171
        }
106
172
 
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
110
176
        if (destBuf) {
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());
118
185
        }
119
186
 
 
187
        SWLog::getSystemLog()->logDebug("FTPLibFTPTransport - returning: %d\n", retVal);
120
188
        return retVal;
121
189
}
122
190