xref: /aosp_15_r20/external/curl/docs/examples/ftpuploadresume.c (revision 6236dae45794135f37c4eb022389c904c8b0090d)
1*6236dae4SAndroid Build Coastguard Worker /***************************************************************************
2*6236dae4SAndroid Build Coastguard Worker  *                                  _   _ ____  _
3*6236dae4SAndroid Build Coastguard Worker  *  Project                     ___| | | |  _ \| |
4*6236dae4SAndroid Build Coastguard Worker  *                             / __| | | | |_) | |
5*6236dae4SAndroid Build Coastguard Worker  *                            | (__| |_| |  _ <| |___
6*6236dae4SAndroid Build Coastguard Worker  *                             \___|\___/|_| \_\_____|
7*6236dae4SAndroid Build Coastguard Worker  *
8*6236dae4SAndroid Build Coastguard Worker  * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
9*6236dae4SAndroid Build Coastguard Worker  *
10*6236dae4SAndroid Build Coastguard Worker  * This software is licensed as described in the file COPYING, which
11*6236dae4SAndroid Build Coastguard Worker  * you should have received as part of this distribution. The terms
12*6236dae4SAndroid Build Coastguard Worker  * are also available at https://curl.se/docs/copyright.html.
13*6236dae4SAndroid Build Coastguard Worker  *
14*6236dae4SAndroid Build Coastguard Worker  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15*6236dae4SAndroid Build Coastguard Worker  * copies of the Software, and permit persons to whom the Software is
16*6236dae4SAndroid Build Coastguard Worker  * furnished to do so, under the terms of the COPYING file.
17*6236dae4SAndroid Build Coastguard Worker  *
18*6236dae4SAndroid Build Coastguard Worker  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19*6236dae4SAndroid Build Coastguard Worker  * KIND, either express or implied.
20*6236dae4SAndroid Build Coastguard Worker  *
21*6236dae4SAndroid Build Coastguard Worker  * SPDX-License-Identifier: curl
22*6236dae4SAndroid Build Coastguard Worker  *
23*6236dae4SAndroid Build Coastguard Worker  ***************************************************************************/
24*6236dae4SAndroid Build Coastguard Worker /* <DESC>
25*6236dae4SAndroid Build Coastguard Worker  * Upload to FTP, resuming failed transfers. Active mode.
26*6236dae4SAndroid Build Coastguard Worker  * </DESC>
27*6236dae4SAndroid Build Coastguard Worker  */
28*6236dae4SAndroid Build Coastguard Worker 
29*6236dae4SAndroid Build Coastguard Worker #include <stdlib.h>
30*6236dae4SAndroid Build Coastguard Worker #include <stdio.h>
31*6236dae4SAndroid Build Coastguard Worker #include <curl/curl.h>
32*6236dae4SAndroid Build Coastguard Worker 
33*6236dae4SAndroid Build Coastguard Worker /* parse headers for Content-Length */
getcontentlengthfunc(void * ptr,size_t size,size_t nmemb,void * stream)34*6236dae4SAndroid Build Coastguard Worker static size_t getcontentlengthfunc(void *ptr, size_t size, size_t nmemb,
35*6236dae4SAndroid Build Coastguard Worker                                    void *stream)
36*6236dae4SAndroid Build Coastguard Worker {
37*6236dae4SAndroid Build Coastguard Worker   int r;
38*6236dae4SAndroid Build Coastguard Worker   long len = 0;
39*6236dae4SAndroid Build Coastguard Worker 
40*6236dae4SAndroid Build Coastguard Worker   r = sscanf(ptr, "Content-Length: %ld\n", &len);
41*6236dae4SAndroid Build Coastguard Worker   if(r)
42*6236dae4SAndroid Build Coastguard Worker     *((long *) stream) = len;
43*6236dae4SAndroid Build Coastguard Worker 
44*6236dae4SAndroid Build Coastguard Worker   return size * nmemb;
45*6236dae4SAndroid Build Coastguard Worker }
46*6236dae4SAndroid Build Coastguard Worker 
47*6236dae4SAndroid Build Coastguard Worker /* discard downloaded data */
discardfunc(void * ptr,size_t size,size_t nmemb,void * stream)48*6236dae4SAndroid Build Coastguard Worker static size_t discardfunc(void *ptr, size_t size, size_t nmemb, void *stream)
49*6236dae4SAndroid Build Coastguard Worker {
50*6236dae4SAndroid Build Coastguard Worker   (void)ptr;
51*6236dae4SAndroid Build Coastguard Worker   (void)stream;
52*6236dae4SAndroid Build Coastguard Worker   return size * nmemb;
53*6236dae4SAndroid Build Coastguard Worker }
54*6236dae4SAndroid Build Coastguard Worker 
55*6236dae4SAndroid Build Coastguard Worker /* read data to upload */
readfunc(char * ptr,size_t size,size_t nmemb,void * stream)56*6236dae4SAndroid Build Coastguard Worker static size_t readfunc(char *ptr, size_t size, size_t nmemb, void *stream)
57*6236dae4SAndroid Build Coastguard Worker {
58*6236dae4SAndroid Build Coastguard Worker   FILE *f = stream;
59*6236dae4SAndroid Build Coastguard Worker   size_t n;
60*6236dae4SAndroid Build Coastguard Worker 
61*6236dae4SAndroid Build Coastguard Worker   if(ferror(f))
62*6236dae4SAndroid Build Coastguard Worker     return CURL_READFUNC_ABORT;
63*6236dae4SAndroid Build Coastguard Worker 
64*6236dae4SAndroid Build Coastguard Worker   n = fread(ptr, size, nmemb, f) * size;
65*6236dae4SAndroid Build Coastguard Worker 
66*6236dae4SAndroid Build Coastguard Worker   return n;
67*6236dae4SAndroid Build Coastguard Worker }
68*6236dae4SAndroid Build Coastguard Worker 
69*6236dae4SAndroid Build Coastguard Worker 
upload(CURL * curlhandle,const char * remotepath,const char * localpath,long timeout,long tries)70*6236dae4SAndroid Build Coastguard Worker static int upload(CURL *curlhandle, const char *remotepath,
71*6236dae4SAndroid Build Coastguard Worker                   const char *localpath, long timeout, long tries)
72*6236dae4SAndroid Build Coastguard Worker {
73*6236dae4SAndroid Build Coastguard Worker   FILE *f;
74*6236dae4SAndroid Build Coastguard Worker   long uploaded_len = 0;
75*6236dae4SAndroid Build Coastguard Worker   CURLcode r = CURLE_GOT_NOTHING;
76*6236dae4SAndroid Build Coastguard Worker   int c;
77*6236dae4SAndroid Build Coastguard Worker 
78*6236dae4SAndroid Build Coastguard Worker   f = fopen(localpath, "rb");
79*6236dae4SAndroid Build Coastguard Worker   if(!f) {
80*6236dae4SAndroid Build Coastguard Worker     perror(NULL);
81*6236dae4SAndroid Build Coastguard Worker     return 0;
82*6236dae4SAndroid Build Coastguard Worker   }
83*6236dae4SAndroid Build Coastguard Worker 
84*6236dae4SAndroid Build Coastguard Worker   curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
85*6236dae4SAndroid Build Coastguard Worker 
86*6236dae4SAndroid Build Coastguard Worker   curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
87*6236dae4SAndroid Build Coastguard Worker 
88*6236dae4SAndroid Build Coastguard Worker   if(timeout)
89*6236dae4SAndroid Build Coastguard Worker     curl_easy_setopt(curlhandle, CURLOPT_SERVER_RESPONSE_TIMEOUT, timeout);
90*6236dae4SAndroid Build Coastguard Worker 
91*6236dae4SAndroid Build Coastguard Worker   curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);
92*6236dae4SAndroid Build Coastguard Worker   curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &uploaded_len);
93*6236dae4SAndroid Build Coastguard Worker 
94*6236dae4SAndroid Build Coastguard Worker   curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, discardfunc);
95*6236dae4SAndroid Build Coastguard Worker 
96*6236dae4SAndroid Build Coastguard Worker   curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);
97*6236dae4SAndroid Build Coastguard Worker   curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);
98*6236dae4SAndroid Build Coastguard Worker 
99*6236dae4SAndroid Build Coastguard Worker   /* enable active mode */
100*6236dae4SAndroid Build Coastguard Worker   curl_easy_setopt(curlhandle, CURLOPT_FTPPORT, "-");
101*6236dae4SAndroid Build Coastguard Worker 
102*6236dae4SAndroid Build Coastguard Worker   /* allow the server no more than 7 seconds to connect back */
103*6236dae4SAndroid Build Coastguard Worker   curl_easy_setopt(curlhandle, CURLOPT_ACCEPTTIMEOUT_MS, 7000L);
104*6236dae4SAndroid Build Coastguard Worker 
105*6236dae4SAndroid Build Coastguard Worker   curl_easy_setopt(curlhandle, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);
106*6236dae4SAndroid Build Coastguard Worker 
107*6236dae4SAndroid Build Coastguard Worker   curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);
108*6236dae4SAndroid Build Coastguard Worker 
109*6236dae4SAndroid Build Coastguard Worker   for(c = 0; (r != CURLE_OK) && (c < tries); c++) {
110*6236dae4SAndroid Build Coastguard Worker     /* are we resuming? */
111*6236dae4SAndroid Build Coastguard Worker     if(c) { /* yes */
112*6236dae4SAndroid Build Coastguard Worker       /* determine the length of the file already written */
113*6236dae4SAndroid Build Coastguard Worker 
114*6236dae4SAndroid Build Coastguard Worker       /*
115*6236dae4SAndroid Build Coastguard Worker        * With NOBODY and NOHEADER, libcurl issues a SIZE command, but the only
116*6236dae4SAndroid Build Coastguard Worker        * way to retrieve the result is to parse the returned Content-Length
117*6236dae4SAndroid Build Coastguard Worker        * header. Thus, getcontentlengthfunc(). We need discardfunc() above
118*6236dae4SAndroid Build Coastguard Worker        * because HEADER dumps the headers to stdout without it.
119*6236dae4SAndroid Build Coastguard Worker        */
120*6236dae4SAndroid Build Coastguard Worker       curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 1L);
121*6236dae4SAndroid Build Coastguard Worker       curl_easy_setopt(curlhandle, CURLOPT_HEADER, 1L);
122*6236dae4SAndroid Build Coastguard Worker 
123*6236dae4SAndroid Build Coastguard Worker       r = curl_easy_perform(curlhandle);
124*6236dae4SAndroid Build Coastguard Worker       if(r != CURLE_OK)
125*6236dae4SAndroid Build Coastguard Worker         continue;
126*6236dae4SAndroid Build Coastguard Worker 
127*6236dae4SAndroid Build Coastguard Worker       curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 0L);
128*6236dae4SAndroid Build Coastguard Worker       curl_easy_setopt(curlhandle, CURLOPT_HEADER, 0L);
129*6236dae4SAndroid Build Coastguard Worker 
130*6236dae4SAndroid Build Coastguard Worker       fseek(f, uploaded_len, SEEK_SET);
131*6236dae4SAndroid Build Coastguard Worker 
132*6236dae4SAndroid Build Coastguard Worker       curl_easy_setopt(curlhandle, CURLOPT_APPEND, 1L);
133*6236dae4SAndroid Build Coastguard Worker     }
134*6236dae4SAndroid Build Coastguard Worker     else { /* no */
135*6236dae4SAndroid Build Coastguard Worker       curl_easy_setopt(curlhandle, CURLOPT_APPEND, 0L);
136*6236dae4SAndroid Build Coastguard Worker     }
137*6236dae4SAndroid Build Coastguard Worker 
138*6236dae4SAndroid Build Coastguard Worker     r = curl_easy_perform(curlhandle);
139*6236dae4SAndroid Build Coastguard Worker   }
140*6236dae4SAndroid Build Coastguard Worker 
141*6236dae4SAndroid Build Coastguard Worker   fclose(f);
142*6236dae4SAndroid Build Coastguard Worker 
143*6236dae4SAndroid Build Coastguard Worker   if(r == CURLE_OK)
144*6236dae4SAndroid Build Coastguard Worker     return 1;
145*6236dae4SAndroid Build Coastguard Worker   else {
146*6236dae4SAndroid Build Coastguard Worker     fprintf(stderr, "%s\n", curl_easy_strerror(r));
147*6236dae4SAndroid Build Coastguard Worker     return 0;
148*6236dae4SAndroid Build Coastguard Worker   }
149*6236dae4SAndroid Build Coastguard Worker }
150*6236dae4SAndroid Build Coastguard Worker 
main(void)151*6236dae4SAndroid Build Coastguard Worker int main(void)
152*6236dae4SAndroid Build Coastguard Worker {
153*6236dae4SAndroid Build Coastguard Worker   CURL *curlhandle = NULL;
154*6236dae4SAndroid Build Coastguard Worker 
155*6236dae4SAndroid Build Coastguard Worker   curl_global_init(CURL_GLOBAL_ALL);
156*6236dae4SAndroid Build Coastguard Worker   curlhandle = curl_easy_init();
157*6236dae4SAndroid Build Coastguard Worker 
158*6236dae4SAndroid Build Coastguard Worker   upload(curlhandle, "ftp://user:[email protected]/path/file", "C:\\file",
159*6236dae4SAndroid Build Coastguard Worker          0, 3);
160*6236dae4SAndroid Build Coastguard Worker 
161*6236dae4SAndroid Build Coastguard Worker   curl_easy_cleanup(curlhandle);
162*6236dae4SAndroid Build Coastguard Worker   curl_global_cleanup();
163*6236dae4SAndroid Build Coastguard Worker 
164*6236dae4SAndroid Build Coastguard Worker   return 0;
165*6236dae4SAndroid Build Coastguard Worker }
166