From f3faac1ef273ae544e37fdfac0211f46f77a47fa Mon Sep 17 00:00:00 2001 From: Gavin Romig-Koch Date: Fri, 28 Aug 2009 15:51:13 -0400 Subject: added scp support using the scp command --- fastback.cpp | 132 ++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 81 insertions(+), 51 deletions(-) diff --git a/fastback.cpp b/fastback.cpp index 3597b8c..61e66e2 100644 --- a/fastback.cpp +++ b/fastback.cpp @@ -542,6 +542,7 @@ int main(int argc, char** argv) { error_t err; + bool use_scp_command = false; err = argp_parse( &fastback_argp, argc, argv, 0, 0, 0); if (err) @@ -629,26 +630,10 @@ main(int argc, char** argv) WriteCommand(cmd,key); } - // generate md5sum string cmd = string("md5sum ") + outfile_name; string md5sum = ReadCommand(cmd); - // upload the file - CURL* handle; - CURLcode curl_err; - FILE* file; - - file = fopen(outfile_name.c_str(),"r"); - if (!file) - { - string msg = fastback_name; - msg += ": error: could not open: "; - msg += outfile_name; - perror(msg.c_str()); - exit(3); - } - // randomize the remote file name // and alter the md5sum output to contain the remote file name string remotefile_name = randomize_filename(filename_basename(outfile_name.c_str())); @@ -666,47 +651,92 @@ main(int argc, char** argv) if (fastback_logfile) show(fastback_logfile,url); - if (curl_global_init(CURL_GLOBAL_ALL)) - { - fprintf(stderr,"%s: error: curl_global_init: could not initialze curl\n", - fastback_name); - exit(3); - } + if (url.substr(0,4) == "scp:") + use_scp_command = true; - handle = curl_easy_init(); - if (!handle) + if (use_scp_command) { - fprintf(stderr,"%s: error: curl_easy_init: could not initialize curl\n", fastback_name); - exit(3); - } - - if (fastback_logfile) - { - curl_err = curl_easy_setopt(handle, CURLOPT_VERBOSE, 1); - check_curl_error(curl_err, url, "curl_easy_setopt(CURLOPT_VERBOSE)"); + if (url.substr(0,6) != "scp://") + { + fprintf(stderr, + "%s: error: invalid scp URL, does not start with 'scp://': %s\n", + fastback_name, url.c_str()); + exit(3); + } - curl_err = curl_easy_setopt(handle, CURLOPT_STDERR, fastback_logfile); - check_curl_error(curl_err, url, "curl_easy_setopt(CURLOPT_STDERR)"); + size_t urlend = url.length(); + size_t hostend = url.find_first_of('/',6); + string host = url.substr(6,hostend-6); + const char* replacement; + if (hostend+1 >= urlend || url[hostend+1] != '~') + replacement = ":/"; + else + replacement = ":"; + + string cmd = string("scp ") + outfile_name + ' ' + host + replacement + url.substr(hostend+1); + if (fastback_logfile) + fprintf(fastback_logfile,"$ %s\n", cmd.c_str()); + RunCommand(cmd); } - curl_err = curl_easy_setopt(handle, CURLOPT_URL, url.c_str()); - check_curl_error(curl_err, url, "curl_easy_setopt(CURLOPT_URL)"); - - curl_err = curl_easy_setopt(handle, CURLOPT_READFUNCTION, fastback_read); - check_curl_error(curl_err, url, "curl_easy_setopt(CURLOPT_READFUNCTION)"); - - curl_err = curl_easy_setopt(handle, CURLOPT_READDATA, (void*)file); - check_curl_error(curl_err, url, "curl_easy_setopt(CURLOPT_READDATA)"); - - curl_err = curl_easy_setopt(handle, CURLOPT_UPLOAD, 1L); - check_curl_error(curl_err, url, "curl_easy_setopt(CURLOPT_READDATA)"); - - curl_err = curl_easy_perform(handle); - check_curl_error(curl_err, url, "curl_easy_perform"); + else + { + CURL* handle; + CURLcode curl_err; + FILE* file; - curl_easy_cleanup(handle); - fclose(file); + file = fopen(outfile_name.c_str(),"r"); + if (!file) + { + string msg = fastback_name; + msg += ": error: could not open: "; + msg += outfile_name; + perror(msg.c_str()); + exit(3); + } + if (curl_global_init(CURL_GLOBAL_ALL)) + { + fprintf(stderr,"%s: error: curl_global_init: could not initialze curl\n", + fastback_name); + exit(3); + } + + handle = curl_easy_init(); + if (!handle) + { + fprintf(stderr,"%s: error: curl_easy_init: could not initialize curl\n", fastback_name); + exit(3); + } + + if (fastback_logfile) + { + curl_err = curl_easy_setopt(handle, CURLOPT_VERBOSE, 1); + check_curl_error(curl_err, url, "curl_easy_setopt(CURLOPT_VERBOSE)"); + + curl_err = curl_easy_setopt(handle, CURLOPT_STDERR, fastback_logfile); + check_curl_error(curl_err, url, "curl_easy_setopt(CURLOPT_STDERR)"); + } + + curl_err = curl_easy_setopt(handle, CURLOPT_URL, url.c_str()); + check_curl_error(curl_err, url, "curl_easy_setopt(CURLOPT_URL)"); + + curl_err = curl_easy_setopt(handle, CURLOPT_READFUNCTION, fastback_read); + check_curl_error(curl_err, url, "curl_easy_setopt(CURLOPT_READFUNCTION)"); + + curl_err = curl_easy_setopt(handle, CURLOPT_READDATA, (void*)file); + check_curl_error(curl_err, url, "curl_easy_setopt(CURLOPT_READDATA)"); + + curl_err = curl_easy_setopt(handle, CURLOPT_UPLOAD, 1L); + check_curl_error(curl_err, url, "curl_easy_setopt(CURLOPT_READDATA)"); + + curl_err = curl_easy_perform(handle); + check_curl_error(curl_err, url, "curl_easy_perform"); + + curl_easy_cleanup(handle); + fclose(file); + } + if (fastback_ticket) printf("Please copy this into %s:\n", fastback_ticket); else @@ -723,7 +753,7 @@ main(int argc, char** argv) printf("%s", key.c_str()); } printf("END:\n"); - + cleanup(); return 0; } -- cgit