2
0

ftpuploadresume.c 4.7 KB

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