X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Ffs%2Fgnunet-publish.c;h=a1b26dbd5845da11eae06fee745c23cdd009a762;hb=1f09f4f7716db5939ec1c9a278b5661616dd72d6;hp=326fed3c6ccc84b112723301c0d6fe30ed85c0c1;hpb=502af2167f7c218366666ca4944bd7cc54b5b19a;p=oweals%2Fgnunet.git diff --git a/src/fs/gnunet-publish.c b/src/fs/gnunet-publish.c index 326fed3c6..a1b26dbd5 100644 --- a/src/fs/gnunet-publish.c +++ b/src/fs/gnunet-publish.c @@ -66,28 +66,60 @@ static int do_disable_creation_time; static GNUNET_SCHEDULER_TaskIdentifier kill_task; +static struct GNUNET_FS_DirScanner *ds; +static struct GNUNET_FS_ShareTreeItem * directory_scan_result; + +static struct GNUNET_FS_Namespace *namespace; + +/** + * FIXME: docu + */ static void do_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { struct GNUNET_FS_PublishContext *p; + kill_task = GNUNET_SCHEDULER_NO_TASK; if (pc != NULL) { p = pc; pc = NULL; GNUNET_FS_publish_stop (p); - if (NULL != meta) - { - GNUNET_CONTAINER_meta_data_destroy (meta); - meta = NULL; - } } + if (NULL != meta) + { + GNUNET_CONTAINER_meta_data_destroy (meta); + meta = NULL; + } } /** - * Called by FS client to give information about the progress of an + * Stop the directory scanner (we had an error). + * + * @param cls closure + * @param tc scheduler context + */ +static void +stop_scanner_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + kill_task = GNUNET_SCHEDULER_NO_TASK; + GNUNET_FS_directory_scan_abort (ds); + ds = NULL; + if (namespace != NULL) + { + GNUNET_FS_namespace_delete (namespace, GNUNET_NO); + namespace = NULL; + } + GNUNET_FS_stop (ctx); + ctx = NULL; + ret = 1; +} + + +/** + * Called by FS client to give information about the progress of an * operation. * * @param cls closure @@ -112,8 +144,7 @@ progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info) if (verbose) { s = GNUNET_STRINGS_relative_time_to_string (info->value.publish.eta); - fprintf (stdout, - _("Publishing `%s' at %llu/%llu (%s remaining)\n"), + FPRINTF (stdout, _("Publishing `%s' at %llu/%llu (%s remaining)\n"), info->value.publish.filename, (unsigned long long) info->value.publish.completed, (unsigned long long) info->value.publish.size, s); @@ -121,24 +152,21 @@ progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info) } break; case GNUNET_FS_STATUS_PUBLISH_ERROR: - fprintf (stderr, - _("Error publishing: %s.\n"), + FPRINTF (stderr, _("Error publishing: %s.\n"), info->value.publish.specifics.error.message); if (kill_task != GNUNET_SCHEDULER_NO_TASK) { GNUNET_SCHEDULER_cancel (kill_task); kill_task = GNUNET_SCHEDULER_NO_TASK; } - GNUNET_SCHEDULER_add_continuation (&do_stop_task, - NULL, - GNUNET_SCHEDULER_REASON_PREREQ_DONE); + kill_task = GNUNET_SCHEDULER_add_now (&do_stop_task, NULL); break; case GNUNET_FS_STATUS_PUBLISH_COMPLETED: - fprintf (stdout, - _("Publishing `%s' done.\n"), info->value.publish.filename); + FPRINTF (stdout, _("Publishing `%s' done.\n"), + info->value.publish.filename); s = GNUNET_FS_uri_to_string (info->value.publish.specifics. completed.chk_uri); - fprintf (stdout, _("URI is `%s'.\n"), s); + FPRINTF (stdout, _("URI is `%s'.\n"), s); GNUNET_free (s); if (info->value.publish.pctx == NULL) { @@ -147,16 +175,19 @@ progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info) GNUNET_SCHEDULER_cancel (kill_task); kill_task = GNUNET_SCHEDULER_NO_TASK; } - GNUNET_SCHEDULER_add_continuation (&do_stop_task, - NULL, - GNUNET_SCHEDULER_REASON_PREREQ_DONE); + kill_task = GNUNET_SCHEDULER_add_now (&do_stop_task, NULL); } break; case GNUNET_FS_STATUS_PUBLISH_STOPPED: GNUNET_break (NULL == pc); return NULL; + case GNUNET_FS_STATUS_UNINDEX_PROGRESS: + return NULL; + case GNUNET_FS_STATUS_UNINDEX_COMPLETED: + FPRINTF (stderr, "%s", _("Cleanup after abort complete.\n")); + return NULL; default: - fprintf (stderr, _("Unexpected status: %d\n"), info->status); + FPRINTF (stderr, _("Unexpected status: %d\n"), info->status); return NULL; } return ""; /* non-null */ @@ -177,18 +208,16 @@ progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info) * @return always 0 */ static int -meta_printer (void *cls, - const char *plugin_name, - enum EXTRACTOR_MetaType type, - enum EXTRACTOR_MetaFormat format, - const char *data_mime_type, const char *data, size_t data_size) +meta_printer (void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type, + enum EXTRACTOR_MetaFormat format, const char *data_mime_type, + const char *data, size_t data_size) { if ((format != EXTRACTOR_METAFORMAT_UTF8) && (format != EXTRACTOR_METAFORMAT_C_STRING)) return 0; if (type == EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME) return 0; - fprintf (stdout, "\t%s - %s\n", EXTRACTOR_metatype_to_string (type), data); + FPRINTF (stdout, "\t%s - %s\n", EXTRACTOR_metatype_to_string (type), data); return 0; } @@ -201,11 +230,10 @@ meta_printer (void *cls, * @param is_mandatory is the keyword mandatory (in a search) * @return GNUNET_OK to continue to iterate, GNUNET_SYSERR to abort */ - static int keyword_printer (void *cls, const char *keyword, int is_mandatory) { - fprintf (stdout, "\t%s\n", keyword); + FPRINTF (stdout, "\t%s\n", keyword); return GNUNET_OK; } @@ -228,13 +256,11 @@ keyword_printer (void *cls, const char *keyword, int is_mandatory) * to abort the iteration */ static int -publish_inspector (void *cls, - struct GNUNET_FS_FileInformation *fi, - uint64_t length, - struct GNUNET_CONTAINER_MetaData *m, +publish_inspector (void *cls, struct GNUNET_FS_FileInformation *fi, + uint64_t length, struct GNUNET_CONTAINER_MetaData *m, struct GNUNET_FS_Uri **uri, - struct GNUNET_FS_BlockOptions *bo, - int *do_index, void **client_info) + struct GNUNET_FS_BlockOptions *bo, int *do_index, + void **client_info) { char *fn; char *fs; @@ -265,19 +291,25 @@ publish_inspector (void *cls, } if (!do_disable_creation_time) GNUNET_CONTAINER_meta_data_add_publication_date (m); + if ( (disable_extractor) && + (NULL != *uri) ) + { + GNUNET_FS_uri_destroy (*uri); + *uri = NULL; + } if (extract_only) { fn = GNUNET_CONTAINER_meta_data_get_by_type (m, EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME); fs = GNUNET_STRINGS_byte_size_fancy (length); - fprintf (stdout, _("Meta data for file `%s' (%s)\n"), fn, fs); + FPRINTF (stdout, _("Meta data for file `%s' (%s)\n"), fn, fs); GNUNET_CONTAINER_meta_data_iterate (m, &meta_printer, NULL); - fprintf (stdout, _("Keywords for file `%s' (%s)\n"), fn, fs); + FPRINTF (stdout, _("Keywords for file `%s' (%s)\n"), fn, fs); GNUNET_free (fn); GNUNET_free (fs); if (NULL != *uri) GNUNET_FS_uri_ksk_get_keywords (*uri, &keyword_printer, NULL); - fprintf (stdout, "\n"); + FPRINTF (stdout, "%s", "\n"); } if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (m)) GNUNET_FS_file_information_inspect (fi, &publish_inspector, fi); @@ -285,13 +317,16 @@ publish_inspector (void *cls, } +/** + * FIXME: docu + */ static void -uri_sks_continuation (void *cls, - const struct GNUNET_FS_Uri *ksk_uri, const char *emsg) +uri_sks_continuation (void *cls, const struct GNUNET_FS_Uri *ksk_uri, + const char *emsg) { if (emsg != NULL) { - fprintf (stderr, "%s\n", emsg); + FPRINTF (stderr, "%s\n", emsg); ret = 1; } GNUNET_FS_uri_destroy (uri); @@ -301,15 +336,18 @@ uri_sks_continuation (void *cls, } +/** + * FIXME: docu + */ static void -uri_ksk_continuation (void *cls, - const struct GNUNET_FS_Uri *ksk_uri, const char *emsg) +uri_ksk_continuation (void *cls, const struct GNUNET_FS_Uri *ksk_uri, + const char *emsg) { struct GNUNET_FS_Namespace *ns; if (emsg != NULL) { - fprintf (stderr, "%s\n", emsg); + FPRINTF (stderr, "%s\n", emsg); ret = 1; } if (pseudonym != NULL) @@ -317,20 +355,14 @@ uri_ksk_continuation (void *cls, ns = GNUNET_FS_namespace_create (ctx, pseudonym); if (ns == NULL) { - fprintf (stderr, _("Failed to create namespace `%s'\n"), pseudonym); + FPRINTF (stderr, _("Failed to create namespace `%s'\n"), pseudonym); ret = 1; } else { - GNUNET_FS_publish_sks (ctx, - ns, - this_id, - next_id, - meta, - uri, - &bo, + GNUNET_FS_publish_sks (ctx, ns, this_id, next_id, meta, uri, &bo, GNUNET_FS_PUBLISH_OPTION_NONE, - uri_sks_continuation, NULL); + &uri_sks_continuation, NULL); GNUNET_assert (GNUNET_OK == GNUNET_FS_namespace_delete (ns, GNUNET_NO)); return; } @@ -342,6 +374,163 @@ uri_ksk_continuation (void *cls, } +/** + * FIXME: docu + */ +static struct GNUNET_FS_FileInformation * +get_file_information (struct GNUNET_FS_ShareTreeItem *item) +{ + struct GNUNET_FS_FileInformation *fi; + struct GNUNET_FS_FileInformation *fic; + struct GNUNET_FS_ShareTreeItem *child; + + if (item->is_directory == GNUNET_YES) + { + GNUNET_CONTAINER_meta_data_delete (item->meta, + EXTRACTOR_METATYPE_MIMETYPE, NULL, 0); + GNUNET_FS_meta_data_make_directory (item->meta); + if (NULL == item->ksk_uri) + { + const char *mime = GNUNET_FS_DIRECTORY_MIME; + item->ksk_uri = GNUNET_FS_uri_ksk_create_from_args (1, &mime); + } + else + GNUNET_FS_uri_ksk_add_keyword (item->ksk_uri, GNUNET_FS_DIRECTORY_MIME, + GNUNET_NO); + fi = GNUNET_FS_file_information_create_empty_directory ( + ctx, NULL, item->ksk_uri, + item->meta, &bo, item->filename); + for (child = item->children_head; child; child = child->next) + { + fic = get_file_information (child); + GNUNET_break (GNUNET_OK == GNUNET_FS_file_information_add (fi, fic)); + } + } + else + { + fi = GNUNET_FS_file_information_create_from_file ( + ctx, NULL, item->filename, + item->ksk_uri, item->meta, !do_insert, + &bo); + } + return fi; +} + + +/** + * FIXME: docu + */ +static void +directory_trim_complete () +{ + struct GNUNET_FS_FileInformation *fi; + + fi = get_file_information (directory_scan_result); + GNUNET_FS_share_tree_free (directory_scan_result); + directory_scan_result = NULL; + if (fi == NULL) + { + FPRINTF (stderr, "%s", _("Could not publish\n")); + if (namespace != NULL) + GNUNET_FS_namespace_delete (namespace, GNUNET_NO); + GNUNET_FS_stop (ctx); + ret = 1; + return; + } + GNUNET_FS_file_information_inspect (fi, &publish_inspector, NULL); + if (extract_only) + { + if (namespace != NULL) + GNUNET_FS_namespace_delete (namespace, GNUNET_NO); + GNUNET_FS_file_information_destroy (fi, NULL, NULL); + GNUNET_FS_stop (ctx); + if (kill_task != GNUNET_SCHEDULER_NO_TASK) + { + GNUNET_SCHEDULER_cancel (kill_task); + kill_task = GNUNET_SCHEDULER_NO_TASK; + } + return; + } + pc = GNUNET_FS_publish_start (ctx, fi, namespace, this_id, next_id, + (do_simulate) ? + GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY : + GNUNET_FS_PUBLISH_OPTION_NONE); + if (NULL == pc) + { + FPRINTF (stderr, "%s", _("Could not start publishing.\n")); + GNUNET_FS_stop (ctx); + ret = 1; + return; + } +} + + +/** + * Function called by the directory scanner as we build the tree + * that we will need to publish later. + * + * @param cls closure + * @param filename which file we are making progress on + * @param is_directory GNUNET_YES if this is a directory, + * GNUNET_NO if this is a file + * GNUNET_SYSERR if it is neither (or unknown) + * @param reason kind of progress we are making + */ +static void +directory_scan_cb (void *cls, + const char *filename, + int is_directory, + enum GNUNET_FS_DirScannerProgressUpdateReason reason) +{ + switch (reason) + { + case GNUNET_FS_DIRSCANNER_FILE_START: + if (verbose > 1) + { + if (is_directory == GNUNET_YES) + FPRINTF (stdout, _("Scanning directory `%s'.\n"), filename); + else + FPRINTF (stdout, _("Scanning file `%s'.\n"), filename); + } + break; + case GNUNET_FS_DIRSCANNER_FILE_IGNORED: + FPRINTF (stderr, + _("There was trouble processing file `%s', skipping it.\n"), + filename); + break; + case GNUNET_FS_DIRSCANNER_ALL_COUNTED: + if (verbose) + FPRINTF (stdout, "%s", _("Preprocessing complete.\n")); + break; + case GNUNET_FS_DIRSCANNER_EXTRACT_FINISHED: + if (verbose > 2) + FPRINTF (stdout, _("Extracting meta data from file `%s' complete.\n"), filename); + break; + case GNUNET_FS_DIRSCANNER_FINISHED: + if (verbose > 1) + FPRINTF (stdout, "%s", _("Meta data extraction has finished.\n")); + directory_scan_result = GNUNET_FS_directory_scan_get_result (ds); + ds = NULL; + GNUNET_FS_share_tree_trim (directory_scan_result); + directory_trim_complete (); + break; + case GNUNET_FS_DIRSCANNER_INTERNAL_ERROR: + FPRINTF (stdout, "%s", _("Internal error scanning directory.\n")); + if (kill_task != GNUNET_SCHEDULER_NO_TASK) + { + GNUNET_SCHEDULER_cancel (kill_task); + kill_task = GNUNET_SCHEDULER_NO_TASK; + } + kill_task = GNUNET_SCHEDULER_add_now (&stop_scanner_task, NULL); + break; + default: + GNUNET_assert (0); + break; + } + fflush (stdout); +} + + /** * Main function that will be run by the scheduler. * @@ -351,15 +540,9 @@ uri_ksk_continuation (void *cls, * @param c configuration */ static void -run (void *cls, - char *const *args, - const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c) +run (void *cls, char *const *args, const char *cfgfile, + const struct GNUNET_CONFIGURATION_Handle *c) { - struct GNUNET_FS_FileInformation *fi; - struct GNUNET_FS_Namespace *namespace; - struct EXTRACTOR_PluginList *plugins; - struct GNUNET_FS_Uri *keywords; - struct stat sbuf; char *ex; char *emsg; @@ -370,8 +553,8 @@ run (void *cls, ret = -1; return; } - if (((uri_string == NULL) || (extract_only)) - && ((args[0] == NULL) || (args[1] != NULL))) + if (((uri_string == NULL) || (extract_only)) && + ((args[0] == NULL) || (args[1] != NULL))) { printf (_("You must specify one and only one filename for insertion.\n")); ret = -1; @@ -387,8 +570,7 @@ run (void *cls, { if (NULL == this_id) { - fprintf (stderr, - _("Option `%s' is required when using option `%s'.\n"), + FPRINTF (stderr, _("Option `%s' is required when using option `%s'.\n"), "-t", "-P"); ret = -1; return; @@ -398,29 +580,26 @@ run (void *cls, { /* ordinary insertion checks */ if (NULL != next_id) { - fprintf (stderr, - _("Option `%s' makes no sense without option `%s'.\n"), + FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"), "-N", "-P"); ret = -1; return; } if (NULL != this_id) { - fprintf (stderr, - _("Option `%s' makes no sense without option `%s'.\n"), + FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"), "-t", "-P"); ret = -1; return; } } cfg = c; - ctx = GNUNET_FS_start (cfg, - "gnunet-publish", - &progress_cb, - NULL, GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END); + ctx = + GNUNET_FS_start (cfg, "gnunet-publish", &progress_cb, NULL, + GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END); if (NULL == ctx) { - fprintf (stderr, _("Could not initialize `%s' subsystem.\n"), "FS"); + FPRINTF (stderr, _("Could not initialize `%s' subsystem.\n"), "FS"); ret = 1; return; } @@ -430,7 +609,7 @@ run (void *cls, namespace = GNUNET_FS_namespace_create (ctx, pseudonym); if (NULL == namespace) { - fprintf (stderr, _("Could not create namespace `%s'\n"), pseudonym); + FPRINTF (stderr, _("Could not create namespace `%s'\n"), pseudonym); GNUNET_FS_stop (ctx); ret = 1; return; @@ -442,7 +621,7 @@ run (void *cls, uri = GNUNET_FS_uri_parse (uri_string, &emsg); if (uri == NULL) { - fprintf (stderr, _("Failed to parse URI: %s\n"), emsg); + FPRINTF (stderr, _("Failed to parse URI: %s\n"), emsg); GNUNET_free (emsg); if (namespace != NULL) GNUNET_FS_namespace_delete (namespace, GNUNET_NO); @@ -450,104 +629,40 @@ run (void *cls, ret = 1; return; } - GNUNET_FS_publish_ksk (ctx, - topKeywords, - meta, - uri, - &bo, - GNUNET_FS_PUBLISH_OPTION_NONE, - &uri_ksk_continuation, NULL); - if (namespace != NULL) - GNUNET_FS_namespace_delete (namespace, GNUNET_NO); - return; - } - plugins = NULL; - if (!disable_extractor) - { - plugins = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY); - if (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_string (cfg, "FS", "EXTRACTORS", &ex)) - { - if (strlen (ex) > 0) - plugins = - EXTRACTOR_plugin_add_config (plugins, ex, - EXTRACTOR_OPTION_DEFAULT_POLICY); - GNUNET_free (ex); - } - } - emsg = NULL; - GNUNET_assert (NULL != args[0]); - if (0 != STAT (args[0], &sbuf)) - { - GNUNET_asprintf (&emsg, _("Could not access file: %s\n"), STRERROR (errno)); - fi = NULL; - } - else if (S_ISDIR (sbuf.st_mode)) - { - fi = GNUNET_FS_file_information_create_from_directory (ctx, - NULL, - args[0], - &GNUNET_FS_directory_scanner_default, - plugins, - !do_insert, - &bo, &emsg); - } - else - { - if (meta == NULL) - meta = GNUNET_CONTAINER_meta_data_create (); - GNUNET_FS_meta_data_extract_from_file (meta, args[0], plugins); - keywords = GNUNET_FS_uri_ksk_create_from_meta_data (meta); - fi = GNUNET_FS_file_information_create_from_file (ctx, - NULL, - args[0], - keywords, - NULL, !do_insert, &bo); - GNUNET_break (fi != NULL); - GNUNET_FS_uri_destroy (keywords); - } - EXTRACTOR_plugin_remove_all (plugins); - if (fi == NULL) - { - fprintf (stderr, _("Could not publish `%s': %s\n"), args[0], emsg); - GNUNET_free (emsg); + GNUNET_FS_publish_ksk (ctx, topKeywords, meta, uri, &bo, + GNUNET_FS_PUBLISH_OPTION_NONE, &uri_ksk_continuation, + NULL); if (namespace != NULL) GNUNET_FS_namespace_delete (namespace, GNUNET_NO); - GNUNET_FS_stop (ctx); - ret = 1; return; } - GNUNET_FS_file_information_inspect (fi, &publish_inspector, NULL); - if (extract_only) + if (GNUNET_OK != + GNUNET_CONFIGURATION_get_value_string (cfg, "FS", "EXTRACTORS", &ex)) + ex = NULL; + if (0 != ACCESS (args[0], R_OK)) { - if (namespace != NULL) - GNUNET_FS_namespace_delete (namespace, GNUNET_NO); - GNUNET_FS_file_information_destroy (fi, NULL, NULL); - GNUNET_FS_stop (ctx); + FPRINTF (stderr, + _("Failed to access `%s': %s\n"), + args[0], + STRERROR (errno)); return; } - pc = GNUNET_FS_publish_start (ctx, - fi, - namespace, - this_id, - next_id, - (do_simulate) - ? GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY - : GNUNET_FS_PUBLISH_OPTION_NONE); - if (NULL == pc) + ds = GNUNET_FS_directory_scan_start (args[0], + disable_extractor, + ex, + &directory_scan_cb, NULL); + if (NULL == ds) { - fprintf (stderr, _("Could not start publishing.\n")); - GNUNET_FS_stop (ctx); - ret = 1; + FPRINTF (stderr, + "%s", _("Failed to start meta directory scanner. Is gnunet-helper-publish-fs installed?\n")); return; } - kill_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, - &do_stop_task, NULL); + kill_task = + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_stop_task, + NULL); } - - /** * The main function to publish content to GNUnet. * @@ -617,6 +732,8 @@ main (int argc, char *const *argv) 0, &GNUNET_GETOPT_set_one, &verbose}, GNUNET_GETOPT_OPTION_END }; + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "GNUnet publish starts\n"); bo.expiration_time = GNUNET_FS_year_to_time (GNUNET_FS_get_current_year () + 2); return (GNUNET_OK ==