Advertisement
Guest User

libcurl problem

a guest
Jul 7th, 2013
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5.  
  6. #include <curl/curl.h>
  7. #define DATABASE_FILE "/tmp/musiclist.txt"
  8. #define REMOTE_DATABASE_FILE "musiclist.txt"
  9. #define true 1
  10. #define false 0
  11.  
  12. #define REMOTE_URL "ftp://orangesquirrels.com"
  13. void download(const char* file_to_write, const char* short_database, const char* addr, const char* msg);
  14. int upload(void);
  15. size_t handle_header(void *ptr, size_t size, size_t nmemb, void *userdata);
  16. void write_to_database(const char* filename, const char* string_to_write);
  17. size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream);
  18.  
  19. struct FtpFile {
  20.     const char *filename;
  21.     FILE *stream;
  22. };
  23. struct stat file_info;
  24. curl_off_t fsize;
  25.  
  26. int main() {
  27.     remove(DATABASE_FILE);
  28.     download(DATABASE_FILE, "musiclist.txt", REMOTE_URL, "Testing 123");
  29.    
  30.     /* Get the file size of the local file. */
  31.     if(stat(DATABASE_FILE, &file_info)) {
  32.         printf("Couldn't open database file %s\n", DATABASE_FILE);
  33.         return 1;
  34.     }
  35.     fsize = (curl_off_t)file_info.st_size;
  36.    
  37.     printf("Local data file size: %" CURL_FORMAT_CURL_OFF_T " bytes.\n", fsize);
  38.    
  39.     write_to_database(DATABASE_FILE, "Testing 123");
  40.     upload();
  41. }
  42.  
  43. size_t handle_header(void *ptr, size_t size, size_t nmemb, void *userdata) {
  44.     return size * nmemb;
  45.     // Do nothing.
  46. }
  47.  
  48. size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream) {
  49.     curl_off_t nread;
  50.     size_t retcode = fread (ptr, size, nmemb, stream);
  51.    
  52.     nread = (curl_off_t)retcode;
  53.    
  54.     fprintf (stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
  55.              " bytes from file.\n", nread);
  56.     return retcode;
  57. }
  58.  
  59. size_t curl_database_write(void *buffer, size_t size, size_t nmemb, void *stream) {
  60.     struct FtpFile *out = (struct FtpFile *)stream;
  61.     if(out && !out->stream) {
  62.         /* open file for writing */
  63.         out->stream = fopen (out->filename, "a+");
  64.         if(!out->stream) {
  65.             return -1; /* failure, can't open file to write */
  66.         }
  67.     }
  68.     printf("Got data.\n");
  69.     return fwrite(buffer, size, nmemb, out->stream);
  70. }
  71.  
  72. void write_to_database(const char* filename, const char* string_to_write) {
  73.     FILE *file = fopen(filename, "a+");
  74.     if (file) {
  75.         printf("Current position in file: %li\n", ftell(file));
  76.         fputs(string_to_write, file);
  77.         fputs("\n", file);
  78.         fclose(file);
  79.     }
  80. }
  81.  
  82. void download(const char* file_to_write, const char* short_database, const char* addr, const char* msg) {
  83.     remove(DATABASE_FILE);
  84.    
  85.     CURL *curl;
  86.     CURLcode res;
  87.    
  88.     struct FtpFile ftpfile={
  89.         DATABASE_FILE, /* name to store the file as if succesful */
  90.         NULL
  91.     };
  92.    
  93.     /* In windows, this will init the winsock stuff */
  94.     curl_global_init(CURL_GLOBAL_ALL);
  95.    
  96.     /* get a curl handle */
  97.     curl = curl_easy_init();
  98.     if(curl) {
  99.         /* specify target */
  100.         curl_easy_setopt(curl, CURLOPT_URL, REMOTE_URL);
  101.         //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  102.         printf("Remote URL: %s\n", REMOTE_URL);
  103.        
  104.         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_database_write);
  105.         curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, handle_header);
  106.         /* Set a pointer to our struct to pass to the callback */
  107.         curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
  108.        
  109.         curl_easy_setopt(curl, CURLOPT_USERNAME, (char *)"private");
  110.        
  111.         curl_easy_setopt(curl, CURLOPT_PASSWORD, (char *)"private");
  112.        
  113.         /* Now run off and do what you've been told! */
  114.         fflush(stdout); // Flush the buffers so we see the message immediately.
  115.         res = curl_easy_perform(curl);
  116.        
  117.         /* Check for errors */
  118.         if (res != CURLE_OK)
  119.         {
  120.             fprintf(stderr, "DWNLD DATAB: curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
  121.         }
  122.        
  123.         if(ftpfile.stream)
  124.             fclose(ftpfile.stream); /* close the local file */
  125.     }
  126.    
  127.     /* always cleanup */
  128.     curl_easy_cleanup(curl);
  129.     curl_global_cleanup();
  130. }
  131.  
  132. int upload(void) {
  133.     printf("Uploading database file back to server...\n");
  134.     CURL *curl;
  135.     CURLcode res;
  136.     FILE *hd_src;
  137.     struct stat file_info;
  138.     curl_off_t fsize;
  139.    
  140.     /* get the file size of the local file */
  141.     if(stat(DATABASE_FILE, &file_info)) {
  142.         printf("Couldn't open database file %s\n", DATABASE_FILE);
  143.         return 1;
  144.     }
  145.     fsize = (curl_off_t)file_info.st_size;
  146.    
  147.     printf("Local data file size: %" CURL_FORMAT_CURL_OFF_T " bytes.\n", fsize);
  148.    
  149.     /* get a FILE * of the same file */
  150.     hd_src = fopen(DATABASE_FILE, "rb");
  151.    
  152.     /* In windows, this will init the winsock stuff */
  153.     curl_global_init(CURL_GLOBAL_ALL);
  154.    
  155.     /* get a curl handle */
  156.     curl = curl_easy_init();
  157.     if(curl) {
  158.         /* We want to use our own read function. */
  159.         curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  160.        
  161.         /* Enable uploading. */
  162.         curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  163.        
  164.         /* Create the URL of the remote database file. */
  165.         char final_url[100];
  166.         strcpy(final_url, "ftp://orangesquirrels.com/");
  167.         strcat(final_url, REMOTE_DATABASE_FILE);
  168.        
  169.         printf("Remote URL is %s !\n", REMOTE_URL);
  170.         //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  171.         curl_easy_setopt(curl, CURLOPT_URL, final_url);
  172.        
  173.         /* Now specify which file to upload. */
  174.         curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
  175.        
  176.         curl_easy_setopt(curl, CURLOPT_USERNAME, (char *)"private");
  177.        
  178.         curl_easy_setopt(curl, CURLOPT_PASSWORD, (char *)"private");
  179.        
  180.         /* Set the size of the file to upload (optional).  If you give a *_LARGE
  181.          option you MUST make sure that the type of the passed-in argument is a
  182.          curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
  183.          make sure that to pass in a type 'long' argument. */
  184.         curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
  185.                          (curl_off_t)fsize);
  186.        
  187.         /* Now run off and do what you've been told! */
  188.         res = curl_easy_perform(curl);
  189.         /* Check for errors. */
  190.         if(res != CURLE_OK) {
  191.             fprintf(stderr, "UPLD DATAB: curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
  192.         }
  193.        
  194.         /* Always cleanup. */
  195.         curl_easy_cleanup(curl);
  196.     }
  197.     fclose(hd_src); /* Close the local file. */
  198.    
  199.     curl_global_cleanup();
  200.     return 0;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement