113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci/* <DESC>
2513498266Sopenharmony_ci * Upload to FTP, resuming failed transfers. Active mode.
2613498266Sopenharmony_ci * </DESC>
2713498266Sopenharmony_ci */
2813498266Sopenharmony_ci
2913498266Sopenharmony_ci#include <stdlib.h>
3013498266Sopenharmony_ci#include <stdio.h>
3113498266Sopenharmony_ci#include <curl/curl.h>
3213498266Sopenharmony_ci
3313498266Sopenharmony_ci/* parse headers for Content-Length */
3413498266Sopenharmony_cistatic size_t getcontentlengthfunc(void *ptr, size_t size, size_t nmemb,
3513498266Sopenharmony_ci                                   void *stream)
3613498266Sopenharmony_ci{
3713498266Sopenharmony_ci  int r;
3813498266Sopenharmony_ci  long len = 0;
3913498266Sopenharmony_ci
4013498266Sopenharmony_ci  r = sscanf(ptr, "Content-Length: %ld\n", &len);
4113498266Sopenharmony_ci  if(r)
4213498266Sopenharmony_ci    *((long *) stream) = len;
4313498266Sopenharmony_ci
4413498266Sopenharmony_ci  return size * nmemb;
4513498266Sopenharmony_ci}
4613498266Sopenharmony_ci
4713498266Sopenharmony_ci/* discard downloaded data */
4813498266Sopenharmony_cistatic size_t discardfunc(void *ptr, size_t size, size_t nmemb, void *stream)
4913498266Sopenharmony_ci{
5013498266Sopenharmony_ci  (void)ptr;
5113498266Sopenharmony_ci  (void)stream;
5213498266Sopenharmony_ci  return size * nmemb;
5313498266Sopenharmony_ci}
5413498266Sopenharmony_ci
5513498266Sopenharmony_ci/* read data to upload */
5613498266Sopenharmony_cistatic size_t readfunc(char *ptr, size_t size, size_t nmemb, void *stream)
5713498266Sopenharmony_ci{
5813498266Sopenharmony_ci  FILE *f = stream;
5913498266Sopenharmony_ci  size_t n;
6013498266Sopenharmony_ci
6113498266Sopenharmony_ci  if(ferror(f))
6213498266Sopenharmony_ci    return CURL_READFUNC_ABORT;
6313498266Sopenharmony_ci
6413498266Sopenharmony_ci  n = fread(ptr, size, nmemb, f) * size;
6513498266Sopenharmony_ci
6613498266Sopenharmony_ci  return n;
6713498266Sopenharmony_ci}
6813498266Sopenharmony_ci
6913498266Sopenharmony_ci
7013498266Sopenharmony_cistatic int upload(CURL *curlhandle, const char *remotepath,
7113498266Sopenharmony_ci                  const char *localpath, long timeout, long tries)
7213498266Sopenharmony_ci{
7313498266Sopenharmony_ci  FILE *f;
7413498266Sopenharmony_ci  long uploaded_len = 0;
7513498266Sopenharmony_ci  CURLcode r = CURLE_GOT_NOTHING;
7613498266Sopenharmony_ci  int c;
7713498266Sopenharmony_ci
7813498266Sopenharmony_ci  f = fopen(localpath, "rb");
7913498266Sopenharmony_ci  if(!f) {
8013498266Sopenharmony_ci    perror(NULL);
8113498266Sopenharmony_ci    return 0;
8213498266Sopenharmony_ci  }
8313498266Sopenharmony_ci
8413498266Sopenharmony_ci  curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
8513498266Sopenharmony_ci
8613498266Sopenharmony_ci  curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
8713498266Sopenharmony_ci
8813498266Sopenharmony_ci  if(timeout)
8913498266Sopenharmony_ci    curl_easy_setopt(curlhandle, CURLOPT_SERVER_RESPONSE_TIMEOUT, timeout);
9013498266Sopenharmony_ci
9113498266Sopenharmony_ci  curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);
9213498266Sopenharmony_ci  curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &uploaded_len);
9313498266Sopenharmony_ci
9413498266Sopenharmony_ci  curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, discardfunc);
9513498266Sopenharmony_ci
9613498266Sopenharmony_ci  curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);
9713498266Sopenharmony_ci  curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);
9813498266Sopenharmony_ci
9913498266Sopenharmony_ci  /* enable active mode */
10013498266Sopenharmony_ci  curl_easy_setopt(curlhandle, CURLOPT_FTPPORT, "-");
10113498266Sopenharmony_ci
10213498266Sopenharmony_ci  /* allow the server no more than 7 seconds to connect back */
10313498266Sopenharmony_ci  curl_easy_setopt(curlhandle, CURLOPT_ACCEPTTIMEOUT_MS, 7000L);
10413498266Sopenharmony_ci
10513498266Sopenharmony_ci  curl_easy_setopt(curlhandle, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);
10613498266Sopenharmony_ci
10713498266Sopenharmony_ci  curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);
10813498266Sopenharmony_ci
10913498266Sopenharmony_ci  for(c = 0; (r != CURLE_OK) && (c < tries); c++) {
11013498266Sopenharmony_ci    /* are we resuming? */
11113498266Sopenharmony_ci    if(c) { /* yes */
11213498266Sopenharmony_ci      /* determine the length of the file already written */
11313498266Sopenharmony_ci
11413498266Sopenharmony_ci      /*
11513498266Sopenharmony_ci       * With NOBODY and NOHEADER, libcurl will issue a SIZE
11613498266Sopenharmony_ci       * command, but the only way to retrieve the result is
11713498266Sopenharmony_ci       * to parse the returned Content-Length header. Thus,
11813498266Sopenharmony_ci       * getcontentlengthfunc(). We need discardfunc() above
11913498266Sopenharmony_ci       * because HEADER will dump the headers to stdout
12013498266Sopenharmony_ci       * without it.
12113498266Sopenharmony_ci       */
12213498266Sopenharmony_ci      curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 1L);
12313498266Sopenharmony_ci      curl_easy_setopt(curlhandle, CURLOPT_HEADER, 1L);
12413498266Sopenharmony_ci
12513498266Sopenharmony_ci      r = curl_easy_perform(curlhandle);
12613498266Sopenharmony_ci      if(r != CURLE_OK)
12713498266Sopenharmony_ci        continue;
12813498266Sopenharmony_ci
12913498266Sopenharmony_ci      curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 0L);
13013498266Sopenharmony_ci      curl_easy_setopt(curlhandle, CURLOPT_HEADER, 0L);
13113498266Sopenharmony_ci
13213498266Sopenharmony_ci      fseek(f, uploaded_len, SEEK_SET);
13313498266Sopenharmony_ci
13413498266Sopenharmony_ci      curl_easy_setopt(curlhandle, CURLOPT_APPEND, 1L);
13513498266Sopenharmony_ci    }
13613498266Sopenharmony_ci    else { /* no */
13713498266Sopenharmony_ci      curl_easy_setopt(curlhandle, CURLOPT_APPEND, 0L);
13813498266Sopenharmony_ci    }
13913498266Sopenharmony_ci
14013498266Sopenharmony_ci    r = curl_easy_perform(curlhandle);
14113498266Sopenharmony_ci  }
14213498266Sopenharmony_ci
14313498266Sopenharmony_ci  fclose(f);
14413498266Sopenharmony_ci
14513498266Sopenharmony_ci  if(r == CURLE_OK)
14613498266Sopenharmony_ci    return 1;
14713498266Sopenharmony_ci  else {
14813498266Sopenharmony_ci    fprintf(stderr, "%s\n", curl_easy_strerror(r));
14913498266Sopenharmony_ci    return 0;
15013498266Sopenharmony_ci  }
15113498266Sopenharmony_ci}
15213498266Sopenharmony_ci
15313498266Sopenharmony_ciint main(void)
15413498266Sopenharmony_ci{
15513498266Sopenharmony_ci  CURL *curlhandle = NULL;
15613498266Sopenharmony_ci
15713498266Sopenharmony_ci  curl_global_init(CURL_GLOBAL_ALL);
15813498266Sopenharmony_ci  curlhandle = curl_easy_init();
15913498266Sopenharmony_ci
16013498266Sopenharmony_ci  upload(curlhandle, "ftp://user:pass@example.com/path/file", "C:\\file",
16113498266Sopenharmony_ci         0, 3);
16213498266Sopenharmony_ci
16313498266Sopenharmony_ci  curl_easy_cleanup(curlhandle);
16413498266Sopenharmony_ci  curl_global_cleanup();
16513498266Sopenharmony_ci
16613498266Sopenharmony_ci  return 0;
16713498266Sopenharmony_ci}
168