2 This file is part of GNUnet.
3 Copyright (C) 2003, 2004, 2006, 2009 GNUnet e.V.
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.
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.
17 * @file fs/fs_directory.c
18 * @brief Helper functions for building directories.
19 * @author Christian Grothoff
22 * - modify directory builder API to support incremental
23 * generation of directories (to allow directories that
24 * would not fit into memory to be created)
25 * - modify directory processor API to support incremental
26 * iteration over FULL directories (without missing entries)
27 * to allow access to directories that do not fit entirely
31 #include "gnunet_fs_service.h"
35 * String that is used to indicate that a file
36 * is a GNUnet directory.
38 #define GNUNET_DIRECTORY_MAGIC "\211GND\r\n\032\n"
42 * Does the meta-data claim that this is a directory?
43 * Checks if the mime-type is that of a GNUnet directory.
45 * @return #GNUNET_YES if it is, #GNUNET_NO if it is not, #GNUNET_SYSERR if
46 * we have no mime-type information (treat as #GNUNET_NO)
49 GNUNET_FS_meta_data_test_for_directory (const struct GNUNET_CONTAINER_MetaData *md)
56 mime = GNUNET_CONTAINER_meta_data_get_by_type (md, EXTRACTOR_METATYPE_MIMETYPE);
59 ret = (0 == strcasecmp (mime, GNUNET_FS_DIRECTORY_MIME)) ? GNUNET_YES : GNUNET_NO;
66 * Set the MIMETYPE information for the given
67 * metadata to "application/gnunet-directory".
69 * @param md metadata to add mimetype to
72 GNUNET_FS_meta_data_make_directory (struct GNUNET_CONTAINER_MetaData *md)
77 GNUNET_CONTAINER_meta_data_get_by_type (md, EXTRACTOR_METATYPE_MIMETYPE);
80 GNUNET_break (0 == strcmp (mime, GNUNET_FS_DIRECTORY_MIME));
84 GNUNET_CONTAINER_meta_data_insert (md, "<gnunet>",
85 EXTRACTOR_METATYPE_MIMETYPE,
86 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
87 GNUNET_FS_DIRECTORY_MIME,
88 strlen (GNUNET_FS_DIRECTORY_MIME) + 1);
93 * Closure for 'find_full_data'.
95 struct GetFullDataClosure
99 * Extracted binary meta data.
104 * Number of bytes stored in data.
111 * Type of a function that libextractor calls for each
112 * meta data item found.
114 * @param cls closure (user-defined)
115 * @param plugin_name name of the plugin that produced this value;
116 * special values can be used (i.e. '<zlib>' for zlib being
117 * used in the main libextractor library and yielding
119 * @param type libextractor-type describing the meta data
120 * @param format basic format information about data
121 * @param data_mime_type mime-type of data (not of the original file);
122 * can be NULL (if mime-type is not known)
123 * @param data actual meta-data found
124 * @param data_len number of bytes in data
125 * @return 0 to continue extracting, 1 to abort
128 find_full_data (void *cls, const char *plugin_name,
129 enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat format,
130 const char *data_mime_type, const char *data, size_t data_len)
132 struct GetFullDataClosure *gfdc = cls;
134 if (type == EXTRACTOR_METATYPE_GNUNET_FULL_DATA)
136 gfdc->size = data_len;
139 gfdc->data = GNUNET_malloc (data_len);
140 GNUNET_memcpy (gfdc->data, data, data_len);
149 * Iterate over all entries in a directory. Note that directories
150 * are structured such that it is possible to iterate over the
151 * individual blocks as well as over the entire directory. Thus
152 * a client can call this function on the buffer in the
153 * GNUNET_FS_ProgressCallback. Also, directories can optionally
154 * include the contents of (small) files embedded in the directory
155 * itself; for those files, the processor may be given the
156 * contents of the file directly by this function.
159 * Note that this function maybe called on parts of directories. Thus
160 * parser errors should not be reported _at all_ (with GNUNET_break).
161 * Still, if some entries can be recovered despite these parsing
162 * errors, the function should try to do this.
164 * @param size number of bytes in data
165 * @param data pointer to the beginning of the directory
166 * @param offset offset of data in the directory
167 * @param dep function to call on each entry
168 * @param dep_cls closure for @a dep
169 * @return #GNUNET_OK if this could be a block in a directory,
170 * #GNUNET_NO if this could be part of a directory (but not 100% OK)
171 * #GNUNET_SYSERR if @a data does not represent a directory
174 GNUNET_FS_directory_list_contents (size_t size,
177 GNUNET_FS_DirectoryEntryProcessor dep,
180 struct GetFullDataClosure full_data;
181 const char *cdata = data;
187 struct GNUNET_FS_Uri *uri;
188 struct GNUNET_CONTAINER_MetaData *md;
192 ((size < 8 + sizeof (uint32_t)) ||
194 GNUNET_FS_DIRECTORY_MAGIC,
196 return GNUNET_SYSERR;
200 GNUNET_memcpy (&mdSize,
203 mdSize = ntohl (mdSize);
204 if (mdSize > size - 8 - sizeof (uint32_t))
207 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
208 _("MAGIC mismatch. This is not a GNUnet directory.\n"));
209 return GNUNET_SYSERR;
211 md = GNUNET_CONTAINER_meta_data_deserialize (&cdata[8 + sizeof (uint32_t)],
216 return GNUNET_SYSERR; /* malformed ! */
224 GNUNET_CONTAINER_meta_data_destroy (md);
225 pos = 8 + sizeof (uint32_t) + mdSize;
229 /* find end of URI */
230 if (cdata[pos] == '\0')
232 /* URI is never empty, must be end of block,
233 * skip to next alignment */
234 align = ((pos / DBLOCK_SIZE) + 1) * DBLOCK_SIZE;
237 /* if we were already aligned, still skip a block! */
238 align += DBLOCK_SIZE;
243 /* malformed - or partial download... */
248 while ((epos < size) && (cdata[epos] != '\0'))
251 return GNUNET_NO; /* malformed - or partial download */
253 uri = GNUNET_FS_uri_parse (&cdata[pos], &emsg);
258 pos--; /* go back to '\0' to force going to next alignment */
261 if (GNUNET_FS_uri_test_ksk (uri))
263 GNUNET_FS_uri_destroy (uri);
265 return GNUNET_NO; /* illegal in directory! */
268 GNUNET_memcpy (&mdSize,
271 mdSize = ntohl (mdSize);
272 pos += sizeof (uint32_t);
273 if (pos + mdSize > size)
275 GNUNET_FS_uri_destroy (uri);
276 return GNUNET_NO; /* malformed - or partial download */
279 md = GNUNET_CONTAINER_meta_data_deserialize (&cdata[pos],
283 GNUNET_FS_uri_destroy (uri);
285 return GNUNET_NO; /* malformed ! */
289 GNUNET_CONTAINER_meta_data_get_by_type (md,
290 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
292 full_data.data = NULL;
293 GNUNET_CONTAINER_meta_data_iterate (md,
305 GNUNET_free_non_null (full_data.data);
306 GNUNET_free_non_null (filename);
307 GNUNET_CONTAINER_meta_data_destroy (md);
308 GNUNET_FS_uri_destroy (uri);
314 * Entries in the directory (builder).
319 * This is a linked list.
321 struct BuilderEntry *next;
324 * Length of this entry.
330 * Internal state of a directory builder.
332 struct GNUNET_FS_DirectoryBuilder
335 * Meta-data for the directory itself.
337 struct GNUNET_CONTAINER_MetaData *meta;
340 * Head of linked list of entries.
342 struct BuilderEntry *head;
345 * Number of entires in the directory.
352 * Create a directory builder.
354 * @param mdir metadata for the directory
356 struct GNUNET_FS_DirectoryBuilder *
357 GNUNET_FS_directory_builder_create (const struct GNUNET_CONTAINER_MetaData
360 struct GNUNET_FS_DirectoryBuilder *ret;
362 ret = GNUNET_new (struct GNUNET_FS_DirectoryBuilder);
364 ret->meta = GNUNET_CONTAINER_meta_data_duplicate (mdir);
366 ret->meta = GNUNET_CONTAINER_meta_data_create ();
367 GNUNET_FS_meta_data_make_directory (ret->meta);
373 * Add an entry to a directory.
375 * @param bld directory to extend
376 * @param uri uri of the entry (must not be a KSK)
377 * @param md metadata of the entry
378 * @param data raw data of the entry, can be NULL, otherwise
379 * data must point to exactly the number of bytes specified
380 * by the uri which must be of type LOC or CHK
383 GNUNET_FS_directory_builder_add (struct GNUNET_FS_DirectoryBuilder *bld,
384 const struct GNUNET_FS_Uri *uri,
385 const struct GNUNET_CONTAINER_MetaData *md,
388 struct GNUNET_FS_Uri *curi;
389 struct BuilderEntry *e;
399 struct GNUNET_CONTAINER_MetaData *meta;
400 const struct GNUNET_CONTAINER_MetaData *meta_use;
402 GNUNET_assert (!GNUNET_FS_uri_test_ksk (uri));
405 GNUNET_assert (!GNUNET_FS_uri_test_sks (uri));
406 if (GNUNET_FS_uri_test_chk (uri))
408 fsize = GNUNET_FS_uri_chk_get_file_size (uri);
412 curi = GNUNET_FS_uri_loc_get_uri (uri);
413 GNUNET_assert (NULL != curi);
414 fsize = GNUNET_FS_uri_chk_get_file_size (curi);
415 GNUNET_FS_uri_destroy (curi);
420 fsize = 0; /* not given */
422 if (fsize > MAX_INLINE_SIZE)
423 fsize = 0; /* too large */
424 uris = GNUNET_FS_uri_to_string (uri);
425 slen = strlen (uris) + 1;
426 mds = GNUNET_CONTAINER_meta_data_get_serialized_size (md);
431 meta = GNUNET_CONTAINER_meta_data_duplicate (md);
432 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>",
433 EXTRACTOR_METATYPE_GNUNET_FULL_DATA,
434 EXTRACTOR_METAFORMAT_BINARY, NULL, data,
436 mdxs = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
437 if ((slen + sizeof (uint32_t) + mdxs - 1) / DBLOCK_SIZE ==
438 (slen + sizeof (uint32_t) + mds - 1) / DBLOCK_SIZE)
440 /* adding full data would not cause us to cross
441 * additional blocks, so add it! */
447 if (mds > GNUNET_MAX_MALLOC_CHECKED / 2)
448 mds = GNUNET_MAX_MALLOC_CHECKED / 2;
449 e = GNUNET_malloc (sizeof (struct BuilderEntry) + slen + mds +
451 ser = (char *) &e[1];
452 GNUNET_memcpy (ser, uris, slen);
454 sptr = &ser[slen + sizeof (uint32_t)];
456 GNUNET_CONTAINER_meta_data_serialize (meta_use, &sptr, mds,
457 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
459 GNUNET_CONTAINER_meta_data_destroy (meta);
465 GNUNET_memcpy (&ser[slen], &big, sizeof (uint32_t));
466 e->len = slen + sizeof (uint32_t) + mds;
474 * Given the start and end position of a block of
475 * data, return the end position of that data
476 * after alignment to the DBLOCK_SIZE.
479 do_align (size_t start_position, size_t end_position)
483 align = (end_position / DBLOCK_SIZE) * DBLOCK_SIZE;
484 if ((start_position < align) && (end_position > align))
485 return align + end_position - start_position;
491 * Compute a permuation of the blocks to
492 * minimize the cost of alignment. Greedy packer.
494 * @param start starting position for the first block
495 * @param count size of the two arrays
496 * @param sizes the sizes of the individual blocks
497 * @param perm the permutation of the blocks (updated)
500 block_align (size_t start, unsigned int count, const size_t * sizes,
514 for (i = 0; i < count; i++)
517 badness = 0x7FFFFFFF;
519 for (j = i; j < count; j++)
522 cend = cpos + sizes[cval];
523 if (cpos % DBLOCK_SIZE == 0)
525 /* prefer placing the largest blocks first */
526 cbad = -(cend % DBLOCK_SIZE);
530 if (cpos / DBLOCK_SIZE == cend / DBLOCK_SIZE)
532 /* Data fits into the same block! Prefer small left-overs! */
533 cbad = DBLOCK_SIZE - cend % DBLOCK_SIZE;
537 /* Would have to waste space to re-align, add big factor, this
538 * case is a real loss (proportional to space wasted)! */
539 cbad = DBLOCK_SIZE * (DBLOCK_SIZE - cpos % DBLOCK_SIZE);
548 GNUNET_assert (best != -1);
550 perm[i] = perm[best];
552 cpos += sizes[perm[i]];
553 cpos = do_align (start, cpos);
559 * Finish building the directory. Frees the
560 * builder context and returns the directory
563 * @param bld directory to finish
564 * @param rsize set to the number of bytes needed
565 * @param rdata set to the encoded directory
566 * @return #GNUNET_OK on success
569 GNUNET_FS_directory_builder_finish (struct GNUNET_FS_DirectoryBuilder *bld,
579 struct BuilderEntry *pos;
580 struct BuilderEntry **bes;
587 size = strlen (GNUNET_DIRECTORY_MAGIC) + sizeof (uint32_t);
588 size += GNUNET_CONTAINER_meta_data_get_serialized_size (bld->meta);
594 sizes = GNUNET_new_array (bld->count,
596 perm = GNUNET_new_array (bld->count,
598 bes = GNUNET_new_array (bld->count,
599 struct BuilderEntry *);
601 for (i = 0; i < bld->count; i++)
608 block_align (size, bld->count, sizes, perm);
609 /* compute final size with alignment */
610 for (i = 0; i < bld->count; i++)
613 size += sizes[perm[i]];
614 size = do_align (psize, size);
618 data = GNUNET_malloc_large (size);
621 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
625 GNUNET_free_non_null (sizes);
626 GNUNET_free_non_null (perm);
627 GNUNET_free_non_null (bes);
628 return GNUNET_SYSERR;
632 GNUNET_DIRECTORY_MAGIC,
633 strlen (GNUNET_DIRECTORY_MAGIC));
634 off = strlen (GNUNET_DIRECTORY_MAGIC);
636 sptr = &data[off + sizeof (uint32_t)];
638 GNUNET_CONTAINER_meta_data_serialize (bld->meta,
640 size - off - sizeof (uint32_t),
641 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
642 GNUNET_assert (ret != -1);
644 GNUNET_memcpy (&data[off],
647 off += sizeof (uint32_t) + ret;
648 for (j = 0; j < bld->count; j++)
653 off = do_align (psize, off);
654 GNUNET_memcpy (&data[off - sizes[i]], &(bes[i])[1], sizes[i]);
655 GNUNET_free (bes[i]);
657 GNUNET_free_non_null (sizes);
658 GNUNET_free_non_null (perm);
659 GNUNET_free_non_null (bes);
660 GNUNET_assert (off == size);
661 GNUNET_CONTAINER_meta_data_destroy (bld->meta);
667 /* end of fs_directory.c */