From 24a0dc4b2ddee68f2ed5d975eb6a587dcc7f0e6a Mon Sep 17 00:00:00 2001 From: Gavin Romig-Koch Date: Thu, 13 Aug 2009 09:59:23 -0400 Subject: basic uploading --- FIXME | 5 ++- Makefile | 4 +- fastback.conf | 2 +- fastback.cpp | 122 +++++++++++++++++++++++++++++++++++++++++++++++++--------- 4 files changed, 111 insertions(+), 22 deletions(-) diff --git a/FIXME b/FIXME index eb8515b..69b5bc2 100644 --- a/FIXME +++ b/FIXME @@ -1,4 +1,7 @@ * config file comments can only be on a line by themselves * config file strings can't have nested double quotes - \ No newline at end of file + +* must catch the case that the given file is not a flat file + + diff --git a/Makefile b/Makefile index 79d4062..c4c9a98 100644 --- a/Makefile +++ b/Makefile @@ -2,8 +2,8 @@ default: fastback fastback: fastback.o - g++ -o $@ $< + g++ -o $@ $$(curl-config --libs) $< fastback.o: fastback.cpp - g++ -c -o $@ $< + g++ -c -o $$(curl-config --cflags) $@ $< diff --git a/fastback.conf b/fastback.conf index aa25d25..be6e04f 100644 --- a/fastback.conf +++ b/fastback.conf @@ -1,6 +1,6 @@ # this is a config file for fastback -URL = ftp://test.example.com +URLDIR = ftp://indus.usersys.redhat.com/incoming/ # ERROR = 103 # error diff --git a/fastback.cpp b/fastback.cpp index 4c464bf..065de8f 100644 --- a/fastback.cpp +++ b/fastback.cpp @@ -36,31 +36,64 @@ The program keeps a log of all files uploaded, where they were uploaded to, encr #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif + +#include + #include #include #include #include #include +#include + // These are command line options // if set they must be malloc'ed memory. -static char* fastback_file = 0; // local file to be uploaded +static char* fastback_filename = 0; // local file to be uploaded static char* fastback_ticket = 0; // ticket to upload to +static char* fastback_URLDIR = 0; + static bool fastback_encrypt = false; // encrypt file before upload // '-n' option explicitly set static bool fastback_newticket = false; +static bool fastback_verbose = true; + + + +//static const char* fastback_default_URLDIR = "ftp://dropbox.redhat.com/incoming/"; +static const char* fastback_default_URLDIR = "ftp://indus.usersys.redhat.com/incoming"; + + + + +static size_t +fastback_read(void *buffer, size_t size, size_t nmemb, void *userp) +{ + return fread(buffer, size, nmemb, (FILE*)userp); +} + +static void +check_error(CURLcode err, const char* msg) +{ + if (err) + { + fprintf(stderr,"%s: returned error: %s\n", msg, curl_easy_strerror(err)); + exit(3); + } +} + static error_t fastback_argp_parser (int key, char *arg, struct argp_state *state) { switch (key) { case ARGP_KEY_ARG: - if (fastback_file) + if (fastback_filename) argp_error(state,"multiple FILE arguments specified"); - fastback_file = strdup(arg); + fastback_filename = strdup(arg); break; case 'e': fastback_encrypt = true; @@ -94,24 +127,21 @@ static struct argp fastback_argp = { "FILE" }; - -static char* fastback_URL; - static void option_parse(const char* line, size_t var_start, size_t var_end, size_t value_start, size_t value_end) { - const char option1[] = "URL"; + const char option1[] = "URLDIR"; size_t option_length = strlen(option1); if (option_length == (var_end - var_start) && memcmp(option1,line+var_start,option_length) == 0) { int value_length = value_end - value_start; - fastback_URL = (char*)malloc(value_length + 1); - memcpy(fastback_URL, line+value_start, value_length); - fastback_URL[value_length] = 0; + fastback_URLDIR = (char*)malloc(value_length + 1); + memcpy(fastback_URLDIR, line+value_start, value_length); + fastback_URLDIR[value_length] = 0; } else @@ -248,9 +278,9 @@ parse_config(const char* filename) static void cleanup() { - free(fastback_file); + free(fastback_filename); free(fastback_ticket); - free(fastback_URL); + free(fastback_URLDIR); } @@ -268,7 +298,7 @@ main(int argc, char** argv) exit(2); } - if (!fastback_file) + if (!fastback_filename) { printf("error: fastback file not set\n"); exit(2); @@ -277,14 +307,17 @@ main(int argc, char** argv) if (parse_config("fastback.conf")) exit(2); - if (fastback_URL) - printf("fastback URL: %s\n", fastback_URL); + if (!fastback_URLDIR) + fastback_URLDIR = strdup(fastback_default_URLDIR); + + if (fastback_URLDIR) + printf("fastback URLDIR: %s\n", fastback_URLDIR); else - printf("fastback URL not set\n"); + printf("fastback URLDIR not set\n"); - if (fastback_file) - printf("fastback file: %s\n", fastback_file); + if (fastback_filename) + printf("fastback file: %s\n", fastback_filename); if (fastback_ticket) printf("fastback ticket: %s\n", fastback_ticket); @@ -302,6 +335,59 @@ main(int argc, char** argv) else printf("don't encrypt file\n"); + CURL* handle; + CURLcode curl_err; + FILE* file; + std::string url; + + file = fopen(fastback_filename,"r"); + if (!file) + { + fprintf(stderr,"fopen: returned error\n"); + exit(3); + } + + url = fastback_URLDIR; + if (url[url.length()-1] != '/') + url += '/'; + url += basename(fastback_filename); + + printf("fastback url: %s\n", url.c_str()); + + if (curl_global_init(CURL_GLOBAL_ALL)) + { + fprintf(stderr,"curl_global_init: returned error\n"); + exit(3); + } + + handle = curl_easy_init(); + if (!handle) + { + fprintf(stderr,"curl_easy_init: returned error\n"); + exit(3); + } + + curl_err = curl_easy_setopt(handle, CURLOPT_VERBOSE, fastback_verbose); + check_error(curl_err, "curl_easy_setopt(CURLOPT_VERBOSE)"); + + curl_err = curl_easy_setopt(handle, CURLOPT_URL, url.c_str()); + check_error(curl_err, "curl_easy_setopt(CURLOPT_URL)"); + + curl_err = curl_easy_setopt(handle, CURLOPT_READFUNCTION, fastback_read); + check_error(curl_err, "curl_easy_setopt(CURLOPT_READFUNCTION)"); + + curl_err = curl_easy_setopt(handle, CURLOPT_READDATA, (void*)file); + check_error(curl_err, "curl_easy_setopt(CURLOPT_READDATA)"); + + curl_err = curl_easy_setopt(handle, CURLOPT_UPLOAD, 1L); + check_error(curl_err, "curl_easy_setopt(CURLOPT_READDATA)"); + + curl_err = curl_easy_perform(handle); + check_error(curl_err, "curl_easy_perform"); + + curl_easy_cleanup(handle); + fclose(file); + cleanup(); return 0; } -- cgit