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-2012 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 using namespace std; 00038 00039 #include <string> 00040 #include <sigc++/sigc++.h> 00041 00042 extern "C" { 00043 #include <curl/curl.h> 00044 #include <sys/types.h> 00045 #include <sys/stat.h> 00046 } 00047 00048 /** Specifies a structure for file operations */ 00049 typedef struct { 00050 const char *filename; 00051 FILE *stream; 00052 } FtpFile; 00053 00054 00055 /** 00056 * A ftp operations class. 00057 * 00058 * This is a class that provides the functions to down- and upload files from/to a ftp server. 00059 * As far as it is supported by the ftp server, resuming is also possible. 00060 * 00061 * @author Patrick Helterhoff 00062 */ 00063 class MMSFiletransfer { 00064 00065 private: 00066 CURL *ehandle; 00067 string remoteUrl; 00068 string logindata; 00069 CURLcode lasterror; 00070 long timeout; 00071 long lowSpeedLimit; 00072 unsigned int port; 00073 00074 /** buffer to cached data from url */ 00075 char *buffer; 00076 00077 /** buffer length */ 00078 unsigned buf_len; 00079 00080 /** fill pointer within buffer */ 00081 unsigned buf_pos; 00082 00083 00084 public: 00085 /** A signal that emits the progress (in percentage) of the current up- or download. */ 00086 sigc::signal<void, const unsigned int> progress; 00087 00088 /** Virtual function for the curl write callback. */ 00089 virtual size_t mem_write_callback(char *buffer, size_t size, size_t nitems, void *outstream); 00090 00091 /** 00092 * Constructor of class MMSFiletransfer. 00093 * 00094 * @param url [in] the remote host and desired directory ("localhost/dir") 00095 */ 00096 MMSFiletransfer(const string url, const unsigned int ftpPort); 00097 00098 /** Destructor of class MMSFiletransfer. */ 00099 virtual ~MMSFiletransfer(); 00100 00101 /** 00102 * Performs a ftp upload for the specified local file. 00103 * 00104 * @param localfile [in] the local file to be uploaded 00105 * @param remoteName [in] name and path of the remote file 00106 * @param resume [in] resume a prior upload 00107 */ 00108 bool performUpload(const string localfile, const string remoteName, bool resume = false); 00109 00110 /** 00111 * Performs a ftp download for the specified remote file. 00112 * 00113 * @param localfile [in] the local file to be saved 00114 * @param remoteName [in] name and path of the remote file 00115 * @param resume [in] resume a prior download 00116 */ 00117 bool performDownload(const string localfile, const string remoteName, bool resume = false); 00118 00119 /** 00120 * Deletes the specified remote file. 00121 * 00122 * @param remoteFile [in] name and path of the remote file 00123 */ 00124 bool deleteRemoteFile(const string remoteFile); 00125 00126 /** 00127 * Retrieves a directory listing and writes it into the memory 00128 * 00129 * @param buffer [out] pointer to a char buffer 00130 * @param directory [in] the remote directory (path from root) 00131 * @param namesOnly [in] flag to retrieve only the names 00132 */ 00133 bool getListing(char **buffer, string directory, bool namesOnly = false); 00134 00135 /** 00136 * Enables verbose output of from the curl lib. 00137 */ 00138 void setVerboseInformation(bool enable); 00139 00140 /** 00141 * Use this to set user and password for the ftp server connection, if necessary. 00142 * 00143 * @param user [in] the ftp user 00144 * @param password [in] the password 00145 */ 00146 void setAuthData(const string user, const string password); 00147 00148 /** 00149 * Changes the remote url. 00150 * The change will be performed on the following ftp operation (upload / download). 00151 * 00152 * @param url [in] the remote host (e.g. "127.0.0.1") 00153 */ 00154 void setRemoteUrl(const string url); 00155 00156 /** Returns the current remote url. */ 00157 const string getRemoteUrl(); 00158 00159 /** 00160 * Sets the port for the ftp connection. 00161 * 00162 * @param ftpPort [in] The port for the ftp connection to the remote server. 00163 */ 00164 void setFtpPort(const unsigned int ftpPort); 00165 00166 /** Returns the current ftp port. */ 00167 const unsigned int getFtpPort(); 00168 00169 /** 00170 * Sets the timeout. 00171 * 00172 * @param timeouts [in] The timeout in seconds. 00173 */ 00174 void setTimeout(const long timemout); 00175 00176 /** Returns the current timeout in seconds. */ 00177 const long getTimeout(); 00178 00179 /** 00180 * Sets the low speed limit to be considered as timeout (default: 100 kb/s). 00181 * 00182 * @param limit [in] The low speed limit in byte per second 00183 */ 00184 void setLowSpeedLimit(const long limit); 00185 00186 /** Returns the current speed limit (bytes per second) to be considered as timeout. */ 00187 const long getLowSpeedLimit(); 00188 00189 /** 00190 * Returns the error number of the last operation, or 0 if no error has occured. 00191 * If the errormsg parameter is supplied it will be filled with a human readable 00192 * errormessage. 00193 * 00194 * @param errormsg [out] If supplied it will be filled with an error message. 00195 * 00196 * @return the errornumber or 0 00197 */ 00198 int getLastError(string *errormsg); 00199 }; 00200 00201 #endif /*__HAVE_CURL__*/ 00202 #endif /*MMSFILETRANSFER_H_*/