aboutsummaryrefslogtreecommitdiff
path: root/src/fs/gnunet-publish.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2013-07-13 18:51:45 +0000
committerChristian Grothoff <christian@grothoff.org>2013-07-13 18:51:45 +0000
commit09c5a48cf38496ac4e7d94677b3a1d037aba71f6 (patch)
tree7f346ff163ad2ebbaf9560037e4ece645ddb44b5 /src/fs/gnunet-publish.c
parent76557699761b37fc6d049c0d4c695e51ae12564d (diff)
downloadgnunet-09c5a48cf38496ac4e7d94677b3a1d037aba71f6.tar.gz
gnunet-09c5a48cf38496ac4e7d94677b3a1d037aba71f6.zip
-documenting and cleaning up gnunet-publish code
Diffstat (limited to 'src/fs/gnunet-publish.c')
-rw-r--r--src/fs/gnunet-publish.c186
1 files changed, 140 insertions, 46 deletions
diff --git a/src/fs/gnunet-publish.c b/src/fs/gnunet-publish.c
index 08b34c6d6..fc3b6ed59 100644
--- a/src/fs/gnunet-publish.c
+++ b/src/fs/gnunet-publish.c
@@ -28,52 +28,124 @@
28#include "platform.h" 28#include "platform.h"
29#include "gnunet_fs_service.h" 29#include "gnunet_fs_service.h"
30 30
31/**
32 * Global return value from 'main'.
33 */
31static int ret = 1; 34static int ret = 1;
32 35
36/**
37 * Command line option 'verbose' set
38 */
33static int verbose; 39static int verbose;
34 40
41/**
42 * Handle to our configuration.
43 */
35static const struct GNUNET_CONFIGURATION_Handle *cfg; 44static const struct GNUNET_CONFIGURATION_Handle *cfg;
36 45
46/**
47 * Handle for interaction with file-sharing service.
48 */
37static struct GNUNET_FS_Handle *ctx; 49static struct GNUNET_FS_Handle *ctx;
38 50
51/**
52 * Handle to FS-publishing operation.
53 */
39static struct GNUNET_FS_PublishContext *pc; 54static struct GNUNET_FS_PublishContext *pc;
40 55
56/**
57 * Meta-data provided via command-line option.
58 */
41static struct GNUNET_CONTAINER_MetaData *meta; 59static struct GNUNET_CONTAINER_MetaData *meta;
42 60
61/**
62 * Keywords provided via command-line option.
63 */
43static struct GNUNET_FS_Uri *topKeywords; 64static struct GNUNET_FS_Uri *topKeywords;
44 65
45static struct GNUNET_FS_Uri *uri; 66/**
46 67 * Options we set for published blocks.
68 */
47static struct GNUNET_FS_BlockOptions bo = { {0LL}, 1, 365, 1 }; 69static struct GNUNET_FS_BlockOptions bo = { {0LL}, 1, 365, 1 };
48 70
71/**
72 * Value of URI provided on command-line (when not publishing
73 * a file but just creating UBlocks to refer to an existing URI).
74 */
49static char *uri_string; 75static char *uri_string;
50 76
77/**
78 * Value of URI provided on command-line (when not publishing
79 * a file but just creating UBlocks to refer to an existing URI);
80 * parsed version of 'uri_string'.
81 */
82static struct GNUNET_FS_Uri *uri;
83
84/**
85 * Command-line option for namespace publishing: identifier for updates
86 * to this publication.
87 */
51static char *next_id; 88static char *next_id;
52 89
90/**
91 * Command-line option for namespace publishing: identifier for this
92 * publication.
93 */
53static char *this_id; 94static char *this_id;
54 95
96/**
97 * Command-line option identifying the pseudonym to use for the publication.
98 */
55static char *pseudonym; 99static char *pseudonym;
56 100
101/**
102 * Command-line option for 'inserting'
103 */
57static int do_insert; 104static int do_insert;
58 105
106/**
107 * Command-line option to disable meta data extraction.
108 */
59static int disable_extractor; 109static int disable_extractor;
60 110
111/**
112 * Command-line option to merely simulate publishing operation.
113 */
61static int do_simulate; 114static int do_simulate;
62 115
116/**
117 * Command-line option to only perform meta data extraction, but not publish.
118 */
63static int extract_only; 119static int extract_only;
64 120
121/**
122 * Command-line option to disable adding creation time.
123 */
65static int do_disable_creation_time; 124static int do_disable_creation_time;
66 125
126/**
127 * Task run on CTRL-C to kill everything nicely.
128 */
67static GNUNET_SCHEDULER_TaskIdentifier kill_task; 129static GNUNET_SCHEDULER_TaskIdentifier kill_task;
68 130
131/**
132 * Handle to the directory scanner (for recursive insertions).
133 */
69static struct GNUNET_FS_DirScanner *ds; 134static struct GNUNET_FS_DirScanner *ds;
70 135
71static struct GNUNET_FS_ShareTreeItem * directory_scan_result; 136/**
72 137 * Which namespace do we publish to? NULL if we do not publish to
138 * a namespace.
139 */
73static struct GNUNET_FS_Namespace *namespace; 140static struct GNUNET_FS_Namespace *namespace;
74 141
142
75/** 143/**
76 * FIXME: docu 144 * We are finished with the publishing operation, clean up all
145 * FS state.
146 *
147 * @param cls NULL
148 * @param tc scheduler context
77 */ 149 */
78static void 150static void
79do_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 151do_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
@@ -110,7 +182,7 @@ stop_scanner_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
110 GNUNET_FS_directory_scan_abort (ds); 182 GNUNET_FS_directory_scan_abort (ds);
111 ds = NULL; 183 ds = NULL;
112 } 184 }
113 if (namespace != NULL) 185 if (NULL != namespace)
114 { 186 {
115 GNUNET_FS_namespace_delete (namespace, GNUNET_NO); 187 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
116 namespace = NULL; 188 namespace = NULL;
@@ -172,9 +244,9 @@ progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
172 completed.chk_uri); 244 completed.chk_uri);
173 FPRINTF (stdout, _("URI is `%s'.\n"), suri); 245 FPRINTF (stdout, _("URI is `%s'.\n"), suri);
174 GNUNET_free (suri); 246 GNUNET_free (suri);
175 if (info->value.publish.pctx == NULL) 247 if (NULL == info->value.publish.pctx)
176 { 248 {
177 if (kill_task != GNUNET_SCHEDULER_NO_TASK) 249 if (GNUNET_SCHEDULER_NO_TASK != kill_task)
178 { 250 {
179 GNUNET_SCHEDULER_cancel (kill_task); 251 GNUNET_SCHEDULER_cancel (kill_task);
180 kill_task = GNUNET_SCHEDULER_NO_TASK; 252 kill_task = GNUNET_SCHEDULER_NO_TASK;
@@ -217,10 +289,10 @@ meta_printer (void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type,
217 enum EXTRACTOR_MetaFormat format, const char *data_mime_type, 289 enum EXTRACTOR_MetaFormat format, const char *data_mime_type,
218 const char *data, size_t data_size) 290 const char *data, size_t data_size)
219{ 291{
220 if ((format != EXTRACTOR_METAFORMAT_UTF8) && 292 if ((EXTRACTOR_METAFORMAT_UTF8 != format) &&
221 (format != EXTRACTOR_METAFORMAT_C_STRING)) 293 (EXTRACTOR_METAFORMAT_C_STRING != format))
222 return 0; 294 return 0;
223 if (type == EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME) 295 if (EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME == type)
224 return 0; 296 return 0;
225 FPRINTF (stdout, "\t%s - %s\n", EXTRACTOR_metatype_to_string (type), data); 297 FPRINTF (stdout, "\t%s - %s\n", EXTRACTOR_metatype_to_string (type), data);
226 return 0; 298 return 0;
@@ -281,7 +353,7 @@ publish_inspector (void *cls, struct GNUNET_FS_FileInformation *fi,
281 } 353 }
282 if (NULL != topKeywords) 354 if (NULL != topKeywords)
283 { 355 {
284 if (*uri != NULL) 356 if (NULL != *uri)
285 { 357 {
286 new_uri = GNUNET_FS_uri_ksk_merge (topKeywords, *uri); 358 new_uri = GNUNET_FS_uri_ksk_merge (topKeywords, *uri);
287 GNUNET_FS_uri_destroy (*uri); 359 GNUNET_FS_uri_destroy (*uri);
@@ -323,13 +395,19 @@ publish_inspector (void *cls, struct GNUNET_FS_FileInformation *fi,
323 395
324 396
325/** 397/**
326 * FIXME: docu 398 * Function called upon completion of the publishing
399 * of the UBLOCK for the SKS URI. As this is the last
400 * step, stop our interaction with FS (clean up).
401 *
402 * @param cls NULL (closure)
403 * @param sks_uri URI for the block that was published
404 * @param emsg error message, NULL on success
327 */ 405 */
328static void 406static void
329uri_sks_continuation (void *cls, const struct GNUNET_FS_Uri *ksk_uri, 407uri_sks_continuation (void *cls, const struct GNUNET_FS_Uri *sks_uri,
330 const char *emsg) 408 const char *emsg)
331{ 409{
332 if (emsg != NULL) 410 if (NULL != emsg)
333 { 411 {
334 FPRINTF (stderr, "%s\n", emsg); 412 FPRINTF (stderr, "%s\n", emsg);
335 ret = 1; 413 ret = 1;
@@ -342,7 +420,13 @@ uri_sks_continuation (void *cls, const struct GNUNET_FS_Uri *ksk_uri,
342 420
343 421
344/** 422/**
345 * FIXME: docu 423 * Function called upon completion of the publishing
424 * of the UBLOCK for the KSK URI. Continue with
425 * publishing the SKS URI (if applicable) or clean up.
426 *
427 * @param cls NULL (closure)
428 * @param ksk_uri URI for the block that was published
429 * @param emsg error message, NULL on success
346 */ 430 */
347static void 431static void
348uri_ksk_continuation (void *cls, const struct GNUNET_FS_Uri *ksk_uri, 432uri_ksk_continuation (void *cls, const struct GNUNET_FS_Uri *ksk_uri,
@@ -350,15 +434,15 @@ uri_ksk_continuation (void *cls, const struct GNUNET_FS_Uri *ksk_uri,
350{ 434{
351 struct GNUNET_FS_Namespace *ns; 435 struct GNUNET_FS_Namespace *ns;
352 436
353 if (emsg != NULL) 437 if (NULL != emsg)
354 { 438 {
355 FPRINTF (stderr, "%s\n", emsg); 439 FPRINTF (stderr, "%s\n", emsg);
356 ret = 1; 440 ret = 1;
357 } 441 }
358 if (pseudonym != NULL) 442 if (NULL != pseudonym)
359 { 443 {
360 ns = GNUNET_FS_namespace_create (ctx, pseudonym); 444 ns = GNUNET_FS_namespace_create (ctx, pseudonym);
361 if (ns == NULL) 445 if (NULL == ns)
362 { 446 {
363 FPRINTF (stderr, _("Failed to create namespace `%s' (illegal filename?)\n"), pseudonym); 447 FPRINTF (stderr, _("Failed to create namespace `%s' (illegal filename?)\n"), pseudonym);
364 ret = 1; 448 ret = 1;
@@ -380,7 +464,11 @@ uri_ksk_continuation (void *cls, const struct GNUNET_FS_Uri *ksk_uri,
380 464
381 465
382/** 466/**
383 * FIXME: docu 467 * Iterate over the results from the directory scan and extract
468 * the desired information for the publishing operation.
469 *
470 * @param item root with the data from the directroy scan
471 * @return handle with the information for the publishing operation
384 */ 472 */
385static struct GNUNET_FS_FileInformation * 473static struct GNUNET_FS_FileInformation *
386get_file_information (struct GNUNET_FS_ShareTreeItem *item) 474get_file_information (struct GNUNET_FS_ShareTreeItem *item)
@@ -389,10 +477,11 @@ get_file_information (struct GNUNET_FS_ShareTreeItem *item)
389 struct GNUNET_FS_FileInformation *fic; 477 struct GNUNET_FS_FileInformation *fic;
390 struct GNUNET_FS_ShareTreeItem *child; 478 struct GNUNET_FS_ShareTreeItem *child;
391 479
392 if (item->is_directory == GNUNET_YES) 480 if (GNUNET_YES == item->is_directory)
393 { 481 {
394 GNUNET_CONTAINER_meta_data_delete (item->meta, 482 GNUNET_CONTAINER_meta_data_delete (item->meta,
395 EXTRACTOR_METATYPE_MIMETYPE, NULL, 0); 483 EXTRACTOR_METATYPE_MIMETYPE,
484 NULL, 0);
396 GNUNET_FS_meta_data_make_directory (item->meta); 485 GNUNET_FS_meta_data_make_directory (item->meta);
397 if (NULL == item->ksk_uri) 486 if (NULL == item->ksk_uri)
398 { 487 {
@@ -401,10 +490,11 @@ get_file_information (struct GNUNET_FS_ShareTreeItem *item)
401 } 490 }
402 else 491 else
403 GNUNET_FS_uri_ksk_add_keyword (item->ksk_uri, GNUNET_FS_DIRECTORY_MIME, 492 GNUNET_FS_uri_ksk_add_keyword (item->ksk_uri, GNUNET_FS_DIRECTORY_MIME,
404 GNUNET_NO); 493 GNUNET_NO);
405 fi = GNUNET_FS_file_information_create_empty_directory ( 494 fi = GNUNET_FS_file_information_create_empty_directory (ctx, NULL,
406 ctx, NULL, item->ksk_uri, 495 item->ksk_uri,
407 item->meta, &bo, item->filename); 496 item->meta,
497 &bo, item->filename);
408 for (child = item->children_head; child; child = child->next) 498 for (child = item->children_head; child; child = child->next)
409 { 499 {
410 fic = get_file_information (child); 500 fic = get_file_information (child);
@@ -413,30 +503,33 @@ get_file_information (struct GNUNET_FS_ShareTreeItem *item)
413 } 503 }
414 else 504 else
415 { 505 {
416 fi = GNUNET_FS_file_information_create_from_file ( 506 fi = GNUNET_FS_file_information_create_from_file (ctx, NULL,
417 ctx, NULL, item->filename, 507 item->filename,
418 item->ksk_uri, item->meta, !do_insert, 508 item->ksk_uri, item->meta,
419 &bo); 509 !do_insert,
510 &bo);
420 } 511 }
421 return fi; 512 return fi;
422} 513}
423 514
424 515
425/** 516/**
426 * FIXME: docu 517 * We've finished scanning the directory and optimized the meta data.
518 * Begin the publication process.
519 *
520 * @param directroy_scan_result result from the directory scan, freed in this function
427 */ 521 */
428static void 522static void
429directory_trim_complete () 523directory_trim_complete (struct GNUNET_FS_ShareTreeItem *directory_scan_result)
430{ 524{
431 struct GNUNET_FS_FileInformation *fi; 525 struct GNUNET_FS_FileInformation *fi;
432 526
433 fi = get_file_information (directory_scan_result); 527 fi = get_file_information (directory_scan_result);
434 GNUNET_FS_share_tree_free (directory_scan_result); 528 GNUNET_FS_share_tree_free (directory_scan_result);
435 directory_scan_result = NULL; 529 if (NULL == fi)
436 if (fi == NULL)
437 { 530 {
438 FPRINTF (stderr, "%s", _("Could not publish\n")); 531 FPRINTF (stderr, "%s", _("Could not publish\n"));
439 if (namespace != NULL) 532 if (NULL != namespace)
440 GNUNET_FS_namespace_delete (namespace, GNUNET_NO); 533 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
441 GNUNET_FS_stop (ctx); 534 GNUNET_FS_stop (ctx);
442 ret = 1; 535 ret = 1;
@@ -445,11 +538,11 @@ directory_trim_complete ()
445 GNUNET_FS_file_information_inspect (fi, &publish_inspector, NULL); 538 GNUNET_FS_file_information_inspect (fi, &publish_inspector, NULL);
446 if (extract_only) 539 if (extract_only)
447 { 540 {
448 if (namespace != NULL) 541 if (NULL != namespace)
449 GNUNET_FS_namespace_delete (namespace, GNUNET_NO); 542 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
450 GNUNET_FS_file_information_destroy (fi, NULL, NULL); 543 GNUNET_FS_file_information_destroy (fi, NULL, NULL);
451 GNUNET_FS_stop (ctx); 544 GNUNET_FS_stop (ctx);
452 if (kill_task != GNUNET_SCHEDULER_NO_TASK) 545 if (GNUNET_SCHEDULER_NO_TASK != kill_task)
453 { 546 {
454 GNUNET_SCHEDULER_cancel (kill_task); 547 GNUNET_SCHEDULER_cancel (kill_task);
455 kill_task = GNUNET_SCHEDULER_NO_TASK; 548 kill_task = GNUNET_SCHEDULER_NO_TASK;
@@ -487,6 +580,8 @@ directory_scan_cb (void *cls,
487 int is_directory, 580 int is_directory,
488 enum GNUNET_FS_DirScannerProgressUpdateReason reason) 581 enum GNUNET_FS_DirScannerProgressUpdateReason reason)
489{ 582{
583 struct GNUNET_FS_ShareTreeItem *directory_scan_result;
584
490 switch (reason) 585 switch (reason)
491 { 586 {
492 case GNUNET_FS_DIRSCANNER_FILE_START: 587 case GNUNET_FS_DIRSCANNER_FILE_START:
@@ -517,7 +612,7 @@ directory_scan_cb (void *cls,
517 directory_scan_result = GNUNET_FS_directory_scan_get_result (ds); 612 directory_scan_result = GNUNET_FS_directory_scan_get_result (ds);
518 ds = NULL; 613 ds = NULL;
519 GNUNET_FS_share_tree_trim (directory_scan_result); 614 GNUNET_FS_share_tree_trim (directory_scan_result);
520 directory_trim_complete (); 615 directory_trim_complete (directory_scan_result);
521 break; 616 break;
522 case GNUNET_FS_DIRSCANNER_INTERNAL_ERROR: 617 case GNUNET_FS_DIRSCANNER_INTERNAL_ERROR:
523 FPRINTF (stdout, "%s", _("Internal error scanning directory.\n")); 618 FPRINTF (stdout, "%s", _("Internal error scanning directory.\n"));
@@ -552,26 +647,26 @@ run (void *cls, char *const *args, const char *cfgfile,
552 char *emsg; 647 char *emsg;
553 648
554 /* check arguments */ 649 /* check arguments */
555 if ((uri_string != NULL) && (extract_only)) 650 if ((NULL != uri_string) && (extract_only))
556 { 651 {
557 printf (_("Cannot extract metadata from a URI!\n")); 652 printf (_("Cannot extract metadata from a URI!\n"));
558 ret = -1; 653 ret = -1;
559 return; 654 return;
560 } 655 }
561 if (((uri_string == NULL) || (extract_only)) && 656 if (((NULL == uri_string) || (extract_only)) &&
562 ((args[0] == NULL) || (args[1] != NULL))) 657 ((NULL == args[0]) || (NULL != args[1])))
563 { 658 {
564 printf (_("You must specify one and only one filename for insertion.\n")); 659 printf (_("You must specify one and only one filename for insertion.\n"));
565 ret = -1; 660 ret = -1;
566 return; 661 return;
567 } 662 }
568 if ((uri_string != NULL) && (args[0] != NULL)) 663 if ((NULL != uri_string) && (NULL != args[0]))
569 { 664 {
570 printf (_("You must NOT specify an URI and a filename.\n")); 665 printf (_("You must NOT specify an URI and a filename.\n"));
571 ret = -1; 666 ret = -1;
572 return; 667 return;
573 } 668 }
574 if (pseudonym != NULL) 669 if (NULL != pseudonym)
575 { 670 {
576 if (NULL == this_id) 671 if (NULL == this_id)
577 { 672 {
@@ -623,8 +718,7 @@ run (void *cls, char *const *args, const char *cfgfile,
623 if (NULL != uri_string) 718 if (NULL != uri_string)
624 { 719 {
625 emsg = NULL; 720 emsg = NULL;
626 uri = GNUNET_FS_uri_parse (uri_string, &emsg); 721 if (NULL == (uri = GNUNET_FS_uri_parse (uri_string, &emsg)))
627 if (uri == NULL)
628 { 722 {
629 FPRINTF (stderr, _("Failed to parse URI: %s\n"), emsg); 723 FPRINTF (stderr, _("Failed to parse URI: %s\n"), emsg);
630 GNUNET_free (emsg); 724 GNUNET_free (emsg);
@@ -637,7 +731,7 @@ run (void *cls, char *const *args, const char *cfgfile,
637 GNUNET_FS_publish_ksk (ctx, topKeywords, meta, uri, &bo, 731 GNUNET_FS_publish_ksk (ctx, topKeywords, meta, uri, &bo,
638 GNUNET_FS_PUBLISH_OPTION_NONE, &uri_ksk_continuation, 732 GNUNET_FS_PUBLISH_OPTION_NONE, &uri_ksk_continuation,
639 NULL); 733 NULL);
640 if (namespace != NULL) 734 if (NULL != namespace)
641 GNUNET_FS_namespace_delete (namespace, GNUNET_NO); 735 GNUNET_FS_namespace_delete (namespace, GNUNET_NO);
642 return; 736 return;
643 } 737 }