sftpuploadresume.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2019, 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 SFTP, resuming a previously aborted transfer.
  24. * </DESC>
  25. */
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <curl/curl.h>
  29. /* read data to upload */
  30. static size_t readfunc(void *ptr, size_t size, size_t nmemb, void *stream)
  31. {
  32. FILE *f = (FILE *)stream;
  33. size_t n;
  34. if(ferror(f))
  35. return CURL_READFUNC_ABORT;
  36. n = fread(ptr, size, nmemb, f) * size;
  37. return n;
  38. }
  39. /*
  40. * sftpGetRemoteFileSize returns the remote file size in byte; -1 on error
  41. */
  42. static curl_off_t sftpGetRemoteFileSize(const char *i_remoteFile)
  43. {
  44. CURLcode result = CURLE_GOT_NOTHING;
  45. curl_off_t remoteFileSizeByte = -1;
  46. CURL *curlHandlePtr = NULL;
  47. curlHandlePtr = curl_easy_init();
  48. curl_easy_setopt(curlHandlePtr, CURLOPT_VERBOSE, 1L);
  49. curl_easy_setopt(curlHandlePtr, CURLOPT_URL, i_remoteFile);
  50. curl_easy_setopt(curlHandlePtr, CURLOPT_NOPROGRESS, 1);
  51. curl_easy_setopt(curlHandlePtr, CURLOPT_NOBODY, 1);
  52. curl_easy_setopt(curlHandlePtr, CURLOPT_HEADER, 1);
  53. curl_easy_setopt(curlHandlePtr, CURLOPT_FILETIME, 1);
  54. result = curl_easy_perform(curlHandlePtr);
  55. if(CURLE_OK == result) {
  56. result = curl_easy_getinfo(curlHandlePtr,
  57. CURLINFO_CONTENT_LENGTH_DOWNLOAD_T,
  58. &remoteFileSizeByte);
  59. if(result)
  60. return -1;
  61. printf("filesize: %" CURL_FORMAT_CURL_OFF_T "\n", remoteFileSizeByte);
  62. }
  63. curl_easy_cleanup(curlHandlePtr);
  64. return remoteFileSizeByte;
  65. }
  66. static int sftpResumeUpload(CURL *curlhandle, const char *remotepath,
  67. const char *localpath)
  68. {
  69. FILE *f = NULL;
  70. CURLcode result = CURLE_GOT_NOTHING;
  71. curl_off_t remoteFileSizeByte = sftpGetRemoteFileSize(remotepath);
  72. if(-1 == remoteFileSizeByte) {
  73. printf("Error reading the remote file size: unable to resume upload\n");
  74. return -1;
  75. }
  76. f = fopen(localpath, "rb");
  77. if(!f) {
  78. perror(NULL);
  79. return 0;
  80. }
  81. curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
  82. curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
  83. curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);
  84. curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);
  85. #ifdef _WIN32
  86. _fseeki64(f, remoteFileSizeByte, SEEK_SET);
  87. #else
  88. fseek(f, (long)remoteFileSizeByte, SEEK_SET);
  89. #endif
  90. curl_easy_setopt(curlhandle, CURLOPT_APPEND, 1L);
  91. result = curl_easy_perform(curlhandle);
  92. fclose(f);
  93. if(result == CURLE_OK)
  94. return 1;
  95. else {
  96. fprintf(stderr, "%s\n", curl_easy_strerror(result));
  97. return 0;
  98. }
  99. }
  100. int main(void)
  101. {
  102. const char *remote = "sftp://user:pass@example.com/path/filename";
  103. const char *filename = "filename";
  104. CURL *curlhandle = NULL;
  105. curl_global_init(CURL_GLOBAL_ALL);
  106. curlhandle = curl_easy_init();
  107. if(!sftpResumeUpload(curlhandle, remote, filename)) {
  108. printf("resumed upload using curl %s failed\n", curl_version());
  109. }
  110. curl_easy_cleanup(curlhandle);
  111. curl_global_cleanup();
  112. return 0;
  113. }