2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU 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.
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.
16 * @file fs/gnunet-directory.c
17 * @brief display content of GNUnet directories
18 * @author Christian Grothoff
21 #include "gnunet_fs_service.h"
26 * Print a meta data entry.
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)
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,
46 if (type == EXTRACTOR_METATYPE_GNUNET_FULL_DATA)
48 printf (_("\t<original file embedded in %u bytes of meta data>\n"),
49 (unsigned int) data_size);
52 if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
53 (format != EXTRACTOR_METAFORMAT_C_STRING))
55 if (type == EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME)
58 printf ("\t%20s: %s\n",
59 dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN,
60 EXTRACTOR_metatype_to_string (type)),
63 printf ("\t%20d: %s\n",
73 * Print an entry in a directory.
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)
88 print_entry (void *cls, const char *filename, const struct GNUNET_FS_Uri *uri,
89 const struct GNUNET_CONTAINER_MetaData *meta, size_t length,
96 GNUNET_CONTAINER_meta_data_get_by_type (meta,
97 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
100 printf (_("Directory `%s' meta data:\n"), name ? name : "");
101 GNUNET_CONTAINER_meta_data_iterate (meta, &item_printer, NULL);
103 printf (_("Directory `%s' contents:\n"), name ? name : "");
104 GNUNET_free_non_null (name);
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);
112 GNUNET_free_non_null (name);
117 * Main function that will be run by the scheduler.
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
125 run (void *cls, char *const *args, const char *cfgfile,
126 const struct GNUNET_CONFIGURATION_Handle *cfg)
128 struct GNUNET_DISK_MapHandle *map;
129 struct GNUNET_DISK_FileHandle *h;
133 const char *filename;
138 FPRINTF (stderr, "%s", _("You must specify a filename to inspect.\n"));
143 while (NULL != (filename = args[i++]))
145 if ((GNUNET_OK != GNUNET_DISK_file_size (filename, &size, GNUNET_YES, GNUNET_YES)) ||
148 GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
149 GNUNET_DISK_PERM_NONE))))
151 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to read directory `%s'\n"),
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"),
164 GNUNET_DISK_file_unmap (map);
165 GNUNET_DISK_file_close (h);
170 * The main function to inspect GNUnet directories.
172 * @param argc number of arguments from the command line
173 * @param argv command line arguments
174 * @return 0 ok, 1 on error
177 main (int argc, char *const *argv)
179 static struct GNUNET_GETOPT_CommandLineOption options[] = {
180 GNUNET_GETOPT_OPTION_END
183 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
187 GNUNET_PROGRAM_run (argc, argv, "gnunet-directory [OPTIONS] FILENAME",
189 ("Display contents of a GNUnet directory"),
190 options, &run, NULL)) ? ret : 1;
191 GNUNET_free ((void*) argv);
195 /* end of gnunet-directory.c */