2003-04-01 15:13:04 +08:00
|
|
|
/*****************************************************************************
|
2004-07-14 22:20:53 +08:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
2003-04-01 15:13:04 +08:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
2005-07-28 21:20:27 +08:00
|
|
|
#include "setup.h" /* struct_stat etc. */
|
2003-04-01 15:13:04 +08:00
|
|
|
#include "test.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
2004-12-01 21:41:22 +08:00
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
2003-04-01 15:13:04 +08:00
|
|
|
#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!
|
|
|
|
*/
|
|
|
|
|
2004-02-05 20:34:17 +08:00
|
|
|
int test(char *URL)
|
2003-04-01 15:13:04 +08:00
|
|
|
{
|
|
|
|
CURL *curl;
|
2006-06-11 01:35:28 +08:00
|
|
|
CURLcode res = CURLE_OK;
|
2003-04-01 15:13:04 +08:00
|
|
|
FILE *hd_src ;
|
|
|
|
int hd ;
|
2005-05-27 19:01:41 +08:00
|
|
|
struct_stat file_info;
|
2006-10-25 13:59:46 +08:00
|
|
|
struct curl_slist *hl;
|
2007-02-17 03:17:05 +08:00
|
|
|
int error;
|
2003-04-01 15:13:04 +08:00
|
|
|
|
|
|
|
struct curl_slist *headerlist=NULL;
|
|
|
|
const char *buf_1 = "RNFR 505";
|
|
|
|
const char *buf_2 = "RNTO 505-forreal";
|
|
|
|
|
2007-10-03 00:05:28 +08:00
|
|
|
if (!libtest_arg2) {
|
2005-07-28 21:20:27 +08:00
|
|
|
fprintf(stderr, "Usage: <url> <file-to-upload>\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-04-01 15:13:04 +08:00
|
|
|
/* get the file size of the local file */
|
2007-10-03 00:05:28 +08:00
|
|
|
hd = stat(libtest_arg2, &file_info);
|
2003-04-01 15:13:04 +08:00
|
|
|
if(hd == -1) {
|
|
|
|
/* can't open file, bail out */
|
2007-02-17 03:17:05 +08:00
|
|
|
error = ERRNO;
|
|
|
|
fprintf(stderr, "stat() failed with error: %d %s\n",
|
|
|
|
error, strerror(error));
|
2007-10-03 00:05:28 +08:00
|
|
|
fprintf(stderr, "WARNING: cannot open file %s\n", libtest_arg2);
|
2003-04-01 15:13:04 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2003-04-15 22:18:37 +08:00
|
|
|
|
|
|
|
if(! file_info.st_size) {
|
2007-10-03 00:05:28 +08:00
|
|
|
fprintf(stderr, "WARNING: file %s has no size!\n", libtest_arg2);
|
2003-04-15 22:18:37 +08:00
|
|
|
return -4;
|
|
|
|
}
|
2003-04-01 15:13:04 +08:00
|
|
|
|
|
|
|
/* get a FILE * of the same file, could also be made with
|
2004-07-14 22:20:53 +08:00
|
|
|
fdopen() from the previous descriptor, but hey this is just
|
2003-04-01 15:13:04 +08:00
|
|
|
an example! */
|
2007-10-03 00:05:28 +08:00
|
|
|
hd_src = fopen(libtest_arg2, "rb");
|
2003-04-01 15:13:04 +08:00
|
|
|
if(NULL == hd_src) {
|
2007-02-17 03:17:05 +08:00
|
|
|
error = ERRNO;
|
|
|
|
fprintf(stderr, "fopen() failed with error: %d %s\n",
|
|
|
|
error, strerror(error));
|
2007-10-03 00:05:28 +08:00
|
|
|
fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
|
2003-04-01 15:13:04 +08:00
|
|
|
return -2; /* if this happens things are major weird */
|
|
|
|
}
|
|
|
|
|
2006-10-25 13:59:46 +08:00
|
|
|
if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
|
|
|
|
fprintf(stderr, "curl_global_init() failed\n");
|
|
|
|
fclose(hd_src);
|
|
|
|
return TEST_ERR_MAJOR_BAD;
|
|
|
|
}
|
2003-04-01 15:13:04 +08:00
|
|
|
|
|
|
|
/* get a curl handle */
|
2006-10-25 13:59:46 +08:00
|
|
|
if ((curl = curl_easy_init()) == NULL) {
|
|
|
|
fprintf(stderr, "curl_easy_init() failed\n");
|
|
|
|
curl_global_cleanup();
|
|
|
|
fclose(hd_src);
|
|
|
|
return TEST_ERR_MAJOR_BAD;
|
|
|
|
}
|
2003-04-01 15:13:04 +08:00
|
|
|
|
2006-10-25 13:59:46 +08:00
|
|
|
/* build a list of commands to pass to libcurl */
|
2003-04-01 15:13:04 +08:00
|
|
|
|
2006-10-25 13:59:46 +08:00
|
|
|
if ((hl = curl_slist_append(headerlist, buf_1)) == NULL) {
|
|
|
|
fprintf(stderr, "curl_slist_append() failed\n");
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
curl_global_cleanup();
|
|
|
|
fclose(hd_src);
|
|
|
|
return TEST_ERR_MAJOR_BAD;
|
|
|
|
}
|
|
|
|
if ((headerlist = curl_slist_append(hl, buf_2)) == NULL) {
|
|
|
|
fprintf(stderr, "curl_slist_append() failed\n");
|
|
|
|
curl_slist_free_all(hl);
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
curl_global_cleanup();
|
|
|
|
fclose(hd_src);
|
|
|
|
return TEST_ERR_MAJOR_BAD;
|
|
|
|
}
|
|
|
|
headerlist = hl;
|
2003-04-01 15:13:04 +08:00
|
|
|
|
2006-10-25 13:59:46 +08:00
|
|
|
/* enable uploading */
|
2008-05-23 05:49:52 +08:00
|
|
|
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
|
2003-04-01 15:13:04 +08:00
|
|
|
|
2006-10-25 13:59:46 +08:00
|
|
|
/* enable verbose */
|
2008-05-23 05:49:52 +08:00
|
|
|
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
|
2003-04-01 15:13:04 +08:00
|
|
|
|
2006-10-25 13:59:46 +08:00
|
|
|
/* specify target */
|
|
|
|
curl_easy_setopt(curl,CURLOPT_URL, URL);
|
2003-04-01 15:13:04 +08:00
|
|
|
|
2006-10-25 13:59:46 +08:00
|
|
|
/* pass in that last of FTP commands to run after the transfer */
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
|
2003-04-01 15:13:04 +08:00
|
|
|
|
2006-10-25 13:59:46 +08:00
|
|
|
/* now specify which file to upload */
|
|
|
|
curl_easy_setopt(curl, CURLOPT_INFILE, hd_src);
|
2003-04-01 15:13:04 +08:00
|
|
|
|
2006-10-25 13:59:46 +08:00
|
|
|
/* and give the size of the upload (optional) */
|
|
|
|
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
|
|
|
|
(curl_off_t)file_info.st_size);
|
2003-04-01 15:13:04 +08:00
|
|
|
|
2006-10-25 13:59:46 +08:00
|
|
|
/* 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);
|
2003-04-01 15:13:04 +08:00
|
|
|
|
2006-10-25 13:59:46 +08:00
|
|
|
/* close the local file */
|
|
|
|
fclose(hd_src);
|
|
|
|
|
|
|
|
curl_easy_cleanup(curl);
|
2003-04-01 15:13:04 +08:00
|
|
|
curl_global_cleanup();
|
2006-10-25 13:59:46 +08:00
|
|
|
|
2003-04-01 15:13:04 +08:00
|
|
|
return res;
|
|
|
|
}
|