types
[oweals/gnunet.git] / src / fs / gnunet-directory.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-directory.c
22  * @brief display content of GNUnet directories
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_fs_service.h"
27
28 static int ret;
29
30 /**
31  * Print a meta data entry.
32  */
33 static int
34 item_printer (void *cls,
35               EXTRACTOR_KeywordType type, 
36               const char *data)
37 {
38   printf ("\t%20s: %s\n",
39           dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN,
40                     EXTRACTOR_getKeywordTypeAsString (type)), data);
41   return GNUNET_OK;
42 }
43
44
45
46 /**
47  * Print an entry in a directory.
48  *
49  * @param cls closure (not used)
50  * @param filename name of the file in the directory
51  * @param uri URI of the file
52  * @param metadata metadata for the file; metadata for
53  *        the directory if everything else is NULL/zero
54  * @param length length of the available data for the file
55  *           (of type size_t since data must certainly fit
56  *            into memory; if files are larger than size_t
57  *            permits, then they will certainly not be
58  *            embedded with the directory itself).
59  * @param data data available for the file (length bytes)
60  */
61 static void
62 print_entry (void *cls,
63              const char *filename,
64              const struct GNUNET_FS_Uri *uri,
65              const struct GNUNET_CONTAINER_MetaData *meta,
66              size_t length,
67              const void *data)
68 {
69   char *string;
70
71   string = GNUNET_FS_uri_to_string (uri);
72   printf ("%s:\n", string);
73   GNUNET_free (string);
74   GNUNET_CONTAINER_meta_data_get_contents (meta,
75                                            &item_printer,
76                                            NULL);
77 }
78
79
80 /**
81  * Main function that will be run by the scheduler.
82  *
83  * @param cls closure
84  * @param sched the scheduler to use
85  * @param args remaining command-line arguments
86  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
87  * @param cfg configuration
88  */
89 static void
90 run (void *cls,
91      struct GNUNET_SCHEDULER_Handle *sched,
92      char *const *args,
93      const char *cfgfile,
94      const struct GNUNET_CONFIGURATION_Handle *cfg)
95 {
96   struct GNUNET_DISK_MapHandle *map;
97   struct GNUNET_DISK_FileHandle *h;
98   void *data;
99   size_t len;
100   uint64_t size;
101   const char *filename = args[0];
102
103   if (NULL == filename)
104     {
105       fprintf (stderr,
106                _("You must specify a filename to inspect."));
107       ret = 1;
108       return;
109     }
110   if ( (GNUNET_OK !=
111         GNUNET_DISK_file_size (filename,
112                                &size,
113                                GNUNET_YES)) ||
114        (NULL == (h = GNUNET_DISK_file_open (filename,
115                                             GNUNET_DISK_OPEN_READ))) )
116     {
117       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
118                   _("Failed to read directory `%s'\n"),
119                   filename);
120       ret = 1;
121       return;
122     }
123   len = (size_t) size;
124   data = GNUNET_DISK_file_map (h,
125                                &map,
126                                GNUNET_DISK_MAP_READ,
127                                len);
128   GNUNET_assert (NULL != data);
129   GNUNET_FS_directory_list_contents (len,
130                                      data,
131                                      0, 
132                                      &print_entry,
133                                      NULL);
134   GNUNET_DISK_file_unmap (map);
135   GNUNET_DISK_file_close (h);
136 }
137
138
139 /**
140  * gnunet-directory command line options
141  */
142 static struct GNUNET_GETOPT_CommandLineOption options[] = {
143   // FIXME: options!
144   GNUNET_GETOPT_OPTION_END
145 };
146
147
148 /**
149  * The main function to inspect GNUnet directories.
150  *
151  * @param argc number of arguments from the command line
152  * @param argv command line arguments
153  * @return 0 ok, 1 on error
154  */
155 int
156 main (int argc, char *const *argv)
157 {
158   return (GNUNET_OK ==
159           GNUNET_PROGRAM_run (argc,
160                               argv,
161                               "gnunet-directory",
162                               gettext_noop
163                               ("Display GNUnet directories."),
164                               options, &run, NULL)) ? ret : 1;
165 }
166
167 /* end of gnunet-directory.c */