• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at http://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22 #include "test.h"
23 
24 #ifdef HAVE_FCNTL_H
25 #include <fcntl.h>
26 #endif
27 
28 #include "memdebug.h"
29 
30 /*
31  * This example shows an FTP upload, with a rename of the file just after
32  * a successful upload.
33  *
34  * Example based on source code provided by Erick Nuwendam. Thanks!
35  */
36 
test(char * URL)37 int test(char *URL)
38 {
39   CURL *curl;
40   CURLcode res = CURLE_OK;
41   FILE *hd_src ;
42   int hd ;
43   struct_stat file_info;
44   struct curl_slist *hl;
45   int error;
46 
47   struct curl_slist *headerlist=NULL;
48   const char *buf_1 = "RNFR 505";
49   const char *buf_2 = "RNTO 505-forreal";
50 
51   if (!libtest_arg2) {
52     fprintf(stderr, "Usage: <url> <file-to-upload>\n");
53     return TEST_ERR_USAGE;
54   }
55 
56   hd_src = fopen(libtest_arg2, "rb");
57   if(NULL == hd_src) {
58     error = ERRNO;
59     fprintf(stderr, "fopen() failed with error: %d %s\n",
60             error, strerror(error));
61     fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
62     return TEST_ERR_MAJOR_BAD; /* if this happens things are major weird */
63   }
64 
65   /* get the file size of the local file */
66   hd = fstat(fileno(hd_src), &file_info);
67   if(hd == -1) {
68     /* can't open file, bail out */
69     error = ERRNO;
70     fprintf(stderr, "fstat() failed with error: %d %s\n",
71             error, strerror(error));
72     fprintf(stderr, "ERROR: cannot open file %s\n", libtest_arg2);
73     fclose(hd_src);
74     return TEST_ERR_MAJOR_BAD;
75   }
76 
77   if(! file_info.st_size) {
78     fprintf(stderr, "ERROR: file %s has zero size!\n", libtest_arg2);
79     fclose(hd_src);
80     return TEST_ERR_MAJOR_BAD;
81   }
82 
83   if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
84     fprintf(stderr, "curl_global_init() failed\n");
85     fclose(hd_src);
86     return TEST_ERR_MAJOR_BAD;
87   }
88 
89   /* get a curl handle */
90   if ((curl = curl_easy_init()) == NULL) {
91     fprintf(stderr, "curl_easy_init() failed\n");
92     curl_global_cleanup();
93     fclose(hd_src);
94     return TEST_ERR_MAJOR_BAD;
95   }
96 
97   /* build a list of commands to pass to libcurl */
98 
99   if ((hl = curl_slist_append(headerlist, buf_1)) == NULL) {
100     fprintf(stderr, "curl_slist_append() failed\n");
101     curl_easy_cleanup(curl);
102     curl_global_cleanup();
103     fclose(hd_src);
104     return TEST_ERR_MAJOR_BAD;
105   }
106   if ((headerlist = curl_slist_append(hl, buf_2)) == NULL) {
107     fprintf(stderr, "curl_slist_append() failed\n");
108     curl_slist_free_all(hl);
109     curl_easy_cleanup(curl);
110     curl_global_cleanup();
111     fclose(hd_src);
112     return TEST_ERR_MAJOR_BAD;
113   }
114   headerlist = hl;
115 
116   /* enable uploading */
117   test_setopt(curl, CURLOPT_UPLOAD, 1L);
118 
119   /* enable verbose */
120   test_setopt(curl, CURLOPT_VERBOSE, 1L);
121 
122   /* specify target */
123   test_setopt(curl,CURLOPT_URL, URL);
124 
125   /* pass in that last of FTP commands to run after the transfer */
126   test_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
127 
128   /* now specify which file to upload */
129   test_setopt(curl, CURLOPT_READDATA, hd_src);
130 
131   /* and give the size of the upload (optional) */
132   test_setopt(curl, CURLOPT_INFILESIZE_LARGE,
133                    (curl_off_t)file_info.st_size);
134 
135   /* Now run off and do what you've been told! */
136   res = curl_easy_perform(curl);
137 
138 test_cleanup:
139 
140   /* clean up the FTP commands list */
141   curl_slist_free_all(headerlist);
142 
143   /* close the local file */
144   fclose(hd_src);
145 
146   curl_easy_cleanup(curl);
147   curl_global_cleanup();
148 
149   return res;
150 }
151