The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
rtranspgdrive.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * rtranspgdrive.cpp - RTransportGDrive
4  *
5  * $Id$
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 <rtranspgdrive.h>
24 
25 #include <fcntl.h>
26 
27 #include <swlog.h>
28 
29 
31 
32 namespace {
33 
34  struct FtpFile {
35  const char *filename;
36  FILE *stream;
37  SWBuf *destBuf;
38  };
39 
40 
41  // initialize/cleanup SYSTEMWIDE library with life of this static.
42  static class RTransportGDrive_init {
43  public:
45  }
46 
48  }
50 
51 
52  static int my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream) {
53  struct FtpFile *out=(struct FtpFile *)stream;
54  if (out && !out->stream && !out->destBuf) {
55  /* open file for writing */
56  out->stream=fopen(out->filename, "wb");
57  if (!out->stream)
58  return -1; /* failure, can't open file to write */
59  }
60  if (out->destBuf) {
61  int s = (int)out->destBuf->size();
62  out->destBuf->size(s+(size*nmemb));
63  memcpy(out->destBuf->getRawData()+s, buffer, size*nmemb);
64  return (int)nmemb;
65  }
66  return (int)fwrite(buffer, size, nmemb, out->stream);
67  }
68 
69 
70  struct MyProgressData {
71  StatusReporter *sr;
72  bool *term;
73  };
74 
75 
76  static int my_fprogress(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow) {
77  if (clientp) {
78  MyProgressData *pd = (MyProgressData *)clientp;
79 SWLOGD("RTransportGDrive report progress: totalSize: %ld; xfered: %ld\n", (long)dltotal, (long)dlnow);
80  if (pd->sr) {
81  if (dltotal < 0) dltotal = 0;
82  if (dlnow < 0) dlnow = 0;
83  if (dlnow > dltotal) dlnow = dltotal;
84  pd->sr->update(dltotal, dlnow);
85  }
86  if (*(pd->term)) return 1;
87  }
88  return 0;
89  }
90 }
91 
92 
94  // session open
95 }
96 
97 
99  // session cleanup
100 }
101 
102 
103 char RTransportGDrive::putURL(const char *destURL, const char *sourcePath, SWBuf *sourceBuf) {
104  return RemoteTransport::putURL(destURL, sourcePath, sourceBuf);
105 }
106 char RTransportGDrive::getURL(const char *destPath, const char *sourceURL, SWBuf *destBuf) {
107  signed char retVal = 0;
108  struct FtpFile ftpfile = {destPath, 0, destBuf};
109 #if 0
110 
111  if (session) {
112 
113  CURLcode res;
114 
115  struct MyProgressData pd;
116  pd.sr = statusReporter;
117  pd.term = &term;
118 
119  curl_easy_setopt(session, CURLOPT_URL, sourceURL);
120 
121  SWBuf credentials = u + ":" + p;
122  curl_easy_setopt(session, CURLOPT_USERPWD, credentials.c_str());
123  curl_easy_setopt(session, CURLOPT_WRITEFUNCTION, my_fwrite);
124  if (!passive)
125  curl_easy_setopt(session, CURLOPT_FTPPORT, "-");
126  curl_easy_setopt(session, CURLOPT_NOPROGRESS, 0);
127  curl_easy_setopt(session, CURLOPT_PROGRESSDATA, &pd);
128  curl_easy_setopt(session, CURLOPT_PROGRESSFUNCTION, my_fprogress);
129  curl_easy_setopt(session, CURLOPT_DEBUGFUNCTION, my_trace);
130  /* Set a pointer to our struct to pass to the callback */
131  curl_easy_setopt(session, CURLOPT_FILE, &ftpfile);
132 
133  /* Switch on full protocol/debug output */
134  curl_easy_setopt(session, CURLOPT_VERBOSE, true);
135  curl_easy_setopt(session, CURLOPT_CONNECTTIMEOUT, 45);
136 
137  /* FTP connection settings */
138 
139 #if (LIBCURL_VERSION_MAJOR > 7) || \
140  ((LIBCURL_VERSION_MAJOR == 7) && (LIBCURL_VERSION_MINOR > 10)) || \
141  ((LIBCURL_VERSION_MAJOR == 7) && (LIBCURL_VERSION_MINOR == 10) && (LIBCURL_VERSION_PATCH >= 5))
142 # define EPRT_AVAILABLE 1
143 #endif
144 
145 #ifdef EPRT_AVAILABLE
146  curl_easy_setopt(session, CURLOPT_FTP_USE_EPRT, 0);
147 SWLOGD("***** using CURLOPT_FTP_USE_EPRT\n");
148 #endif
149 
150 
151 SWLOGD("***** About to perform curl easy action. \n");
152 SWLOGD("***** destPath: %s \n", destPath);
153 SWLOGD("***** sourceURL: %s \n", sourceURL);
154  res = curl_easy_perform(session);
155 SWLOGD("***** Finished performing curl easy action. \n");
156 
157  // it seems CURL tries to use this option data later for some reason, so we unset here
158  curl_easy_setopt(session, CURLOPT_PROGRESSDATA, (void*)NULL);
159 
160  if(CURLE_OK != res) {
161  retVal = -1;
162  }
163  }
164 #endif
165 
166  if (ftpfile.stream)
167  fclose(ftpfile.stream); /* close the local file */
168 
169  return retVal;
170 }
171 
172 
174 
#define SWORD_NAMESPACE_START
Definition: defs.h:39
virtual char putURL(const char *destURL, const char *sourcePath, SWBuf *sourceBuf=0)
Definition: swbuf.h:47
unsigned long size
Definition: filemgr.h:42
RTransportGDrive(const char *host, StatusReporter *statusReporter=0)
static int my_trace(CURL *handle, curl_infotype type, unsigned char *data, size_t size, void *userp)
Definition: curlftpt.cpp:97
preg buffer
Definition: regex.c:8089
static int my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
Definition: curlftpt.cpp:57
static int my_fprogress(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
Definition: curlftpt.cpp:81
return NULL
Definition: regex.c:7953
const char * c_str() const
Definition: swbuf.h:158
virtual char putURL(const char *destURL, const char *sourcePath, SWBuf *sourceBuf=0)
Definition: remotetrans.cpp:85
#define SWORD_NAMESPACE_END
Definition: defs.h:40
#define SWLOGD(...)
Definition: defs.h:187
static class SWORD_NAMESPACE_START::RTransportGDrive_init _rTransportGDrive_init
StatusReporter * statusReporter
Definition: remotetrans.h:59
virtual char getURL(const char *destPath, const char *sourceURL, SWBuf *destBuf=0)