cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: STOR (nil)?

From: Guido Reina <guidoreina_at_isdn-si.com>
Date: Wed, 30 Oct 2002 09:27:20 +0100

Hi Daniel,

I am sorry for my delay (I am also working in another project :( ). I am
sure it is not libcurl's fault but mine, here is my code (I hope you
understand what I am doing). Thank you very much for your time

Guido.

static BOOL MustUpload (CURL *curl, char *url, char *file_name, DWORD
dwLocalFileSize, DWORD *pdwRemoteFileSize)
{
    struct curl_slist *headerlist = NULL;
    char cmd[_MAX_FNAME + 10];
    CURLcode res;

    *pdwRemoteFileSize = 0;

    sprintf (cmd, "SIZE %s", file_name);
    headerlist = curl_slist_append (headerlist, cmd);

    curl_easy_setopt (curl, CURLOPT_VERBOSE, TRUE);
    curl_easy_setopt (curl, CURLOPT_DEBUGFUNCTION, MyDebug);
    curl_easy_setopt (curl, CURLOPT_DEBUGDATA, pdwFileSize);

    curl_easy_setopt (curl, CURLOPT_URL, url);

    curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);

    res = curl_easy_perform (curl);

    curl_slist_free_all (headerlist);

    curl_easy_setopt (curl, CURLOPT_VERBOSE, FALSE);

    if (res != CURLE_OK) {
        return TRUE;
    } else {
        if (dwLocalFileSize == *pdwRemoteFileSize) {
            return FALSE;
        } else {
            *pdwRemoteFileSize = 0;

            return TRUE;
        }
    }
}

static int MyDebug (CURL *curl, curl_infotype infotype, char *data, size_t
datalen, void *pdwRemoteFileSize)
{
    char *ptr = data;

    if (strncmp (data, "213 ", 4) == 0) {
        ptr += 4;

        *((DWORD *) pdwRemoteFileSize)= StrToInt (ptr);
    }

    return 0;
}

// Get a curl handle.
curl = curl_easy_init ();
if (!curl) {
  fclose (hd_src);
  return;
}

// Enable uploading.
curl_easy_setopt (curl, CURLOPT_UPLOAD, TRUE);

// Set port.
curl_easy_setopt (curl, CURLOPT_PORT, 21);

// Set login:password.
sprintf (login_password, "%s:%s", login, password);
curl_easy_setopt (curl, CURLOPT_USERPWD, login_password);

// Initial directory.
strcpy (remote_directory, "1/2/3");

// Here I create the tree directory (MKDIR 1 -> MKDIR 1/2 -> MKDIR 1/2/3
while (ptr) {
  curl_easy_setopt (curl, CURLOPT_URL, url);

  ptr = strchr (ptr + 1, '/');
  if (ptr) {
   len = ptr - remote_directory;
  } else {
   len = strlen (remote_directory);
  }

  memset (msg, 0, sizeof (msg));

  strcpy (msg, "MKD ");
  strncat (msg, remote_directory, len);

  headerlist = curl_slist_append (headerlist, msg);

  curl_easy_setopt (curl, CURLOPT_POSTQUOTE, headerlist);

  res = curl_easy_perform (curl);

  curl_slist_free_all (headerlist);
  headerlist = NULL;

  curl_easy_cleanup (curl); // I know I shouldn't cleanup and call
curl_easy_init, that I can use the same connection but somehow didn't work
for me.

  // Get a curl handle.
  curl = curl_easy_init ();
  if (!curl) {
   fclose (hd_src);

   strErrorMsg.FormatMessage (IDS_E_CURL_EASY_INIT);
   WriteErrorLog (strErrorMsg);

   return 0;
  }

  // Enable uploading.
  curl_easy_setopt (curl, CURLOPT_UPLOAD, TRUE);

  // Set port.
  curl_easy_setopt (curl, CURLOPT_PORT, pFileProgress->pFtpSite->port);

  // Set login:password.
  curl_easy_setopt (curl, CURLOPT_USERPWD, login_password);
 }

 // At this point the directory where we are going to place the file should
exist.

 if (*remote_directory) {
  sprintf (url, "ftp://%s/%s/", URL, remote_directory);
 } else {
  sprintf (url, "ftp://%s/", URL);
 }

 if (MustUpload (curl, url, pFileProgress->file_name, pFileProgress)) {
  // Specify target.

  if (*remote_directory) {
   sprintf (url, "ftp://%s/%s/%s", URL, remote_directory, file_name);
  } else {
   sprintf (url, "ftp://%s/%s", URL, file_name);
  }

  curl_easy_setopt (curl, CURLOPT_URL, url);

  curl_easy_setopt (curl, CURLOPT_NOPROGRESS, FALSE);
  curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, MyProgress);
  curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, pFileProgress);

  curl_easy_setopt (curl, CURLOPT_POSTQUOTE, NULL);

  curl_easy_setopt (curl, CURLOPT_INFILE, hd_src);

  curl_easy_setopt (curl, CURLOPT_INFILESIZE, file_size);

  do {
   res = curl_easy_perform (curl);

   if ((res == CURLE_SEND_ERROR) || (res == CURLE_COULDNT_CONNECT) || (res
== CURLE_FTP_COULDNT_STOR_FILE)) {
    curl_easy_setopt (curl, CURLOPT_FTPAPPEND, TRUE);
    curl_easy_setopt (curl, CURLOPT_RESUME_FROM, bytes_transfered);
   }
  } while ((res == CURLE_PARTIAL_FILE) || (res == CURLE_SEND_ERROR) || (res
== CURLE_COULDNT_CONNECT) || (res == CURLE_FTP_COULDNT_STOR_FILE));

  curl_slist_free_all (headerlist);
 } else {
  res = CURLE_OK;
 }

-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
Received on 2002-10-30