ftpuploadresume.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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 https://curl.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /* <DESC>
  25. * Upload to FTP, resuming failed transfers. Active mode.
  26. * </DESC>
  27. */
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <curl/curl.h>
  31. /* parse headers for Content-Length */
  32. static size_t getcontentlengthfunc(void *ptr, size_t size, size_t nmemb,
  33. void *stream)
  34. {
  35. int r;
  36. long len = 0;
  37. r = sscanf(ptr, "Content-Length: %ld\n", &len);
  38. if(r)
  39. *((long *) stream) = len;
  40. return size * nmemb;
  41. }
  42. /* discard downloaded data */
  43. static size_t discardfunc(void *ptr, size_t size, size_t nmemb, void *stream)
  44. {
  45. (void)ptr;
  46. (void)stream;
  47. return size * nmemb;
  48. }
  49. /* read data to upload */
  50. static size_t readfunc(char *ptr, size_t size, size_t nmemb, void *stream)
  51. {
  52. FILE *f = stream;
  53. size_t n;
  54. if(ferror(f))
  55. return CURL_READFUNC_ABORT;
  56. n = fread(ptr, size, nmemb, f) * size;
  57. return n;
  58. }
  59. static int upload(CURL *curlhandle, const char *remotepath,
  60. const char *localpath, long timeout, long tries)
  61. {
  62. FILE *f;
  63. long uploaded_len = 0;
  64. CURLcode r = CURLE_GOT_NOTHING;
  65. int c;
  66. f = fopen(localpath, "rb");
  67. if(!f) {
  68. perror(NULL);
  69. return 0;
  70. }
  71. curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
  72. curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
  73. if(timeout)
  74. curl_easy_setopt(curlhandle, CURLOPT_SERVER_RESPONSE_TIMEOUT, timeout);
  75. curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);
  76. curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &uploaded_len);
  77. curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, discardfunc);
  78. curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);
  79. curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);
  80. /* enable active mode */
  81. curl_easy_setopt(curlhandle, CURLOPT_FTPPORT, "-");
  82. /* allow the server no more than 7 seconds to connect back */
  83. curl_easy_setopt(curlhandle, CURLOPT_ACCEPTTIMEOUT_MS, 7000L);
  84. curl_easy_setopt(curlhandle, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);
  85. curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);
  86. for(c = 0; (r != CURLE_OK) && (c < tries); c++) {
  87. /* are we resuming? */
  88. if(c) { /* yes */
  89. /* determine the length of the file already written */
  90. /*
  91. * With NOBODY and NOHEADER, libcurl will issue a SIZE
  92. * command, but the only way to retrieve the result is
  93. * to parse the returned Content-Length header. Thus,
  94. * getcontentlengthfunc(). We need discardfunc() above
  95. * because HEADER will dump the headers to stdout
  96. * without it.
  97. */
  98. curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 1L);
  99. curl_easy_setopt(curlhandle, CURLOPT_HEADER, 1L);
  100. r = curl_easy_perform(curlhandle);
  101. if(r != CURLE_OK)
  102. continue;
  103. curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 0L);
  104. curl_easy_setopt(curlhandle, CURLOPT_HEADER, 0L);
  105. fseek(f, uploaded_len, SEEK_SET);
  106. curl_easy_setopt(curlhandle, CURLOPT_APPEND, 1L);
  107. }
  108. else { /* no */
  109. curl_easy_setopt(curlhandle, CURLOPT_APPEND, 0L);
  110. }
  111. r = curl_easy_perform(curlhandle);
  112. }
  113. fclose(f);
  114. if(r == CURLE_OK)
  115. return 1;
  116. else {
  117. fprintf(stderr, "%s\n", curl_easy_strerror(r));
  118. return 0;
  119. }
  120. }
  121. int main(void)
  122. {
  123. CURL *curlhandle = NULL;
  124. curl_global_init(CURL_GLOBAL_ALL);
  125. curlhandle = curl_easy_init();
  126. upload(curlhandle, "ftp://user:pass@example.com/path/file", "C:\\file",
  127. 0, 3);
  128. curl_easy_cleanup(curlhandle);
  129. curl_global_cleanup();
  130. return 0;
  131. }