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