sftpuploadresume.c 3.8 KB

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