aboutsummaryrefslogtreecommitdiff
path: root/src/fs/gnunet-download.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/fs/gnunet-download.c')
-rw-r--r--src/fs/gnunet-download.c446
1 files changed, 226 insertions, 220 deletions
diff --git a/src/fs/gnunet-download.c b/src/fs/gnunet-download.c
index 00e8336a2..eda6765ab 100644
--- a/src/fs/gnunet-download.c
+++ b/src/fs/gnunet-download.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file fs/gnunet-download.c 21 * @file fs/gnunet-download.c
22 * @brief downloading for files on GNUnet 22 * @brief downloading for files on GNUnet
@@ -54,21 +54,21 @@ static int local_only;
54 54
55 55
56static void 56static void
57cleanup_task (void *cls) 57cleanup_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
64static void 64static void
65shutdown_task (void *cls) 65shutdown_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,30 +80,30 @@ 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 */
82static void 82static void
83display_bar (unsigned long long x, unsigned long long n, unsigned int w) 83display_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 ! WINDOWS 90#if !WINDOWS
91 if (0 == isatty (1)) 91 if (0 == isatty(1))
92 return; 92 return;
93#else 93#else
94 if (FILE_TYPE_CHAR != GetFileType (GetStdHandle (STD_OUTPUT_HANDLE))) 94 if (FILE_TYPE_CHAR != GetFileType(GetStdHandle(STD_OUTPUT_HANDLE)))
95 return; 95 return;
96#endif 96#endif
97 ratio_complete = x / (float) n; 97 ratio_complete = x / (float)n;
98 endeq = ratio_complete * w; 98 endeq = ratio_complete * w;
99 GNUNET_snprintf (buf, sizeof (buf), "%3d%% [", (int) (ratio_complete * 100)); 99 GNUNET_snprintf(buf, sizeof(buf), "%3d%% [", (int)(ratio_complete * 100));
100 for (p = 0; p < endeq; p++) 100 for (p = 0; p < endeq; p++)
101 strcat (buf, "="); 101 strcat(buf, "=");
102 for (p = endeq; p < w; p++) 102 for (p = endeq; p < w; p++)
103 strcat (buf, " "); 103 strcat(buf, " ");
104 strcat (buf, "]\r"); 104 strcat(buf, "]\r");
105 printf ("%s", buf); 105 printf("%s", buf);
106 fflush (stdout); 106 fflush(stdout);
107} 107}
108 108
109 109
@@ -121,100 +121,106 @@ display_bar (unsigned long long x, unsigned long long n, unsigned int w)
121 * field in the `struct GNUNET_FS_ProgressInfo` 121 * field in the `struct GNUNET_FS_ProgressInfo`
122 */ 122 */
123static void * 123static void *
124progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info) 124progress_cb(void *cls, const struct GNUNET_FS_ProgressInfo *info)
125{ 125{
126 char *s; 126 char *s;
127 const char *s2; 127 const char *s2;
128 char *t; 128 char *t;
129 129
130 switch (info->status) 130 switch (info->status)
131 {
132 case GNUNET_FS_STATUS_DOWNLOAD_START:
133 if (verbose > 1)
134 fprintf (stderr,
135 _ ("Starting download `%s'.\n"),
136 info->value.download.filename);
137 break;
138 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
139 if (verbose)
140 { 131 {
141 s = GNUNET_strdup ( 132 case GNUNET_FS_STATUS_DOWNLOAD_START:
142 GNUNET_STRINGS_relative_time_to_string (info->value.download.eta, 133 if (verbose > 1)
143 GNUNET_YES)); 134 fprintf(stderr,
144 if (info->value.download.specifics.progress.block_download_duration 135 _("Starting download `%s'.\n"),
145 .rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) 136 info->value.download.filename);
146 s2 = _ ("<unknown time>"); 137 break;
138
139 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
140 if (verbose)
141 {
142 s = GNUNET_strdup(
143 GNUNET_STRINGS_relative_time_to_string(info->value.download.eta,
144 GNUNET_YES));
145 if (info->value.download.specifics.progress.block_download_duration
146 .rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
147 s2 = _("<unknown time>");
148 else
149 s2 = GNUNET_STRINGS_relative_time_to_string(info->value.download
150 .specifics.progress
151 .block_download_duration,
152 GNUNET_YES);
153 t = GNUNET_STRINGS_byte_size_fancy(
154 info->value.download.completed * 1000LL /
155 (info->value.download.duration.rel_value_us + 1));
156 fprintf(
157 stdout,
158 _(
159 "Downloading `%s' at %llu/%llu (%s remaining, %s/s). Block took %s to download\n"),
160 info->value.download.filename,
161 (unsigned long long)info->value.download.completed,
162 (unsigned long long)info->value.download.size,
163 s,
164 t,
165 s2);
166 GNUNET_free(s);
167 GNUNET_free(t);
168 }
147 else 169 else
148 s2 = GNUNET_STRINGS_relative_time_to_string (info->value.download 170 {
149 .specifics.progress 171 display_bar(info->value.download.completed,
150 .block_download_duration, 172 info->value.download.size,
151 GNUNET_YES); 173 60);
152 t = GNUNET_STRINGS_byte_size_fancy ( 174 }
153 info->value.download.completed * 1000LL / 175 break;
154 (info->value.download.duration.rel_value_us + 1)); 176
155 fprintf ( 177 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
156 stdout, 178#if !WINDOWS
157 _ ( 179 if (0 != isatty(1))
158 "Downloading `%s' at %llu/%llu (%s remaining, %s/s). Block took %s to download\n"), 180 fprintf(stdout, "\n");
159 info->value.download.filename,
160 (unsigned long long) info->value.download.completed,
161 (unsigned long long) info->value.download.size,
162 s,
163 t,
164 s2);
165 GNUNET_free (s);
166 GNUNET_free (t);
167 }
168 else
169 {
170 display_bar (info->value.download.completed,
171 info->value.download.size,
172 60);
173 }
174 break;
175 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
176#if ! WINDOWS
177 if (0 != isatty (1))
178 fprintf (stdout, "\n");
179#else 181#else
180 if (FILE_TYPE_CHAR == GetFileType (GetStdHandle (STD_OUTPUT_HANDLE))) 182 if (FILE_TYPE_CHAR == GetFileType(GetStdHandle(STD_OUTPUT_HANDLE)))
181 fprintf (stdout, "\n"); 183 fprintf(stdout, "\n");
182#endif 184#endif
183 fprintf (stderr, 185 fprintf(stderr,
184 _ ("Error downloading: %s.\n"), 186 _("Error downloading: %s.\n"),
185 info->value.download.specifics.error.message); 187 info->value.download.specifics.error.message);
186 GNUNET_SCHEDULER_shutdown (); 188 GNUNET_SCHEDULER_shutdown();
187 break; 189 break;
188 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED: 190
189 s = GNUNET_STRINGS_byte_size_fancy ( 191 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
190 info->value.download.completed * 1000 / 192 s = GNUNET_STRINGS_byte_size_fancy(
191 (info->value.download.duration.rel_value_us + 1)); 193 info->value.download.completed * 1000 /
192#if ! WINDOWS 194 (info->value.download.duration.rel_value_us + 1));
193 if (0 != isatty (1)) 195#if !WINDOWS
194 fprintf (stdout, "\n"); 196 if (0 != isatty(1))
197 fprintf(stdout, "\n");
195#else 198#else
196 if (FILE_TYPE_CHAR == GetFileType (GetStdHandle (STD_OUTPUT_HANDLE))) 199 if (FILE_TYPE_CHAR == GetFileType(GetStdHandle(STD_OUTPUT_HANDLE)))
197 fprintf (stdout, "\n"); 200 fprintf(stdout, "\n");
198#endif 201#endif
199 fprintf (stdout, 202 fprintf(stdout,
200 _ ("Downloading `%s' done (%s/s).\n"), 203 _("Downloading `%s' done (%s/s).\n"),
201 info->value.download.filename, 204 info->value.download.filename,
202 s); 205 s);
203 GNUNET_free (s); 206 GNUNET_free(s);
204 if (info->value.download.dc == dc) 207 if (info->value.download.dc == dc)
205 GNUNET_SCHEDULER_shutdown (); 208 GNUNET_SCHEDULER_shutdown();
206 break; 209 break;
207 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED: 210
208 if (info->value.download.dc == dc) 211 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
209 GNUNET_SCHEDULER_add_now (&cleanup_task, NULL); 212 if (info->value.download.dc == dc)
210 break; 213 GNUNET_SCHEDULER_add_now(&cleanup_task, NULL);
211 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE: 214 break;
212 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE: 215
213 break; 216 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
214 default: 217 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
215 fprintf (stderr, _ ("Unexpected status: %d\n"), info->status); 218 break;
216 break; 219
217 } 220 default:
221 fprintf(stderr, _("Unexpected status: %d\n"), info->status);
222 break;
223 }
218 return NULL; 224 return NULL;
219} 225}
220 226
@@ -228,84 +234,84 @@ progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
228 * @param c configuration 234 * @param c configuration
229 */ 235 */
230static void 236static void
231run (void *cls, 237run(void *cls,
232 char *const *args, 238 char *const *args,
233 const char *cfgfile, 239 const char *cfgfile,
234 const struct GNUNET_CONFIGURATION_Handle *c) 240 const struct GNUNET_CONFIGURATION_Handle *c)
235{ 241{
236 struct GNUNET_FS_Uri *uri; 242 struct GNUNET_FS_Uri *uri;
237 char *emsg; 243 char *emsg;
238 enum GNUNET_FS_DownloadOptions options; 244 enum GNUNET_FS_DownloadOptions options;
239 245
240 if (NULL == args[0]) 246 if (NULL == args[0])
241 { 247 {
242 fprintf (stderr, "%s", _ ("You need to specify a URI argument.\n")); 248 fprintf(stderr, "%s", _("You need to specify a URI argument.\n"));
243 return; 249 return;
244 } 250 }
245 uri = GNUNET_FS_uri_parse (args[0], &emsg); 251 uri = GNUNET_FS_uri_parse(args[0], &emsg);
246 if (NULL == uri) 252 if (NULL == uri)
247 { 253 {
248 fprintf (stderr, _ ("Failed to parse URI: %s\n"), emsg); 254 fprintf(stderr, _("Failed to parse URI: %s\n"), emsg);
249 GNUNET_free (emsg); 255 GNUNET_free(emsg);
250 ret = 1; 256 ret = 1;
251 return; 257 return;
252 } 258 }
253 if ((! GNUNET_FS_uri_test_chk (uri)) && (! GNUNET_FS_uri_test_loc (uri))) 259 if ((!GNUNET_FS_uri_test_chk(uri)) && (!GNUNET_FS_uri_test_loc(uri)))
254 { 260 {
255 fprintf (stderr, "%s", _ ("Only CHK or LOC URIs supported.\n")); 261 fprintf(stderr, "%s", _("Only CHK or LOC URIs supported.\n"));
256 ret = 1; 262 ret = 1;
257 GNUNET_FS_uri_destroy (uri); 263 GNUNET_FS_uri_destroy(uri);
258 return; 264 return;
259 } 265 }
260 if (NULL == filename) 266 if (NULL == filename)
261 { 267 {
262 fprintf (stderr, "%s", _ ("Target filename must be specified.\n")); 268 fprintf(stderr, "%s", _("Target filename must be specified.\n"));
263 ret = 1; 269 ret = 1;
264 GNUNET_FS_uri_destroy (uri); 270 GNUNET_FS_uri_destroy(uri);
265 return; 271 return;
266 } 272 }
267 cfg = c; 273 cfg = c;
268 ctx = GNUNET_FS_start (cfg, 274 ctx = GNUNET_FS_start(cfg,
269 "gnunet-download", 275 "gnunet-download",
270 &progress_cb, 276 &progress_cb,
271 NULL, 277 NULL,
272 GNUNET_FS_FLAGS_NONE, 278 GNUNET_FS_FLAGS_NONE,
273 GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM, 279 GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM,
274 parallelism, 280 parallelism,
275 GNUNET_FS_OPTIONS_REQUEST_PARALLELISM, 281 GNUNET_FS_OPTIONS_REQUEST_PARALLELISM,
276 request_parallelism, 282 request_parallelism,
277 GNUNET_FS_OPTIONS_END); 283 GNUNET_FS_OPTIONS_END);
278 if (NULL == ctx) 284 if (NULL == ctx)
279 { 285 {
280 fprintf (stderr, _ ("Could not initialize `%s' subsystem.\n"), "FS"); 286 fprintf(stderr, _("Could not initialize `%s' subsystem.\n"), "FS");
281 GNUNET_FS_uri_destroy (uri); 287 GNUNET_FS_uri_destroy(uri);
282 ret = 1; 288 ret = 1;
283 return; 289 return;
284 } 290 }
285 options = GNUNET_FS_DOWNLOAD_OPTION_NONE; 291 options = GNUNET_FS_DOWNLOAD_OPTION_NONE;
286 if (do_recursive) 292 if (do_recursive)
287 options |= GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE; 293 options |= GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE;
288 if (local_only) 294 if (local_only)
289 options |= GNUNET_FS_DOWNLOAD_OPTION_LOOPBACK_ONLY; 295 options |= GNUNET_FS_DOWNLOAD_OPTION_LOOPBACK_ONLY;
290 dc = GNUNET_FS_download_start (ctx, 296 dc = GNUNET_FS_download_start(ctx,
291 uri, 297 uri,
292 NULL, 298 NULL,
293 filename, 299 filename,
294 NULL, 300 NULL,
295 0, 301 0,
296 GNUNET_FS_uri_chk_get_file_size (uri), 302 GNUNET_FS_uri_chk_get_file_size(uri),
297 anonymity, 303 anonymity,
298 options, 304 options,
299 NULL, 305 NULL,
300 NULL); 306 NULL);
301 GNUNET_FS_uri_destroy (uri); 307 GNUNET_FS_uri_destroy(uri);
302 if (dc == NULL) 308 if (dc == NULL)
303 { 309 {
304 GNUNET_FS_stop (ctx); 310 GNUNET_FS_stop(ctx);
305 ctx = NULL; 311 ctx = NULL;
306 return; 312 return;
307 } 313 }
308 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); 314 GNUNET_SCHEDULER_add_shutdown(&shutdown_task, NULL);
309} 315}
310 316
311 317
@@ -317,75 +323,75 @@ run (void *cls,
317 * @return 0 ok, 1 on error 323 * @return 0 ok, 1 on error
318 */ 324 */
319int 325int
320main (int argc, char *const *argv) 326main(int argc, char *const *argv)
321{ 327{
322 struct GNUNET_GETOPT_CommandLineOption options[] = 328 struct GNUNET_GETOPT_CommandLineOption options[] =
323 {GNUNET_GETOPT_option_uint ('a', 329 { GNUNET_GETOPT_option_uint('a',
324 "anonymity", 330 "anonymity",
325 "LEVEL", 331 "LEVEL",
326 gettext_noop ( 332 gettext_noop(
327 "set the desired LEVEL of receiver-anonymity"), 333 "set the desired LEVEL of receiver-anonymity"),
328 &anonymity), 334 &anonymity),
329 335
330 GNUNET_GETOPT_option_flag ( 336 GNUNET_GETOPT_option_flag(
331 'D', 337 'D',
332 "delete-incomplete", 338 "delete-incomplete",
333 gettext_noop ("delete incomplete downloads (when aborted with CTRL-C)"), 339 gettext_noop("delete incomplete downloads (when aborted with CTRL-C)"),
334 &delete_incomplete), 340 &delete_incomplete),
335 341
336 GNUNET_GETOPT_option_flag ( 342 GNUNET_GETOPT_option_flag(
337 'n', 343 'n',
338 "no-network", 344 "no-network",
339 gettext_noop ("only search the local peer (no P2P network search)"), 345 gettext_noop("only search the local peer (no P2P network search)"),
340 &local_only), 346 &local_only),
341 GNUNET_GETOPT_option_string ('o', 347 GNUNET_GETOPT_option_string('o',
342 "output", 348 "output",
343 "FILENAME", 349 "FILENAME",
344 gettext_noop ("write the file to FILENAME"), 350 gettext_noop("write the file to FILENAME"),
345 &filename), 351 &filename),
346 GNUNET_GETOPT_option_uint ( 352 GNUNET_GETOPT_option_uint(
347 'p', 353 'p',
348 "parallelism", 354 "parallelism",
349 "DOWNLOADS", 355 "DOWNLOADS",
350 gettext_noop ( 356 gettext_noop(
351 "set the maximum number of parallel downloads that is allowed"), 357 "set the maximum number of parallel downloads that is allowed"),
352 &parallelism), 358 &parallelism),
353 GNUNET_GETOPT_option_uint ( 359 GNUNET_GETOPT_option_uint(
354 'r', 360 'r',
355 "request-parallelism", 361 "request-parallelism",
356 "REQUESTS", 362 "REQUESTS",
357 gettext_noop ( 363 gettext_noop(
358 "set the maximum number of parallel requests for blocks that is allowed"), 364 "set the maximum number of parallel requests for blocks that is allowed"),
359 &request_parallelism), 365 &request_parallelism),
360 GNUNET_GETOPT_option_flag ('R', 366 GNUNET_GETOPT_option_flag('R',
361 "recursive", 367 "recursive",
362 gettext_noop ( 368 gettext_noop(
363 "download a GNUnet directory recursively"), 369 "download a GNUnet directory recursively"),
364 &do_recursive), 370 &do_recursive),
365 GNUNET_GETOPT_option_increment_uint ( 371 GNUNET_GETOPT_option_increment_uint(
366 'V', 372 'V',
367 "verbose", 373 "verbose",
368 gettext_noop ("be verbose (print progress information)"), 374 gettext_noop("be verbose (print progress information)"),
369 &verbose), 375 &verbose),
370 GNUNET_GETOPT_OPTION_END}; 376 GNUNET_GETOPT_OPTION_END };
371 377
372 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) 378 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
373 return 2; 379 return 2;
374 380
375 ret = 381 ret =
376 (GNUNET_OK == 382 (GNUNET_OK ==
377 GNUNET_PROGRAM_run ( 383 GNUNET_PROGRAM_run(
378 argc, 384 argc,
379 argv, 385 argv,
380 "gnunet-download [OPTIONS] URI", 386 "gnunet-download [OPTIONS] URI",
381 gettext_noop ( 387 gettext_noop(
382 "Download files from GNUnet using a GNUnet CHK or LOC URI (gnunet://fs/chk/...)"), 388 "Download files from GNUnet using a GNUnet CHK or LOC URI (gnunet://fs/chk/...)"),
383 options, 389 options,
384 &run, 390 &run,
385 NULL)) 391 NULL))
386 ? ret 392 ? ret
387 : 1; 393 : 1;
388 GNUNET_free ((void *) argv); 394 GNUNET_free((void *)argv);
389 return ret; 395 return ret;
390} 396}
391 397