GNUnet  0.10.x
Data Structures | Macros | Functions
fs_directory.c File Reference

Helper functions for building directories. More...

#include "platform.h"
#include "gnunet_fs_service.h"
#include "fs_api.h"
Include dependency graph for fs_directory.c:

Go to the source code of this file.

Data Structures

struct  GetFullDataClosure
 Closure for 'find_full_data'. More...
 
struct  BuilderEntry
 Entries in the directory (builder). More...
 
struct  GNUNET_FS_DirectoryBuilder
 Internal state of a directory builder. More...
 

Macros

#define GNUNET_DIRECTORY_MAGIC   "\211GND\r\n\032\n"
 String that is used to indicate that a file is a GNUnet directory. More...
 

Functions

int GNUNET_FS_meta_data_test_for_directory (const struct GNUNET_CONTAINER_MetaData *md)
 Does the meta-data claim that this is a directory? Checks if the mime-type is that of a GNUnet directory. More...
 
void GNUNET_FS_meta_data_make_directory (struct GNUNET_CONTAINER_MetaData *md)
 Set the MIMETYPE information for the given metadata to "application/gnunet-directory". More...
 
static int find_full_data (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_len)
 Type of a function that libextractor calls for each meta data item found. More...
 
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. More...
 
struct GNUNET_FS_DirectoryBuilderGNUNET_FS_directory_builder_create (const struct GNUNET_CONTAINER_MetaData *mdir)
 Create a directory builder. More...
 
void GNUNET_FS_directory_builder_add (struct GNUNET_FS_DirectoryBuilder *bld, const struct GNUNET_FS_Uri *uri, const struct GNUNET_CONTAINER_MetaData *md, const void *data)
 Add an entry to a directory. More...
 
static size_t do_align (size_t start_position, size_t end_position)
 Given the start and end position of a block of data, return the end position of that data after alignment to the DBLOCK_SIZE. More...
 
static void block_align (size_t start, unsigned int count, const size_t *sizes, unsigned int *perm)
 Compute a permuation of the blocks to minimize the cost of alignment. More...
 
int GNUNET_FS_directory_builder_finish (struct GNUNET_FS_DirectoryBuilder *bld, size_t *rsize, void **rdata)
 Finish building the directory. More...
 

Detailed Description

Helper functions for building directories.

Author
Christian Grothoff

TODO:

Definition in file fs_directory.c.

Macro Definition Documentation

◆ GNUNET_DIRECTORY_MAGIC

#define GNUNET_DIRECTORY_MAGIC   "\211GND\r\n\032\n"

String that is used to indicate that a file is a GNUnet directory.

Definition at line 43 of file fs_directory.c.

Referenced by GNUNET_FS_directory_builder_finish().

Function Documentation

◆ find_full_data()

static int find_full_data ( 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_len 
)
static

Type of a function that libextractor calls for each meta data item found.

Parameters
clsclosure (user-defined)
plugin_namename of the plugin that produced this value; special values can be used (i.e. '<zlib>' for zlib being used in the main libextractor library and yielding meta data).
typelibextractor-type describing the meta data
formatbasic format information about data
data_mime_typemime-type of data (not of the original file); can be NULL (if mime-type is not known)
dataactual meta-data found
data_lennumber of bytes in data
Returns
0 to continue extracting, 1 to abort

Definition at line 133 of file fs_directory.c.

References GetFullDataClosure::data, EXTRACTOR_METATYPE_GNUNET_FULL_DATA, GNUNET_malloc, GNUNET_memcpy, and GetFullDataClosure::size.

Referenced by GNUNET_FS_directory_list_contents().

