00001 /*************************************************************************** 00002 * Copyright (C) 2005-2007 Stefan Schwarzer, Jens Schneider, * 00003 * Matthias Hardt, Guido Madaus * 00004 * * 00005 * Copyright (C) 2007-2008 BerLinux Solutions GbR * 00006 * Stefan Schwarzer & Guido Madaus * 00007 * * 00008 * Copyright (C) 2009-2013 BerLinux Solutions GmbH * 00009 * * 00010 * Authors: * 00011 * Stefan Schwarzer <stefan.schwarzer@diskohq.org>, * 00012 * Matthias Hardt <matthias.hardt@diskohq.org>, * 00013 * Jens Schneider <jens.schneider@diskohq.org>, * 00014 * Guido Madaus <guido.madaus@diskohq.org>, * 00015 * Patrick Helterhoff <patrick.helterhoff@diskohq.org>, * 00016 * René Bählkow <rene.baehlkow@diskohq.org> * 00017 * * 00018 * This library is free software; you can redistribute it and/or * 00019 * modify it under the terms of the GNU Lesser General Public * 00020 * License version 2.1 as published by the Free Software Foundation. * 00021 * * 00022 * This library is distributed in the hope that it will be useful, * 00023 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00025 * Lesser General Public License for more details. * 00026 * * 00027 * You should have received a copy of the GNU Lesser General Public * 00028 * License along with this library; if not, write to the * 00029 * Free Software Foundation, Inc., * 00030 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * 00031 **************************************************************************/ 00032 00033 #ifndef MMSFILETRANSFER_H_ 00034 #define MMSFILETRANSFER_H_ 00035 #ifdef __HAVE_CURL__ 00036 00037 #include <string> 00038 00039 using namespace std; 00040 00041 #include <sigc++/sigc++.h> 00042 00043 extern "C" { 00044 #include <curl/curl.h> 00045 #include <sys/types.h> 00046 #include <sys/stat.h> 00047 } 00048 00049 /** Specifies a structure for file operations */ 00050 typedef struct { 00051 const char *filename; 00052 FILE *stream; 00053 } FtpFile; 00054 00055 00056 /** 00057 * A ftp operations class. 00058 * 00059 * This is a class that provides the functions to down- and upload files from/to a ftp server. 00060 * As far as it is supported by the ftp server, resuming is also possible. 00061 * 00062 * @author Patrick Helterhoff 00063 */ 00064 class MMSFiletransfer { 00065 00066 private: 00067 CURL *ehandle; 00068 string remoteUrl; 00069 string logindata; 00070 CURLcode lasterror; 00071 long timeout; 00072 long lowSpeedLimit; 00073 unsigned int port; 00074 00075 /** buffer to cached data from url */ 00076 char *buffer; 00077 00078 /** buffer length */ 00079 unsigned buf_len; 00080 00081 /** fill pointer within buffer */ 00082 unsigned buf_pos; 00083 00084 00085 public: 00086 /** A signal that emits the progress (in percentage) of the current up- or download. */ 00087 sigc::signal<void, const unsigned int> progress; 00088 00089 /** Virtual function for the curl write callback. */ 00090 virtual size_t mem_write_callback(char *buffer, size_t size, size_t nitems, void *outstream); 00091 00092 /** 00093 * Constructor of class MMSFiletransfer. 00094 * 00095 * @param url [in] the remote host and desired directory ("localhost/dir") 00096 */ 00097 MMSFiletransfer(const string url, const unsigned int ftpPort); 00098 00099 /** Destructor of class MMSFiletransfer. */ 00100 virtual ~MMSFiletransfer(); 00101 00102 /** 00103 * Performs a ftp upload for the specified local file. 00104 * 00105 * @param localfile [in] the local file to be uploaded 00106 * @param remoteName [in] name and path of the remote file 00107 * @param resume [in] resume a prior upload 00108 */ 00109 bool performUpload(const string localfile, const string remoteName, bool resume = false); 00110 00111 /** 00112 * Performs a ftp download for the specified remote file. 00113 * 00114 * @param localfile [in] the local file to be saved 00115 * @param remoteName [in] name and path of the remote file 00116 * @param resume [in] resume a prior download 00117 */ 00118 bool performDownload(const string localfile, const string remoteName, bool resume = false); 00119 00120 /** 00121 * Deletes the specified remote file. 00122 * 00123 * @param remoteFile [in] name and path of the remote file 00124 */ 00125 bool deleteRemoteFile(const string remoteFile); 00126 00127 /** 00128 * Retrieves a directory listing and writes it into the memory 00129 * 00130 * @param buffer [out] pointer to a char buffer 00131 * @param directory [in] the remote directory (path from root) 00132 * @param namesOnly [in] flag to retrieve only the names 00133 */ 00134 bool getListing(char **buffer, string directory, bool namesOnly = false); 00135 00136 /** 00137 * Enables verbose output of from the curl lib. 00138 */ 00139 void setVerboseInformation(bool enable); 00140 00141 /** 00142 * Use this to set user and password for the ftp server connection, if necessary. 00143 * 00144 * @param user [in] the ftp user 00145 * @param password [in] the password 00146 */ 00147 void setAuthData(const string user, const string password); 00148 00149 /** 00150 * Changes the remote url. 00151 * The change will be performed on the following ftp operation (upload / download). 00152 * 00153 * @param url [in] the remote host (e.g. "127.0.0.1") 00154 */ 00155 void setRemoteUrl(const string url); 00156 00157 /** Returns the current remote url. */ 00158 const string getRemoteUrl(); 00159 00160 /** 00161 * Sets the port for the ftp connection. 00162 * 00163 * @param ftpPort [in] The port for the ftp connection to the remote server. 00164 */ 00165 void setFtpPort(const unsigned int ftpPort); 00166 00167 /** Returns the current ftp port. */ 00168 const unsigned int getFtpPort(); 00169 00170 /** 00171 * Sets the timeout. 00172 * 00173 * @param timeouts [in] The timeout in seconds. 00174 */ 00175 void setTimeout(const long timemout); 00176 00177 /** Returns the current timeout in seconds. */ 00178 const long getTimeout(); 00179 00180 /** 00181 * Sets the low speed limit to be considered as timeout (default: 100 kb/s). 00182 * 00183 * @param limit [in] The low speed limit in byte per second 00184 */ 00185 void setLowSpeedLimit(const long limit); 00186 00187 /** Returns the current speed limit (bytes per second) to be considered as timeout. */ 00188 const long getLowSpeedLimit(); 00189 00190 /** 00191 * Returns the error number of the last operation, or 0 if no error has occured. 00192 * If the errormsg parameter is supplied it will be filled with a human readable 00193 * errormessage. 00194 * 00195 * @param errormsg [out] If supplied it will be filled with an error message. 00196 * 00197 * @return the errornumber or 0 00198 */ 00199 int getLastError(string *errormsg); 00200 }; 00201 00202 #endif /*__HAVE_CURL__*/ 00203 #endif /*MMSFILETRANSFER_H_*/