ftpuploadresume.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 issues a SIZE command, but the only
  92. * way to retrieve the result is to parse the returned Content-Length
  93. * header. Thus, getcontentlengthfunc(). We need discardfunc() above
  94. * because HEADER dumps the headers to stdout without it.
  95. */
  96. curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 1L);
  97. curl_easy_setopt(curlhandle, CURLOPT_HEADER, 1L);
  98. r = curl_easy_perform(curlhandle);
  99. if(r != CURLE_OK)
  100. continue;
  101. curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 0L);
  102. curl_easy_setopt(curlhandle, CURLOPT_HEADER, 0L);
  103. fseek(f, uploaded_len, SEEK_SET);
  104. curl_easy_setopt(curlhandle, CURLOPT_APPEND, 1L);
  105. }
  106. else { /* no */
  107. curl_easy_setopt(curlhandle, CURLOPT_APPEND, 0L);
  108. }
  109. r = curl_easy_perform(curlhandle);
  110. }
  111. fclose(f);
  112. if(r == CURLE_OK)
  113. return 1;
  114. else {
  115. fprintf(stderr, "%s\n", curl_easy_strerror(r));
  116. return 0;
  117. }
  118. }
  119. int main(void)
  120. {
  121. CURL *curlhandle = NULL;
  122. curl_global_init(CURL_GLOBAL_ALL);
  123. curlhandle = curl_easy_init();
  124. upload(curlhandle, "ftp://user:pass@example.com/path/file", "C:\\file",
  125. 0, 3);
  126. curl_easy_cleanup(curlhandle);
  127. curl_global_cleanup();
  128. return 0;
  129. }