sftpuploadresume.c 3.8 KB

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