Commit b4e84ca7 authored by Daniel Stenberg's avatar Daniel Stenberg
Browse files

lib505.c is a new test case for ftp uploading with rename

parent 47970b9e
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@ INCLUDES = -I$(top_srcdir)/include/curl -I$(top_builddir)/lib
LIBDIR = ../../lib

# here are all tools used for running libcurl tests
noinst_PROGRAMS = lib500 lib501 lib502 lib503 lib504
noinst_PROGRAMS = lib500 lib501 lib502 lib503 lib504 lib505

lib500_SOURCES = lib500.c first.c test.h
lib500_LDADD = $(LIBDIR)/libcurl.la
@@ -31,3 +31,7 @@ lib504_SOURCES = lib504.c first.c test.h
lib504_LDADD = $(LIBDIR)/libcurl.la
lib504_DEPENDENCIES = $(LIBDIR)/libcurl.la

lib505_SOURCES = lib505.c first.c test.h
lib505_LDADD = $(LIBDIR)/libcurl.la
lib505_DEPENDENCIES = $(LIBDIR)/libcurl.la

tests/libtest/lib505.c

0 → 100644
+107 −0
Original line number Diff line number Diff line
/*****************************************************************************
 *                                  _   _ ____  _     
 *  Project                     ___| | | |  _ \| |    
 *                             / __| | | | |_) | |    
 *                            | (__| |_| |  _ <| |___ 
 *                             \___|\___/|_| \_\_____|
 *
 * $Id$
 */

#include "test.h"

#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_SYS_TYPES_
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

/*
 * This example shows an FTP upload, with a rename of the file just after
 * a successful upload.
 *
 * Example based on source code provided by Erick Nuwendam. Thanks!
 */

CURLcode test(char *URL)
{
  CURL *curl;
  CURLcode res;
  FILE *hd_src ;
  int hd ;
  struct stat file_info;

  struct curl_slist *headerlist=NULL;
  const char *buf_1 = "RNFR 505";
  const char *buf_2 = "RNTO 505-forreal";

  /* get the file size of the local file */
  hd = open(arg2, O_RDONLY) ;
  if(hd == -1) {
    /* can't open file, bail out */
    return -1;
  }
  fstat(hd, &file_info);
  close(hd);

  /* get a FILE * of the same file, could also be made with
     fdopen() from the previous descriptor, but hey this is just 
     an example! */
  hd_src = fopen(arg2, "rb");
  if(NULL == hd_src) {
    return -2; /* if this happens things are major weird */
  }

  /* In windows, this will init the winsock stuff */
  curl_global_init(CURL_GLOBAL_ALL);

  /* get a curl handle */
  curl = curl_easy_init();
  if(curl) {
    /* build a list of commands to pass to libcurl */
    headerlist = curl_slist_append(headerlist, buf_1);
    headerlist = curl_slist_append(headerlist, buf_2);

    /* enable uploading */
    curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ;

    /* enable verbose */
    curl_easy_setopt(curl, CURLOPT_VERBOSE, TRUE) ;

    /* specify target */
    curl_easy_setopt(curl,CURLOPT_URL, URL);

    /* pass in that last of FTP commands to run after the transfer */
    curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);

    /* now specify which file to upload */
    curl_easy_setopt(curl, CURLOPT_INFILE, hd_src);

    /* and give the size of the upload (optional) */
    curl_easy_setopt(curl, CURLOPT_INFILESIZE, file_info.st_size);

    /* Now run off and do what you've been told! */
    res = curl_easy_perform(curl);

    /* clean up the FTP commands list */
    curl_slist_free_all (headerlist);

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  fclose(hd_src); /* close the local file */

  curl_global_cleanup();
  return res;
}