GNUnet  0.11.x
fs_file_information.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2009, 2011 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 
26 #include "platform.h"
27 #if HAVE_EXTRACTOR_H
28 #include <extractor.h>
29 #endif
30 #include "gnunet_fs_service.h"
31 #include "fs_api.h"
32 #include "fs_tree.h"
33 
34 
44 const char *
46 {
47  if (NULL != s->dir)
48  return NULL;
49  return s->serialization;
50 }
51 
52 
59 const char *
61 {
62  return s->filename;
63 }
64 
65 
74 void
76  const char *filename)
77 {
79  if (filename)
80  s->filename = GNUNET_strdup (filename);
81  else
82  s->filename = NULL;
83 }
84 
85 
102  struct GNUNET_FS_Handle *h,
103  void *client_info,
104  const char *filename,
105  const struct GNUNET_FS_Uri *keywords,
106  const struct GNUNET_CONTAINER_MetaData *meta,
107  int do_index,
108  const struct GNUNET_FS_BlockOptions *bo)
109 {
110  struct FileInfo *fi;
111  uint64_t fsize;
113  const char *fn;
114  const char *ss;
115 
116  /* FIXME: should include_symbolic_links be GNUNET_NO or GNUNET_YES here? */
117  if (GNUNET_OK !=
118  GNUNET_DISK_file_size (filename, &fsize, GNUNET_NO, GNUNET_YES))
119  {
121  return NULL;
122  }
123  fi = GNUNET_FS_make_file_reader_context_ (filename);
124  if (NULL == fi)
125  {
126  GNUNET_break (0);
127  return NULL;
128  }
129  ret =
131  client_info,
132  fsize,
134  fi,
135  keywords,
136  meta,
137  do_index,
138  bo);
139  if (ret == NULL)
140  return NULL;
141  ret->h = h;
142  ret->filename = GNUNET_strdup (filename);
143  fn = filename;
144  while (NULL != (ss = strstr (fn, DIR_SEPARATOR_STR)))
145  fn = ss + 1;
146 /* FIXME: If we assume that on other platforms CRT is UTF-8-aware, then
147  * this should be changed to EXTRACTOR_METAFORMAT_UTF8
148  */
150  "<gnunet>",
153  "text/plain",
154  fn,
155  strlen (fn) + 1);
156  return ret;
157 }
158 
159 
178  struct GNUNET_FS_Handle *h,
179  void *client_info,
180  uint64_t length,
181  void *data,
182  const struct GNUNET_FS_Uri *keywords,
183  const struct GNUNET_CONTAINER_MetaData *meta,
184  int do_index,
185  const struct GNUNET_FS_BlockOptions *bo)
186 {
187  if (GNUNET_YES == do_index)
188  {
189  GNUNET_break (0);
190  return NULL;
191  }
193  client_info,
194  length,
195  &
197  data,
198  keywords,
199  meta,
200  do_index,
201  bo);
202 }
203 
204 
223  struct GNUNET_FS_Handle *h,
224  void *client_info,
225  uint64_t length,
227  void *reader_cls,
228  const struct GNUNET_FS_Uri *keywords,
229  const struct GNUNET_CONTAINER_MetaData *meta,
230  int do_index,
231  const struct GNUNET_FS_BlockOptions *bo)
232 {
234 
235  if ((GNUNET_YES == do_index) && (reader != &GNUNET_FS_data_reader_file_))
236  {
237  GNUNET_break (0);
238  return NULL;
239  }
240  ret = GNUNET_new (struct GNUNET_FS_FileInformation);
241  ret->h = h;
242  ret->client_info = client_info;
244  if (ret->meta == NULL)
246  ret->keywords = (keywords == NULL) ? NULL : GNUNET_FS_uri_dup (keywords);
247  ret->data.file.reader = reader;
248  ret->data.file.reader_cls = reader_cls;
249  ret->data.file.do_index = do_index;
250  ret->data.file.file_size = length;
251  ret->bo = *bo;
252  return ret;
253 }
254 
255 
262 int
264  const struct GNUNET_FS_FileInformation *ent)
265 {
266  return ent->is_directory;
267 }
268 
269 
284  struct GNUNET_FS_Handle *h,
285  void *client_info,
286  const struct GNUNET_FS_Uri *keywords,
287  const struct GNUNET_CONTAINER_MetaData *meta,
288  const struct GNUNET_FS_BlockOptions *bo,
289  const char *filename)
290 {
292 
293  ret = GNUNET_new (struct GNUNET_FS_FileInformation);
294  ret->h = h;
295  ret->client_info = client_info;
297  ret->keywords = GNUNET_FS_uri_dup (keywords);
298  ret->bo = *bo;
299  ret->is_directory = GNUNET_YES;
300  if (filename != NULL)
301  ret->filename = GNUNET_strdup (filename);
302  return ret;
303 }
304 
305 
317 int
319  struct GNUNET_FS_FileInformation *ent)
320 {
321  if ((ent->dir != NULL) || (ent->next != NULL) ||
322  (dir->is_directory != GNUNET_YES))
323  {
324  GNUNET_break (0);
325  return GNUNET_SYSERR;
326  }
327  ent->dir = dir;
328  ent->next = dir->data.dir.entries;
329  dir->data.dir.entries = ent;
330  dir->data.dir.dir_size = 0;
331  return GNUNET_OK;
332 }
333 
334 
349 void
352  void *proc_cls)
353 {
354  struct GNUNET_FS_FileInformation *pos;
355  int no;
356 
357  no = GNUNET_NO;
358  if (GNUNET_OK !=
359  proc (proc_cls,
360  dir,
361  (dir->is_directory == GNUNET_YES) ? dir->data.dir.dir_size
362  : dir->data.file.file_size,
363  dir->meta,
364  &dir->keywords,
365  &dir->bo,
366  (dir->is_directory == GNUNET_YES) ? &no : &dir->data.file.do_index,
367  &dir->client_info))
368  return;
369  if (dir->is_directory != GNUNET_YES)
370  return;
371  pos = dir->data.dir.entries;
372  while (pos != NULL)
373  {
374  no = GNUNET_NO;
375  if (GNUNET_OK !=
376  proc (proc_cls,
377  pos,
378  (pos->is_directory == GNUNET_YES) ? pos->data.dir.dir_size
379  : pos->data.file.file_size,
380  pos->meta,
381  &pos->keywords,
382  &pos->bo,
383  (pos->is_directory == GNUNET_YES) ? &no
384  : &pos->data.file.do_index,
385  &pos->client_info))
386  break;
387  pos = pos->next;
388  }
389 }
390 
391 
402 void
405  void *cleaner_cls)
406 {
407  struct GNUNET_FS_FileInformation *pos;
408  int no;
409 
410  no = GNUNET_NO;
411  if (GNUNET_YES == fi->is_directory)
412  {
413  /* clean up directory */
414  while (NULL != (pos = fi->data.dir.entries))
415  {
416  fi->data.dir.entries = pos->next;
417  GNUNET_FS_file_information_destroy (pos, cleaner, cleaner_cls);
418  }
419  /* clean up client-info */
420  if (NULL != cleaner)
421  cleaner (cleaner_cls,
422  fi,
423  fi->data.dir.dir_size,
424  fi->meta,
425  &fi->keywords,
426  &fi->bo,
427  &no,
428  &fi->client_info);
430  }
431  else
432  {
433  /* call clean-up function of the reader */
434  if (NULL != fi->data.file.reader)
435  {
436  (void) fi->data.file.reader (fi->data.file.reader_cls, 0, 0, NULL, NULL);
437  fi->data.file.reader = NULL;
438  }
439  /* clean up client-info */
440  if (NULL != cleaner)
441  cleaner (cleaner_cls,
442  fi,
443  fi->data.file.file_size,
444  fi->meta,
445  &fi->keywords,
446  &fi->bo,
447  &fi->data.file.do_index,
448  &fi->client_info);
449  }
452  if (NULL != fi->sks_uri)
454  if (NULL != fi->chk_uri)
456  /* clean up serialization */
457  if ((NULL != fi->serialization) && (0 != unlink (fi->serialization)))
459  "unlink",
460  fi->serialization);
461  if (NULL != fi->keywords)
463  if (NULL != fi->meta)
466  if (NULL != fi->te)
467  {
468  GNUNET_FS_tree_encoder_finish (fi->te, NULL);
469  fi->te = NULL;
470  }
471  GNUNET_free (fi);
472 }
473 
474 
475 /* end of fs_file_information.c */
struct GNUNET_FS_FileInformation * entries
Linked list of entries in the directory.
Definition: fs_api.h:361
void * reader_cls
Closure for reader.
Definition: fs_api.h:321
static char * dir
Set to the directory where runtime files are stored.
Definition: gnunet-arm.c:89
struct GNUNET_FS_FileInformation * dir
If this is a file in a directory, "dir" refers to the directory; otherwise NULL.
Definition: fs_api.h:237
struct GNUNET_FS_FileInformation * GNUNET_FS_file_information_create_empty_directory(struct GNUNET_FS_Handle *h, void *client_info, const struct GNUNET_FS_Uri *keywords, const struct GNUNET_CONTAINER_MetaData *meta, const struct GNUNET_FS_BlockOptions *bo, const char *filename)
Create an entry for an empty directory in a publish-structure.
int(* GNUNET_FS_FileInformationProcessor)(void *cls, struct GNUNET_FS_FileInformation *fi, uint64_t length, struct GNUNET_CONTAINER_MetaData *meta, struct GNUNET_FS_Uri **uri, struct GNUNET_FS_BlockOptions *bo, int *do_index, void **client_info)
Function called on entries in a struct GNUNET_FS_FileInformation iteration.
struct GNUNET_CONTAINER_MetaData * meta
Metadata to use for the file.
Definition: fs_api.h:252
void * client_info
Pointer kept for the client.
Definition: fs_api.h:247
char * emsg
Error message (non-NULL if this operation failed).
Definition: fs_api.h:296
char * serialization
Under what filename is this struct serialized (for operational persistence).
Definition: fs_api.h:286
Master context for most FS operations.
Definition: fs_api.h:1068
GNUNET_FS_DataReader reader
Function that can be used to read the data for the file.
Definition: fs_api.h:316
int GNUNET_FS_file_information_add(struct GNUNET_FS_FileInformation *dir, struct GNUNET_FS_FileInformation *ent)
Add an entry to a directory in a publish-structure.
size_t GNUNET_FS_data_reader_copy_(void *cls, uint64_t offset, size_t max, void *buf, char **emsg)
Function that provides data by copying from a buffer.
Definition: fs_api.c:561
struct GNUNET_FS_FileInformation * GNUNET_FS_file_information_create_from_data(struct GNUNET_FS_Handle *h, void *client_info, uint64_t length, void *data, const struct GNUNET_FS_Uri *keywords, const struct GNUNET_CONTAINER_MetaData *meta, int do_index, const struct GNUNET_FS_BlockOptions *bo)
Create an entry for a file in a publish-structure.
char * filename
Name of the file or directory (must be an absolute path).
Definition: fs_api.h:301
struct GNUNET_FS_FileInformation * GNUNET_FS_file_information_create_from_file(struct GNUNET_FS_Handle *h, void *client_info, const char *filename, const struct GNUNET_FS_Uri *keywords, const struct GNUNET_CONTAINER_MetaData *meta, int do_index, const struct GNUNET_FS_BlockOptions *bo)
Create an entry for a file in a publish-structure.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
size_t GNUNET_FS_data_reader_file_(void *cls, uint64_t offset, size_t max, void *buf, char **emsg)
Function that provides data by reading from a file.
Definition: fs_api.c:457
Information for a file or directory that is about to be published.
Definition: fs_api.h:226
struct GNUNET_FS_Handle * h
Handle to the master context.
Definition: fs_api.h:242
union GNUNET_FS_FileInformation::@16 data
Data describing either the file or the directory.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
void * dir_data
Pointer to the data for the directory (or NULL if not available).
Definition: fs_api.h:373
struct GNUNET_FS_Uri * GNUNET_FS_uri_dup(const struct GNUNET_FS_Uri *uri)
Duplicate URI.
Definition: fs_uri.c:998
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:99
void GNUNET_FS_file_information_set_filename(struct GNUNET_FS_FileInformation *s, const char *filename)
Set the filename in the file information structure.
Meta data to associate with a file, directory or namespace.
struct GNUNET_FS_TreeEncoder * te
Encoder being used to publish this file.
Definition: fs_api.h:291
struct GNUNET_FS_FileInformation::@16::@17 file
Data for a file.
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
static char * fn
Filename of the unique file.
struct GNUNET_CONTAINER_MetaData * GNUNET_CONTAINER_meta_data_create(void)
Create a fresh meta data container.
#define DIR_SEPARATOR_STR
Definition: platform.h:168
int is_directory
Is this struct for a file or directory?
Definition: fs_api.h:390
static char * filename
void GNUNET_CONTAINER_meta_data_destroy(struct GNUNET_CONTAINER_MetaData *md)
Free meta data.
struct GNUNET_FS_Uri * chk_uri
CHK for this file or directory.
Definition: fs_api.h:263
void GNUNET_FS_uri_destroy(struct GNUNET_FS_Uri *uri)
Free URI.
Definition: fs_uri.c:678
const char * GNUNET_FS_file_information_get_filename(struct GNUNET_FS_FileInformation *s)
Obtain the filename from the file information structure.
int GNUNET_CONTAINER_meta_data_insert(struct GNUNET_CONTAINER_MetaData *md, const char *plugin_name, enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat format, const char *data_mime_type, const char *data, size_t data_size)
Extend metadata.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
void * GNUNET_FS_make_file_reader_context_(const char *filename)
Create the closure for the GNUNET_FS_data_reader_file_() callback.
Definition: fs_api.c:525
Settings for publishing a block (which may of course also apply to an entire directory or file)...
struct GNUNET_FS_FileInformation * next
Files in a directory are kept as a linked list.
Definition: fs_api.h:231
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:257
int GNUNET_FS_file_information_is_directory(const struct GNUNET_FS_FileInformation *ent)
Test if a given entry represents a directory.
struct GNUNET_FS_BlockOptions bo
Block options for the file.
Definition: fs_api.h:274
int do_index
Should the file be indexed or inserted?
Definition: fs_api.h:338
size_t(* GNUNET_FS_DataReader)(void *cls, uint64_t offset, size_t max, void *buf, char **emsg)
Function that provides data.
shared definitions for the FS library
void GNUNET_FS_file_information_inspect(struct GNUNET_FS_FileInformation *dir, GNUNET_FS_FileInformationProcessor proc, void *proc_cls)
Inspect a file or directory in a publish-structure.
A Universal Resource Identifier (URI), opaque.
Definition: fs_api.h:165
static struct GNUNET_CONTAINER_MetaData * meta
Meta-data provided via command-line option.
struct GNUNET_FS_Uri * keywords
Keywords to use for KBlocks.
Definition: fs_api.h:257
void GNUNET_FS_file_information_destroy(struct GNUNET_FS_FileInformation *fi, GNUNET_FS_FileInformationProcessor cleaner, void *cleaner_cls)
Destroy publish-structure.
struct GNUNET_FS_FileInformation * GNUNET_FS_file_information_create_from_reader(struct GNUNET_FS_Handle *h, void *client_info, uint64_t length, GNUNET_FS_DataReader reader, void *reader_cls, const struct GNUNET_FS_Uri *keywords, const struct GNUNET_CONTAINER_MetaData *meta, int do_index, const struct GNUNET_FS_BlockOptions *bo)
Create an entry for a file in a publish-structure.
void GNUNET_FS_tree_encoder_finish(struct GNUNET_FS_TreeEncoder *te, char **emsg)
Clean up a tree encoder and return information about possible errors.
Definition: fs_tree.c:443
#define GNUNET_YES
Definition: gnunet_common.h:77
uint32_t data
The data value.
size_t dir_size
Size of the directory itself (in bytes); 0 if the size has not yet been calculated.
Definition: fs_api.h:367
struct GNUNET_FS_Uri * sks_uri
SKS URI for this file or directory.
Definition: fs_api.h:269
Closure for GNUNET_FS_data_reader_file_().
Definition: fs_api.c:422
const char * GNUNET_FS_file_information_get_id(struct GNUNET_FS_FileInformation *s)
Obtain the name under which this file information structure is stored on disk.
static struct GNUNET_FS_BlockOptions bo
Options we set for published blocks.
#define GNUNET_free(ptr)
Wrapper around free.
Merkle-tree-ish-CHK file encoding for GNUnet.
struct GNUNET_CONTAINER_MetaData * GNUNET_CONTAINER_meta_data_duplicate(const struct GNUNET_CONTAINER_MetaData *md)
Duplicate a MetaData token.