glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / fs / gnunet-directory.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14 */
15 /**
16  * @file fs/gnunet-directory.c
17  * @brief display content of GNUnet directories
18  * @author Christian Grothoff
19  */
20 #include "platform.h"
21 #include "gnunet_fs_service.h"
22
23 static int ret;
24
25 /**
26  * Print a meta data entry.
27  *
28  * @param cls closure (unused)
29  * @param plugin_name name of the plugin that generated the meta data
30  * @param type type of the keyword
31  * @param format format of data
32  * @param data_mime_type mime type of data
33  * @param data value of the meta data
34  * @param data_size number of bytes in @a data
35  * @return always 0 (to continue iterating)
36  */
37 static int
38 item_printer (void *cls,
39               const char *plugin_name,
40               enum EXTRACTOR_MetaType type,
41               enum EXTRACTOR_MetaFormat format,
42               const char *data_mime_type,
43               const char *data,
44               size_t data_size)
45 {
46   if (type == EXTRACTOR_METATYPE_GNUNET_FULL_DATA)
47   {
48     printf (_("\t<original file embedded in %u bytes of meta data>\n"),
49             (unsigned int) data_size);
50     return 0;
51   }
52   if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
53       (format != EXTRACTOR_METAFORMAT_C_STRING))
54     return 0;
55   if (type == EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME)
56     return 0;
57 #if HAVE_LIBEXTRACTOR
58   printf ("\t%20s: %s\n",
59           dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN,
60                     EXTRACTOR_metatype_to_string (type)),
61     data);
62 #else
63   printf ("\t%20d: %s\n",
64           type,
65           data);
66 #endif
67   return 0;
68 }
69
70
71
72 /**
73  * Print an entry in a directory.
74  *
75  * @param cls closure (not used)
76  * @param filename name of the file in the directory
77  * @param uri URI of the file
78  * @param meta metadata for the file; metadata for
79  *        the directory if everything else is NULL/zero
80  * @param length length of the available data for the file
81  *           (of type size_t since data must certainly fit
82  *            into memory; if files are larger than size_t
83  *            permits, then they will certainly not be
84  *            embedded with the directory itself).
85  * @param data data available for the file (length bytes)
86  */
87 static void
88 print_entry (void *cls, const char *filename, const struct GNUNET_FS_Uri *uri,
89              const struct GNUNET_CONTAINER_MetaData *meta, size_t length,
90              const void *data)
91 {
92   char *string;
93   char *name;
94
95   name =
96       GNUNET_CONTAINER_meta_data_get_by_type (meta,
97                                               EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
98   if (uri == NULL)
99   {
100     printf (_("Directory `%s' meta data:\n"), name ? name : "");
101     GNUNET_CONTAINER_meta_data_iterate (meta, &item_printer, NULL);
102     printf ("\n");
103     printf (_("Directory `%s' contents:\n"), name ? name : "");
104     GNUNET_free_non_null (name);
105     return;
106   }
107   string = GNUNET_FS_uri_to_string (uri);
108   printf ("%s (%s):\n", name ? name : "", string);
109   GNUNET_free (string);
110   GNUNET_CONTAINER_meta_data_iterate (meta, &item_printer, NULL);
111   printf ("\n");
112   GNUNET_free_non_null (name);
113 }
114
115
116 /**
117  * Main function that will be run by the scheduler.
118  *
119  * @param cls closure
120  * @param args remaining command-line arguments
121  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
122  * @param cfg configuration
123  */
124 static void
125 run (void *cls, char *const *args, const char *cfgfile,
126      const struct GNUNET_CONFIGURATION_Handle *cfg)
127 {
128   struct GNUNET_DISK_MapHandle *map;
129   struct GNUNET_DISK_FileHandle *h;
130   void *data;
131   size_t len;
132   uint64_t size;
133   const char *filename;
134   int i;
135
136   if (NULL == args[0])
137   {
138     FPRINTF (stderr, "%s",  _("You must specify a filename to inspect.\n"));
139     ret = 1;
140     return;
141   }
142   i = 0;
143   while (NULL != (filename = args[i++]))
144   {
145     if ((GNUNET_OK != GNUNET_DISK_file_size (filename, &size, GNUNET_YES, GNUNET_YES)) ||
146         (NULL ==
147          (h =
148           GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
149                                  GNUNET_DISK_PERM_NONE))))
150     {
151       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to read directory `%s'\n"),
152                   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     if (GNUNET_OK != GNUNET_FS_directory_list_contents (len, data, 0, &print_entry, NULL))
160       fprintf (stdout, _("`%s' is not a GNUnet directory\n"),
161                filename);
162     else
163       printf ("\n");
164     GNUNET_DISK_file_unmap (map);
165     GNUNET_DISK_file_close (h);
166   }
167 }
168
169 /**
170  * The main function to inspect GNUnet directories.
171  *
172  * @param argc number of arguments from the command line
173  * @param argv command line arguments
174  * @return 0 ok, 1 on error
175  */
176 int
177 main (int argc, char *const *argv)
178 {
179   static struct GNUNET_GETOPT_CommandLineOption options[] = {
180     GNUNET_GETOPT_OPTION_END
181   };
182
183   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
184     return 2;
185
186   ret = (GNUNET_OK ==
187          GNUNET_PROGRAM_run (argc, argv, "gnunet-directory [OPTIONS] FILENAME",
188                              gettext_noop
189                              ("Display contents of a GNUnet directory"),
190                              options, &run, NULL)) ? ret : 1;
191   GNUNET_free ((void*) argv);
192   return ret;
193 }
194
195 /* end of gnunet-directory.c */