136 {
137  struct GetFullDataClosure *gfdc = cls;
138 
140  {
141  gfdc->size = data_len;
142  if (data_len > 0)
143  {
144  gfdc->data = GNUNET_malloc (data_len);
145  GNUNET_memcpy (gfdc->data, data, data_len);
146  }
147  return 1;
148  }
149  return 0;
150 }
size_t size
Number of bytes stored in data.
Definition: fs_directory.c:111
Closure for &#39;find_full_data&#39;.
Definition: fs_directory.c:100
#define GNUNET_memcpy(dst, src, n)
void * data
Extracted binary meta data.
Definition: fs_directory.c:106
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
uint32_t data
The data value.
#define GNUNET_malloc(size)
Wrapper around malloc.
Here is the caller graph for this function:

◆ do_align()

static size_t do_align ( size_t  start_position,
size_t  end_position 
)
static

Given the start and end position of a block of data, return the end position of that data after alignment to the DBLOCK_SIZE.

Definition at line 484 of file fs_directory.c.

References DBLOCK_SIZE.

Referenced by block_align(), GNUNET_FS_directory_builder_finish(), GNUNET_MST_from_buffer(), GNUNET_SERVER_mst_receive(), and mst_receive().

485 {
486  size_t align;
487 
488  align = (end_position / DBLOCK_SIZE) * DBLOCK_SIZE;
489  if ((start_position < align) && (end_position > align))
490  return align + end_position - start_position;
491  return end_position;
492 }
#define DBLOCK_SIZE
Size of the individual blocks used for file-sharing.
Definition: fs.h:40
Here is the caller graph for this function:

◆ block_align()

static void block_align ( size_t  start,
unsigned int  count,
const size_t *  sizes,
unsigned int *  perm 
)
static

Compute a permuation of the blocks to minimize the cost of alignment.

Greedy packer.

Parameters
startstarting position for the first block
countsize of the two arrays
sizesthe sizes of the individual blocks
permthe permutation of the blocks (updated)

Definition at line 505 of file fs_directory.c.

References DBLOCK_SIZE, do_align(), GNUNET_assert, and start.

Referenced by GNUNET_FS_directory_builder_finish().

507 {
508  unsigned int i;
509  unsigned int j;
510  unsigned int tmp;
511  unsigned int best;
512  ssize_t badness;
513  size_t cpos;
514  size_t cend;
515  ssize_t cbad;
516  unsigned int cval;
517 
518  cpos = start;
519  for (i = 0; i < count; i++)
520  {
521  start = cpos;
522  badness = 0x7FFFFFFF;
523  best = -1;
524  for (j = i; j < count; j++)
525  {
526  cval = perm[j];
527  cend = cpos + sizes[cval];
528  if (cpos % DBLOCK_SIZE == 0)
529  {
530  /* prefer placing the largest blocks first */
531  cbad = -(cend % DBLOCK_SIZE);
532  }
533  else
534  {
535  if (cpos / DBLOCK_SIZE == cend / DBLOCK_SIZE)
536  {
537  /* Data fits into the same block! Prefer small left-overs! */
538  cbad = DBLOCK_SIZE - cend % DBLOCK_SIZE;
539  }
540  else
541  {
542  /* Would have to waste space to re-align, add big factor, this
543  * case is a real loss (proportional to space wasted)! */
544  cbad = DBLOCK_SIZE * (DBLOCK_SIZE - cpos % DBLOCK_SIZE);
545  }
546  }
547  if (cbad < badness)
548  {
549  best = j;
550  badness = cbad;
551  }
552  }
553  GNUNET_assert (best != -1);
554  tmp = perm[i];
555  perm[i] = perm[best];
556  perm[best] = tmp;
557  cpos += sizes[perm[i]];
558  cpos = do_align (start, cpos);
559  }
560 }
static size_t do_align(size_t start_position, size_t end_position)
Given the start and end position of a block of data, return the end position of that data after align...
Definition: fs_directory.c:484
static int start
Set if we are to start default services (including ARM).
Definition: gnunet-arm.c:39
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define DBLOCK_SIZE
Size of the individual blocks used for file-sharing.
Definition: fs.h:40
Here is the call graph for this function:
Here is the caller graph for this function: