summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGavin Romig-Koch <gavin@localhost.localdomain>2009-08-13 13:04:48 -0400
committerGavin Romig-Koch <gavin@localhost.localdomain>2009-08-13 13:04:48 -0400
commit621eac1711b294c6589dc80317b9edc43d02d377 (patch)
treea951a5e017b16beeccabc5a75d50837a8b4c306c
parent24a0dc4b2ddee68f2ed5d975eb6a587dcc7f0e6a (diff)
downloadfastback-621eac1711b294c6589dc80317b9edc43d02d377.tar.gz
fastback-621eac1711b294c6589dc80317b9edc43d02d377.tar.xz
fastback-621eac1711b294c6589dc80317b9edc43d02d377.zip
clean up error handling, log and verbose output, print receipt
-rw-r--r--fastback.conf2
-rw-r--r--fastback.cpp208
2 files changed, 139 insertions, 71 deletions
diff --git a/fastback.conf b/fastback.conf
index be6e04f..0fec5af 100644
--- a/fastback.conf
+++ b/fastback.conf
@@ -4,4 +4,4 @@ URLDIR = ftp://indus.usersys.redhat.com/incoming/
# ERROR = 103
# error
- \ No newline at end of file
+
diff --git a/fastback.cpp b/fastback.cpp
index 065de8f..7eb62d1 100644
--- a/fastback.cpp
+++ b/fastback.cpp
@@ -46,6 +46,7 @@ The program keeps a log of all files uploaded, where they were uploaded to, encr
#include <argp.h>
#include <curl/curl.h>
+static const char fastback_name[] = "fastback";
// These are command line options
// if set they must be malloc'ed memory.
@@ -59,7 +60,8 @@ static bool fastback_encrypt = false; // encrypt file before upload
// '-n' option explicitly set
static bool fastback_newticket = false;
-static bool fastback_verbose = true;
+static bool fastback_verbose = false;
+static bool fastback_logging = false;
@@ -76,12 +78,49 @@ fastback_read(void *buffer, size_t size, size_t nmemb, void *userp)
}
static void
-check_error(CURLcode err, const char* msg)
+show(FILE* file, std::string url)
+{
+ if (fastback_URLDIR)
+ fprintf(file,"fastback URLDIR: %s\n", fastback_URLDIR);
+ else
+ fprintf(file,"fastback URLDIR not set\n");
+
+ if (fastback_filename)
+ fprintf(file,"fastback file: %s\n", fastback_filename);
+
+ if (fastback_ticket)
+ fprintf(file,"fastback ticket: %s\n", fastback_ticket);
+
+ else
+ {
+ if (fastback_newticket)
+ fprintf(file,"fastback new ticket explicitly set\n");
+ else
+ fprintf(file,"fastback new ticket by default\n");
+ }
+
+ if (fastback_encrypt)
+ fprintf(file,"encrypt file\n");
+ else
+ fprintf(file,"don't encrypt file\n");
+
+ fprintf(file,"fastback URL: %s\n", url.c_str());
+}
+
+static void
+check_curl_error(CURLcode err, std::string url, const char* msg)
{
if (err)
{
- fprintf(stderr,"%s: returned error: %s\n", msg, curl_easy_strerror(err));
- exit(3);
+ show(stderr, url);
+ std::string tmsg = fastback_name;
+ tmsg += ": error: ";
+ tmsg += msg;
+ tmsg += ": ";
+ tmsg += curl_easy_strerror(err);
+ tmsg += "\n";
+ fprintf(stderr,tmsg.c_str());
+ exit(4);
}
}
@@ -109,6 +148,12 @@ fastback_argp_parser (int key, char *arg, struct argp_state *state)
else
fastback_ticket = strdup(arg);
break;
+ case 'v':
+ fastback_verbose = true;
+ break;
+ case 'l':
+ fastback_logging = true;
+ break;
default:
return ARGP_ERR_UNKNOWN;
}
@@ -119,6 +164,8 @@ static struct argp_option fastback_options[] = {
{"ticket",'t', "TICKET", 0, "the ticket to associate FILE with"},
{0,'n',0,0,"create a new ticket for FILE"},
{"encrypt",'e',0,0,"encrypt FILE before uploading"},
+ {0,'v',0,0,"be verbose"},
+ {"logging",'l',0,0,"print logfile to stderr"},
{ 0 }};
static struct argp fastback_argp = {
@@ -127,11 +174,13 @@ static struct argp fastback_argp = {
"FILE"
};
-static void
-option_parse(const char* line,
+static bool
+option_parse(const char* filename,
+ const char* line,
size_t var_start, size_t var_end,
size_t value_start, size_t value_end)
{
+ bool config_file_error = false;
const char option1[] = "URLDIR";
size_t option_length = strlen(option1);
@@ -151,27 +200,32 @@ option_parse(const char* line,
memcpy(var, line+var_start, var_length);
var[var_length] = 0;
- fprintf(stderr, "error: unknown config option: %s\n", var);
+ fprintf(stderr,"%s: %s\n", filename, line);
+ fprintf(stderr, "%s: error: unknown config option: %s\n",
+ fastback_name, var);
+ config_file_error = true;
free(var);
}
+
+ return config_file_error;
}
static void
-error_regerror (int errcode, regex_t *compiled, const char* msg)
+error_regerror (int errcode, regex_t *compiled, const char* func)
{
size_t length = regerror (errcode, compiled, NULL, 0);
char *buffer = (char*)malloc (length);
(void) regerror (errcode, compiled, buffer, length);
- fprintf(stderr, "%s: %s\n", msg, buffer);
+ fprintf(stderr, "%s: error: %s: %s\n", fastback_name, func, buffer);
free(buffer);
}
-static int
+static bool
parse_config(const char* filename)
{
- int syntax_error = 0;
+ int config_file_error = 0;
int err;
regex_t regexp;
FILE* file;
@@ -185,12 +239,12 @@ parse_config(const char* filename)
"\\([[:alpha:]][[:alnum:]]*\\)[[:space:]]*="
"[[:space:]]*\\(\"\\([^\"]*\\)\"\\|\\([^[:space:]]*\\)\\)[[:space:]]*"
"\\)$";
- const int debug_pattern = 0;
+ const bool debug_pattern = false;
err = regcomp( &regexp, pattern, 0);
if (err)
{
- error_regerror(err, &regexp, "error from regcomp");
+ error_regerror(err, &regexp, "regcomp");
exit(4);
}
@@ -200,7 +254,10 @@ parse_config(const char* filename)
file = fopen(filename,"r");
if (!file)
{
- perror("error opening file");
+ std::string msg = fastback_name;
+ msg += ": error: could not open: ";
+ msg += filename;
+ perror(msg.c_str());
exit(4);
}
@@ -219,10 +276,12 @@ parse_config(const char* filename)
if (debug_pattern)
{
int i;
- fprintf(stderr,"line: %s\n", line);
+ fprintf(stderr,"%s: debug: line: %s: %s\n",
+ fastback_name, filename, line);
for (i=0; i < matchsize; i++)
if (matchptr[i].rm_so == -1)
- fprintf(stderr,"match (%d): no match\n", i);
+ fprintf(stderr,"%s: debug: match (%d): no match\n",
+ fastback_name, i);
else
{
int j;
@@ -232,36 +291,39 @@ parse_config(const char* filename)
buf[j] = 0;
if (j >= 1000)
{
- fprintf(stderr,"J too large\n");
+ fprintf(stderr,"INTERNAL ERROR: J too large\n");
exit(4);
}
- printf("match (%d): \"%s\"\n", i, buf);
+ fprintf(stderr, "%s: debug: match (%d): \"%s\"\n",
+ fastback_name, i, buf);
}
}
if (matchptr[2].rm_so != -1)
{
if (matchptr[4].rm_so != -1)
- option_parse(line,
- matchptr[2].rm_so, matchptr[2].rm_eo,
- matchptr[4].rm_so, matchptr[4].rm_eo);
+ config_file_error |=
+ option_parse(filename, line,
+ matchptr[2].rm_so, matchptr[2].rm_eo,
+ matchptr[4].rm_so, matchptr[4].rm_eo);
else if (matchptr[5].rm_so != -1)
- option_parse(line,
- matchptr[2].rm_so, matchptr[2].rm_eo,
- matchptr[5].rm_so, matchptr[5].rm_eo);
- else
- fprintf(stderr,"config var set to nothing\n");
+ config_file_error |=
+ option_parse(filename, line,
+ matchptr[2].rm_so, matchptr[2].rm_eo,
+ matchptr[5].rm_so, matchptr[5].rm_eo);
}
}
else if (err == REG_NOMATCH)
{
- fprintf(stderr,"error invalid config line: %s\n", line);
- syntax_error = 1;
+ fprintf(stderr,"%s: %s\n", filename, line);
+ fprintf(stderr,"%s: error: invalid config line\n", fastback_name);
+ config_file_error = true;
}
else
{
- error_regerror(err, &regexp, "error from regcomp");
+ fprintf(stderr,"%s: %s\n", filename, line);
+ error_regerror(err, &regexp, "regexe");
exit(4);
}
@@ -269,12 +331,11 @@ parse_config(const char* filename)
}
fclose(file);
- return syntax_error;
+ return config_file_error;
}
-
static void
cleanup()
{
@@ -292,15 +353,26 @@ main(int argc, char** argv)
err = argp_parse( &fastback_argp, argc, argv, 0, 0, 0);
if (err)
{
- fprintf(stderr, "error from argp_parse: %d (errno: %d)\n", err, errno);
- perror("fastback:");
+ if (errno == err)
+ {
+ std::string msg = fastback_name;
+ msg += ": error: argp_parse";
+ perror(msg.c_str());
+ }
+ else
+ fprintf(stderr, "%s: error from argp_parse: error code %d\n",
+ fastback_name, err);
cleanup();
exit(2);
}
if (!fastback_filename)
{
- printf("error: fastback file not set\n");
+ fprintf(stderr, "%s: error: no FILE given on command line\n",
+ fastback_name);
+ fprintf(stderr,
+ "Try `%s --help' or `fastback --usage' for more information.\n",
+ fastback_name);
exit(2);
}
@@ -310,30 +382,7 @@ main(int argc, char** argv)
if (!fastback_URLDIR)
fastback_URLDIR = strdup(fastback_default_URLDIR);
- if (fastback_URLDIR)
- printf("fastback URLDIR: %s\n", fastback_URLDIR);
- else
- printf("fastback URLDIR not set\n");
-
-
- if (fastback_filename)
- printf("fastback file: %s\n", fastback_filename);
-
- if (fastback_ticket)
- printf("fastback ticket: %s\n", fastback_ticket);
-
- else
- {
- if (fastback_newticket)
- printf("fastback new ticket explicitly set\n");
- else
- printf("fastback new ticket by default\n");
- }
- if (fastback_encrypt)
- printf("encrypt file\n");
- else
- printf("don't encrypt file\n");
CURL* handle;
CURLcode curl_err;
@@ -343,7 +392,10 @@ main(int argc, char** argv)
file = fopen(fastback_filename,"r");
if (!file)
{
- fprintf(stderr,"fopen: returned error\n");
+ std::string msg = fastback_name;
+ msg += ": error: could not open: ";
+ msg += fastback_filename;
+ perror(msg.c_str());
exit(3);
}
@@ -352,42 +404,58 @@ main(int argc, char** argv)
url += '/';
url += basename(fastback_filename);
- printf("fastback url: %s\n", url.c_str());
-
+ if (fastback_verbose)
+ show(stdout,url);
+
+ if (fastback_logging)
+ show(stderr,url);
+
if (curl_global_init(CURL_GLOBAL_ALL))
{
- fprintf(stderr,"curl_global_init: returned error\n");
+ fprintf(stderr,"%s: error: curl_global_init: could not initialze curl\n",
+ fastback_name);
exit(3);
}
handle = curl_easy_init();
if (!handle)
{
- fprintf(stderr,"curl_easy_init: returned error\n");
+ fprintf(stderr,"%s: error: curl_easy_init: could not initialize curl\n", fastback_name);
exit(3);
}
- curl_err = curl_easy_setopt(handle, CURLOPT_VERBOSE, fastback_verbose);
- check_error(curl_err, "curl_easy_setopt(CURLOPT_VERBOSE)");
-
+ // note that it is the fastback logging option which invokes curl verbose
+ curl_err = curl_easy_setopt(handle, CURLOPT_VERBOSE, fastback_logging);
+ check_curl_error(curl_err, url, "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)");
+ check_curl_error(curl_err, url, "curl_easy_setopt(CURLOPT_URL)");
curl_err = curl_easy_setopt(handle, CURLOPT_READFUNCTION, fastback_read);
- check_error(curl_err, "curl_easy_setopt(CURLOPT_READFUNCTION)");
+ check_curl_error(curl_err, url, "curl_easy_setopt(CURLOPT_READFUNCTION)");
curl_err = curl_easy_setopt(handle, CURLOPT_READDATA, (void*)file);
- check_error(curl_err, "curl_easy_setopt(CURLOPT_READDATA)");
+ check_curl_error(curl_err, url, "curl_easy_setopt(CURLOPT_READDATA)");
curl_err = curl_easy_setopt(handle, CURLOPT_UPLOAD, 1L);
- check_error(curl_err, "curl_easy_setopt(CURLOPT_READDATA)");
+ check_curl_error(curl_err, url, "curl_easy_setopt(CURLOPT_READDATA)");
curl_err = curl_easy_perform(handle);
- check_error(curl_err, "curl_easy_perform");
+ check_curl_error(curl_err, url, "curl_easy_perform");
curl_easy_cleanup(handle);
fclose(file);
+ if (fastback_ticket)
+ printf("Please paste this into %s:\n", fastback_ticket);
+ else
+ printf("Please create a new ticket, and paste this into it:\n");
+ printf("FASTBACK:\n");
+ if (fastback_ticket)
+ printf("TICKET: %s\n", fastback_ticket);
+ printf("UPLOAD: %s\n", url.c_str());
+ printf("END:\n");
+
cleanup();
return 0;
}