1 // This file is part of BOINC.
2 // http://boinc.berkeley.edu
3 // Copyright (C) 2008 University of California
4 //
5 // BOINC is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Lesser General Public License
7 // as published by the Free Software Foundation,
8 // either version 3 of the License, or (at your option) any later version.
9 //
10 // BOINC is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 // See the GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
17 
18 // upload a file from a host
19 //
20 // get_file [options]
21 // --host_id N              ID of host to upload from
22 // --file_name name         file name
23 // [ --url x ]              URL of upload server (can specify several)
24 // [ --max_latency x ]      max latency, seconds (default 1 week)
25 // [ --max_nbytes x ]       max file size (default 1 GB)
26 //
27 // Run from the project root dir.
28 
29 #include "config.h"
30 
31 #if HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34 #include <sys/param.h>
35 #include <stdlib.h>
36 #include <string>
37 #include <time.h>
38 
39 #include "backend_lib.h"
40 #include "filesys.h"
41 #include "str_replace.h"
42 
43 #include "sched_config.h"
44 #include "sched_util.h"
45 #include "svn_version.h"
46 
usage()47 void usage() {
48     fprintf(stderr, "Gets a file from a host.\n"
49         "Usage: get_file [options]\n\n"
50         "  --host_id id           host DB ID\n"
51         "  --file_name name       Name of file\n"
52         "  [-- url X]             URL of upload server\n"
53         "  [ -v | --version ]     Show version\n"
54         "  [ -h | --help ]        Show help\n"
55     );
56 }
57 
main(int argc,char ** argv)58 int main(int argc, char** argv) {
59     int i, retval;
60     char file_name[256];
61     int host_id;
62     vector<const char*> urls;
63     double max_latency = 7*86400;
64     double max_nbytes = 1e9;
65 
66     strcpy(file_name, "");
67     host_id = 0;
68 
69     check_stop_daemons();
70 
71     for (i=1; i<argc; i++) {
72         if (is_arg(argv[i], "host_id")) {
73             if (!argv[++i]) {
74                 usage();
75                 exit(1);
76             }
77             host_id = atoi(argv[i]);
78         } else if (is_arg(argv[i], "file_name")) {
79             if (!argv[++i]) {
80                 usage();
81                 exit(1);
82             }
83             safe_strcpy(file_name, argv[i]);
84         } else if (is_arg(argv[i], "h") || is_arg(argv[i], "help")) {
85             usage();
86             exit(0);
87         } else if (is_arg(argv[i], "v") || is_arg(argv[i], "version")) {
88             printf("%s\n", SVN_VERSION);
89             exit(0);
90         } else if (is_arg(argv[i], "url")) {
91             urls.push_back(argv[++i]);
92         } else if (is_arg(argv[i], "max_latency")) {
93             max_latency = atof(argv[++i]);
94         } else if (is_arg(argv[i], "max_nbytes")) {
95             max_nbytes = atof(argv[++i]);
96         } else {
97             usage();
98             exit(1);
99         }
100     }
101 
102     if (!strlen(file_name) || host_id == 0) {
103         usage();
104         exit(1);
105     }
106 
107     retval = config.parse_file();
108     if (retval) {
109         fprintf(stderr, "Can't parse config.xml: %s\n", boincerror(retval));
110         exit(1);
111     }
112 
113     retval = boinc_db.open(
114         config.db_name, config.db_host, config.db_user, config.db_passwd
115     );
116     if (retval) {
117         fprintf(stderr, "boinc_db.open failed: %s\n", boincerror(retval));
118         exit(1);
119     }
120 
121     if (urls.size() == 0) {
122         urls.push_back(config.upload_url);
123     }
124 
125     R_RSA_PRIVATE_KEY key;
126     bool generate_upload_certificate = !config.dont_generate_upload_certificates;
127     if (generate_upload_certificate) {
128         char keypath[MAXPATHLEN];
129         sprintf(keypath, "%s/upload_private", config.key_dir);
130         retval = read_key_file(keypath, key);
131         if (retval) {
132             fprintf(stderr, "can't read key\n");
133             exit(1);
134         }
135     }
136 
137     retval = create_get_file_msg(
138         host_id, file_name, urls, max_nbytes,
139         dtime() + max_latency,
140         generate_upload_certificate, key
141     );
142 
143     if (retval) {
144         fprintf(stderr, "get_file() failed: %s\n", boincerror(retval));
145     }
146     boinc_db.close();
147 }
148 
149 const char *BOINC_RCSID_37238a0141 = "$Id$";
150