tighten formatting rules
[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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
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 @a 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,
48               const char *data,
49               size_t data_size)
50 {
51   if (type == EXTRACTOR_METATYPE_GNUNET_FULL_DATA)
52   {
53     printf (_ ("\t<original file embedded in %u bytes of meta data>\n"),
54             (unsigned int) data_size);
55     return 0;
56   }
57   if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
58       (format != EXTRACTOR_METAFORMAT_C_STRING))
59     return 0;
60   if (type == EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME)
61     return 0;
62 #if HAVE_LIBEXTRACTOR
63   printf ("\t%20s: %s\n",
64           dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN,
65                     EXTRACTOR_metatype_to_string (type)),
66           data);
67 #else
68   printf ("\t%20d: %s\n", type, data);
69 #endif
70   return 0;
71 }
72
73
74 /**
75  * Print an entry in a directory.
76  *
77  * @param cls closure (not used)
78  * @param filename name of the file in the directory
79  * @param uri URI of the file
80  * @param meta metadata for the file; metadata for
81  *        the directory if everything else is NULL/zero
82  * @param length length of the available data for the file
83  *           (of type size_t since data must certainly fit
84  *            into memory; if files are larger than size_t
85  *            permits, then they will certainly not be
86  *            embedded with the directory itself).
87  * @param data data available for the file (length bytes)
88  */
89 static void
90 print_entry (void *cls,
91              const char *filename,
92              const struct GNUNET_FS_Uri *uri,
93              const struct GNUNET_CONTAINER_MetaData *meta,
94              size_t length,
95              const void *data)
96 {
97   char *string;
98   char *name;
99
100   name = GNUNET_CONTAINER_meta_data_get_by_type (
101     meta,
102     EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
103   if (uri == NULL)
104   {
105     printf (_ ("Directory `%s' meta data:\n"), name ? name : "");
106     GNUNET_CONTAINER_meta_data_iterate (meta, &item_printer, NULL);
107     printf ("\n");
108     printf (_ ("Directory `%s' contents:\n"), name ? name : "");
109     GNUNET_free_non_null (name);
110     return;
111   }
112   string = GNUNET_FS_uri_to_string (uri);
113   printf ("%s (%s):\n", name ? name : "", string);
114   GNUNET_free (string);
115   GNUNET_CONTAINER_meta_data_iterate (meta, &item_printer, NULL);
116   printf ("\n");
117   GNUNET_free_non_null (name);
118 }
119
120
121 /**
122  * Main function that will be run by the scheduler.
123  *
124  * @param cls closure
125  * @param args remaining command-line arguments
126  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
127  * @param cfg configuration
128  */
129 static void
130 run (void *cls,
131      char *const *args,
132      const char *cfgfile,
133      const struct GNUNET_CONFIGURATION_Handle *cfg)
134 {
135   struct GNUNET_DISK_MapHandle *map;
136   struct GNUNET_DISK_FileHandle *h;
137   void *data;
138   size_t len;
139   uint64_t size;
140   const char *filename;
141   int i;
142
143   if (NULL == args[0])
144   {
145     fprintf (stderr, "%s", _ ("You must specify a filename to inspect.\n"));
146     ret = 1;
147     return;
148   }
149   i = 0;
150   while (NULL != (filename = args[i++]))
151   {
152     if ((GNUNET_OK !=
153          GNUNET_DISK_file_size (filename, &size, GNUNET_YES, GNUNET_YES)) ||
154         (NULL == (h = GNUNET_DISK_file_open (filename,
155                                              GNUNET_DISK_OPEN_READ,
156                                              GNUNET_DISK_PERM_NONE))))
157     {
158       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
159                   _ ("Failed to read directory `%s'\n"),
160                   filename);
161       ret = 1;
162       continue;
163     }
164     len = (size_t) size;
165     data = GNUNET_DISK_file_map (h, &map, GNUNET_DISK_MAP_TYPE_READ, len);
166     GNUNET_assert (NULL != data);
167     if (GNUNET_OK !=
168         GNUNET_FS_directory_list_contents (len, data, 0, &print_entry, NULL))
169       fprintf (stdout, _ ("`%s' is not a GNUnet directory\n"), filename);
170     else
171       printf ("\n");
172     GNUNET_DISK_file_unmap (map);
173     GNUNET_DISK_file_close (h);
174   }
175 }
176
177
178 /**
179  * The main function to inspect GNUnet directories.
180  *
181  * @param argc number of arguments from the command line
182  * @param argv command line arguments
183  * @return 0 ok, 1 on error
184  */
185 int
186 main (int argc, char *const *argv)
187 {
188   static struct GNUNET_GETOPT_CommandLineOption options[] = {
189     GNUNET_GETOPT_OPTION_END
190   };
191
192   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
193     return 2;
194
195   ret = (GNUNET_OK ==
196          GNUNET_PROGRAM_run (argc,
197                              argv,
198                              "gnunet-directory [OPTIONS] FILENAME",
199                              gettext_noop (
200                                "Display contents of a GNUnet directory"),
201                              options,
202                              &run,
203                              NULL))
204         ? ret
205         : 1;
206   GNUNET_free ((void *) argv);
207   return ret;
208 }
209
210
211 /* end of gnunet-directory.c */