ftpupload.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #include <stdio.h>
  23. #include <string.h>
  24. #include <curl/curl.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <fcntl.h>
  28. #include <errno.h>
  29. #ifdef WIN32
  30. #include <io.h>
  31. #else
  32. #include <unistd.h>
  33. #endif
  34. /* <DESC>
  35. * Performs an FTP upload and renames the file just after a successful
  36. * transfer.
  37. * </DESC>
  38. */
  39. #define LOCAL_FILE "/tmp/uploadthis.txt"
  40. #define UPLOAD_FILE_AS "while-uploading.txt"
  41. #define REMOTE_URL "ftp://example.com/" UPLOAD_FILE_AS
  42. #define RENAME_FILE_TO "renamed-and-fine.txt"
  43. /* NOTE: if you want this example to work on Windows with libcurl as a
  44. DLL, you MUST also provide a read callback with CURLOPT_READFUNCTION.
  45. Failing to do so will give you a crash since a DLL may not use the
  46. variable's memory when passed in to it from an app like this. */
  47. static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
  48. {
  49. curl_off_t nread;
  50. /* in real-world cases, this would probably get this data differently
  51. as this fread() stuff is exactly what the library already would do
  52. by default internally */
  53. size_t retcode = fread(ptr, size, nmemb, stream);
  54. nread = (curl_off_t)retcode;
  55. fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
  56. " bytes from file\n", nread);
  57. return retcode;
  58. }
  59. int main(void)
  60. {
  61. CURL *curl;
  62. CURLcode res;
  63. FILE *hd_src;
  64. struct stat file_info;
  65. curl_off_t fsize;
  66. struct curl_slist *headerlist = NULL;
  67. static const char buf_1 [] = "RNFR " UPLOAD_FILE_AS;
  68. static const char buf_2 [] = "RNTO " RENAME_FILE_TO;
  69. /* get the file size of the local file */
  70. if(stat(LOCAL_FILE, &file_info)) {
  71. printf("Couldn't open '%s': %s\n", LOCAL_FILE, strerror(errno));
  72. return 1;
  73. }
  74. fsize = (curl_off_t)file_info.st_size;
  75. printf("Local file size: %" CURL_FORMAT_CURL_OFF_T " bytes.\n", fsize);
  76. /* get a FILE * of the same file */
  77. hd_src = fopen(LOCAL_FILE, "rb");
  78. /* In windows, this will init the winsock stuff */
  79. curl_global_init(CURL_GLOBAL_ALL);
  80. /* get a curl handle */
  81. curl = curl_easy_init();
  82. if(curl) {
  83. /* build a list of commands to pass to libcurl */
  84. headerlist = curl_slist_append(headerlist, buf_1);
  85. headerlist = curl_slist_append(headerlist, buf_2);
  86. /* we want to use our own read function */
  87. curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  88. /* enable uploading */
  89. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  90. /* specify target */
  91. curl_easy_setopt(curl, CURLOPT_URL, REMOTE_URL);
  92. /* pass in that last of FTP commands to run after the transfer */
  93. curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
  94. /* now specify which file to upload */
  95. curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
  96. /* Set the size of the file to upload (optional). If you give a *_LARGE
  97. option you MUST make sure that the type of the passed-in argument is a
  98. curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
  99. make sure that to pass in a type 'long' argument. */
  100. curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
  101. (curl_off_t)fsize);
  102. /* Now run off and do what you've been told! */
  103. res = curl_easy_perform(curl);
  104. /* Check for errors */
  105. if(res != CURLE_OK)
  106. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  107. curl_easy_strerror(res));
  108. /* clean up the FTP commands list */
  109. curl_slist_free_all(headerlist);
  110. /* always cleanup */
  111. curl_easy_cleanup(curl);
  112. }
  113. fclose(hd_src); /* close the local file */
  114. curl_global_cleanup();
  115. return 0;
  116. }