#include "gnunet_fs_service.h"
#include "fs.h"
-#ifndef EXTRACTOR_GNUNET_FULL_DATA
-#define EXTRACTOR_GNUNET_FULL_DATA 137
-#endif
-
/**
* String that is used to indicate that a file
* is a GNUnet directory.
char *mime;
int ret;
- mime = GNUNET_CONTAINER_meta_data_get_by_type (md, EXTRACTOR_MIMETYPE);
+ if (NULL == md)
+ return GNUNET_SYSERR;
+ mime = GNUNET_CONTAINER_meta_data_get_by_type (md, EXTRACTOR_METATYPE_MIMETYPE);
if (mime == NULL)
return GNUNET_SYSERR;
ret = (0 == strcmp (mime, GNUNET_FS_DIRECTORY_MIME)) ? GNUNET_YES : GNUNET_NO;
{
char *mime;
- mime = GNUNET_CONTAINER_meta_data_get_by_type (md, EXTRACTOR_MIMETYPE);
+ mime = GNUNET_CONTAINER_meta_data_get_by_type (md, EXTRACTOR_METATYPE_MIMETYPE);
if (mime != NULL)
{
GNUNET_break (0 == strcmp (mime,
return;
}
GNUNET_CONTAINER_meta_data_insert (md,
- EXTRACTOR_MIMETYPE,
- GNUNET_FS_DIRECTORY_MIME);
+ "<gnunet>",
+ EXTRACTOR_METATYPE_MIMETYPE,
+ EXTRACTOR_METAFORMAT_UTF8,
+ "text/plain",
+ GNUNET_FS_DIRECTORY_MIME,
+ strlen (GNUNET_FS_DIRECTORY_MIME)+1);
+}
+
+
+/**
+ * Closure for 'find_full_data'.
+ */
+struct GetFullDataClosure
+{
+
+ /**
+ * Extracted binary meta data.
+ */
+ void *data;
+
+ /**
+ * Number of bytes stored in data.
+ */
+ size_t size;
+};
+
+
+/**
+ * Type of a function that libextractor calls for each
+ * meta data item found.
+ *
+ * @param cls closure (user-defined)
+ * @param plugin_name name 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).
+ * @param type libextractor-type describing the meta data
+ * @param format basic format information about data
+ * @param data_mime_type mime-type of data (not of the original file);
+ * can be NULL (if mime-type is not known)
+ * @param data actual meta-data found
+ * @param data_len number of bytes in data
+ * @return 0 to continue extracting, 1 to abort
+ */
+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)
+{
+ struct GetFullDataClosure *gfdc = cls;
+
+ if (type == EXTRACTOR_METATYPE_GNUNET_FULL_DATA)
+ {
+ gfdc->size = data_len;
+ if (data_len > 0)
+ {
+ gfdc->data = GNUNET_malloc (data_len);
+ memcpy (gfdc->data,
+ data,
+ data_len);
+ }
+ return 1;
+ }
+ return 0;
}
GNUNET_FS_DirectoryEntryProcessor dep,
void *dep_cls)
{
+ struct GetFullDataClosure full_data;
const char *cdata = data;
- char *file_data;
char *emsg;
uint64_t pos;
uint64_t align;
/* URI is never empty, must be end of block,
skip to next alignment */
align =
- ((pos / GNUNET_FS_DBLOCK_SIZE) + 1) * GNUNET_FS_DBLOCK_SIZE;
+ ((pos / DBLOCK_SIZE) + 1) * DBLOCK_SIZE;
if (align == pos)
{
/* if we were already aligned, still skip a block! */
- align += GNUNET_FS_DBLOCK_SIZE;
+ align += DBLOCK_SIZE;
}
pos = align;
if (pos >= size)
}
pos += mdSize;
filename = GNUNET_CONTAINER_meta_data_get_by_type (md,
- EXTRACTOR_FILENAME);
- file_data = GNUNET_CONTAINER_meta_data_get_by_type (md,
- EXTRACTOR_GNUNET_FULL_DATA);
+ EXTRACTOR_METATYPE_FILENAME);
+ full_data.size = 0;
+ full_data.data = NULL;
+ GNUNET_CONTAINER_meta_data_iterate (md,
+ &find_full_data,
+ &full_data);
if (dep != NULL)
- dep (dep_cls,
- filename,
- uri,
- md,
- (file_data != NULL) ? strlen(file_data) : 0,
- file_data);
- GNUNET_free_non_null (file_data);
+ {
+ dep (dep_cls,
+ filename,
+ uri,
+ md,
+ full_data.size,
+ full_data.data);
+ }
+ GNUNET_free_non_null (full_data.data);
GNUNET_free_non_null (filename);
GNUNET_CONTAINER_meta_data_destroy (md);
GNUNET_FS_uri_destroy (uri);
struct GNUNET_FS_DirectoryBuilder *ret;
ret = GNUNET_malloc(sizeof(struct GNUNET_FS_DirectoryBuilder));
- ret->meta = GNUNET_CONTAINER_meta_data_duplicate (mdir);
+ if (mdir != NULL)
+ ret->meta = GNUNET_CONTAINER_meta_data_duplicate (mdir);
+ else
+ ret->meta = GNUNET_CONTAINER_meta_data_create ();
GNUNET_FS_meta_data_make_directory (ret->meta);
return ret;
}
const struct GNUNET_CONTAINER_MetaData *md,
const void *data)
{
+ struct GNUNET_FS_Uri *curi;
struct BuilderEntry *e;
uint64_t fsize;
uint32_t big;
size_t mdxs;
char *uris;
char *ser;
+ char *sptr;
size_t slen;
struct GNUNET_CONTAINER_MetaData *meta;
const struct GNUNET_CONTAINER_MetaData *meta_use;
if (GNUNET_FS_uri_test_chk (uri))
fsize = GNUNET_FS_uri_chk_get_file_size (uri);
else
- fsize = GNUNET_FS_uri_chk_get_file_size (GNUNET_FS_uri_loc_get_uri (uri));
+ {
+ curi = GNUNET_FS_uri_loc_get_uri (uri);
+ fsize = GNUNET_FS_uri_chk_get_file_size (curi);
+ GNUNET_FS_uri_destroy (curi);
+ }
else
fsize = 0; /* not given */
- if (fsize > GNUNET_FS_MAX_INLINE_SIZE)
+ if (fsize > MAX_INLINE_SIZE)
fsize = 0; /* too large */
- if (memchr (data, fsize, '\0')) // FIXME: check memchr args!
- fsize = 0; /* must not have 0's in data! */
uris = GNUNET_FS_uri_to_string (uri);
slen = strlen (uris) + 1;
mds =
- GNUNET_CONTAINER_meta_data_get_serialized_size (md,
- GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
+ GNUNET_CONTAINER_meta_data_get_serialized_size (md);
meta_use = md;
meta = NULL;
if (fsize > 0)
{
meta = GNUNET_CONTAINER_meta_data_duplicate (md);
GNUNET_CONTAINER_meta_data_insert (meta,
- EXTRACTOR_GNUNET_FULL_DATA,
- data);
+ "<gnunet>",
+ EXTRACTOR_METATYPE_GNUNET_FULL_DATA,
+ EXTRACTOR_METAFORMAT_BINARY,
+ NULL,
+ data,
+ fsize);
mdxs =
- GNUNET_CONTAINER_meta_data_get_serialized_size (meta,
- GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
- if ( (slen + sizeof (uint32_t) + mdxs - 1) / GNUNET_FS_DBLOCK_SIZE ==
- (slen + sizeof (uint32_t) + mds - 1) / GNUNET_FS_DBLOCK_SIZE)
+ GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
+ if ( (slen + sizeof (uint32_t) + mdxs - 1) / DBLOCK_SIZE ==
+ (slen + sizeof (uint32_t) + mds - 1) / DBLOCK_SIZE)
{
/* adding full data would not cause us to cross
additional blocks, so add it! */
ser = (char*) &e[1];
memcpy (ser, uris, slen);
GNUNET_free (uris);
+ sptr = &ser[slen + sizeof(uint32_t)];
ret = GNUNET_CONTAINER_meta_data_serialize (meta_use,
- &ser[slen + sizeof(uint32_t)],
+ &sptr,
mds,
GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
if (NULL != meta)
/**
* Given the start and end position of a block of
* data, return the end position of that data
- * after alignment to the GNUNET_FS_DBLOCK_SIZE.
+ * after alignment to the DBLOCK_SIZE.
*/
static size_t
do_align (size_t start_position,
{
size_t align;
- align = (end_position / GNUNET_FS_DBLOCK_SIZE) * GNUNET_FS_DBLOCK_SIZE;
+ align = (end_position / DBLOCK_SIZE) * DBLOCK_SIZE;
if ((start_position < align) && (end_position > align))
return align + end_position - start_position;
return end_position;
{
cval = perm[j];
cend = cpos + sizes[cval];
- if (cpos % GNUNET_FS_DBLOCK_SIZE == 0)
+ if (cpos % DBLOCK_SIZE == 0)
{
/* prefer placing the largest blocks first */
- cbad = -(cend % GNUNET_FS_DBLOCK_SIZE);
+ cbad = -(cend % DBLOCK_SIZE);
}
else
{
- if (cpos / GNUNET_FS_DBLOCK_SIZE ==
- cend / GNUNET_FS_DBLOCK_SIZE)
+ if (cpos / DBLOCK_SIZE ==
+ cend / DBLOCK_SIZE)
{
/* Data fits into the same block! Prefer small left-overs! */
cbad =
- GNUNET_FS_DBLOCK_SIZE - cend % GNUNET_FS_DBLOCK_SIZE;
+ DBLOCK_SIZE - cend % DBLOCK_SIZE;
}
else
{
/* Would have to waste space to re-align, add big factor, this
case is a real loss (proportional to space wasted)! */
cbad =
- GNUNET_FS_DBLOCK_SIZE * (GNUNET_FS_DBLOCK_SIZE -
+ DBLOCK_SIZE * (DBLOCK_SIZE -
cpos %
- GNUNET_FS_DBLOCK_SIZE);
+ DBLOCK_SIZE);
}
}
if (cbad < badness)
* @param bld directory to finish
* @param rsize set to the number of bytes needed
* @param rdata set to the encoded directory
+ * @return GNUNET_OK on success
*/
-void
+int
GNUNET_FS_directory_builder_finish (struct GNUNET_FS_DirectoryBuilder *bld,
size_t *rsize,
void **rdata)
{
char *data;
+ char *sptr;
size_t *sizes;
unsigned int *perm;
unsigned int i;
uint32_t big;
size = 8 + sizeof (uint32_t);
- size += GNUNET_CONTAINER_meta_data_get_serialized_size (bld->meta,
- GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
- if (bld->count > 0)
+ size += GNUNET_CONTAINER_meta_data_get_serialized_size (bld->meta);
+ sizes = NULL;
+ perm = NULL;
+ bes = NULL;
+ if (0 < bld->count)
{
sizes = GNUNET_malloc (bld->count * sizeof (size_t));
perm = GNUNET_malloc (bld->count * sizeof (unsigned int));
sizes[i] = pos->len;
pos = pos->next;
}
- }
- block_align (size,
- bld->count,
- sizes,
- perm);
-
- /* compute final size with alignment */
- for (i = 0; i < bld->count; i++)
- {
- psize = size;
- size += sizes[perm[i]];
- size = do_align (psize, size);
+ block_align (size,
+ bld->count,
+ sizes,
+ perm);
+ /* compute final size with alignment */
+ for (i = 0; i < bld->count; i++)
+ {
+ psize = size;
+ size += sizes[perm[i]];
+ size = do_align (psize, size);
+ }
}
*rsize = size;
- data = GNUNET_malloc (size);
+ data = GNUNET_malloc_large (size);
+ if (data == NULL)
+ {
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
+ "malloc");
+ *rsize = 0;
+ *rdata = NULL;
+ GNUNET_free_non_null (sizes);
+ GNUNET_free_non_null (perm);
+ GNUNET_free_non_null (bes);
+ return GNUNET_SYSERR;
+ }
*rdata = data;
memcpy (data, GNUNET_DIRECTORY_MAGIC, 8);
off = 8;
+ sptr = &data[off + sizeof (uint32_t)];
ret = GNUNET_CONTAINER_meta_data_serialize (bld->meta,
- &data[off +
- sizeof (uint32_t)],
+ &sptr,
size - off - sizeof (uint32_t),
GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
GNUNET_assert (ret != -1);
sizes[i]);
GNUNET_free (bes[i]);
}
- GNUNET_free (sizes);
- GNUNET_free (perm);
- GNUNET_free (bes);
+ GNUNET_free_non_null (sizes);
+ GNUNET_free_non_null (perm);
+ GNUNET_free_non_null (bes);
GNUNET_assert (off == size);
GNUNET_CONTAINER_meta_data_destroy (bld->meta);
GNUNET_free (bld);
+ return GNUNET_OK;
}