cURL / Mailing Lists / curl-library / Single Mail

curl-library

ftp send and rename example fails for me

From: <aplumb_at_westpac.com.au>
Date: Mon, 17 Feb 2003 16:13:15 +1100

Hi all......

I downloaded the ftpupload.c example which is supposed to upload a file
then rename it on the remote server..... The upload worked fine, but the
rename just didn't happen - Error code 21 - which means that libcurl got a
return code over 400 from the ftp server. I had to make a few modifications
to the example source code (added some header files to compile it, and a
usename and password + setting on verbose mode).

Can someone tell me if there is something wrong with the example, or am I
doing something wrong here ??

Source included here, with output below:

*********************************************************************************
/*****************************************************************************
 * _ _ ____ _
 * Project ___| | | | _ \| |
 * / __| | | | |_) | |
 * | (__| |_| | _ <| |___
 * \___|\___/|_| \_\_____|
 *
 * $Id: ftpupload.c,v 1.1 2001/08/29 07:12:04 bagder Exp $
 */

#include <stdio.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <curl/curl.h>

/*
 * 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!
 */

#define LOCAL_FILE "ipfile.txt"
#define UPLOAD_FILE_AS "temp/newipfile.txt"
#define REMOTE_URL "ftp://10.6.115.45/" UPLOAD_FILE_AS
#define RENAME_FILE_TO "temp/newipfile2.txt"

int main(int argc, char **argv)
{
  CURL *curl;
  CURLcode res;
  FILE *ftpfile;
  FILE * hd_src ;
  int hd ;
  struct stat file_info;

  struct curl_slist *headerlist=NULL;
  char buf_1 [] = "RNFR " UPLOAD_FILE_AS;
  char buf_2 [] = "RNTO " RENAME_FILE_TO;

  /* get the file size of the local file */
  hd = open(LOCAL_FILE, O_RDONLY) ;
  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(LOCAL_FILE, "rb");

  /* 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);

/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERPWD, "andrew:andrew");

/* Switch on full protocol/debug output */
curl_easy_setopt(curl, CURLOPT_VERBOSE, TRUE);

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

    /* specify target */
    curl_easy_setopt(curl,CURLOPT_URL, REMOTE_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 0;
}

*********************************************************************************

The output that this program produces is:

$ sendren
* About to connect() to 10.6.115.45:21
* Connected to 10.6.115.45 ((nil)) port 21
> USER andrew
> PASS andrew
* We have successfully logged in
> PWD
* Entry path is '/'
> CWD temp
> EPSV
> PASV
* About to connect() to 10.6.115.45:4905
* Connecting to 10.6.115.45 ((nil)) port 4905
* Connected the data stream with PASV!
> TYPE I
> STOR newipfile.txt
> RNFR temp/newipfile.txt
* Connection #0 left intact
* Closing connection #0

*********************************************************************************

So I am left with the file on the server being temp/newipfile.txt - not
temp/newipfile2.txt as I would expect.

I would appreciate any help on this one because I am stumped....

Thanks & Regards,

Andrew Plumb.

-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
Received on 2003-02-17