2
0

ftpupload.c 4.5 KB

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