GNUnet  0.10.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 
58 const char *
60 {
61  return s->filename;
62 }
63 
64 
73 void
75  const char *filename)
76 {
78  if (filename)
79  s->filename = GNUNET_strdup(filename);
80  else
81  s->filename = NULL;
82 }
83 
84 
101  struct GNUNET_FS_Handle *h,
102  void *client_info,
103  const char *filename,
104  const struct GNUNET_FS_Uri *keywords,
105  const struct GNUNET_CONTAINER_MetaData *meta,
106  int do_index,
107  const struct GNUNET_FS_BlockOptions *bo)
108 {
109  struct FileInfo *fi;
110  uint64_t fsize;
112  const char *fn;
113  const char *ss;
114 
115  /* FIXME: should include_symbolic_links be GNUNET_NO or GNUNET_YES here? */
116  if (GNUNET_OK !=
117  GNUNET_DISK_file_size(filename, &fsize, GNUNET_NO, GNUNET_YES))
118  {
120  return NULL;
121  }
123  if (NULL == fi)
124  {
125  GNUNET_break(0);
126  return NULL;
127  }
128  ret =
130  client_info,
131  fsize,
133  fi,
134  keywords,
135  meta,
136  do_index,
137  bo);
138  if (ret == NULL)
139  return NULL;
140  ret->h = h;
141  ret->filename = GNUNET_strdup(filename);
142  fn = filename;
143  while (NULL != (ss = strstr(fn, DIR_SEPARATOR_STR)))
144  fn = ss + 1;
145 /* FIXME: If we assume that on other platforms CRT is UTF-8-aware, then
146  * this should be changed to EXTRACTOR_METAFORMAT_UTF8
147  */
149  "<gnunet>",
152  "text/plain",
153  fn,
154  strlen(fn) + 1);
155  return ret;
156 }
157 
158 
177  struct GNUNET_FS_Handle *h,
178  void *client_info,
179  uint64_t length,
180  void *data,
181  const struct GNUNET_FS_Uri *keywords,
182  const struct GNUNET_CONTAINER_MetaData *meta,
183  int do_index,
184  const struct GNUNET_FS_BlockOptions *bo)
185 {
186  if (GNUNET_YES == do_index)
187  {
188  GNUNET_break(0);
189  return NULL;
190  }
192  client_info,
193  length,
195  data,
196  keywords,
197  meta,
198  do_index,
199  bo);
200 }
201 
202 
221  struct GNUNET_FS_Handle *h,
222  void *client_info,
223  uint64_t length,
225  void *reader_cls,
226  const struct GNUNET_FS_Uri *keywords,
227  const struct GNUNET_CONTAINER_MetaData *meta,
228  int do_index,
229  const struct GNUNET_FS_BlockOptions *bo)
230 {
232 
233  if ((GNUNET_YES == do_index) && (reader != &GNUNET_FS_data_reader_file_))
234  {
235  GNUNET_break(0);
236  return NULL;
237  }
239  ret->h = h;
240  ret->client_info = client_info;
242  if (ret->meta == NULL)
244  ret->keywords = (keywords == NULL) ? NULL : GNUNET_FS_uri_dup(keywords);
245  ret->data.file.reader = reader;
246  ret->data.file.reader_cls = reader_cls;
247  ret->data.file.do_index = do_index;
248  ret->data.file.file_size = length;
249  ret->bo = *bo;
250  return ret;
251 }
252 
253 
260 int
262  const struct GNUNET_FS_FileInformation *ent)
263 {
264  return ent->is_directory;
265 }
266 
267 
282  struct GNUNET_FS_Handle *h,
283  void *client_info,
284  const struct GNUNET_FS_Uri *keywords,
285  const struct GNUNET_CONTAINER_MetaData *meta,
286  const struct GNUNET_FS_BlockOptions *bo,
287  const char *filename)
288 {
290 
292  ret->h = h;
293  ret->client_info = client_info;
295  ret->keywords = GNUNET_FS_uri_dup(keywords);
296  ret->bo = *bo;
297  ret->is_directory = GNUNET_YES;
298  if (filename != NULL)
299  ret->filename = GNUNET_strdup(filename);
300  return ret;
301 }
302 
303 
315 int
317  struct GNUNET_FS_FileInformation *ent)
318 {
319  if ((ent->dir != NULL) || (ent->next != NULL) ||
320  (dir->is_directory != GNUNET_YES))
321  {
322  GNUNET_break(0);
323  return GNUNET_SYSERR;
324  }
325  ent->dir = dir;
326  ent->next = dir->data.dir.entries;
327  dir->data.dir.entries = ent;
328  dir->data.dir.dir_size = 0;
329  return GNUNET_OK;
330 }
331 
332 
347 void
350  void *proc_cls)
351 {
352  struct GNUNET_FS_FileInformation *pos;
353  int no;
354 
355  no = GNUNET_NO;
356  if (GNUNET_OK !=
357  proc(proc_cls,
358  dir,
359  (dir->is_directory == GNUNET_YES) ? dir->data.dir.dir_size
360  : dir->data.file.file_size,
361  dir->meta,
362  &dir->keywords,
363  &dir->bo,
364  (dir->is_directory == GNUNET_YES) ? &no : &dir->data.file.do_index,
365  &dir->client_info))
366  return;
367  if (dir->is_directory != GNUNET_YES)
368  return;
369  pos = dir->data.dir.entries;
370  while (pos != NULL)
371  {
372  no = GNUNET_NO;
373  if (GNUNET_OK !=
374  proc(proc_cls,
375  pos,
376  (pos->is_directory == GNUNET_YES) ? pos->data.dir.dir_size
377  : pos->data.file.file_size,
378  pos->meta,
379  &pos->keywords,
380  &pos->bo,
381  (pos->is_directory == GNUNET_YES) ? &no
382  : &pos->data.file.do_index,
383  &pos->client_info))
384  break;
385  pos = pos->next;
386  }
387 }
388 
389 
400 void
403  void *cleaner_cls)
404 {
405  struct GNUNET_FS_FileInformation *pos;
406  int no;
407 
408  no = GNUNET_NO;
409  if (GNUNET_YES == fi->is_directory)
410  {
411  /* clean up directory */
412  while (NULL != (pos = fi->data.dir.entries))
413  {
414  fi->data.dir.entries = pos->next;
415  GNUNET_FS_file_information_destroy(pos, cleaner, cleaner_cls);
416  }
417  /* clean up client-info */
418  if (NULL != cleaner)
419  cleaner(cleaner_cls,
420  fi,
421  fi->data.dir.dir_size,
422  fi->meta,
423  &fi->keywords,
424  &fi->bo,
425  &no,
426  &fi->client_info);
428  }
429  else
430  {
431  /* call clean-up function of the reader */
432  if (NULL != fi->data.file.reader)
433  {
434  (void)fi->data.file.reader(fi->data.file.reader_cls, 0, 0, NULL, NULL);
435  fi->data.file.reader = NULL;
436  }
437  /* clean up client-info */
438  if (NULL != cleaner)
439  cleaner(cleaner_cls,
440  fi,
441  fi->data.file.file_size,
442  fi->meta,
443  &fi->keywords,
444  &fi->bo,
445  &fi->data.file.do_index,
446  &fi->client_info);
447  }
450  if (NULL != fi->sks_uri)
452  if (NULL != fi->chk_uri)
454  /* clean up serialization */
455  if ((NULL != fi->serialization) && (0 != unlink(fi->serialization)))
457  "unlink",
458  fi->serialization);
459  if (NULL != fi->keywords)
461  if (NULL != fi->meta)
464  if (NULL != fi->te)
465  {
467  fi->te = NULL;
468  }
469  GNUNET_free(fi);
470 }
471 
472 
473 /* end of fs_file_information.c */
struct GNUNET_FS_FileInformation * entries
Linked list of entries in the directory.
Definition: fs_api.h:350
void * reader_cls
Closure for reader.
Definition: fs_api.h:311
static char * dir
Set to the directory where runtime files are stored.
Definition: gnunet-arm.c:84
struct GNUNET_FS_FileInformation * dir
If this is a file in a directory, "dir" refers to the directory; otherwise NULL.
Definition: fs_api.h:229
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:244
struct GNUNET_FS_FileInformation::@19::@20 file
Data for a file.
void * client_info
Pointer kept for the client.
Definition: fs_api.h:239
char * emsg
Error message (non-NULL if this operation failed).
Definition: fs_api.h:288
char * serialization
Under what filename is this struct serialized (for operational persistence).
Definition: fs_api.h:278
Master context for most FS operations.
Definition: fs_api.h:1056
GNUNET_FS_DataReader reader
Function that can be used to read the data for the file.
Definition: fs_api.h:306
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:560
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:293
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.
#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:456
Information for a file or directory that is about to be published.
Definition: fs_api.h:219
struct GNUNET_FS_Handle * h
Handle to the master context.
Definition: fs_api.h:234
static int ret
Final status code.
Definition: gnunet-arm.c:89
#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:362
struct GNUNET_FS_Uri * GNUNET_FS_uri_dup(const struct GNUNET_FS_Uri *uri)
Duplicate URI.
Definition: fs_uri.c:995
#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:94
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:283
#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:379
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:255
void GNUNET_FS_uri_destroy(struct GNUNET_FS_Uri *uri)
Free URI.
Definition: fs_uri.c:675
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:524
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:223
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:254
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:266
int do_index
Should the file be indexed or inserted?
Definition: fs_api.h:328
size_t(* GNUNET_FS_DataReader)(void *cls, uint64_t offset, size_t max, void *buf, char **emsg)
Function that provides data.
union GNUNET_FS_FileInformation::@19 data
Data describing either the file or the directory.
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:162
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:249
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:442
#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:356
struct GNUNET_FS_Uri * sks_uri
SKS URI for this file or directory.
Definition: fs_api.h:261
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.