310c8a1ae67b6c61a4c506188f8113f85d431e0c
[oweals/gnunet.git] / src / fs / gnunet-fs.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 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-fs.c
22  * @brief special file-sharing functions
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_fs_service.h"
27
28 /**
29  * Return value.
30  */
31 static int ret;
32
33 /**
34  * Handle to FS service.
35  */
36 static struct GNUNET_FS_Handle *fs;
37
38 /**
39  * Option -i given?
40  */
41 static int list_indexed_files;
42
43 /**
44  * Option -v given?
45  */
46 static int verbose;
47
48
49 /**
50  * Shutdown this process.
51  *
52  * @param cls unused
53  * @param tc unused
54  */
55 static void
56 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
57 {
58   GNUNET_FS_stop (fs);
59   fs = NULL;
60 }
61
62
63 /**
64  * Print indexed filenames to stdout.
65  *
66  * @param cls closure
67  * @param filename the name of the file
68  * @param file_id hash of the contents of the indexed file
69  * @return GNUNET_OK to continue iteration
70  */
71 static int
72 print_indexed (void *cls, const char *filename, const GNUNET_HashCode * file_id)
73 {
74   if (verbose)
75     fprintf (stdout, "%s: %s\n", GNUNET_h2s (file_id), filename);
76   else
77     fprintf (stdout, "%s\n", filename);
78   return GNUNET_OK;
79 }
80
81
82 /**
83  * Main function that will be run by the scheduler.
84  *
85  * @param cls closure
86  * @param args remaining command-line arguments
87  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
88  * @param cfg configuration
89  */
90 static void
91 run (void *cls, char *const *args, const char *cfgfile,
92      const struct GNUNET_CONFIGURATION_Handle *cfg)
93 {
94   if (list_indexed_files)
95   {
96     fs = GNUNET_FS_start (cfg, "gnunet-fs", NULL, NULL, GNUNET_FS_FLAGS_NONE);
97     if (NULL == fs)
98     {
99       ret = 1;
100       return;
101     }
102     GNUNET_FS_get_indexed_files (fs, &print_indexed, NULL, &do_shutdown, NULL);
103   }
104 }
105
106 /**
107  * The main function to access special file-sharing functions.
108  *
109  * @param argc number of arguments from the command line
110  * @param argv command line arguments
111  * @return 0 ok, 1 on error
112  */
113 int
114 main (int argc, char *const *argv)
115 {
116   static struct GNUNET_GETOPT_CommandLineOption options[] = {
117     {'i', "list-indexed", NULL,
118      gettext_noop ("print a list of all indexed files"), 0,
119      &GNUNET_GETOPT_set_one, &list_indexed_files},
120     GNUNET_GETOPT_OPTION_VERBOSE (&verbose),
121     GNUNET_GETOPT_OPTION_END
122   };
123   return (GNUNET_OK ==
124           GNUNET_PROGRAM_run (argc, argv, "gnunet-fs [OPTIONS]",
125                               gettext_noop ("Special file-sharing operations"),
126                               options, &run, NULL)) ? ret : 1;
127 }
128
129 /* end of gnunet-fs.c */