cURL / Mailing Lists / curl-library / Single Mail

curl-library

Help with partial file upload - result 18 - (0 bytes)

From: Leon Vanderploeg <leonv12_at_msn.com>
Date: Tue, 31 Oct 2006 15:50:54 -0700

Greetings,

I am new to cURL, and am trying to make it work. I have been able to get to
a successful connection, but the file is always 0 bytes (result 18).

Most of the code is copy/paste from examples. Apparently I don't have the
CURLOPT_READFUNCTION set up correctly, as it is showing -1 bytes (error
condition). The file does get created in the remote FTP directory, but it
is 0 (zero) bytes.

The OS is Windows Server 2003. SmartFTP client works fine.

Any help would be GREATLY appreciated.

Thanks,
Leon

=========================================================================
Here's the source code:
/***************************************************************************
**
*
* project to get ftp in C working
*
*/

#define TRUE 1
#define FALSE 0

#include <stdio.h>
 
#include <curl/curl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

#define LOCAL_FILE "stat_file.dat"
#define UPLOAD_FILE_AS "stat_file.dat"
#define REMOTE_URL "ftp://ftp.vaultnow.com/" UPLOAD_FILE_AS
#define RENAME_FILE_TO "stat_file.dat"
#define response_timeout 30
#define debug "debug"

struct data {
  char trace_ascii; /* 1 or 0 */
};

static
void dump(const char *text,
          FILE *stream, unsigned char *ptr, size_t size,
          char nohex)
{
  size_t i;
  size_t c;

  unsigned int width=0x10;

  if(nohex)
    /* without the hex output, we can fit more on screen */
    width = 0x40;

  fprintf(stream, "%s, %d bytes (0x%zx)\n", text, size, size);

  for(i=0; i<size; i+= width) {

    fprintf(stream, "%04zx: ", i);

    if(!nohex) {
      /* hex not disabled, show it */
      for(c = 0; c < width; c++)
        if(i+c < size)
          fprintf(stream, "%02x ", ptr[i+c]);
        else
          fputs(" ", stream);
    }

    for(c = 0; (c < width) && (i+c < size); c++) {
      /* check for 0D0A; if found, skip past and start a new line of output
*/
      if (nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
        i+=(c+2-width);
        break;
      }
      fprintf(stream, "%c",
              (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
      /* check again for 0D0A, to avoid an extra \n if it's at width */
      if (nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
        i+=(c+3-width);
        break;
      }
    }
    fputc('\n', stream); /* newline */
  }
  fflush(stream);
}

static
int my_trace(CURL *handle, curl_infotype type,
             unsigned char *data, size_t size,
             void *userp)
{
  struct data *config = (struct data *)userp;
  const char *text;
  (void)handle; /* prevent compiler warning */

  switch (type) {
  case CURLINFO_TEXT:
    fprintf(stderr, "== Info: %s", data);
  default: /* in case a new one is introduced to shock us */
    return 0;

  case CURLINFO_HEADER_OUT:
    text = "=> Send header";
    break;
  case CURLINFO_DATA_OUT:
    text = "=> Send data";
    break;
  case CURLINFO_HEADER_IN:
    text = "<= Recv header";
    break;
  case CURLINFO_DATA_IN:
    text = "<= Recv data";
    break;
  case CURLINFO_SSL_DATA_IN:
    text = "<= Recv SSL data";
    break;
  case CURLINFO_SSL_DATA_OUT:
    text = "<= Send SSL data";
    break;
  }

  dump(text, stderr, data, size, config->trace_ascii);
  return 0;
}

size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
{
  size_t retcode;

  int fd = (int)stream;

printf("fd is %d: \n",fd);

  retcode = read(fd, ptr, size * nmemb);

  fprintf(stderr, "*** We read %d bytes from file\n", retcode);

  return retcode;
}

int main(int argc, char **argv)
{
 CURL *curl;

  struct data config;
  config.trace_ascii = 1; /* enable ascii tracing */

 CURLcode res;
 FILE *ftpfile;
 FILE * hd_src ;
 int hd ;
 int debug_flag;
 int result;
 struct stat file_info;

 /* if first arg in call is "debug", then turn on debug tracing */
 if (!strcmp(argv[1], debug) )
 {
     debug_flag = 1;
     printf("debug flag set to 1\n");
 }

 if (debug_flag == TRUE)
   printf("starting curl\n");

 /* get the file size of the local file */
 

 hd = open(LOCAL_FILE, O_RDONLY) ;
 fstat(hd, &file_info);
 close(hd) ;

   if (debug_flag == TRUE)
      printf("file size is: %d\n", file_info.st_size);

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

 if (hd_src == NULL)
 {
    printf("File open failed. Exiting.\n");
    exit (1);
 }

 /* In windows, this will init the winsock stuff */
 curl_global_init(CURL_GLOBAL_ALL);
 
 /* get a curl handle */
 curl = curl_easy_init();
 if(curl) {

    curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
    curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &config);

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

   /* specify target */
   curl_easy_setopt(curl,CURLOPT_URL, REMOTE_URL);
 
   curl_easy_setopt(curl, CURLOPT_FTP_RESPONSE_TIMEOUT, response_timeout);

   curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);

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

   /* Set the size of the file to upload (optional). If you give a *_LARGE
      option you MUST make sure that the type of the passed-in argument is a
      curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
      make sure that to pass in a type 'long' argument. */
   
   curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
                   (curl_off_t)file_info.st_size);

   curl_easy_setopt(curl, CURLOPT_USERPWD, "simple_at_vaultnow.com:s1mpl3");
   

   if (debug_flag == TRUE)
   {
     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
     curl_easy_setopt(curl, CURLOPT_HEADER, 1);
     printf("calling curl_easy_perform\n");
   }

   /* pass in that last of FTP commands to run after the transfer */

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

   if (debug_flag == TRUE)
      printf("result is %d\n",res);

   /* always cleanup */
   curl_easy_cleanup(curl);
 }

