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 * - 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 == strcmp (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)
81 mime = GNUNET_CONTAINER_meta_data_get_by_type (md, EXTRACTOR_METATYPE_MIMETYPE);
84 GNUNET_break (0 == strcmp (mime,
85 GNUNET_FS_DIRECTORY_MIME));
89 GNUNET_CONTAINER_meta_data_insert (md,
91 EXTRACTOR_METATYPE_MIMETYPE,
92 EXTRACTOR_METAFORMAT_UTF8,
94 GNUNET_FS_DIRECTORY_MIME,
95 strlen (GNUNET_FS_DIRECTORY_MIME)+1);
100 * Closure for 'find_full_data'.
102 struct GetFullDataClosure
106 * Extracted binary meta data.
111 * Number of bytes stored in data.
118 * Type of a function that libextractor calls for each
119 * meta data item found.
121 * @param cls closure (user-defined)
122 * @param plugin_name name of the plugin that produced this value;
123 * special values can be used (i.e. '<zlib>' for zlib being
124 * used in the main libextractor library and yielding
126 * @param type libextractor-type describing the meta data
127 * @param format basic format information about data
128 * @param data_mime_type mime-type of data (not of the original file);
129 * can be NULL (if mime-type is not known)
130 * @param data actual meta-data found
131 * @param data_len number of bytes in data
132 * @return 0 to continue extracting, 1 to abort
135 find_full_data (void *cls,
136 const char *plugin_name,
137 enum EXTRACTOR_MetaType type,
138 enum EXTRACTOR_MetaFormat format,
139 const char *data_mime_type,
143 struct GetFullDataClosure *gfdc = cls;
145 if (type == EXTRACTOR_METATYPE_GNUNET_FULL_DATA)
147 gfdc->size = data_len;
150 gfdc->data = GNUNET_malloc (data_len);
162 * Iterate over all entries in a directory. Note that directories
163 * are structured such that it is possible to iterate over the
164 * individual blocks as well as over the entire directory. Thus
165 * a client can call this function on the buffer in the
166 * GNUNET_FS_ProgressCallback. Also, directories can optionally
167 * include the contents of (small) files embedded in the directory
168 * itself; for those files, the processor may be given the
169 * contents of the file directly by this function.
172 * Note that this function maybe called on parts of directories. Thus
173 * parser errors should not be reported _at all_ (with GNUNET_break).
174 * Still, if some entries can be recovered despite these parsing
175 * errors, the function should try to do this.
177 * @param size number of bytes in data
178 * @param data pointer to the beginning of the directory
179 * @param offset offset of data in the directory
180 * @param dep function to call on each entry
181 * @param dep_cls closure for dep
184 GNUNET_FS_directory_list_contents (size_t size,
187 GNUNET_FS_DirectoryEntryProcessor dep,
190 struct GetFullDataClosure full_data;
191 const char *cdata = data;
197 struct GNUNET_FS_Uri *uri;
198 struct GNUNET_CONTAINER_MetaData *md;
203 (size >= 8 + sizeof (uint32_t)) &&
204 (0 == memcmp (cdata, GNUNET_FS_DIRECTORY_MAGIC, 8)) )
206 memcpy (&mdSize, &cdata[8], sizeof (uint32_t));
207 mdSize = ntohl (mdSize);
208 if (mdSize > size - 8 - sizeof (uint32_t))
211 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
212 _("Not a GNUnet directory.\n"));
215 md = GNUNET_CONTAINER_meta_data_deserialize (&cdata[8 +
221 return; /* 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 */
240 ((pos / DBLOCK_SIZE) + 1) * DBLOCK_SIZE;
243 /* if we were already aligned, still skip a block! */
244 align += DBLOCK_SIZE;
249 /* malformed - or partial download... */
254 while ((epos < size) && (cdata[epos] != '\0'))
257 return; /* malformed - or partial download */
259 uri = GNUNET_FS_uri_parse (&cdata[pos], &emsg);
264 pos--; /* go back to '\0' to force going to next alignment */
267 if (GNUNET_FS_uri_test_ksk (uri))
269 GNUNET_FS_uri_destroy (uri);
271 return; /* illegal in directory! */
274 memcpy (&mdSize, &cdata[pos], sizeof (uint32_t));
275 mdSize = ntohl (mdSize);
276 pos += sizeof (uint32_t);
277 if (pos + mdSize > size)
279 GNUNET_FS_uri_destroy (uri);
280 return; /* malformed - or partial download */
283 md = GNUNET_CONTAINER_meta_data_deserialize (&cdata[pos], mdSize);
286 GNUNET_FS_uri_destroy (uri);
288 return; /* malformed ! */
291 filename = GNUNET_CONTAINER_meta_data_get_by_type (md,
292 EXTRACTOR_METATYPE_FILENAME);
294 full_data.data = NULL;
295 GNUNET_CONTAINER_meta_data_iterate (md,
307 GNUNET_free_non_null (full_data.data);
308 GNUNET_free_non_null (filename);
309 GNUNET_CONTAINER_meta_data_destroy (md);
310 GNUNET_FS_uri_destroy (uri);
315 * Entries in the directory (builder).
320 * This is a linked list.
322 struct BuilderEntry *next;
325 * Length of this entry.
331 * Internal state of a directory builder.
333 struct GNUNET_FS_DirectoryBuilder
336 * Meta-data for the directory itself.
338 struct GNUNET_CONTAINER_MetaData *meta;
341 * Head of linked list of entries.
343 struct BuilderEntry *head;
346 * Number of entires in the directory.
353 * Create a directory builder.
355 * @param mdir metadata for the directory
357 struct GNUNET_FS_DirectoryBuilder *
358 GNUNET_FS_directory_builder_create (const struct GNUNET_CONTAINER_MetaData *mdir)
360 struct GNUNET_FS_DirectoryBuilder *ret;
362 ret = GNUNET_malloc(sizeof(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));
404 if (GNUNET_FS_uri_test_chk (uri))
405 fsize = GNUNET_FS_uri_chk_get_file_size (uri);
408 curi = GNUNET_FS_uri_loc_get_uri (uri);
409 fsize = GNUNET_FS_uri_chk_get_file_size (curi);
410 GNUNET_FS_uri_destroy (curi);
413 fsize = 0; /* not given */
414 if (fsize > MAX_INLINE_SIZE)
415 fsize = 0; /* too large */
416 uris = GNUNET_FS_uri_to_string (uri);
417 slen = strlen (uris) + 1;
419 GNUNET_CONTAINER_meta_data_get_serialized_size (md);
424 meta = GNUNET_CONTAINER_meta_data_duplicate (md);
425 GNUNET_CONTAINER_meta_data_insert (meta,
427 EXTRACTOR_METATYPE_GNUNET_FULL_DATA,
428 EXTRACTOR_METAFORMAT_BINARY,
433 GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
434 if ( (slen + sizeof (uint32_t) + mdxs - 1) / DBLOCK_SIZE ==
435 (slen + sizeof (uint32_t) + mds - 1) / DBLOCK_SIZE)
437 /* adding full data would not cause us to cross
438 additional blocks, so add it! */
444 if (mds > GNUNET_MAX_MALLOC_CHECKED / 2)
445 mds = GNUNET_MAX_MALLOC_CHECKED / 2;
446 e = GNUNET_malloc (sizeof(struct BuilderEntry) +
447 slen + mds + sizeof (uint32_t));
449 memcpy (ser, uris, slen);
451 sptr = &ser[slen + sizeof(uint32_t)];
452 ret = GNUNET_CONTAINER_meta_data_serialize (meta_use,
455 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
457 GNUNET_CONTAINER_meta_data_destroy (meta);
463 memcpy (&ser[slen], &big, sizeof (uint32_t));
464 e->len = slen + sizeof (uint32_t) + mds;
472 * Given the start and end position of a block of
473 * data, return the end position of that data
474 * after alignment to the DBLOCK_SIZE.
477 do_align (size_t start_position,
482 align = (end_position / DBLOCK_SIZE) * DBLOCK_SIZE;
483 if ((start_position < align) && (end_position > align))
484 return align + end_position - start_position;
490 * Compute a permuation of the blocks to
491 * minimize the cost of alignment. Greedy packer.
493 * @param start starting position for the first block
494 * @param count size of the two arrays
495 * @param sizes the sizes of the individual blocks
496 * @param perm the permutation of the blocks (updated)
499 block_align (size_t start,
515 for (i = 0; i < count; i++)
518 badness = 0x7FFFFFFF;
520 for (j = i; j < count; j++)
523 cend = cpos + sizes[cval];
524 if (cpos % DBLOCK_SIZE == 0)
526 /* prefer placing the largest blocks first */
527 cbad = -(cend % DBLOCK_SIZE);
531 if (cpos / DBLOCK_SIZE ==
534 /* Data fits into the same block! Prefer small left-overs! */
536 DBLOCK_SIZE - cend % DBLOCK_SIZE;
540 /* Would have to waste space to re-align, add big factor, this
541 case is a real loss (proportional to space wasted)! */
543 DBLOCK_SIZE * (DBLOCK_SIZE -
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 = 8 + sizeof (uint32_t);
593 size += GNUNET_CONTAINER_meta_data_get_serialized_size (bld->meta);
599 sizes = GNUNET_malloc (bld->count * sizeof (size_t));
600 perm = GNUNET_malloc (bld->count * sizeof (unsigned int));
601 bes = GNUNET_malloc (bld->count * sizeof (struct BuilderEntry *));
603 for (i = 0; i < bld->count; i++)
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 return GNUNET_SYSERR;
633 memcpy (data, GNUNET_DIRECTORY_MAGIC, 8);
636 sptr = &data[off + sizeof (uint32_t)];
637 ret = GNUNET_CONTAINER_meta_data_serialize (bld->meta,
639 size - off - sizeof (uint32_t),
640 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
641 GNUNET_assert (ret != -1);
643 memcpy (&data[8], &big, sizeof (uint32_t));
644 off += sizeof (uint32_t) + ret;
645 for (j = 0; j < bld->count; j++)
650 off = do_align (psize, off);
651 memcpy (&data[off - sizes[i]],
654 GNUNET_free (bes[i]);
656 GNUNET_free_non_null (sizes);
657 GNUNET_free_non_null (perm);
658 GNUNET_free_non_null (bes);
659 GNUNET_assert (off == size);
660 GNUNET_CONTAINER_meta_data_destroy (bld->meta);
666 /* end of fs_directory.c */