gnunet-unindex.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file fs/gnunet-unindex.c
  19. * @brief unindex files published on GNUnet
  20. * @author Christian Grothoff
  21. * @author Krista Bennett
  22. * @author James Blackwell
  23. * @author Igor Wronsky
  24. */
  25. #include "platform.h"
  26. #include "gnunet_fs_service.h"
  27. static int ret;
  28. static int verbose;
  29. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  30. static struct GNUNET_FS_Handle *ctx;
  31. static struct GNUNET_FS_UnindexContext *uc;
  32. static void
  33. cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  34. {
  35. GNUNET_FS_stop (ctx);
  36. ctx = NULL;
  37. }
  38. static void
  39. shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  40. {
  41. struct GNUNET_FS_UnindexContext *u;
  42. if (uc != NULL)
  43. {
  44. u = uc;
  45. uc = NULL;
  46. GNUNET_FS_unindex_stop (u);
  47. }
  48. }
  49. /**
  50. * Called by FS client to give information about the progress of an
  51. * operation.
  52. *
  53. * @param cls closure
  54. * @param info details about the event, specifying the event type
  55. * and various bits about the event
  56. * @return client-context (for the next progress call
  57. * for this operation; should be set to NULL for
  58. * SUSPEND and STOPPED events). The value returned
  59. * will be passed to future callbacks in the respective
  60. * field in the GNUNET_FS_ProgressInfo struct.
  61. */
  62. static void *
  63. progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
  64. {
  65. char *s;
  66. switch (info->status)
  67. {
  68. case GNUNET_FS_STATUS_UNINDEX_START:
  69. break;
  70. case GNUNET_FS_STATUS_UNINDEX_PROGRESS:
  71. if (verbose)
  72. {
  73. s = GNUNET_STRINGS_relative_time_to_string (info->value.unindex.eta);
  74. fprintf (stdout, _("Unindexing at %llu/%llu (%s remaining)\n"),
  75. (unsigned long long) info->value.unindex.completed,
  76. (unsigned long long) info->value.unindex.size, s);
  77. GNUNET_free (s);
  78. }
  79. break;
  80. case GNUNET_FS_STATUS_UNINDEX_ERROR:
  81. fprintf (stderr, _("Error unindexing: %s.\n"),
  82. info->value.unindex.specifics.error.message);
  83. GNUNET_SCHEDULER_shutdown ();
  84. break;
  85. case GNUNET_FS_STATUS_UNINDEX_COMPLETED:
  86. fprintf (stdout, _("Unindexing done.\n"));
  87. GNUNET_SCHEDULER_shutdown ();
  88. break;
  89. case GNUNET_FS_STATUS_UNINDEX_STOPPED:
  90. GNUNET_SCHEDULER_add_continuation (&cleanup_task, NULL,
  91. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  92. break;
  93. default:
  94. fprintf (stderr, _("Unexpected status: %d\n"), info->status);
  95. break;
  96. }
  97. return NULL;
  98. }
  99. /**
  100. * Main function that will be run by the scheduler.
  101. *
  102. * @param cls closure
  103. * @param args remaining command-line arguments
  104. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  105. * @param c configuration
  106. */
  107. static void
  108. run (void *cls, char *const *args, const char *cfgfile,
  109. const struct GNUNET_CONFIGURATION_Handle *c)
  110. {
  111. /* check arguments */
  112. if ((args[0] == NULL) || (args[1] != NULL))
  113. {
  114. printf (_("You must specify one and only one filename for unindexing.\n"));
  115. ret = -1;
  116. return;
  117. }
  118. cfg = c;
  119. ctx =
  120. GNUNET_FS_start (cfg, "gnunet-unindex", &progress_cb, NULL,
  121. GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
  122. if (NULL == ctx)
  123. {
  124. fprintf (stderr, _("Could not initialize `%s' subsystem.\n"), "FS");
  125. ret = 1;
  126. return;
  127. }
  128. uc = GNUNET_FS_unindex_start (ctx, args[0], NULL);
  129. if (NULL == uc)
  130. {
  131. fprintf (stderr, _("Could not start unindex operation.\n"));
  132. GNUNET_FS_stop (ctx);
  133. return;
  134. }
  135. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
  136. NULL);
  137. }
  138. /**
  139. * The main function to unindex content.
  140. *
  141. * @param argc number of arguments from the command line
  142. * @param argv command line arguments
  143. * @return 0 ok, 1 on error
  144. */
  145. int
  146. main (int argc, char *const *argv)
  147. {
  148. static const struct GNUNET_GETOPT_CommandLineOption options[] = {
  149. {'V', "verbose", NULL,
  150. gettext_noop ("be verbose (print progress information)"),
  151. 0, &GNUNET_GETOPT_set_one, &verbose},
  152. GNUNET_GETOPT_OPTION_END
  153. };
  154. return (GNUNET_OK ==
  155. GNUNET_PROGRAM_run (argc, argv, "gnunet-unindex [OPTIONS] FILENAME",
  156. gettext_noop
  157. ("Unindex a file that was previously indexed with gnunet-publish."),
  158. options, &run, NULL)) ? ret : 1;
  159. }
  160. /* end of gnunet-unindex.c */