GNUnet 0.21.1
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_FS_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_FS_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_FS_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_FS_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 permutation 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:

  • modify directory builder API to support incremental generation of directories (to allow directories that would not fit into memory to be created)
  • modify directory processor API to support incremental iteration over FULL directories (without missing entries) to allow access to directories that do not fit entirely into memory

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 44 of file fs_directory.c.

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 (e.g. '<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 136 of file fs_directory.c.

139{
140 struct GetFullDataClosure *gfdc = cls;
141
142 if (type == EXTRACTOR_METATYPE_GNUNET_FULL_DATA)
143 {
144 gfdc->size = data_len;
145 if (data_len > 0)
146 {
147 gfdc->data = GNUNET_malloc (data_len);
148 GNUNET_memcpy (gfdc->data, data, data_len);
149 }
150 return 1;
151 }
152 return 0;
153}
static char * data
The data to insert into the dht.
static uint32_t type
Type string converted to DNS type value.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_malloc(size)
Wrapper around malloc.
Closure for 'find_full_data'.
Definition: fs_directory.c:105
size_t size
Number of bytes stored in data.
Definition: fs_directory.c:114
void * data
Extracted binary meta data.
Definition: fs_directory.c:109

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

Referenced by GNUNET_FS_directory_list_contents().

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 488 of file fs_directory.c.

489{
490 size_t align;
491
492 align = (end_position / DBLOCK_SIZE) * DBLOCK_SIZE;
493 if ((start_position < align) && (end_position > align))
494 return align + end_position - start_position;
495 return end_position;
496}
#define DBLOCK_SIZE
Size of the individual blocks used for file-sharing.
Definition: fs.h:41

References DBLOCK_SIZE.

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

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 permutation 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 509 of file fs_directory.c.

511{
512 unsigned int i;
513 unsigned int j;
514 unsigned int tmp;
515 unsigned int best;
516 ssize_t badness;
517 size_t cpos;
518 size_t cend;
519 ssize_t cbad;
520 unsigned int cval;
521
522 cpos = start;
523 for (i = 0; i < count; i++)
524 {
525 start = cpos;
526 badness = 0x7FFFFFFF;
527 best = -1;
528 for (j = i; j < count; j++)
529 {
530 cval = perm[j];
531 cend = cpos + sizes[cval];
532 if (cpos % DBLOCK_SIZE == 0)
533 {
534 /* prefer placing the largest blocks first */
535 cbad = -(cend % DBLOCK_SIZE);
536 }
537 else
538 {
539 if (cpos / DBLOCK_SIZE == cend / DBLOCK_SIZE)
540 {
541 /* Data fits into the same block! Prefer small left-overs! */
542 cbad = DBLOCK_SIZE - cend % DBLOCK_SIZE;
543 }
544 else
545 {
546 /* Would have to waste space to re-align, add big factor, this
547 * case is a real loss (proportional to space wasted)! */
548 cbad = DBLOCK_SIZE * (DBLOCK_SIZE - cpos % DBLOCK_SIZE);
549 }
550 }
551 if (cbad < badness)
552 {
553 best = j;
554 badness = cbad;
555 }
556 }
557 GNUNET_assert (best != -1);
558 tmp = perm[i];
559 perm[i] = perm[best];
560 perm[best] = tmp;
561 cpos += sizes[perm[i]];
562 cpos = do_align (start, cpos);
563 }
564}
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:488
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.

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

Referenced by GNUNET_FS_directory_builder_finish().

Here is the call graph for this function:
Here is the caller graph for this function: