a6fa4266422b0b176e233f725310baa7708067c2
[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 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-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  * @param cls closure (unused)
34  * @param plugin_name name of the plugin that generated the meta data
35  * @param type type of the keyword
36  * @param format format of data
37  * @param data_mime_type mime type of data
38  * @param data value of the meta data
39  * @param data_size number of bytes in data
40  * @return always 0 (to continue iterating)
41  */
42 static int
43 item_printer (void *cls,
44               const char *plugin_name,
45               enum EXTRACTOR_MetaType type,
46               enum EXTRACTOR_MetaFormat format,
47               const char *data_mime_type, const char *data, size_t data_size)
48 {
49   if (type == EXTRACTOR_METATYPE_GNUNET_FULL_DATA)
50   {
51     printf (_("\t<original file embedded in %u bytes of meta data>\n"),
52             (unsigned int) data_size);
53     return 0;
54   }
55   if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
56       (format != EXTRACTOR_METAFORMAT_C_STRING))
57     return 0;
58   if (type == EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME)
59     return 0;
60   printf ("\t%20s: %s\n",
61           dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN,
62                     EXTRACTOR_metatype_to_string (type)), data);
63   return 0;
64 }
65
66
67
68 /**
69  * Print an entry in a directory.
70  *
71  * @param cls closure (not used)
72  * @param filename name of the file in the directory
73  * @param uri URI of the file
74  * @param meta metadata for the file; metadata for
75  *        the directory if everything else is NULL/zero
76  * @param length length of the available data for the file
77  *           (of type size_t since data must certainly fit
78  *            into memory; if files are larger than size_t
79  *            permits, then they will certainly not be
80  *            embedded with the directory itself).
81  * @param data data available for the file (length bytes)
82  */
83 static void
84 print_entry (void *cls,
85              const char *filename,
86              const struct GNUNET_FS_Uri *uri,
87              const struct GNUNET_CONTAINER_MetaData *meta,
88              size_t length, const void *data)
89 {
90   char *string;
91   char *name;
92
93   name = GNUNET_CONTAINER_meta_data_get_by_type (meta,
94                                                  EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
95   if (uri == NULL)
96   {
97     printf (_("Directory `%s' meta data:\n"), name);
98     GNUNET_CONTAINER_meta_data_iterate (meta, &item_printer, NULL);
99     printf ("\n");
100     printf (_("Directory `%s' contents:\n"), name);
101     GNUNET_free (name);
102     return;
103   }
104   string = GNUNET_FS_uri_to_string (uri);
105   printf ("%s (%s):\n", name, string);
106   GNUNET_free (string);
107   GNUNET_CONTAINER_meta_data_iterate (meta, &item_printer, NULL);
108   printf ("\n");
109   GNUNET_free (name);
110 }
111
112
113 /**
114  * Main function that will be run by the scheduler.
115  *
116  * @param cls closure
117  * @param args remaining command-line arguments
118  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
119  * @param cfg configuration
120  */
121 static void
122 run (void *cls,
123      char *const *args,
124      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
125 {
126   struct GNUNET_DISK_MapHandle *map;
127   struct GNUNET_DISK_FileHandle *h;
128   void *data;
129   size_t len;
130   uint64_t size;
131   const char *filename;
132   int i;
133
134   if (NULL == args[0])
135   {
136     fprintf (stderr, _("You must specify a filename to inspect."));
137     ret = 1;
138     return;
139   }
140   i = 0;
141   while (NULL != (filename = args[i++]))
142   {
143     if ((GNUNET_OK !=
144          GNUNET_DISK_file_size (filename,
145                                 &size,
146                                 GNUNET_YES)) ||
147         (NULL == (h = GNUNET_DISK_file_open (filename,
148                                              GNUNET_DISK_OPEN_READ,
149                                              GNUNET_DISK_PERM_NONE))))
150     {
151       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
152                   _("Failed to read directory `%s'\n"), filename);
153       ret = 1;
154       continue;
155     }
156     len = (size_t) size;
157     data = GNUNET_DISK_file_map (h, &map, GNUNET_DISK_MAP_TYPE_READ, len);
158     GNUNET_assert (NULL != data);
159     GNUNET_FS_directory_list_contents (len, data, 0, &print_entry, NULL);
160     printf ("\n");
161     GNUNET_DISK_file_unmap (map);
162     GNUNET_DISK_file_close (h);
163   }
164 }
165
166 /**
167  * The main function to inspect GNUnet directories.
168  *
169  * @param argc number of arguments from the command line
170  * @param argv command line arguments
171  * @return 0 ok, 1 on error
172  */
173 int
174 main (int argc, char *const *argv)
175 {
176   static struct GNUNET_GETOPT_CommandLineOption options[] = {
177     GNUNET_GETOPT_OPTION_END
178   };
179   return (GNUNET_OK ==
180           GNUNET_PROGRAM_run (argc,
181                               argv,
182                               "gnunet-directory [OPTIONS] FILENAME",
183                               gettext_noop
184                               ("Display contents of a GNUnet directory"),
185                               options, &run, NULL)) ? ret : 1;
186 }
187
188 /* end of gnunet-directory.c */