diff options
Diffstat (limited to 'fastback.cpp')
-rw-r--r-- | fastback.cpp | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/fastback.cpp b/fastback.cpp new file mode 100644 index 0000000..d024dd3 --- /dev/null +++ b/fastback.cpp @@ -0,0 +1,146 @@ + + +/* +The file is named on the command line. + +An option to the command will cause the file to be encrypted before upload, and the output of the command will include the encryption key used. + +An option to the command will allow the user to associate a ticket name/number with the file. + +$ fastback FILE [ -t TICKET | -n ] [ -e ] + +where FILE is the file to be uploaded; where -e indicates that the file should be encrypted before it's uploaded; where -n indicates a new ticket; where TICKET is the name of the ticket + +If -t is not specified, -n is assumed. + +The name of the uploaded file will be the FILE name, prefixed with the TICKET name, and suffixed with a string of random characters to make the file unique. + +The file will be compressed if it is not already compressed. + +The configuration file is called fastback.conf. + +The program keeps a log of all files uploaded, where they were uploaded to, encryption key used (if any), and a log of the messages from the transfer. +*/ + + + +// $ fastback FILE [ -t TICKET | -n ] [ -e ] +// where FILE is the file to be uploaded +// where -e indicates that the file should be encrypted +// where -n indicates a new ticket +// where TICKET is the name of the ticket +// +// If -t is not specified, -n is assumed. +// + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif +#include <stdlib.h> +#include <string.h> +#include <argp.h> + +// 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_ticket = 0; // ticket to upload to + +static bool fastback_encrypt = false; // encrypt file before upload + +// '-n' option explicitly set +static bool fastback_newticket = false; + +static error_t +fastback_argp_parser (int key, char *arg, struct argp_state *state) +{ + switch (key) + { + case ARGP_KEY_ARG: + if (fastback_file) + argp_error(state,"multiple FILE arguments specified"); + fastback_file = strdup(arg); + break; + case 'e': + fastback_encrypt = true; + break; + case 'n': + if (fastback_ticket) + argp_error(state, "invalid options: -n and -t conflict"); + fastback_newticket = true; + break; + case 't': + if (fastback_newticket) + argp_error(state, "invalid options: -n and -t conflict"); + else + fastback_ticket = strdup(arg); + break; + default: + return ARGP_ERR_UNKNOWN; + } + return 0; +} + +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 }}; + +static struct argp fastback_argp = { + fastback_options, + fastback_argp_parser, + "FILE" +}; + +static void +cleanup() +{ + free(fastback_file); + free(fastback_ticket); +} + + +int +main(int argc, char** argv) +{ + error_t err; + + 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:"); + cleanup(); + exit(2); + } + + if (! fastback_file) + { + printf("error: fastback file not set\n"); + exit(2); + } + + if (fastback_file) + printf("fastback file: %s\n", fastback_file); + + 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"); + + cleanup(); + return 0; +} + + |