gnunet-unindex.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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,
  34. const struct GNUNET_SCHEDULER_TaskContext *tc)
  35. {
  36. GNUNET_FS_stop (ctx);
  37. ctx = NULL;
  38. }
  39. static void
  40. shutdown_task (void *cls,
  41. const struct GNUNET_SCHEDULER_TaskContext *tc)
  42. {
  43. struct GNUNET_FS_UnindexContext *u;
  44. if (uc != NULL)
  45. {
  46. u = uc;
  47. uc = NULL;
  48. GNUNET_FS_unindex_stop (u);
  49. }
  50. }
  51. /**
  52. * Called by FS client to give information about the progress of an
  53. * operation.
  54. *
  55. * @param cls closure
  56. * @param info details about the event, specifying the event type
  57. * and various bits about the event
  58. * @return client-context (for the next progress call
  59. * for this operation; should be set to NULL for
  60. * SUSPEND and STOPPED events). The value returned
  61. * will be passed to future callbacks in the respective
  62. * field in the GNUNET_FS_ProgressInfo struct.
  63. */
  64. static void *
  65. progress_cb (void *cls,
  66. const struct GNUNET_FS_ProgressInfo *info)
  67. {
  68. char *s;
  69. switch (info->status)
  70. {
  71. case GNUNET_FS_STATUS_UNINDEX_START:
  72. break;
  73. case GNUNET_FS_STATUS_UNINDEX_PROGRESS:
  74. if (verbose)
  75. {
  76. s = GNUNET_STRINGS_relative_time_to_string(info->value.unindex.eta);
  77. fprintf (stdout,
  78. _("Unindexing at %llu/%llu (%s remaining)\n"),
  79. (unsigned long long) info->value.unindex.completed,
  80. (unsigned long long) info->value.unindex.size,
  81. s);
  82. GNUNET_free (s);
  83. }
  84. break;
  85. case GNUNET_FS_STATUS_UNINDEX_ERROR:
  86. fprintf (stderr,
  87. _("Error unindexing: %s.\n"),
  88. info->value.unindex.specifics.error.message);
  89. GNUNET_SCHEDULER_shutdown ();
  90. break;
  91. case GNUNET_FS_STATUS_UNINDEX_COMPLETED:
  92. fprintf (stdout,
  93. _("Unindexing done.\n"));
  94. GNUNET_SCHEDULER_shutdown ();
  95. break;
  96. case GNUNET_FS_STATUS_UNINDEX_STOPPED:
  97. GNUNET_SCHEDULER_add_continuation (&cleanup_task,
  98. NULL,
  99. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  100. break;
  101. default:
  102. fprintf (stderr,
  103. _("Unexpected status: %d\n"),
  104. info->status);
  105. break;
  106. }
  107. return NULL;
  108. }
  109. /**
  110. * Main function that will be run by the scheduler.
  111. *
  112. * @param cls closure
  113. * @param args remaining command-line arguments
  114. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  115. * @param c configuration
  116. */
  117. static void
  118. run (void *cls,
  119. char *const *args,
  120. const char *cfgfile,
  121. const struct GNUNET_CONFIGURATION_Handle *c)
  122. {
  123. /* check arguments */
  124. if ( (args[0] == NULL) || (args[1] != NULL) )
  125. {
  126. printf (_
  127. ("You must specify one and only one filename for unindexing.\n"));
  128. ret = -1;
  129. return;
  130. }
  131. cfg = c;
  132. ctx = GNUNET_FS_start (cfg,
  133. "gnunet-unindex",
  134. &progress_cb,
  135. NULL,
  136. GNUNET_FS_FLAGS_NONE,
  137. GNUNET_FS_OPTIONS_END);
  138. if (NULL == ctx)
  139. {
  140. fprintf (stderr,
  141. _("Could not initialize `%s' subsystem.\n"),
  142. "FS");
  143. ret = 1;
  144. return;
  145. }
  146. uc = GNUNET_FS_unindex_start (ctx,
  147. args[0],
  148. NULL);
  149. if (NULL == uc)
  150. {
  151. fprintf (stderr,
  152. _("Could not start unindex operation.\n"));
  153. GNUNET_FS_stop (ctx);
  154. return;
  155. }
  156. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
  157. &shutdown_task,
  158. NULL);
  159. }
  160. /**
  161. * The main function to unindex content.
  162. *
  163. * @param argc number of arguments from the command line
  164. * @param argv command line arguments
  165. * @return 0 ok, 1 on error
  166. */
  167. int
  168. main (int argc, char *const *argv)
  169. {
  170. static const struct GNUNET_GETOPT_CommandLineOption options[] = {
  171. {'V', "verbose", NULL,
  172. gettext_noop ("be verbose (print progress information)"),
  173. 0, &GNUNET_GETOPT_set_one, &verbose},
  174. GNUNET_GETOPT_OPTION_END
  175. };
  176. return (GNUNET_OK ==
  177. GNUNET_PROGRAM_run (argc,
  178. argv,
  179. "gnunet-unindex [OPTIONS] FILENAME",
  180. gettext_noop
  181. ("Unindex a file that was previously indexed with gnunet-publish."),
  182. options, &run, NULL)) ? ret : 1;
  183. }
  184. /* end of gnunet-unindex.c */