Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <curl/curl.h>
- #define DATABASE_FILE "/tmp/musiclist.txt"
- #define REMOTE_DATABASE_FILE "musiclist.txt"
- #define true 1
- #define false 0
- #define REMOTE_URL "ftp://orangesquirrels.com"
- void download(const char* file_to_write, const char* short_database, const char* addr, const char* msg);
- int upload(void);
- size_t handle_header(void *ptr, size_t size, size_t nmemb, void *userdata);
- void write_to_database(const char* filename, const char* string_to_write);
- size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream);
- struct FtpFile {
- const char *filename;
- FILE *stream;
- };
- struct stat file_info;
- curl_off_t fsize;
- int main() {
- remove(DATABASE_FILE);
- download(DATABASE_FILE, "musiclist.txt", REMOTE_URL, "Testing 123");
- /* Get the file size of the local file. */
- if(stat(DATABASE_FILE, &file_info)) {
- printf("Couldn't open database file %s\n", DATABASE_FILE);
- return 1;
- }
- fsize = (curl_off_t)file_info.st_size;
- printf("Local data file size: %" CURL_FORMAT_CURL_OFF_T " bytes.\n", fsize);
- write_to_database(DATABASE_FILE, "Testing 123");
- upload();
- }
- size_t handle_header(void *ptr, size_t size, size_t nmemb, void *userdata) {
- return size * nmemb;
- // Do nothing.
- }
- size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream) {
- curl_off_t nread;
- size_t retcode = fread (ptr, size, nmemb, stream);
- nread = (curl_off_t)retcode;
- fprintf (stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
- " bytes from file.\n", nread);
- return retcode;
- }
- size_t curl_database_write(void *buffer, size_t size, size_t nmemb, void *stream) {
- struct FtpFile *out = (struct FtpFile *)stream;
- if(out && !out->stream) {
- /* open file for writing */
- out->stream = fopen (out->filename, "a+");
- if(!out->stream) {
- return -1; /* failure, can't open file to write */
- }
- }
- printf("Got data.\n");
- return fwrite(buffer, size, nmemb, out->stream);
- }
- void write_to_database(const char* filename, const char* string_to_write) {
- FILE *file = fopen(filename, "a+");
- if (file) {
- printf("Current position in file: %li\n", ftell(file));
- fputs(string_to_write, file);
- fputs("\n", file);
- fclose(file);
- }
- }
- void download(const char* file_to_write, const char* short_database, const char* addr, const char* msg) {
- remove(DATABASE_FILE);
- CURL *curl;
- CURLcode res;
- struct FtpFile ftpfile={
- DATABASE_FILE, /* name to store the file as if succesful */
- NULL
- };
- /* In windows, this will init the winsock stuff */
- curl_global_init(CURL_GLOBAL_ALL);
- /* get a curl handle */
- curl = curl_easy_init();
- if(curl) {
- /* specify target */
- curl_easy_setopt(curl, CURLOPT_URL, REMOTE_URL);
- //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
- printf("Remote URL: %s\n", REMOTE_URL);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_database_write);
- curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, handle_header);
- /* Set a pointer to our struct to pass to the callback */
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
- curl_easy_setopt(curl, CURLOPT_USERNAME, (char *)"private");
- curl_easy_setopt(curl, CURLOPT_PASSWORD, (char *)"private");
- /* Now run off and do what you've been told! */
- fflush(stdout); // Flush the buffers so we see the message immediately.
- res = curl_easy_perform(curl);
- /* Check for errors */
- if (res != CURLE_OK)
- {
- fprintf(stderr, "DWNLD DATAB: curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
- }
- if(ftpfile.stream)
- fclose(ftpfile.stream); /* close the local file */
- }
- /* always cleanup */
- curl_easy_cleanup(curl);
- curl_global_cleanup();
- }
- int upload(void) {
- printf("Uploading database file back to server...\n");
- CURL *curl;
- CURLcode res;
- FILE *hd_src;
- struct stat file_info;
- curl_off_t fsize;
- /* get the file size of the local file */
- if(stat(DATABASE_FILE, &file_info)) {
- printf("Couldn't open database file %s\n", DATABASE_FILE);
- return 1;
- }
- fsize = (curl_off_t)file_info.st_size;
- printf("Local data file size: %" CURL_FORMAT_CURL_OFF_T " bytes.\n", fsize);
- /* get a FILE * of the same file */
- hd_src = fopen(DATABASE_FILE, "rb");
- /* In windows, this will init the winsock stuff */
- curl_global_init(CURL_GLOBAL_ALL);
- /* get a curl handle */
- curl = curl_easy_init();
- if(curl) {
- /* We want to use our own read function. */
- curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
- /* Enable uploading. */
- curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
- /* Create the URL of the remote database file. */
- char final_url[100];
- strcpy(final_url, "ftp://orangesquirrels.com/");
- strcat(final_url, REMOTE_DATABASE_FILE);
- printf("Remote URL is %s !\n", REMOTE_URL);
- //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
- curl_easy_setopt(curl, CURLOPT_URL, final_url);
- /* Now specify which file to upload. */
- curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
- curl_easy_setopt(curl, CURLOPT_USERNAME, (char *)"private");
- curl_easy_setopt(curl, CURLOPT_PASSWORD, (char *)"private");
- /* Set the size of the file to upload (optional). If you give a *_LARGE
- option you MUST make sure that the type of the passed-in argument is a
- curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
- make sure that to pass in a type 'long' argument. */
- curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
- (curl_off_t)fsize);
- /* Now run off and do what you've been told! */
- res = curl_easy_perform(curl);
- /* Check for errors. */
- if(res != CURLE_OK) {
- fprintf(stderr, "UPLD DATAB: curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
- }
- /* Always cleanup. */
- curl_easy_cleanup(curl);
- }
- fclose(hd_src); /* Close the local file. */
- curl_global_cleanup();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement