sftpuploadresume.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2018, 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. printf("filesize: %" CURL_FORMAT_CURL_OFF_T "\n", remoteFileSizeByte);
  60. }
  61. curl_easy_cleanup(curlHandlePtr);
  62. return remoteFileSizeByte;
  63. }
  64. static int sftpResumeUpload(CURL *curlhandle, const char *remotepath,
  65. const char *localpath)
  66. {
  67. FILE *f = NULL;
  68. CURLcode result = CURLE_GOT_NOTHING;
  69. curl_off_t remoteFileSizeByte = sftpGetRemoteFileSize(remotepath);
  70. if(-1 == remoteFileSizeByte) {
  71. printf("Error reading the remote file size: unable to resume upload\n");
  72. return -1;
  73. }
  74. f = fopen(localpath, "rb");
  75. if(!f) {
  76. perror(NULL);
  77. return 0;
  78. }
  79. curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
  80. curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
  81. curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);
  82. curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);
  83. #ifdef _WIN32
  84. _fseeki64(f, remoteFileSizeByte, SEEK_SET);
  85. #else
  86. fseek(f, (long)remoteFileSizeByte, SEEK_SET);
  87. #endif
  88. curl_easy_setopt(curlhandle, CURLOPT_APPEND, 1L);
  89. result = curl_easy_perform(curlhandle);
  90. fclose(f);
  91. if(result == CURLE_OK)
  92. return 1;
  93. else {
  94. fprintf(stderr, "%s\n", curl_easy_strerror(result));
  95. return 0;
  96. }
  97. }
  98. int main(void)
  99. {
  100. const char *remote = "sftp://user:pass@example.com/path/filename";
  101. const char *filename = "filename";
  102. CURL *curlhandle = NULL;
  103. curl_global_init(CURL_GLOBAL_ALL);
  104. curlhandle = curl_easy_init();
  105. if(!sftpResumeUpload(curlhandle, remote, filename)) {
  106. printf("resumed upload using curl %s failed\n", curl_version());
  107. }
  108. curl_easy_cleanup(curlhandle);
  109. curl_global_cleanup();
  110. return 0;
  111. }