/*
 fclose(hd_src); */ /* close the local file */

 
 curl_global_cleanup();
 return 0;
}

=========================================================================
Here's the trace
== Info: About to connect() to ftp.vaultnow.com port 21
== Info: Trying 66.235.193.158... == Info: connected
== Info: Connected to ftp.vaultnow.com (66.235.193.158) port 21
<= Recv header, 53 bytes (0xzx)
000zx: 220---------- Welcome to Pure-FTPd [TLS] ----------
<= Recv header, 42 bytes (0xzx)
000zx: 220-You are user number 1 of 50 allowed.
<= Recv header, 47 bytes (0xzx)
000zx: 220-Local time is now 21:55. Server port: 21.
<= Recv header, 62 bytes (0xzx)
000zx: 220 You will be disconnected after 15 minutes of inactivity.
=> Send header, 26 bytes (0xzx)
000zx: USER simple_at_vaultnow.com
<= Recv header, 52 bytes (0xzx)
000zx: 331 User simple_at_vaultnow.com OK. Password required
=> Send header, 13 bytes (0xzx)
000zx: PASS s1mpl3
<= Recv header, 61 bytes (0xzx)
000zx: 230-User simple_at_vaultnow.com has group access to: vaultnow
<= Recv header, 43 bytes (0xzx)
000zx: 230 OK. Current restricted directory is /
=> Send header, 5 bytes (0xzx)
000zx: PWD
<= Recv header, 34 bytes (0xzx)
000zx: 257 "/" is your current location
== Info: Entry path is '/'
=> Send header, 6 bytes (0xzx)
000zx: EPSV
== Info: Connect data stream passively
<= Recv header, 21 bytes (0xzx)
000zx: 500 Unknown command
== Info: disabling EPSV usage
=> Send header, 6 bytes (0xzx)
000zx: PASV
<= Recv header, 51 bytes (0xzx)
000zx: 227 Entering Passive Mode (66,235,193,158,31,161)
== Info: Trying 66.235.193.158... == Info: connected
== Info: Connecting to 66.235.193.158 (66.235.193.158) port 8097
=> Send header, 8 bytes (0xzx)
000zx: TYPE I
<= Recv header, 30 bytes (0xzx)
000zx: 200 TYPE is now 8-bit binary
=> Send header, 20 bytes (0xzx)
000zx: STOR stat_file.dat
<= Recv header, 30 bytes (0xzx)
000zx: 150 Accepted data connection
*** We read -1 bytes from file
<= Recv header, 35 bytes (0xzx)
000zx: 226 File successfully transferred
== Info: Uploaded unaligned file size (0 out of 24 bytes)
== Info: Connection #0 to host ftp.vaultnow.com left intact
== Info: Transferred a partial file
=> Send header, 6 bytes (0xzx)
000zx: QUIT
<= Recv header, 54 bytes (0xzx)
000zx: 221-Goodbye. You uploaded 0 and downloaded 0 kbytes.
<= Recv header, 13 bytes (0xzx)
000zx: 221 Logout.
== Info: Closing connection #0
Received on 2006-11-01