GNUnet  0.19.3
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 
27 #include "gnunet_fs_service.h"
28 
29 static int ret;
30 
43 static int
44 item_printer (void *cls,
45  const char *plugin_name,
46  enum EXTRACTOR_MetaType type,
47  enum EXTRACTOR_MetaFormat format,
48  const char *data_mime_type,
49  const char *data,
50  size_t data_size)
51 {
52  if (type == EXTRACTOR_METATYPE_GNUNET_FULL_DATA)
53  {
54  printf (_ ("\t<original file embedded in %u bytes of meta data>\n"),
55  (unsigned int) data_size);
56  return 0;
57  }
58  if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
59  (format != EXTRACTOR_METAFORMAT_C_STRING))
60  return 0;
62  return 0;
63 #if HAVE_LIBEXTRACTOR
64  printf ("\t%20s: %s\n",
66  EXTRACTOR_metatype_to_string (type)),
67  data);
68 #else
69  printf ("\t%20d: %s\n", type, data);
70 #endif
71  return 0;
72 }
73 
74 
90 static void
91 print_entry (void *cls,
92  const char *filename,
93  const struct GNUNET_FS_Uri *uri,
94  const struct GNUNET_FS_MetaData *meta,
95  size_t length,
96  const void *data)
97 {
98  char *string;
99  char *name;
100 
102  meta,
104  if (uri == NULL)
105  {
106  printf (_ ("Directory `%s' meta data:\n"), name ? name : "");
108  printf ("\n");
109  printf (_ ("Directory `%s' contents:\n"), name ? name : "");
110  GNUNET_free (name);
111  return;
112  }
113  string = GNUNET_FS_uri_to_string (uri);
114  printf ("%s (%s):\n", name ? name : "", string);
115  GNUNET_free (string);
117  printf ("\n");
118  GNUNET_free (name);
119 }
120 
121 
130 static void
131 run (void *cls,
132  char *const *args,
133  const char *cfgfile,
134  const struct GNUNET_CONFIGURATION_Handle *cfg)
135 {
136  struct GNUNET_DISK_MapHandle *map;
137  struct GNUNET_DISK_FileHandle *h;
138  void *data;
139  size_t len;
140  uint64_t size;
141  const char *filename;
142  int i;
143 
144  if (NULL == args[0])
145  {
146  fprintf (stderr, "%s", _ ("You must specify a filename to inspect.\n"));
147  ret = 1;
148  return;
149  }
150  i = 0;
151  while (NULL != (filename = args[i++]))
152  {
153  if ((GNUNET_OK !=
155  (NULL == (h = GNUNET_DISK_file_open (filename,
158  {
160  _ ("Failed to read directory `%s'\n"),
161  filename);
162  ret = 1;
163  continue;
164  }
165  len = (size_t) size;
167  GNUNET_assert (NULL != data);
168  if (GNUNET_OK !=
170  fprintf (stdout, _ ("`%s' is not a GNUnet directory\n"), filename);
171  else
172  printf ("\n");
175  }
176 }
177 
178 
186 int
187 main (int argc, char *const *argv)
188 {
189  static struct GNUNET_GETOPT_CommandLineOption options[] = {
191  };
192 
193  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
194  return 2;
195 
196  ret = (GNUNET_OK ==
197  GNUNET_PROGRAM_run (argc,
198  argv,
199  "gnunet-directory [OPTIONS] FILENAME",
200  gettext_noop (
201  "Display contents of a GNUnet directory"),
202  options,
203  &run,
204  NULL))
205  ? ret
206  : 1;
207  GNUNET_free_nz ((void *) argv);
208  return ret;
209 }
210 
211 
212 /* end of gnunet-directory.c */
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
#define gettext_noop(String)
Definition: gettext.h:70
#define dgettext(Domainname, Msgid)
Definition: gettext.h:47
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static size_t data_size
Number of bytes in data.
Definition: gnunet-abd.c:187
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:99
static struct GNUNET_CONTAINER_MultiPeerMap * map
Handle to the map used to store old latency values for peers.
static void print_entry(void *cls, const char *filename, const struct GNUNET_FS_Uri *uri, const struct GNUNET_FS_MetaData *meta, size_t length, const void *data)
Print an entry in a directory.
static int ret
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 main(int argc, char *const *argv)
The main function to inspect GNUnet directories.
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.
static char * filename
uint32_t data
The data value.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
static struct GNUNET_FS_Uri * uri
Value of URI provided on command-line (when not publishing a file but just creating UBlocks to refer ...
static struct GNUNET_FS_MetaData * meta
Meta-data provided via command-line option.
static char * plugin_name
Name of our plugin.
API for file sharing via GNUnet.
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:1234
enum GNUNET_GenericReturnValue 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:221
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:1377
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1305
enum GNUNET_GenericReturnValue GNUNET_DISK_file_unmap(struct GNUNET_DISK_MapHandle *h)
Unmap a file.
Definition: disk.c:1408
@ GNUNET_DISK_OPEN_READ
Open the file for reading.
@ GNUNET_DISK_PERM_NONE
Nobody is allowed to do anything to the file.
@ GNUNET_DISK_MAP_TYPE_READ
Read-only memory map.
#define EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME
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:182
char * GNUNET_FS_uri_to_string(const struct GNUNET_FS_Uri *uri)
Convert a URI to a UTF-8 String.
Definition: fs_uri.c:2017
#define GNUNET_log(kind,...)
@ GNUNET_OK
@ GNUNET_YES
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_ERROR
#define GNUNET_free(ptr)
Wrapper around free.
#define GNUNET_free_nz(ptr)
Wrapper around free.
int GNUNET_FS_meta_data_iterate(const struct GNUNET_FS_MetaData *md, EXTRACTOR_MetaDataProcessor iter, void *iter_cls)
Iterate over MD entries.
Definition: meta_data.c:422
char * GNUNET_FS_meta_data_get_by_type(const struct GNUNET_FS_MetaData *md, enum EXTRACTOR_MetaType type)
Get the first MD entry of the given type.
Definition: meta_data.c:442
enum GNUNET_GenericReturnValue 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,...
Definition: program.c:400
enum GNUNET_GenericReturnValue GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1222
static unsigned int size
Size of the "table".
Definition: peer.c:68
#define _(String)
GNU gettext support macro.
Definition: platform.h:177
#define LIBEXTRACTOR_GETTEXT_DOMAIN
Definition: platform.h:178
const char * name
Handle used to access files (and pipes).
Handle for a memory-mapping operation.
Definition: disk.c:1358
Meta data to associate with a file, directory or namespace.
Definition: meta_data.c:96
A Universal Resource Identifier (URI), opaque.
Definition: fs_api.h:167
Definition of a command line option.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model