GNUnet  0.10.x
gnunet-directory.c
Go to the documentation of this file.
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 */
25 #include "platform.h"
26 #include "gnunet_fs_service.h"
27 
28 static int ret;
29 
42 static int
43 item_printer (void *cls,
44  const char *plugin_name,
46  enum EXTRACTOR_MetaFormat format,
47  const char *data_mime_type,
48  const char *data,
49  size_t data_size)
50 {
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) &&
59  return 0;
61  return 0;
62 #if HAVE_LIBEXTRACTOR
63  printf ("\t%20s: %s\n",
65  EXTRACTOR_metatype_to_string (type)),
66  data);
67 #else
68  printf ("\t%20d: %s\n",
69  type,
70  data);
71 #endif
72  return 0;
73 }
74 
75 
76 
92 static void
93 print_entry (void *cls, const char *filename, const struct GNUNET_FS_Uri *uri,
94  const struct GNUNET_CONTAINER_MetaData *meta, size_t length,
95  const void *data)
96 {
97  char *string;
98  char *name;
99 
100  name =
103  if (uri == NULL)
104  {
105  printf (_("Directory `%s' meta data:\n"), name ? name : "");
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);
116  printf ("\n");
117  GNUNET_free_non_null (name);
118 }
119 
120 
129 static void
130 run (void *cls, char *const *args, const char *cfgfile,
131  const struct GNUNET_CONFIGURATION_Handle *cfg)
132 {
133  struct GNUNET_DISK_MapHandle *map;
134  struct GNUNET_DISK_FileHandle *h;
135  void *data;
136  size_t len;
137  uint64_t size;
138  const char *filename;
139  int i;
140 
141  if (NULL == args[0])
142  {
143  FPRINTF (stderr, "%s", _("You must specify a filename to inspect.\n"));
144  ret = 1;
145  return;
146  }
147  i = 0;
148  while (NULL != (filename = args[i++]))
149  {
150  if ((GNUNET_OK != GNUNET_DISK_file_size (filename, &size, GNUNET_YES, GNUNET_YES)) ||
151  (NULL ==
152  (h =
155  {
156  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to read directory `%s'\n"),
157  filename);
158  ret = 1;
159  continue;
160  }
161  len = (size_t) size;
162  data = GNUNET_DISK_file_map (h, &map, GNUNET_DISK_MAP_TYPE_READ, len);
163  GNUNET_assert (NULL != data);
164  if (GNUNET_OK != GNUNET_FS_directory_list_contents (len, data, 0, &print_entry, NULL))
165  fprintf (stdout, _("`%s' is not a GNUnet directory\n"),
166  filename);
167  else
168  printf ("\n");
171  }
172 }
173 
181 int
182 main (int argc, char *const *argv)
183 {
184  static struct GNUNET_GETOPT_CommandLineOption options[] = {
186  };
187 
188  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
189  return 2;
190 
191  ret = (GNUNET_OK ==
192  GNUNET_PROGRAM_run (argc, argv, "gnunet-directory [OPTIONS] FILENAME",
194  ("Display contents of a GNUnet directory"),
195  options, &run, NULL)) ? ret : 1;
196  GNUNET_free ((void*) argv);
197  return ret;
198 }
199 
200 /* end of gnunet-directory.c */
Open the file for reading.
EXTRACTOR_MetaFormat
Format in which the extracted meta data is presented.
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1817
static void print_entry(void *cls, const char *filename, const struct GNUNET_FS_Uri *uri, const struct GNUNET_CONTAINER_MetaData *meta, size_t length, const void *data)
Print an entry in a directory.
#define FPRINTF
Definition: plibc.h:683
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1521
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static int item_printer(void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat format, const char *data_mime_type, const char *data, size_t data_size)
Print a meta data entry.
void * GNUNET_DISK_file_map(const struct GNUNET_DISK_FileHandle *h, struct GNUNET_DISK_MapHandle **m, enum GNUNET_DISK_MapType access, size_t len)
Map a file into memory.
Definition: disk.c:2020
Nobody is allowed to do anything to the file.
0-terminated, UTF-8 encoded string.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
Definition of a command line option.
char * GNUNET_CONTAINER_meta_data_get_by_type(const struct GNUNET_CONTAINER_MetaData *md, enum EXTRACTOR_MetaType type)
Get the first MD entry of the given type.
EXTRACTOR_MetaType
Enumeration defining various sources of keywords.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:94
static struct GNUNET_CONTAINER_MultiPeerMap * map
Handle to the map used to store old latency values for peers.
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
char * GNUNET_FS_uri_to_string(const struct GNUNET_FS_Uri *uri)
Convert a URI to a UTF-8 String.
Definition: fs_uri.c:1988
int main(int argc, char *const *argv)
The main function to inspect GNUnet directories.
static struct GNUNET_FS_Uri * uri
Value of URI provided on command-line (when not publishing a file but just creating UBlocks to refer ...
Meta data to associate with a file, directory or namespace.
#define dgettext(Domainname, Msgid)
Definition: gettext.h:46
Read-only memory map.
static char * filename
static char * plugin_name
Solver plugin name as string.
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
static unsigned int size
Size of the "table".
Definition: peer.c:67
int GNUNET_DISK_file_unmap(struct GNUNET_DISK_MapHandle *h)
Unmap a file.
Definition: disk.c:2100
const char * name
int GNUNET_FS_directory_list_contents(size_t size, const void *data, uint64_t offset, GNUNET_FS_DirectoryEntryProcessor dep, void *dep_cls)
Iterate over all entries in a directory.
Definition: fs_directory.c:179
int GNUNET_DISK_file_size(const char *filename, uint64_t *size, int include_symbolic_links, int single_file_mode)
Get the size of the file (or directory) of the given file (in bytes).
Definition: disk.c:289
configuration data
Definition: configuration.c:85
#define LIBEXTRACTOR_GETTEXT_DOMAIN
Definition: platform.h:209
A Universal Resource Identifier (URI), opaque.
Definition: fs_api.h:168
static struct GNUNET_CONTAINER_MetaData * meta
Meta-data provided via command-line option.
int GNUNET_CONTAINER_meta_data_iterate(const struct GNUNET_CONTAINER_MetaData *md, EXTRACTOR_MetaDataProcessor iter, void *iter_cls)
Iterate over MD entries.
#define GNUNET_log(kind,...)
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
Main function that will be run by the scheduler.
int GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration, parse options).
Definition: program.c:361
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
#define GNUNET_YES
Definition: gnunet_common.h:80
static int ret
uint32_t data
The data value.
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition: disk.c:1673
static size_t data_size
Number of bytes in data.
Handle used to access files (and pipes).
Handle for a memory-mapping operation.
Definition: disk.c:1985
#define GNUNET_free(ptr)
Wrapper around free.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
#define gettext_noop(String)
Definition: gettext.h:69