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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file fs/fs_directory.c
23 * @brief Helper functions for building directories.
24 * @author Christian Grothoff
27 * - modify directory builder API to support incremental
28 * generation of directories (to allow directories that
29 * would not fit into memory to be created)
30 * - modify directory processor API to support incremental
31 * iteration over FULL directories (without missing entries)
32 * to allow access to directories that do not fit entirely
36 #include "gnunet_fs_service.h"
40 * String that is used to indicate that a file
41 * is a GNUnet directory.
43 #define GNUNET_DIRECTORY_MAGIC "\211GND\r\n\032\n"
47 * Does the meta-data claim that this is a directory?
48 * Checks if the mime-type is that of a GNUnet directory.
50 * @return #GNUNET_YES if it is, #GNUNET_NO if it is not, #GNUNET_SYSERR if
51 * we have no mime-type information (treat as #GNUNET_NO)
54 GNUNET_FS_meta_data_test_for_directory (const struct GNUNET_CONTAINER_MetaData *md)
61 mime = GNUNET_CONTAINER_meta_data_get_by_type (md, EXTRACTOR_METATYPE_MIMETYPE);
64 ret = (0 == strcasecmp (mime, GNUNET_FS_DIRECTORY_MIME)) ? GNUNET_YES : GNUNET_NO;
71 * Set the MIMETYPE information for the given
72 * metadata to "application/gnunet-directory".
74 * @param md metadata to add mimetype to
77 GNUNET_FS_meta_data_make_directory (struct GNUNET_CONTAINER_MetaData *md)
82 GNUNET_CONTAINER_meta_data_get_by_type (md, EXTRACTOR_METATYPE_MIMETYPE);
85 GNUNET_break (0 == strcmp (mime, GNUNET_FS_DIRECTORY_MIME));
89 GNUNET_CONTAINER_meta_data_insert (md, "<gnunet>",
90 EXTRACTOR_METATYPE_MIMETYPE,
91 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
92 GNUNET_FS_DIRECTORY_MIME,
93 strlen (GNUNET_FS_DIRECTORY_MIME) + 1);
98 * Closure for 'find_full_data'.
100 struct GetFullDataClosure
104 * Extracted binary meta data.
109 * Number of bytes stored in data.
116 * Type of a function that libextractor calls for each
117 * meta data item found.
119 * @param cls closure (user-defined)
120 * @param plugin_name name of the plugin that produced this value;
121 * special values can be used (i.e. '<zlib>' for zlib being
122 * used in the main libextractor library and yielding
124 * @param type libextractor-type describing the meta data
125 * @param format basic format information about data
126 * @param data_mime_type mime-type of data (not of the original file);
127 * can be NULL (if mime-type is not known)
128 * @param data actual meta-data found
129 * @param data_len number of bytes in data
130 * @return 0 to continue extracting, 1 to abort
133 find_full_data (void *cls, const char *plugin_name,
134 enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat format,
135 const char *data_mime_type, const char *data, size_t data_len)
137 struct GetFullDataClosure *gfdc = cls;
139 if (type == EXTRACTOR_METATYPE_GNUNET_FULL_DATA)
141 gfdc->size = data_len;
144 gfdc->data = GNUNET_malloc (data_len);
145 GNUNET_memcpy (gfdc->data, data, data_len);
154 * Iterate over all entries in a directory. Note that directories
155 * are structured such that it is possible to iterate over the
156 * individual blocks as well as over the entire directory. Thus
157 * a client can call this function on the buffer in the
158 * GNUNET_FS_ProgressCallback. Also, directories can optionally
159 * include the contents of (small) files embedded in the directory
160 * itself; for those files, the processor may be given the
161 * contents of the file directly by this function.
164 * Note that this function maybe called on parts of directories. Thus
165 * parser errors should not be reported _at all_ (with GNUNET_break).
166 * Still, if some entries can be recovered despite these parsing
167 * errors, the function should try to do this.
169 * @param size number of bytes in data
170 * @param data pointer to the beginning of the directory
171 * @param offset offset of data in the directory
172 * @param dep function to call on each entry
173 * @param dep_cls closure for @a dep
174 * @return #GNUNET_OK if this could be a block in a directory,
175 * #GNUNET_NO if this could be part of a directory (but not 100% OK)
176 * #GNUNET_SYSERR if @a data does not represent a directory
179 GNUNET_FS_directory_list_contents (size_t size,
182 GNUNET_FS_DirectoryEntryProcessor dep,
185 struct GetFullDataClosure full_data;
186 const char *cdata = data;
192 struct GNUNET_FS_Uri *uri;
193 struct GNUNET_CONTAINER_MetaData *md;
197 ((size < 8 + sizeof (uint32_t)) ||
199 GNUNET_FS_DIRECTORY_MAGIC,
201 return GNUNET_SYSERR;
205 GNUNET_memcpy (&mdSize,
208 mdSize = ntohl (mdSize);
209 if (mdSize > size - 8 - sizeof (uint32_t))
212 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
213 _("MAGIC mismatch. This is not a GNUnet directory.\n"));
214 return GNUNET_SYSERR;
216 md = GNUNET_CONTAINER_meta_data_deserialize (&cdata[8 + sizeof (uint32_t)],
221 return GNUNET_SYSERR; /* malformed ! */
229 GNUNET_CONTAINER_meta_data_destroy (md);
230 pos = 8 + sizeof (uint32_t) + mdSize;
234 /* find end of URI */
235 if (cdata[pos] == '\0')
237 /* URI is never empty, must be end of block,
238 * skip to next alignment */
239 align = ((pos / DBLOCK_SIZE) + 1) * DBLOCK_SIZE;
242 /* if we were already aligned, still skip a block! */
243 align += DBLOCK_SIZE;
248 /* malformed - or partial download... */
253 while ((epos < size) && (cdata[epos] != '\0'))
256 return GNUNET_NO; /* malformed - or partial download */
258 uri = GNUNET_FS_uri_parse (&cdata[pos], &emsg);
263 pos--; /* go back to '\0' to force going to next alignment */
266 if (GNUNET_FS_uri_test_ksk (uri))
268 GNUNET_FS_uri_destroy (uri);
270 return GNUNET_NO; /* illegal in directory! */
273 GNUNET_memcpy (&mdSize,
276 mdSize = ntohl (mdSize);
277 pos += sizeof (uint32_t);
278 if (pos + mdSize > size)
280 GNUNET_FS_uri_destroy (uri);
281 return GNUNET_NO; /* malformed - or partial download */
284 md = GNUNET_CONTAINER_meta_data_deserialize (&cdata[pos],
288 GNUNET_FS_uri_destroy (uri);
290 return GNUNET_NO; /* malformed ! */
294 GNUNET_CONTAINER_meta_data_get_by_type (md,
295 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
297 full_data.data = NULL;
298 GNUNET_CONTAINER_meta_data_iterate (md,
310 GNUNET_free_non_null (full_data.data);
311 GNUNET_free_non_null (filename);
312 GNUNET_CONTAINER_meta_data_destroy (md);
313 GNUNET_FS_uri_destroy (uri);
319 * Entries in the directory (builder).
324 * This is a linked list.
326 struct BuilderEntry *next;
329 * Length of this entry.
335 * Internal state of a directory builder.
337 struct GNUNET_FS_DirectoryBuilder
340 * Meta-data for the directory itself.
342 struct GNUNET_CONTAINER_MetaData *meta;
345 * Head of linked list of entries.
347 struct BuilderEntry *head;
350 * Number of entires in the directory.
357 * Create a directory builder.
359 * @param mdir metadata for the directory
361 struct GNUNET_FS_DirectoryBuilder *
362 GNUNET_FS_directory_builder_create (const struct GNUNET_CONTAINER_MetaData
365 struct GNUNET_FS_DirectoryBuilder *ret;
367 ret = GNUNET_new (struct GNUNET_FS_DirectoryBuilder);
369 ret->meta = GNUNET_CONTAINER_meta_data_duplicate (mdir);
371 ret->meta = GNUNET_CONTAINER_meta_data_create ();
372 GNUNET_FS_meta_data_make_directory (ret->meta);
378 * Add an entry to a directory.
380 * @param bld directory to extend
381 * @param uri uri of the entry (must not be a KSK)
382 * @param md metadata of the entry
383 * @param data raw data of the entry, can be NULL, otherwise
384 * data must point to exactly the number of bytes specified
385 * by the uri which must be of type LOC or CHK
388 GNUNET_FS_directory_builder_add (struct GNUNET_FS_DirectoryBuilder *bld,
389 const struct GNUNET_FS_Uri *uri,
390 const struct GNUNET_CONTAINER_MetaData *md,
393 struct GNUNET_FS_Uri *curi;
394 struct BuilderEntry *e;
404 struct GNUNET_CONTAINER_MetaData *meta;
405 const struct GNUNET_CONTAINER_MetaData *meta_use;
407 GNUNET_assert (!GNUNET_FS_uri_test_ksk (uri));
410 GNUNET_assert (!GNUNET_FS_uri_test_sks (uri));
411 if (GNUNET_FS_uri_test_chk (uri))
413 fsize = GNUNET_FS_uri_chk_get_file_size (uri);
417 curi = GNUNET_FS_uri_loc_get_uri (uri);
418 GNUNET_assert (NULL != curi);
419 fsize = GNUNET_FS_uri_chk_get_file_size (curi);
420 GNUNET_FS_uri_destroy (curi);
425 fsize = 0; /* not given */
427 if (fsize > MAX_INLINE_SIZE)
428 fsize = 0; /* too large */
429 uris = GNUNET_FS_uri_to_string (uri);
430 slen = strlen (uris) + 1;
431 mds = GNUNET_CONTAINER_meta_data_get_serialized_size (md);
436 meta = GNUNET_CONTAINER_meta_data_duplicate (md);
437 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>",
438 EXTRACTOR_METATYPE_GNUNET_FULL_DATA,
439 EXTRACTOR_METAFORMAT_BINARY, NULL, data,
441 mdxs = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
442 if ((slen + sizeof (uint32_t) + mdxs - 1) / DBLOCK_SIZE ==
443 (slen + sizeof (uint32_t) + mds - 1) / DBLOCK_SIZE)
445 /* adding full data would not cause us to cross
446 * additional blocks, so add it! */
452 if (mds > GNUNET_MAX_MALLOC_CHECKED / 2)
453 mds = GNUNET_MAX_MALLOC_CHECKED / 2;
454 e = GNUNET_malloc (sizeof (struct BuilderEntry) + slen + mds +
456 ser = (char *) &e[1];
457 GNUNET_memcpy (ser, uris, slen);
459 sptr = &ser[slen + sizeof (uint32_t)];
461 GNUNET_CONTAINER_meta_data_serialize (meta_use, &sptr, mds,
462 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
464 GNUNET_CONTAINER_meta_data_destroy (meta);
470 GNUNET_memcpy (&ser[slen], &big, sizeof (uint32_t));
471 e->len = slen + sizeof (uint32_t) + mds;
479 * Given the start and end position of a block of
480 * data, return the end position of that data
481 * after alignment to the DBLOCK_SIZE.
484 do_align (size_t start_position, size_t end_position)
488 align = (end_position / DBLOCK_SIZE) * DBLOCK_SIZE;
489 if ((start_position < align) && (end_position > align))
490 return align + end_position - start_position;
496 * Compute a permuation of the blocks to
497 * minimize the cost of alignment. Greedy packer.
499 * @param start starting position for the first block
500 * @param count size of the two arrays
501 * @param sizes the sizes of the individual blocks
502 * @param perm the permutation of the blocks (updated)
505 block_align (size_t start, unsigned int count, const size_t * sizes,
519 for (i = 0; i < count; i++)
522 badness = 0x7FFFFFFF;
524 for (j = i; j < count; j++)
527 cend = cpos + sizes[cval];
528 if (cpos % DBLOCK_SIZE == 0)
530 /* prefer placing the largest blocks first */
531 cbad = -(cend % DBLOCK_SIZE);
535 if (cpos / DBLOCK_SIZE == cend / DBLOCK_SIZE)
537 /* Data fits into the same block! Prefer small left-overs! */
538 cbad = DBLOCK_SIZE - cend % DBLOCK_SIZE;
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);
553 GNUNET_assert (best != -1);
555 perm[i] = perm[best];
557 cpos += sizes[perm[i]];
558 cpos = do_align (start, cpos);
564 * Finish building the directory. Frees the
565 * builder context and returns the directory
568 * @param bld directory to finish
569 * @param rsize set to the number of bytes needed
570 * @param rdata set to the encoded directory
571 * @return #GNUNET_OK on success
574 GNUNET_FS_directory_builder_finish (struct GNUNET_FS_DirectoryBuilder *bld,
584 struct BuilderEntry *pos;
585 struct BuilderEntry **bes;
592 size = strlen (GNUNET_DIRECTORY_MAGIC) + sizeof (uint32_t);
593 size += GNUNET_CONTAINER_meta_data_get_serialized_size (bld->meta);
599 sizes = GNUNET_new_array (bld->count,
601 perm = GNUNET_new_array (bld->count,
603 bes = GNUNET_new_array (bld->count,
604 struct BuilderEntry *);
606 for (i = 0; i < bld->count; i++)
613 block_align (size, bld->count, sizes, perm);
614 /* compute final size with alignment */
615 for (i = 0; i < bld->count; i++)
618 size += sizes[perm[i]];
619 size = do_align (psize, size);
623 data = GNUNET_malloc_large (size);
626 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
630 GNUNET_free_non_null (sizes);
631 GNUNET_free_non_null (perm);
632 GNUNET_free_non_null (bes);
633 return GNUNET_SYSERR;
637 GNUNET_DIRECTORY_MAGIC,
638 strlen (GNUNET_DIRECTORY_MAGIC));
639 off = strlen (GNUNET_DIRECTORY_MAGIC);
641 sptr = &data[off + sizeof (uint32_t)];
643 GNUNET_CONTAINER_meta_data_serialize (bld->meta,
645 size - off - sizeof (uint32_t),
646 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
647 GNUNET_assert (ret != -1);
649 GNUNET_memcpy (&data[off],
652 off += sizeof (uint32_t) + ret;
653 for (j = 0; j < bld->count; j++)
658 off = do_align (psize, off);
659 GNUNET_memcpy (&data[off - sizes[i]], &(bes[i])[1], sizes[i]);
660 GNUNET_free (bes[i]);
662 GNUNET_free_non_null (sizes);
663 GNUNET_free_non_null (perm);
664 GNUNET_free_non_null (bes);
665 GNUNET_assert (off == size);
666 GNUNET_CONTAINER_meta_data_destroy (bld->meta);
672 /* end of fs_directory.c */