2011-03-10 05:48:02 -05:00
|
|
|
/***************************************************************************
|
2004-07-14 10:20:53 -04:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
2003-04-01 02:13:04 -05:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2020-10-02 04:58:52 -04:00
|
|
|
* Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-03-10 05:48:02 -05:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2011-03-10 05:48:02 -05:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2003-04-01 02:13:04 -05:00
|
|
|
#include "test.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h"
|
2008-09-20 00:26:55 -04:00
|
|
|
|
2003-04-01 02:13:04 -05:00
|
|
|
/*
|
|
|
|
* 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 07:34:17 -05:00
|
|
|
int test(char *URL)
|
2003-04-01 02:13:04 -05:00
|
|
|
{
|
|
|
|
CURL *curl;
|
2006-06-10 13:35:28 -04:00
|
|
|
CURLcode res = CURLE_OK;
|
2016-04-03 05:57:34 -04:00
|
|
|
FILE *hd_src;
|
|
|
|
int hd;
|
2005-05-27 07:01:41 -04:00
|
|
|
struct_stat file_info;
|
2006-10-25 01:59:46 -04:00
|
|
|
struct curl_slist *hl;
|
2003-04-01 02:13:04 -05:00
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
struct curl_slist *headerlist = NULL;
|
2003-04-01 02:13:04 -05:00
|
|
|
const char *buf_1 = "RNFR 505";
|
|
|
|
const char *buf_2 = "RNTO 505-forreal";
|
|
|
|
|
2016-04-03 05:57:34 -04:00
|
|
|
if(!libtest_arg2) {
|
2005-07-28 09:20:27 -04:00
|
|
|
fprintf(stderr, "Usage: <url> <file-to-upload>\n");
|
2014-05-05 09:30:05 -04:00
|
|
|
return TEST_ERR_USAGE;
|
2005-07-28 09:20:27 -04:00
|
|
|
}
|
|
|
|
|
2010-04-07 01:51:05 -04:00
|
|
|
hd_src = fopen(libtest_arg2, "rb");
|
|
|
|
if(NULL == hd_src) {
|
2016-04-03 05:57:34 -04:00
|
|
|
fprintf(stderr, "fopen failed with error: %d %s\n",
|
2017-07-11 01:53:23 -04:00
|
|
|
errno, strerror(errno));
|
2010-04-07 01:51:05 -04:00
|
|
|
fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
|
2014-05-05 09:30:05 -04:00
|
|
|
return TEST_ERR_MAJOR_BAD; /* if this happens things are major weird */
|
2010-04-07 01:51:05 -04:00
|
|
|
}
|
|
|
|
|
2003-04-01 02:13:04 -05:00
|
|
|
/* get the file size of the local file */
|
2010-04-07 01:51:05 -04:00
|
|
|
hd = fstat(fileno(hd_src), &file_info);
|
2003-04-01 02:13:04 -05:00
|
|
|
if(hd == -1) {
|
|
|
|
/* can't open file, bail out */
|
2010-04-07 01:51:05 -04:00
|
|
|
fprintf(stderr, "fstat() failed with error: %d %s\n",
|
2017-07-11 01:53:23 -04:00
|
|
|
errno, strerror(errno));
|
2010-04-07 01:51:05 -04:00
|
|
|
fprintf(stderr, "ERROR: cannot open file %s\n", libtest_arg2);
|
|
|
|
fclose(hd_src);
|
2014-05-05 09:30:05 -04:00
|
|
|
return TEST_ERR_MAJOR_BAD;
|
2003-04-01 02:13:04 -05:00
|
|
|
}
|
2003-04-15 10:18:37 -04:00
|
|
|
|
2020-10-02 04:58:52 -04:00
|
|
|
if(!file_info.st_size) {
|
2010-04-07 01:51:05 -04:00
|
|
|
fprintf(stderr, "ERROR: file %s has zero size!\n", libtest_arg2);
|
|
|
|
fclose(hd_src);
|
2014-05-05 09:30:05 -04:00
|
|
|
return TEST_ERR_MAJOR_BAD;
|
2003-04-15 10:18:37 -04:00
|
|
|
}
|
2003-04-01 02:13:04 -05:00
|
|
|
|
2016-04-03 05:57:34 -04:00
|
|
|
if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
|
2006-10-25 01:59:46 -04:00
|
|
|
fprintf(stderr, "curl_global_init() failed\n");
|
|
|
|
fclose(hd_src);
|
|
|
|
return TEST_ERR_MAJOR_BAD;
|
|
|
|
}
|
2003-04-01 02:13:04 -05:00
|
|
|
|
|
|
|
/* get a curl handle */
|
2016-04-03 05:57:34 -04:00
|
|
|
curl = curl_easy_init();
|
|
|
|
if(!curl) {
|
2006-10-25 01:59:46 -04:00
|
|
|
fprintf(stderr, "curl_easy_init() failed\n");
|
|
|
|
curl_global_cleanup();
|
|
|
|
fclose(hd_src);
|
|
|
|
return TEST_ERR_MAJOR_BAD;
|
|
|
|
}
|
2003-04-01 02:13:04 -05:00
|
|
|
|
2006-10-25 01:59:46 -04:00
|
|
|
/* build a list of commands to pass to libcurl */
|
2003-04-01 02:13:04 -05:00
|
|
|
|
2016-04-03 05:57:34 -04:00
|
|
|
hl = curl_slist_append(headerlist, buf_1);
|
|
|
|
if(!hl) {
|
2006-10-25 01:59:46 -04:00
|
|
|
fprintf(stderr, "curl_slist_append() failed\n");
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
curl_global_cleanup();
|
|
|
|
fclose(hd_src);
|
|
|
|
return TEST_ERR_MAJOR_BAD;
|
|
|
|
}
|
2016-04-03 05:57:34 -04:00
|
|
|
headerlist = curl_slist_append(hl, buf_2);
|
|
|
|
if(!headerlist) {
|
2006-10-25 01:59:46 -04:00
|
|
|
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 02:13:04 -05:00
|
|
|
|
2006-10-25 01:59:46 -04:00
|
|
|
/* enable uploading */
|
2010-02-05 13:07:19 -05:00
|
|
|
test_setopt(curl, CURLOPT_UPLOAD, 1L);
|
2003-04-01 02:13:04 -05:00
|
|
|
|
2006-10-25 01:59:46 -04:00
|
|
|
/* enable verbose */
|
2010-02-05 13:07:19 -05:00
|
|
|
test_setopt(curl, CURLOPT_VERBOSE, 1L);
|
2003-04-01 02:13:04 -05:00
|
|
|
|
2006-10-25 01:59:46 -04:00
|
|
|
/* specify target */
|
2016-04-03 05:57:34 -04:00
|
|
|
test_setopt(curl, CURLOPT_URL, URL);
|
2003-04-01 02:13:04 -05:00
|
|
|
|
2006-10-25 01:59:46 -04:00
|
|
|
/* pass in that last of FTP commands to run after the transfer */
|
2010-02-05 13:07:19 -05:00
|
|
|
test_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
|
2003-04-01 02:13:04 -05:00
|
|
|
|
2006-10-25 01:59:46 -04:00
|
|
|
/* now specify which file to upload */
|
2014-06-17 19:17:23 -04:00
|
|
|
test_setopt(curl, CURLOPT_READDATA, hd_src);
|
2003-04-01 02:13:04 -05:00
|
|
|
|
2006-10-25 01:59:46 -04:00
|
|
|
/* and give the size of the upload (optional) */
|
2010-02-05 13:07:19 -05:00
|
|
|
test_setopt(curl, CURLOPT_INFILESIZE_LARGE,
|
2006-10-25 01:59:46 -04:00
|
|
|
(curl_off_t)file_info.st_size);
|
2003-04-01 02:13:04 -05:00
|
|
|
|
2006-10-25 01:59:46 -04:00
|
|
|
/* Now run off and do what you've been told! */
|
|
|
|
res = curl_easy_perform(curl);
|
|
|
|
|
2010-02-05 13:07:19 -05:00
|
|
|
test_cleanup:
|
|
|
|
|
2006-10-25 01:59:46 -04:00
|
|
|
/* clean up the FTP commands list */
|
|
|
|
curl_slist_free_all(headerlist);
|
2003-04-01 02:13:04 -05:00
|
|
|
|
2006-10-25 01:59:46 -04:00
|
|
|
/* close the local file */
|
|
|
|
fclose(hd_src);
|
|
|
|
|
|
|
|
curl_easy_cleanup(curl);
|
2003-04-01 02:13:04 -05:00
|
|
|
curl_global_cleanup();
|
2006-10-25 01:59:46 -04:00
|
|
|
|
2003-04-01 02:13:04 -05:00
|
|
|
return res;
|
|
|
|
}
|