diff options
Diffstat (limited to 'src/fs/gnunet-download.c')
-rw-r--r-- | src/fs/gnunet-download.c | 382 |
1 files changed, 191 insertions, 191 deletions
diff --git a/src/fs/gnunet-download.c b/src/fs/gnunet-download.c index 14a43e6ec..05e102eac 100644 --- a/src/fs/gnunet-download.c +++ b/src/fs/gnunet-download.c | |||
@@ -54,21 +54,21 @@ static int local_only; | |||
54 | 54 | ||
55 | 55 | ||
56 | static void | 56 | static void |
57 | cleanup_task(void *cls) | 57 | cleanup_task (void *cls) |
58 | { | 58 | { |
59 | GNUNET_FS_stop(ctx); | 59 | GNUNET_FS_stop (ctx); |
60 | ctx = NULL; | 60 | ctx = NULL; |
61 | } | 61 | } |
62 | 62 | ||
63 | 63 | ||
64 | static void | 64 | static void |
65 | shutdown_task(void *cls) | 65 | shutdown_task (void *cls) |
66 | { | 66 | { |
67 | if (NULL != dc) | 67 | if (NULL != dc) |
68 | { | 68 | { |
69 | GNUNET_FS_download_stop(dc, delete_incomplete); | 69 | GNUNET_FS_download_stop (dc, delete_incomplete); |
70 | dc = NULL; | 70 | dc = NULL; |
71 | } | 71 | } |
72 | } | 72 | } |
73 | 73 | ||
74 | 74 | ||
@@ -80,25 +80,25 @@ shutdown_task(void *cls) | |||
80 | * @param w desired number of steps in the progress bar | 80 | * @param w desired number of steps in the progress bar |
81 | */ | 81 | */ |
82 | static void | 82 | static void |
83 | display_bar(unsigned long long x, unsigned long long n, unsigned int w) | 83 | display_bar (unsigned long long x, unsigned long long n, unsigned int w) |
84 | { | 84 | { |
85 | char buf[w + 20]; | 85 | char buf[w + 20]; |
86 | unsigned int p; | 86 | unsigned int p; |
87 | unsigned int endeq; | 87 | unsigned int endeq; |
88 | float ratio_complete; | 88 | float ratio_complete; |
89 | 89 | ||
90 | if (0 == isatty(1)) | 90 | if (0 == isatty (1)) |
91 | return; | 91 | return; |
92 | ratio_complete = x / (float)n; | 92 | ratio_complete = x / (float) n; |
93 | endeq = ratio_complete * w; | 93 | endeq = ratio_complete * w; |
94 | GNUNET_snprintf(buf, sizeof(buf), "%3d%% [", (int)(ratio_complete * 100)); | 94 | GNUNET_snprintf (buf, sizeof(buf), "%3d%% [", (int) (ratio_complete * 100)); |
95 | for (p = 0; p < endeq; p++) | 95 | for (p = 0; p < endeq; p++) |
96 | strcat(buf, "="); | 96 | strcat (buf, "="); |
97 | for (p = endeq; p < w; p++) | 97 | for (p = endeq; p < w; p++) |
98 | strcat(buf, " "); | 98 | strcat (buf, " "); |
99 | strcat(buf, "]\r"); | 99 | strcat (buf, "]\r"); |
100 | printf("%s", buf); | 100 | printf ("%s", buf); |
101 | fflush(stdout); | 101 | fflush (stdout); |
102 | } | 102 | } |
103 | 103 | ||
104 | 104 | ||
@@ -116,96 +116,96 @@ display_bar(unsigned long long x, unsigned long long n, unsigned int w) | |||
116 | * field in the `struct GNUNET_FS_ProgressInfo` | 116 | * field in the `struct GNUNET_FS_ProgressInfo` |
117 | */ | 117 | */ |
118 | static void * | 118 | static void * |
119 | progress_cb(void *cls, const struct GNUNET_FS_ProgressInfo *info) | 119 | progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info) |
120 | { | 120 | { |
121 | char *s; | 121 | char *s; |
122 | const char *s2; | 122 | const char *s2; |
123 | char *t; | 123 | char *t; |
124 | 124 | ||
125 | switch (info->status) | 125 | switch (info->status) |
126 | { | ||
127 | case GNUNET_FS_STATUS_DOWNLOAD_START: | ||
128 | if (verbose > 1) | ||
129 | fprintf (stderr, | ||
130 | _ ("Starting download `%s'.\n"), | ||
131 | info->value.download.filename); | ||
132 | break; | ||
133 | |||
134 | case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS: | ||
135 | if (verbose) | ||
126 | { | 136 | { |
127 | case GNUNET_FS_STATUS_DOWNLOAD_START: | 137 | s = GNUNET_strdup ( |
128 | if (verbose > 1) | 138 | GNUNET_STRINGS_relative_time_to_string (info->value.download.eta, |
129 | fprintf(stderr, | 139 | GNUNET_YES)); |
130 | _("Starting download `%s'.\n"), | 140 | if (info->value.download.specifics.progress.block_download_duration |
131 | info->value.download.filename); | 141 | .rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) |
132 | break; | 142 | s2 = _ ("<unknown time>"); |
133 | |||
134 | case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS: | ||
135 | if (verbose) | ||
136 | { | ||
137 | s = GNUNET_strdup( | ||
138 | GNUNET_STRINGS_relative_time_to_string(info->value.download.eta, | ||
139 | GNUNET_YES)); | ||
140 | if (info->value.download.specifics.progress.block_download_duration | ||
141 | .rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) | ||
142 | s2 = _("<unknown time>"); | ||
143 | else | ||
144 | s2 = GNUNET_STRINGS_relative_time_to_string(info->value.download | ||
145 | .specifics.progress | ||
146 | .block_download_duration, | ||
147 | GNUNET_YES); | ||
148 | t = GNUNET_STRINGS_byte_size_fancy( | ||
149 | info->value.download.completed * 1000LL / | ||
150 | (info->value.download.duration.rel_value_us + 1)); | ||
151 | fprintf( | ||
152 | stdout, | ||
153 | _( | ||
154 | "Downloading `%s' at %llu/%llu (%s remaining, %s/s). Block took %s to download\n"), | ||
155 | info->value.download.filename, | ||
156 | (unsigned long long)info->value.download.completed, | ||
157 | (unsigned long long)info->value.download.size, | ||
158 | s, | ||
159 | t, | ||
160 | s2); | ||
161 | GNUNET_free(s); | ||
162 | GNUNET_free(t); | ||
163 | } | ||
164 | else | 143 | else |
165 | { | 144 | s2 = GNUNET_STRINGS_relative_time_to_string (info->value.download |
166 | display_bar(info->value.download.completed, | 145 | .specifics.progress |
167 | info->value.download.size, | 146 | .block_download_duration, |
168 | 60); | 147 | GNUNET_YES); |
169 | } | 148 | t = GNUNET_STRINGS_byte_size_fancy ( |
170 | break; | 149 | info->value.download.completed * 1000LL |
171 | 150 | / (info->value.download.duration.rel_value_us + 1)); | |
172 | case GNUNET_FS_STATUS_DOWNLOAD_ERROR: | 151 | fprintf ( |
173 | if (0 != isatty(1)) | 152 | stdout, |
174 | fprintf(stdout, "\n"); | 153 | _ ( |
175 | fprintf(stderr, | 154 | "Downloading `%s' at %llu/%llu (%s remaining, %s/s). Block took %s to download\n"), |
176 | _("Error downloading: %s.\n"), | 155 | info->value.download.filename, |
177 | info->value.download.specifics.error.message); | 156 | (unsigned long long) info->value.download.completed, |
178 | GNUNET_SCHEDULER_shutdown(); | 157 | (unsigned long long) info->value.download.size, |
179 | break; | 158 | s, |
180 | 159 | t, | |
181 | case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED: | 160 | s2); |
182 | s = GNUNET_STRINGS_byte_size_fancy( | 161 | GNUNET_free (s); |
183 | info->value.download.completed * 1000 / | 162 | GNUNET_free (t); |
184 | (info->value.download.duration.rel_value_us + 1)); | 163 | } |
185 | if (0 != isatty(1)) | 164 | else |
186 | fprintf(stdout, "\n"); | 165 | { |
187 | fprintf(stdout, | 166 | display_bar (info->value.download.completed, |
188 | _("Downloading `%s' done (%s/s).\n"), | 167 | info->value.download.size, |
189 | info->value.download.filename, | 168 | 60); |
190 | s); | ||
191 | GNUNET_free(s); | ||
192 | if (info->value.download.dc == dc) | ||
193 | GNUNET_SCHEDULER_shutdown(); | ||
194 | break; | ||
195 | |||
196 | case GNUNET_FS_STATUS_DOWNLOAD_STOPPED: | ||
197 | if (info->value.download.dc == dc) | ||
198 | GNUNET_SCHEDULER_add_now(&cleanup_task, NULL); | ||
199 | break; | ||
200 | |||
201 | case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE: | ||
202 | case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE: | ||
203 | break; | ||
204 | |||
205 | default: | ||
206 | fprintf(stderr, _("Unexpected status: %d\n"), info->status); | ||
207 | break; | ||
208 | } | 169 | } |
170 | break; | ||
171 | |||
172 | case GNUNET_FS_STATUS_DOWNLOAD_ERROR: | ||
173 | if (0 != isatty (1)) | ||
174 | fprintf (stdout, "\n"); | ||
175 | fprintf (stderr, | ||
176 | _ ("Error downloading: %s.\n"), | ||
177 | info->value.download.specifics.error.message); | ||
178 | GNUNET_SCHEDULER_shutdown (); | ||
179 | break; | ||
180 | |||
181 | case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED: | ||
182 | s = GNUNET_STRINGS_byte_size_fancy ( | ||
183 | info->value.download.completed * 1000 | ||
184 | / (info->value.download.duration.rel_value_us + 1)); | ||
185 | if (0 != isatty (1)) | ||
186 | fprintf (stdout, "\n"); | ||
187 | fprintf (stdout, | ||
188 | _ ("Downloading `%s' done (%s/s).\n"), | ||
189 | info->value.download.filename, | ||
190 | s); | ||
191 | GNUNET_free (s); | ||
192 | if (info->value.download.dc == dc) | ||
193 | GNUNET_SCHEDULER_shutdown (); | ||
194 | break; | ||
195 | |||
196 | case GNUNET_FS_STATUS_DOWNLOAD_STOPPED: | ||
197 | if (info->value.download.dc == dc) | ||
198 | GNUNET_SCHEDULER_add_now (&cleanup_task, NULL); | ||
199 | break; | ||
200 | |||
201 | case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE: | ||
202 | case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE: | ||
203 | break; | ||
204 | |||
205 | default: | ||
206 | fprintf (stderr, _ ("Unexpected status: %d\n"), info->status); | ||
207 | break; | ||
208 | } | ||
209 | return NULL; | 209 | return NULL; |
210 | } | 210 | } |
211 | 211 | ||
@@ -219,84 +219,84 @@ progress_cb(void *cls, const struct GNUNET_FS_ProgressInfo *info) | |||
219 | * @param c configuration | 219 | * @param c configuration |
220 | */ | 220 | */ |
221 | static void | 221 | static void |
222 | run(void *cls, | 222 | run (void *cls, |
223 | char *const *args, | 223 | char *const *args, |
224 | const char *cfgfile, | 224 | const char *cfgfile, |
225 | const struct GNUNET_CONFIGURATION_Handle *c) | 225 | const struct GNUNET_CONFIGURATION_Handle *c) |
226 | { | 226 | { |
227 | struct GNUNET_FS_Uri *uri; | 227 | struct GNUNET_FS_Uri *uri; |
228 | char *emsg; | 228 | char *emsg; |
229 | enum GNUNET_FS_DownloadOptions options; | 229 | enum GNUNET_FS_DownloadOptions options; |
230 | 230 | ||
231 | if (NULL == args[0]) | 231 | if (NULL == args[0]) |
232 | { | 232 | { |
233 | fprintf(stderr, "%s", _("You need to specify a URI argument.\n")); | 233 | fprintf (stderr, "%s", _ ("You need to specify a URI argument.\n")); |
234 | return; | 234 | return; |
235 | } | 235 | } |
236 | uri = GNUNET_FS_uri_parse(args[0], &emsg); | 236 | uri = GNUNET_FS_uri_parse (args[0], &emsg); |
237 | if (NULL == uri) | 237 | if (NULL == uri) |
238 | { | 238 | { |
239 | fprintf(stderr, _("Failed to parse URI: %s\n"), emsg); | 239 | fprintf (stderr, _ ("Failed to parse URI: %s\n"), emsg); |
240 | GNUNET_free(emsg); | 240 | GNUNET_free (emsg); |
241 | ret = 1; | 241 | ret = 1; |
242 | return; | 242 | return; |
243 | } | 243 | } |
244 | if ((!GNUNET_FS_uri_test_chk(uri)) && (!GNUNET_FS_uri_test_loc(uri))) | 244 | if ((! GNUNET_FS_uri_test_chk (uri)) && (! GNUNET_FS_uri_test_loc (uri))) |
245 | { | 245 | { |
246 | fprintf(stderr, "%s", _("Only CHK or LOC URIs supported.\n")); | 246 | fprintf (stderr, "%s", _ ("Only CHK or LOC URIs supported.\n")); |
247 | ret = 1; | 247 | ret = 1; |
248 | GNUNET_FS_uri_destroy(uri); | 248 | GNUNET_FS_uri_destroy (uri); |
249 | return; | 249 | return; |
250 | } | 250 | } |
251 | if (NULL == filename) | 251 | if (NULL == filename) |
252 | { | 252 | { |
253 | fprintf(stderr, "%s", _("Target filename must be specified.\n")); | 253 | fprintf (stderr, "%s", _ ("Target filename must be specified.\n")); |
254 | ret = 1; | 254 | ret = 1; |
255 | GNUNET_FS_uri_destroy(uri); | 255 | GNUNET_FS_uri_destroy (uri); |
256 | return; | 256 | return; |
257 | } | 257 | } |
258 | cfg = c; | 258 | cfg = c; |
259 | ctx = GNUNET_FS_start(cfg, | 259 | ctx = GNUNET_FS_start (cfg, |
260 | "gnunet-download", | 260 | "gnunet-download", |
261 | &progress_cb, | 261 | &progress_cb, |
262 | NULL, | 262 | NULL, |
263 | GNUNET_FS_FLAGS_NONE, | 263 | GNUNET_FS_FLAGS_NONE, |
264 | GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM, | 264 | GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM, |
265 | parallelism, | 265 | parallelism, |
266 | GNUNET_FS_OPTIONS_REQUEST_PARALLELISM, | 266 | GNUNET_FS_OPTIONS_REQUEST_PARALLELISM, |
267 | request_parallelism, | 267 | request_parallelism, |
268 | GNUNET_FS_OPTIONS_END); | 268 | GNUNET_FS_OPTIONS_END); |
269 | if (NULL == ctx) | 269 | if (NULL == ctx) |
270 | { | 270 | { |
271 | fprintf(stderr, _("Could not initialize `%s' subsystem.\n"), "FS"); | 271 | fprintf (stderr, _ ("Could not initialize `%s' subsystem.\n"), "FS"); |
272 | GNUNET_FS_uri_destroy(uri); | 272 | GNUNET_FS_uri_destroy (uri); |
273 | ret = 1; | 273 | ret = 1; |
274 | return; | 274 | return; |
275 | } | 275 | } |
276 | options = GNUNET_FS_DOWNLOAD_OPTION_NONE; | 276 | options = GNUNET_FS_DOWNLOAD_OPTION_NONE; |
277 | if (do_recursive) | 277 | if (do_recursive) |
278 | options |= GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE; | 278 | options |= GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE; |
279 | if (local_only) | 279 | if (local_only) |
280 | options |= GNUNET_FS_DOWNLOAD_OPTION_LOOPBACK_ONLY; | 280 | options |= GNUNET_FS_DOWNLOAD_OPTION_LOOPBACK_ONLY; |
281 | dc = GNUNET_FS_download_start(ctx, | 281 | dc = GNUNET_FS_download_start (ctx, |
282 | uri, | 282 | uri, |
283 | NULL, | 283 | NULL, |
284 | filename, | 284 | filename, |
285 | NULL, | 285 | NULL, |
286 | 0, | 286 | 0, |
287 | GNUNET_FS_uri_chk_get_file_size(uri), | 287 | GNUNET_FS_uri_chk_get_file_size (uri), |
288 | anonymity, | 288 | anonymity, |
289 | options, | 289 | options, |
290 | NULL, | 290 | NULL, |
291 | NULL); | 291 | NULL); |
292 | GNUNET_FS_uri_destroy(uri); | 292 | GNUNET_FS_uri_destroy (uri); |
293 | if (dc == NULL) | 293 | if (dc == NULL) |
294 | { | 294 | { |
295 | GNUNET_FS_stop(ctx); | 295 | GNUNET_FS_stop (ctx); |
296 | ctx = NULL; | 296 | ctx = NULL; |
297 | return; | 297 | return; |
298 | } | 298 | } |
299 | GNUNET_SCHEDULER_add_shutdown(&shutdown_task, NULL); | 299 | GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); |
300 | } | 300 | } |
301 | 301 | ||
302 | 302 | ||
@@ -308,75 +308,75 @@ run(void *cls, | |||
308 | * @return 0 ok, 1 on error | 308 | * @return 0 ok, 1 on error |
309 | */ | 309 | */ |
310 | int | 310 | int |
311 | main(int argc, char *const *argv) | 311 | main (int argc, char *const *argv) |
312 | { | 312 | { |
313 | struct GNUNET_GETOPT_CommandLineOption options[] = | 313 | struct GNUNET_GETOPT_CommandLineOption options[] = |
314 | { GNUNET_GETOPT_option_uint('a', | 314 | { GNUNET_GETOPT_option_uint ('a', |
315 | "anonymity", | 315 | "anonymity", |
316 | "LEVEL", | 316 | "LEVEL", |
317 | gettext_noop( | 317 | gettext_noop ( |
318 | "set the desired LEVEL of receiver-anonymity"), | 318 | "set the desired LEVEL of receiver-anonymity"), |
319 | &anonymity), | 319 | &anonymity), |
320 | 320 | ||
321 | GNUNET_GETOPT_option_flag( | 321 | GNUNET_GETOPT_option_flag ( |
322 | 'D', | 322 | 'D', |
323 | "delete-incomplete", | 323 | "delete-incomplete", |
324 | gettext_noop("delete incomplete downloads (when aborted with CTRL-C)"), | 324 | gettext_noop ("delete incomplete downloads (when aborted with CTRL-C)"), |
325 | &delete_incomplete), | 325 | &delete_incomplete), |
326 | 326 | ||
327 | GNUNET_GETOPT_option_flag( | 327 | GNUNET_GETOPT_option_flag ( |
328 | 'n', | 328 | 'n', |
329 | "no-network", | 329 | "no-network", |
330 | gettext_noop("only search the local peer (no P2P network search)"), | 330 | gettext_noop ("only search the local peer (no P2P network search)"), |
331 | &local_only), | 331 | &local_only), |
332 | GNUNET_GETOPT_option_string('o', | 332 | GNUNET_GETOPT_option_string ('o', |
333 | "output", | 333 | "output", |
334 | "FILENAME", | 334 | "FILENAME", |
335 | gettext_noop("write the file to FILENAME"), | 335 | gettext_noop ("write the file to FILENAME"), |
336 | &filename), | 336 | &filename), |
337 | GNUNET_GETOPT_option_uint( | 337 | GNUNET_GETOPT_option_uint ( |
338 | 'p', | 338 | 'p', |
339 | "parallelism", | 339 | "parallelism", |
340 | "DOWNLOADS", | 340 | "DOWNLOADS", |
341 | gettext_noop( | 341 | gettext_noop ( |
342 | "set the maximum number of parallel downloads that is allowed"), | 342 | "set the maximum number of parallel downloads that is allowed"), |
343 | ¶llelism), | 343 | ¶llelism), |
344 | GNUNET_GETOPT_option_uint( | 344 | GNUNET_GETOPT_option_uint ( |
345 | 'r', | 345 | 'r', |
346 | "request-parallelism", | 346 | "request-parallelism", |
347 | "REQUESTS", | 347 | "REQUESTS", |
348 | gettext_noop( | 348 | gettext_noop ( |
349 | "set the maximum number of parallel requests for blocks that is allowed"), | 349 | "set the maximum number of parallel requests for blocks that is allowed"), |
350 | &request_parallelism), | 350 | &request_parallelism), |
351 | GNUNET_GETOPT_option_flag('R', | 351 | GNUNET_GETOPT_option_flag ('R', |
352 | "recursive", | 352 | "recursive", |
353 | gettext_noop( | 353 | gettext_noop ( |
354 | "download a GNUnet directory recursively"), | 354 | "download a GNUnet directory recursively"), |
355 | &do_recursive), | 355 | &do_recursive), |
356 | GNUNET_GETOPT_option_increment_uint( | 356 | GNUNET_GETOPT_option_increment_uint ( |
357 | 'V', | 357 | 'V', |
358 | "verbose", | 358 | "verbose", |
359 | gettext_noop("be verbose (print progress information)"), | 359 | gettext_noop ("be verbose (print progress information)"), |
360 | &verbose), | 360 | &verbose), |
361 | GNUNET_GETOPT_OPTION_END }; | 361 | GNUNET_GETOPT_OPTION_END }; |
362 | 362 | ||
363 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv)) | 363 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) |
364 | return 2; | 364 | return 2; |
365 | 365 | ||
366 | ret = | 366 | ret = |
367 | (GNUNET_OK == | 367 | (GNUNET_OK == |
368 | GNUNET_PROGRAM_run( | 368 | GNUNET_PROGRAM_run ( |
369 | argc, | 369 | argc, |
370 | argv, | 370 | argv, |
371 | "gnunet-download [OPTIONS] URI", | 371 | "gnunet-download [OPTIONS] URI", |
372 | gettext_noop( | 372 | gettext_noop ( |
373 | "Download files from GNUnet using a GNUnet CHK or LOC URI (gnunet://fs/chk/...)"), | 373 | "Download files from GNUnet using a GNUnet CHK or LOC URI (gnunet://fs/chk/...)"), |
374 | options, | 374 | options, |
375 | &run, | 375 | &run, |
376 | NULL)) | 376 | NULL)) |
377 | ? ret | 377 | ? ret |
378 | : 1; | 378 | : 1; |
379 | GNUNET_free((void *)argv); | 379 | GNUNET_free ((void *) argv); |
380 | return ret; | 380 | return ret; |
381 | } | 381 | } |
382 | 382 | ||