1a24f7cbd680491799b59f0893ced121b781f2ec
[platform/upstream/curl.git] / docs / examples / ftpuploadresume.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2015, 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 /* <DESC>
23  * Upload to FTP, resuming failed transfers.
24  * </DESC>
25  */
26
27 #include <stdlib.h>
28 #include <stdio.h>
29
30 #include <curl/curl.h>
31
32 #if defined(_MSC_VER) && (_MSC_VER < 1300)
33 #  error _snscanf requires MSVC 7.0 or later.
34 #endif
35
36 /* The MinGW headers are missing a few Win32 function definitions,
37    you shouldn't need this if you use VC++ */
38 #if defined(__MINGW32__) && !defined(__MINGW64__)
39 int __cdecl _snscanf(const char * input, size_t length, const char * format, ...);
40 #endif
41
42
43 /* parse headers for Content-Length */
44 size_t getcontentlengthfunc(void *ptr, size_t size, size_t nmemb, void *stream)
45 {
46   int r;
47   long len = 0;
48
49   /* _snscanf() is Win32 specific */
50   r = _snscanf(ptr, size * nmemb, "Content-Length: %ld\n", &len);
51
52   if (r) /* Microsoft: we don't read the specs */
53     *((long *) stream) = len;
54
55   return size * nmemb;
56 }
57
58 /* discard downloaded data */
59 size_t discardfunc(void *ptr, size_t size, size_t nmemb, void *stream)
60 {
61   return size * nmemb;
62 }
63
64 /* read data to upload */
65 size_t readfunc(void *ptr, size_t size, size_t nmemb, void *stream)
66 {
67   FILE *f = stream;
68   size_t n;
69
70   if (ferror(f))
71     return CURL_READFUNC_ABORT;
72
73   n = fread(ptr, size, nmemb, f) * size;
74
75   return n;
76 }
77
78
79 int upload(CURL *curlhandle, const char * remotepath, const char * localpath,
80            long timeout, long tries)
81 {
82   FILE *f;
83   long uploaded_len = 0;
84   CURLcode r = CURLE_GOT_NOTHING;
85   int c;
86
87   f = fopen(localpath, "rb");
88   if (f == NULL) {
89     perror(NULL);
90     return 0;
91   }
92
93   curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
94
95   curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
96
97   if (timeout)
98     curl_easy_setopt(curlhandle, CURLOPT_FTP_RESPONSE_TIMEOUT, timeout);
99
100   curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);
101   curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &uploaded_len);
102
103   curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, discardfunc);
104
105   curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);
106   curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);
107
108   curl_easy_setopt(curlhandle, CURLOPT_FTPPORT, "-"); /* disable passive mode */
109   curl_easy_setopt(curlhandle, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);
110
111   curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);
112
113   for (c = 0; (r != CURLE_OK) && (c < tries); c++) {
114     /* are we resuming? */
115     if (c) { /* yes */
116       /* determine the length of the file already written */
117
118       /*
119        * With NOBODY and NOHEADER, libcurl will issue a SIZE
120        * command, but the only way to retrieve the result is
121        * to parse the returned Content-Length header. Thus,
122        * getcontentlengthfunc(). We need discardfunc() above
123        * because HEADER will dump the headers to stdout
124        * without it.
125        */
126       curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 1L);
127       curl_easy_setopt(curlhandle, CURLOPT_HEADER, 1L);
128
129       r = curl_easy_perform(curlhandle);
130       if (r != CURLE_OK)
131         continue;
132
133       curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 0L);
134       curl_easy_setopt(curlhandle, CURLOPT_HEADER, 0L);
135
136       fseek(f, uploaded_len, SEEK_SET);
137
138       curl_easy_setopt(curlhandle, CURLOPT_APPEND, 1L);
139     }
140     else { /* no */
141       curl_easy_setopt(curlhandle, CURLOPT_APPEND, 0L);
142     }
143
144     r = curl_easy_perform(curlhandle);
145   }
146
147   fclose(f);
148
149   if (r == CURLE_OK)
150     return 1;
151   else {
152     fprintf(stderr, "%s\n", curl_easy_strerror(r));
153     return 0;
154   }
155 }
156
157 int main(int c, char **argv)
158 {
159   CURL *curlhandle = NULL;
160
161   curl_global_init(CURL_GLOBAL_ALL);
162   curlhandle = curl_easy_init();
163
164   upload(curlhandle, "ftp://user:pass@example.com/path/file", "C:\\file", 0, 3);
165
166   curl_easy_cleanup(curlhandle);
167   curl_global_cleanup();
168
169   return 0;
170 }