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