ftpuploadresume.c 4.2 KB

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