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