2 This file is part of GNUnet.
3 (C) 2003, 2004, 2006, 2009 Christian Grothoff (and other contributing authors)
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 2, 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file fs/fs_directory.c
23 * @brief Helper functions for building directories.
24 * @author Christian Grothoff
27 * - add support for embedded file data (use padding room!)
28 * - modify directory builder API to support incremental
29 * generation of directories (to allow directories that
30 * would not fit into memory to be created)
31 * - modify directory processor API to support incremental
32 * iteration over FULL directories (without missing entries)
33 * to allow access to directories that do not fit entirely
37 #include "gnunet_fs_service.h"
40 #ifndef EXTRACTOR_GNUNET_FULL_DATA
41 #define EXTRACTOR_GNUNET_FULL_DATA 137
45 * Does the meta-data claim that this is a directory?
46 * Checks if the mime-type is that of a GNUnet directory.
48 * @return GNUNET_YES if it is, GNUNET_NO if it is not, GNUNET_SYSERR if
49 * we have no mime-type information (treat as 'GNUNET_NO')
52 GNUNET_FS_meta_data_test_for_directory (const struct GNUNET_CONTAINER_MetaData *md)
57 mime = GNUNET_CONTAINER_meta_data_get_by_type (md, EXTRACTOR_MIMETYPE);
60 ret = (0 == strcmp (mime, GNUNET_FS_DIRECTORY_MIME)) ? GNUNET_YES : GNUNET_NO;
67 * Set the MIMETYPE information for the given
68 * metadata to "application/gnunet-directory".
70 * @param md metadata to add mimetype to
73 GNUNET_FS_meta_data_make_directory (struct GNUNET_CONTAINER_MetaData *md)
77 mime = GNUNET_CONTAINER_meta_data_get_by_type (md, EXTRACTOR_MIMETYPE);
80 GNUNET_break (0 == strcmp (mime,
81 GNUNET_FS_DIRECTORY_MIME));
85 GNUNET_CONTAINER_meta_data_insert (md,
87 GNUNET_FS_DIRECTORY_MIME);
92 * Iterate over all entries in a directory. Note that directories
93 * are structured such that it is possible to iterate over the
94 * individual blocks as well as over the entire directory. Thus
95 * a client can call this function on the buffer in the
96 * GNUNET_FS_ProgressCallback. Also, directories can optionally
97 * include the contents of (small) files embedded in the directory
98 * itself; for those files, the processor may be given the
99 * contents of the file directly by this function.
102 * Note that this function maybe called on parts of directories. Thus
103 * parser errors should not be reported _at all_ (with GNUNET_break).
104 * Still, if some entries can be recovered despite these parsing
105 * errors, the function should try to do this.
107 * @param size number of bytes in data
108 * @param data pointer to the beginning of the directory
109 * @param offset offset of data in the directory
110 * @param dep function to call on each entry
111 * @param dep_cls closure for dep
114 GNUNET_FS_directory_list_contents (size_t size,
117 GNUNET_FS_DirectoryEntryProcessor dep,
120 const char *cdata = data;
126 struct GNUNET_FS_Uri *uri;
127 struct GNUNET_CONTAINER_MetaData *md;
132 (size >= 8 + sizeof (uint32_t)) &&
133 (0 == memcmp (cdata, GNUNET_FS_DIRECTORY_MAGIC, 8)) )
135 memcpy (&mdSize, &cdata[8], sizeof (uint32_t));
136 mdSize = ntohl (mdSize);
137 if (mdSize > size - 8 - sizeof (uint32_t))
140 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
141 _("Not a GNUnet directory.\n"));
144 md = GNUNET_CONTAINER_meta_data_deserialize (&cdata[8 +
150 return; /* malformed ! */
158 GNUNET_CONTAINER_meta_data_destroy (md);
159 pos = 8 + sizeof (uint32_t) + mdSize;
163 /* find end of URI */
164 if (cdata[pos] == '\0')
166 /* URI is never empty, must be end of block,
167 skip to next alignment */
169 ((pos / GNUNET_FS_DBLOCK_SIZE) + 1) * GNUNET_FS_DBLOCK_SIZE;
172 /* if we were already aligned, still skip a block! */
173 align += GNUNET_FS_DBLOCK_SIZE;
178 /* malformed - or partial download... */
183 while ((epos < size) && (cdata[epos] != '\0'))
186 return; /* malformed - or partial download */
188 uri = GNUNET_FS_uri_parse (&cdata[pos], &emsg);
193 pos--; /* go back to '\0' to force going to next alignment */
196 if (GNUNET_FS_uri_test_ksk (uri))
198 GNUNET_FS_uri_destroy (uri);
200 return; /* illegal in directory! */
203 memcpy (&mdSize, &cdata[pos], sizeof (uint32_t));
204 mdSize = ntohl (mdSize);
205 pos += sizeof (uint32_t);
206 if (pos + mdSize > size)
208 GNUNET_FS_uri_destroy (uri);
209 return; /* malformed - or partial download */
212 md = GNUNET_CONTAINER_meta_data_deserialize (&cdata[pos], mdSize);
215 GNUNET_FS_uri_destroy (uri);
217 return; /* malformed ! */
220 // EXTRACTOR_GNUNET_FULL_DATA
221 /* FIXME: add support for embedded data */
222 filename = GNUNET_CONTAINER_meta_data_get_by_type (md,
231 GNUNET_free_non_null (filename);
232 GNUNET_CONTAINER_meta_data_destroy (md);
233 GNUNET_FS_uri_destroy (uri);
238 * Entries in the directory (builder).
243 * This is a linked list.
245 struct BuilderEntry *next;
248 * Length of this entry.
254 * Internal state of a directory builder.
256 struct GNUNET_FS_DirectoryBuilder
259 * Meta-data for the directory itself.
261 struct GNUNET_CONTAINER_MetaData *meta;
264 * Head of linked list of entries.
266 struct BuilderEntry *head;
269 * Number of entires in the directory.
276 * Create a directory builder.
278 * @param mdir metadata for the directory
280 struct GNUNET_FS_DirectoryBuilder *
281 GNUNET_FS_directory_builder_create (const struct GNUNET_CONTAINER_MetaData *mdir)
283 struct GNUNET_FS_DirectoryBuilder *ret;
285 ret = GNUNET_malloc(sizeof(struct GNUNET_FS_DirectoryBuilder));
286 ret->meta = GNUNET_CONTAINER_meta_data_duplicate (mdir);
287 GNUNET_FS_meta_data_make_directory (ret->meta);
293 * Add an entry to a directory.
295 * @param bld directory to extend
296 * @param uri uri of the entry (must not be a KSK)
297 * @param md metadata of the entry
298 * @param data raw data of the entry, can be NULL, otherwise
299 * data must point to exactly the number of bytes specified
300 * by the uri which must be of type LOC or CHK
303 GNUNET_FS_directory_builder_add (struct GNUNET_FS_DirectoryBuilder *bld,
304 const struct GNUNET_FS_Uri *uri,
305 const struct GNUNET_CONTAINER_MetaData *md,
308 struct BuilderEntry *e;
316 struct GNUNET_CONTAINER_MetaData *meta;
317 const struct GNUNET_CONTAINER_MetaData *meta_use;
319 GNUNET_assert (! GNUNET_FS_uri_ksk_test (uri));
321 if (GNUNET_FS_uri_chk_test (uri))
322 fsize = GNUNET_FS_uri_chk_get_size (uri);
324 fsize = GNUNET_FS_uri_chk_get_size (GNUNET_FS_uri_loc_get_uri (uri));
326 fsize = 0; /* not given */
327 if (fsize > GNUNET_FS_MAX_INLINE_SIZE)
328 fsize = 0; /* too large */
329 if (memchr (data, fsize, '\0')) // FIXME: check memchr args!
330 fsize = 0; /* must not have 0's in data! */
331 uris = GNUNET_FS_uri_to_string (uri);
332 slen = strlen (uris) + 1;
334 GNUNET_CONTAINER_meta_data_get_serialized_size (md,
335 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
340 meta = GNUNET_CONTAINER_meta_data_duplicate (md);
341 GNUNET_CONTAINER_meta_data_insert (meta,
342 EXTRACTOR_GNUNET_FULL_DATA,
345 GNUNET_CONTAINER_meta_data_get_serialized_size (meta,
346 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
347 if ( (slen + sizeof (uint32_t) + mdxs - 1) / GNUNET_FS_DBLOCK_SIZE ==
348 (slen + sizeof (uint32_t) + mds - 1) / GNUNET_FS_DBLOCK_SIZE)
350 /* adding full data would not cause us to cross
351 additional blocks, so add it! */
357 if (mds > GNUNET_MAX_MALLOC_CHECKED / 2)
358 mds = GNUNET_MAX_MALLOC_CHECKED / 2;
359 e = GNUNET_malloc (sizeof(struct BuilderEntry) +
360 slen + mds + sizeof (uint32_t));
362 memcpy (ser, uris, slen);
364 ret = GNUNET_CONTAINER_meta_data_serialize (meta_use,
365 &ser[slen + sizeof(uint32_t)],
367 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
369 GNUNET_CONTAINER_meta_data_destroy (meta);
375 memcpy (&ser[slen], &big, sizeof (uint32_t));
376 e->len = slen + sizeof (uint32_t) + mds;
384 * Given the start and end position of a block of
385 * data, return the end position of that data
386 * after alignment to the GNUNET_FS_DBLOCK_SIZE.
389 do_align (size_t start_position,
394 align = (end_position / GNUNET_FS_DBLOCK_SIZE) * GNUNET_FS_DBLOCK_SIZE;
395 if ((start_position < align) && (end_position > align))
396 return align + end_position - start_position;
402 * Compute a permuation of the blocks to
403 * minimize the cost of alignment. Greedy packer.
405 * @param start starting position for the first block
406 * @param count size of the two arrays
407 * @param sizes the sizes of the individual blocks
408 * @param perm the permutation of the blocks (updated)
411 block_align (size_t start,
427 for (i = 0; i < count; i++)
430 badness = 0x7FFFFFFF;
432 for (j = i; j < count; j++)
435 cend = cpos + sizes[cval];
436 if (cpos % GNUNET_FS_DBLOCK_SIZE == 0)
438 /* prefer placing the largest blocks first */
439 cbad = -(cend % GNUNET_FS_DBLOCK_SIZE);
443 if (cpos / GNUNET_FS_DBLOCK_SIZE ==
444 cend / GNUNET_FS_DBLOCK_SIZE)
446 /* Data fits into the same block! Prefer small left-overs! */
448 GNUNET_FS_DBLOCK_SIZE - cend % GNUNET_FS_DBLOCK_SIZE;
452 /* Would have to waste space to re-align, add big factor, this
453 case is a real loss (proportional to space wasted)! */
455 GNUNET_FS_DBLOCK_SIZE * (GNUNET_FS_DBLOCK_SIZE -
457 GNUNET_FS_DBLOCK_SIZE);
467 perm[i] = perm[best];
469 cpos += sizes[perm[i]];
470 cpos = do_align (start, cpos);
476 * Finish building the directory. Frees the
477 * builder context and returns the directory
480 * @param bld directory to finish
481 * @param rsize set to the number of bytes needed
482 * @param rdata set to the encoded directory
485 GNUNET_FS_directory_builder_finish (struct GNUNET_FS_DirectoryBuilder *bld,
494 struct BuilderEntry *pos;
495 struct BuilderEntry **bes;
502 size = 8 + sizeof (uint32_t);
503 size += GNUNET_meta_data_get_serialized_size (bld->meta,
504 GNUNET_SERIALIZE_FULL);
507 sizes = GNUNET_malloc (bld->count * sizeof (size_t));
508 perm = GNUNET_malloc (bld->count * sizeof (unsigned int));
509 bes = GNUNET_malloc (bld->count * sizeof (struct BuilderEntry *));
511 for (i = 0; i < bld->count; i++)
515 sizes[i] = pos->size;
524 /* compute final size with alignment */
525 for (i = 0; i < bld->count; i++)
528 size += sizes[perm[i]];
529 size = do_align (psize, size);
532 data = GNUNET_malloc (size);
534 memcpy (data, GNUNET_DIRECTORY_MAGIC, 8);
537 ret = GNUNET_CONTAINER_meta_data_serialize (meta,
540 size - pos - sizeof (uint32_t),
541 GNUNET_SERIALIZE_FULL);
542 GNUNET_assert (ret != -1);
544 memcpy (&(*data)[8], &big, sizeof (uint32_t));
545 pos += sizeof (uint32_t) + ret;
546 for (j = 0; j < count; j++)
551 pos = do_align (psize, pos);
552 memcpy (&data[pos - sizes[i]],
555 GNUNET_free (bes[i]);
560 GNUNET_assert (pos == size);
561 GNUNET_CONTAINER_meta_data_destroy (bld->meta);
566 /* end of fs_directory.c */