The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ftplibftpt.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * ftplibftpt.cpp - FTPLibFTPTransport
4  *
5  * $Id: ftplibftpt.cpp 3822 2020-11-03 18:54:47Z scribe $
6  *
7  * Copyright 2004-2013 CrossWire Bible Society (http://www.crosswire.org)
8  * CrossWire Bible Society
9  * P. O. Box 2528
10  * Tempe, AZ 85280-2528
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License as published by the
14  * Free Software Foundation version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details.
20  *
21  */
22 
23 #include <stdio.h>
24 #include <fcntl.h>
25 
26 #include <ftplib.h>
27 
28 #include <ftplibftpt.h>
29 #include <swlog.h>
30 #include <filemgr.h>
31 
32 
34 
35 namespace {
36 
37  struct MyProgressData {
38  StatusReporter *sr;
39  long totalSize;
40  bool *term;
41  };
42 
43  static int my_swbufwriter(netbuf *nControl, void *buffer, size_t size, void *swbuf) {
44  SWBuf &output = *(SWBuf *)swbuf;
45  int s = (int)output.size();
46  output.size(s+size);
47  memcpy(output.getRawData()+s, buffer, size);
48  return (int)size;
49  }
50 
51  static int my_filewriter(netbuf *nControl, void *buffer, size_t size, void *fd) {
52  int output = *((int *)fd);
53  FileMgr::write(output, buffer, size);
54  return (int)size;
55  }
56 
57 #if defined(__GNUC__)
58 #pragma GCC diagnostic push
59 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
60 #endif
61 
62  static int my_fprogress(netbuf *nControl, int xfered, void *arg) {
63  if (arg) {
64  MyProgressData *pd = (MyProgressData *)arg;
65 //SWLOGD("FTPLibFTPTransport report progress: totalSize: %ld; xfered: %d\n", pd->totalSize, xfered);
66  if (pd->sr) {
67  pd->sr->update(pd->totalSize, xfered);
68  }
69  if (*(pd->term)) return 0;
70  }
71  return 1;
72  }
73 
74 #if defined(__GNUC__)
75 #pragma GCC diagnostic pop
76 #endif
77 
78 
79  // initialize/cleanup SYSTEMWIDE library with life of this static.
80  static class FTPLibFTPTransport_init {
81  public:
83  FtpInit();
84  }
85 
87  }
88 
90 
91 }
92 
93 
95 
96  ftpConnection = 0;
97 }
98 
99 
101  if (ftpConnection)
103 }
104 
105 
107  char retVal = 0;
108  if (ftpConnection == 0) {
109 SWLOGD("connecting to host: %s...\n", host.c_str());
110  if (FtpConnect(host, &ftpConnection)) {
113 
114 SWLOGD("connected. logging in...\n");
115  if (FtpLogin(u.c_str(), p.c_str(), ftpConnection)) {
116 SWLOGD("logged in.\n");
117  retVal = 0;
118  }
119  else {
120  SWLog::getSystemLog()->logError("Failed to login to %s\n", host.c_str());
121  retVal = -2;
122  }
123  }
124  else {
125  SWLog::getSystemLog()->logError("Failed to connect to %s\n", host.c_str());
126  retVal = -2;
127  }
128  }
129  return retVal;
130 }
131 
132 
133 char FTPLibFTPTransport::getURL(const char *destPath, const char *sourceURL, SWBuf *destBuf) {
134 
135  char retVal = 0;
136 
137 SWLOGD("FTPLibFTPTransport::getURL(%s, %s, ...);\n", (destPath)?destPath:"(null)", sourceURL);
138  // assert we can login
139  retVal = assureLoggedIn();
140  if (retVal) return retVal;
141 SWLOGD("FTPLibFTPTransport - logged in.\n");
142 
143  SWBuf sourcePath = sourceURL;
144 
145  SWBuf outFile;
146  if (!destBuf) {
147  outFile = destPath;
148  }
149 
150  sourcePath << (6 + host.length()); // shift << "ftp://hostname";
151 SWLOGD("getting file %s to %s\n", sourcePath.c_str(), destBuf ? "*internal buffer*" : outFile.c_str());
152  struct MyProgressData pd;
153  pd.sr = statusReporter;
154  pd.term = &term;
155  pd.totalSize = 0;
156  int fd = 0;
157  if (destBuf) {
160  }
161  else {
162  fd = FileMgr::createPathAndFile(outFile);
165  }
166 
170 
171  if (sourcePath.endsWith("/") || sourcePath.endsWith("\\")) {
172 //SWLOGD("getting test directory %s\n", sourcePath.c_str());
173 // FtpDir(NULL, sourcePath, ftpConnection);
174 SWLOGD("getting real directory %s\n", sourcePath.c_str());
175  retVal = FtpDir(0, sourcePath, ftpConnection) - 1;
176 SWLOGD("got real directory %s to %s\n", sourcePath.c_str(), destBuf ? "*internal buffer*" : outFile.c_str());
177  }
178  else {
179 SWLOGD("getting file %s\n", sourcePath.c_str());
180  int size;
181  FtpSize(sourcePath, &size, FTPLIB_IMAGE, ftpConnection);
182  pd.totalSize = size;
183  retVal = FtpGet(0, sourcePath, FTPLIB_IMAGE, ftpConnection) - 1;
184  }
185  if (fd > 0) FileMgr::closeFile(fd);
186 SWLOGD("FTPLibFTPTransport - returning: %d\n", retVal);
187  return retVal;
188 }
189 
190 
192 
#define FTPLIB_CALLBACK
Definition: ftplib.h:60
#define SWORD_NAMESPACE_START
Definition: defs.h:39
Definition: ftplib.c:123
FTPLibFTPTransport(const char *host, StatusReporter *statusReporter=0)
Definition: ftplibftpt.cpp:94
Definition: swbuf.h:47
unsigned long length() const
Definition: swbuf.h:197
#define FTPLIB_CALLBACK_WRITER
Definition: ftplib.h:64
#define FTPLIB_IMAGE
Definition: ftplib.h:51
static SWLog * getSystemLog()
Definition: swlog.cpp:53
char getURL(const char *destPath, const char *sourceURL, SWBuf *destBuf=0)
Definition: ftplibftpt.cpp:133
#define FTPLIB_CALLBACK_WRITERARG
Definition: ftplib.h:65
#define FTPLIB_PORT
Definition: ftplib.h:57
GLOBALDEF void FtpInit(void)
Definition: ftplib.c:416
GLOBALDEF int FtpConnect(const char *host, netbuf **nControl)
Definition: ftplib.c:444
GLOBALDEF int FtpOptions(int opt, long val, netbuf *nControl)
Definition: ftplib.c:604
preg buffer
Definition: regex.c:8089
GLOBALDEF int FtpLogin(const char *user, const char *pass, netbuf *nControl)
Definition: ftplib.c:675
static void closeFile(int fd)
Definition: filemgr.cpp:491
GLOBALDEF void FtpQuit(netbuf *nControl)
Definition: ftplib.c:1414
bool endsWith(const SWBuf &postfix) const
Definition: swbuf.h:501
static int my_fprogress(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
Definition: curlftpt.cpp:81
static class SWORD_NAMESPACE_START::FTPLibFTPTransport_init _ftpLibFTPTransport_init
netbuf * ftpConnection
Definition: ftplibftpt.h:35
GLOBALDEF int FtpGet(const char *outputfile, const char *path, char mode, netbuf *nControl)
Definition: ftplib.c:1354
char * getRawData()
Definition: swbuf.h:379
const char * c_str() const
Definition: swbuf.h:158
GLOBALDEF int FtpDir(const char *outputfile, const char *path, netbuf *nControl)
Definition: ftplib.c:1296
static int my_swbufwriter(netbuf *nControl, void *buffer, size_t size, void *swbuf)
Definition: ftplibftpt.cpp:43
unsigned long size() const
Definition: swbuf.h:185
static int my_filewriter(netbuf *nControl, void *buffer, size_t size, void *fd)
Definition: ftplibftpt.cpp:51
static long write(int fd, const void *buf, long count)
Definition: filemgr.cpp:115
GLOBALDEF int FtpSize(const char *path, int *size, char mode, netbuf *nControl)
Definition: ftplib.c:1306
#define FTPLIB_PASSIVE
Definition: ftplib.h:56
int size
Definition: regex.c:5043
static int createPathAndFile(const char *fName)
Definition: filemgr.cpp:479
void logError(const char *fmt,...) const
Definition: swlog.cpp:87
#define FTPLIB_IDLETIME
Definition: ftplib.h:61
#define FTPLIB_CALLBACKARG
Definition: ftplib.h:62
#define SWORD_NAMESPACE_END
Definition: defs.h:40
#define FTPLIB_CONNMODE
Definition: ftplib.h:59
#define SWLOGD(...)
Definition: defs.h:187
StatusReporter * statusReporter
Definition: remotetrans.h:59
#define FTPLIB_CALLBACKBYTES
Definition: ftplib.h:63