"Fossies" - the Fresh Open Source Software Archive

Member "sitecopy-0.16.6/src/ftpdriver.c" (4 Apr 2005, 4439 Bytes) of archive /linux/www/sitecopy-0.16.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ftpdriver.c" see the Fossies "Dox" file reference documentation.

    1 /* 
    2    sitecopy FTP protocol driver module
    3    Copyright (C) 2000-2005, Joe Orton <joe@manyfish.co.uk>
    4                                                                      
    5    This program is free software; you can redistribute it and/or modify
    6    it under the terms of the GNU General Public License as published by
    7    the Free Software Foundation; either version 2 of the License, or
    8    (at your option) any later version.
    9   
   10    This program is distributed in the hope that it will be useful,
   11    but WITHOUT ANY WARRANTY; without even the implied warranty of
   12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13    GNU General Public License for more details.
   14   
   15    You should have received a copy of the GNU General Public License
   16    along with this program; if not, write to the Free Software
   17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   18 
   19 */
   20 
   21 #include <ne_socket.h>
   22 
   23 #include "protocol.h"
   24 #include "ftp.h"
   25 
   26 static int f2s(int errcode)
   27 {
   28     switch (errcode) {
   29     case FTP_OK:
   30     return SITE_OK;
   31     case FTP_LOOKUP:
   32     return SITE_LOOKUP;
   33     case FTP_CONNECT:
   34     return SITE_CONNECT;
   35     case FTP_FAILED:
   36     return SITE_FAILED;
   37     case FTP_LOGIN:
   38     return SITE_AUTH;
   39     default:
   40     return SITE_ERRORS;
   41     }
   42 }    
   43 
   44 static int get_server_port(struct site *site)
   45 {
   46     return 21;
   47 }
   48 
   49 static int init(void **session, struct site *site) 
   50 {
   51     int ret;
   52     ftp_session *sess = ftp_init();
   53     ret = ftp_set_server(sess, &site->server);
   54     if (ret == FTP_OK) {
   55     if (site->ftp_pasv_mode) {
   56         ftp_set_passive(sess, 1);
   57     }
   58     if (site->ftp_use_cwd) {
   59         ftp_set_usecwd(sess, 1);
   60     }
   61     ret = ftp_open(sess);
   62     }
   63     *session = sess;
   64     /* map it to a SITE_* code. */
   65     ret = f2s(ret);
   66     if (ret == SITE_ERRORS)
   67     ret = SITE_FAILED;
   68     return ret;
   69 }
   70 
   71 static void finish(void *session)
   72 {
   73     ftp_session *sess = session;
   74     ftp_finish(sess);
   75 }
   76 
   77 static int file_move(void *session, const char *from, const char *to)
   78 {
   79     ftp_session *sess = session;
   80     return f2s(ftp_move(sess, from, to));
   81 }
   82 
   83 static int file_upload(void *session, const char *local, const char *remote, int ascii)
   84 {
   85     ftp_session *sess = session;
   86     return f2s(ftp_put(sess, local, remote, ascii));
   87 }
   88 
   89 static int file_upload_cond(void *session,
   90                 const char *local, const char *remote,
   91                 int ascii, time_t t)
   92 {
   93     ftp_session *sess = session;
   94     return f2s(ftp_put_cond(sess, local, remote, ascii, t));
   95 }
   96 
   97 static int file_get_modtime(void *session, const char *remote, time_t *modtime)
   98 {
   99     ftp_session *sess = session;
  100     return f2s(ftp_get_modtime(sess, remote, modtime));
  101 }
  102     
  103 static int file_download(void *session, const char *local, const char *remote,
  104              int ascii)
  105 {
  106     ftp_session *sess = session;
  107     return f2s(ftp_get(sess, local, remote, ascii));
  108 }
  109 
  110 static int file_read(void *session, const char *remote, 
  111              ne_block_reader reader, void *userdata)
  112 {
  113     ftp_session *sess = session;
  114     return f2s(ftp_read_file(sess, remote, reader, userdata));
  115 }
  116 
  117 static int file_delete(void *session, const char *filename)
  118 {
  119     ftp_session *sess = session;
  120     return f2s(ftp_delete(sess, filename));
  121 }
  122 
  123 static int file_chmod(void *session, const char *filename, mode_t mode)
  124 {
  125     ftp_session *sess = session;
  126     return f2s(ftp_chmod(sess, filename, mode));
  127 }
  128 
  129 static int dir_create(void *session, const char *dirname)
  130 {
  131     ftp_session *sess = session;
  132     return f2s(ftp_mkdir(sess, dirname));
  133 }
  134 
  135 static int dir_remove(void *session, const char *dirname)
  136 {
  137     ftp_session *sess = session;
  138     return f2s(ftp_rmdir(sess, dirname));
  139 }
  140 
  141 static int fetch_list(void *session, const char *dirname, int need_modtimes,
  142               struct proto_file **files) 
  143 {
  144     ftp_session *sess = session;
  145     int ret;
  146     
  147     ret = ftp_fetch(sess, dirname, files);
  148 
  149     if (ret == FTP_OK && need_modtimes) {
  150     ret = ftp_fetch_modtimes(sess, dirname, *files);
  151     }
  152 
  153     return f2s(ret);
  154 }
  155 
  156 static const char *error(void *session)
  157 {
  158     ftp_session *sess = session;
  159     return ftp_get_error(sess);
  160 }
  161 
  162 /* The protocol drivers */
  163 const struct proto_driver ftp_driver = {
  164     init, 
  165     finish,
  166     file_move,
  167     file_upload,
  168     file_upload_cond,
  169     file_get_modtime,
  170     file_download,
  171     file_read,
  172     file_delete,
  173     file_chmod,
  174     dir_create,
  175     dir_remove,
  176     NULL, /* create link */
  177     NULL, /* change link target */
  178     NULL, /* delete link */
  179     fetch_list,
  180     error,
  181     get_server_port,
  182     get_server_port,
  183     "FTP"
  184 };