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