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 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 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 'data' does not represent a directory
179 GNUNET_FS_directory_list_contents (size_t size, const void *data,
181 GNUNET_FS_DirectoryEntryProcessor dep,
184 struct GetFullDataClosure full_data;
185 const char *cdata = data;
191 struct GNUNET_FS_Uri *uri;
192 struct GNUNET_CONTAINER_MetaData *md;
196 ((size < 8 + sizeof (uint32_t)) ||
197 (0 != memcmp (cdata, GNUNET_FS_DIRECTORY_MAGIC, 8))))
198 return GNUNET_SYSERR;
202 memcpy (&mdSize, &cdata[8], sizeof (uint32_t));
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 ! */
218 dep (dep_cls, NULL, NULL, md, 0, NULL);
219 GNUNET_CONTAINER_meta_data_destroy (md);
220 pos = 8 + sizeof (uint32_t) + mdSize;
224 /* find end of URI */
225 if (cdata[pos] == '\0')
227 /* URI is never empty, must be end of block,
228 * skip to next alignment */
229 align = ((pos / DBLOCK_SIZE) + 1) * DBLOCK_SIZE;
232 /* if we were already aligned, still skip a block! */
233 align += DBLOCK_SIZE;
238 /* malformed - or partial download... */
243 while ((epos < size) && (cdata[epos] != '\0'))
246 return GNUNET_NO; /* malformed - or partial download */
248 uri = GNUNET_FS_uri_parse (&cdata[pos], &emsg);
253 pos--; /* go back to '\0' to force going to next alignment */
256 if (GNUNET_FS_uri_test_ksk (uri))
258 GNUNET_FS_uri_destroy (uri);
260 return GNUNET_NO; /* illegal in directory! */
263 memcpy (&mdSize, &cdata[pos], sizeof (uint32_t));
264 mdSize = ntohl (mdSize);
265 pos += sizeof (uint32_t);
266 if (pos + mdSize > size)
268 GNUNET_FS_uri_destroy (uri);
269 return GNUNET_NO; /* malformed - or partial download */
272 md = GNUNET_CONTAINER_meta_data_deserialize (&cdata[pos], mdSize);
275 GNUNET_FS_uri_destroy (uri);
277 return GNUNET_NO; /* malformed ! */
281 GNUNET_CONTAINER_meta_data_get_by_type (md,
282 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
284 full_data.data = NULL;
285 GNUNET_CONTAINER_meta_data_iterate (md, &find_full_data, &full_data);
288 dep (dep_cls, filename, uri, md, full_data.size, full_data.data);
290 GNUNET_free_non_null (full_data.data);
291 GNUNET_free_non_null (filename);
292 GNUNET_CONTAINER_meta_data_destroy (md);
293 GNUNET_FS_uri_destroy (uri);
299 * Entries in the directory (builder).
304 * This is a linked list.
306 struct BuilderEntry *next;
309 * Length of this entry.
315 * Internal state of a directory builder.
317 struct GNUNET_FS_DirectoryBuilder
320 * Meta-data for the directory itself.
322 struct GNUNET_CONTAINER_MetaData *meta;
325 * Head of linked list of entries.
327 struct BuilderEntry *head;
330 * Number of entires in the directory.
337 * Create a directory builder.
339 * @param mdir metadata for the directory
341 struct GNUNET_FS_DirectoryBuilder *
342 GNUNET_FS_directory_builder_create (const struct GNUNET_CONTAINER_MetaData
345 struct GNUNET_FS_DirectoryBuilder *ret;
347 ret = GNUNET_new (struct GNUNET_FS_DirectoryBuilder);
349 ret->meta = GNUNET_CONTAINER_meta_data_duplicate (mdir);
351 ret->meta = GNUNET_CONTAINER_meta_data_create ();
352 GNUNET_FS_meta_data_make_directory (ret->meta);
358 * Add an entry to a directory.
360 * @param bld directory to extend
361 * @param uri uri of the entry (must not be a KSK)
362 * @param md metadata of the entry
363 * @param data raw data of the entry, can be NULL, otherwise
364 * data must point to exactly the number of bytes specified
365 * by the uri which must be of type LOC or CHK
368 GNUNET_FS_directory_builder_add (struct GNUNET_FS_DirectoryBuilder *bld,
369 const struct GNUNET_FS_Uri *uri,
370 const struct GNUNET_CONTAINER_MetaData *md,
373 struct GNUNET_FS_Uri *curi;
374 struct BuilderEntry *e;
384 struct GNUNET_CONTAINER_MetaData *meta;
385 const struct GNUNET_CONTAINER_MetaData *meta_use;
387 GNUNET_assert (!GNUNET_FS_uri_test_ksk (uri));
390 GNUNET_assert (!GNUNET_FS_uri_test_sks (uri));
391 if (GNUNET_FS_uri_test_chk (uri))
393 fsize = GNUNET_FS_uri_chk_get_file_size (uri);
397 curi = GNUNET_FS_uri_loc_get_uri (uri);
398 GNUNET_assert (NULL != curi);
399 fsize = GNUNET_FS_uri_chk_get_file_size (curi);
400 GNUNET_FS_uri_destroy (curi);
405 fsize = 0; /* not given */
407 if (fsize > MAX_INLINE_SIZE)
408 fsize = 0; /* too large */
409 uris = GNUNET_FS_uri_to_string (uri);
410 slen = strlen (uris) + 1;
411 mds = GNUNET_CONTAINER_meta_data_get_serialized_size (md);
416 meta = GNUNET_CONTAINER_meta_data_duplicate (md);
417 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>",
418 EXTRACTOR_METATYPE_GNUNET_FULL_DATA,
419 EXTRACTOR_METAFORMAT_BINARY, NULL, data,
421 mdxs = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
422 if ((slen + sizeof (uint32_t) + mdxs - 1) / DBLOCK_SIZE ==
423 (slen + sizeof (uint32_t) + mds - 1) / DBLOCK_SIZE)
425 /* adding full data would not cause us to cross
426 * additional blocks, so add it! */
432 if (mds > GNUNET_MAX_MALLOC_CHECKED / 2)
433 mds = GNUNET_MAX_MALLOC_CHECKED / 2;
434 e = GNUNET_malloc (sizeof (struct BuilderEntry) + slen + mds +
436 ser = (char *) &e[1];
437 memcpy (ser, uris, slen);
439 sptr = &ser[slen + sizeof (uint32_t)];
441 GNUNET_CONTAINER_meta_data_serialize (meta_use, &sptr, mds,
442 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
444 GNUNET_CONTAINER_meta_data_destroy (meta);
450 memcpy (&ser[slen], &big, sizeof (uint32_t));
451 e->len = slen + sizeof (uint32_t) + mds;
459 * Given the start and end position of a block of
460 * data, return the end position of that data
461 * after alignment to the DBLOCK_SIZE.
464 do_align (size_t start_position, size_t end_position)
468 align = (end_position / DBLOCK_SIZE) * DBLOCK_SIZE;
469 if ((start_position < align) && (end_position > align))
470 return align + end_position - start_position;
476 * Compute a permuation of the blocks to
477 * minimize the cost of alignment. Greedy packer.
479 * @param start starting position for the first block
480 * @param count size of the two arrays
481 * @param sizes the sizes of the individual blocks
482 * @param perm the permutation of the blocks (updated)
485 block_align (size_t start, unsigned int count, const size_t * sizes,
499 for (i = 0; i < count; i++)
502 badness = 0x7FFFFFFF;
504 for (j = i; j < count; j++)
507 cend = cpos + sizes[cval];
508 if (cpos % DBLOCK_SIZE == 0)
510 /* prefer placing the largest blocks first */
511 cbad = -(cend % DBLOCK_SIZE);
515 if (cpos / DBLOCK_SIZE == cend / DBLOCK_SIZE)
517 /* Data fits into the same block! Prefer small left-overs! */
518 cbad = DBLOCK_SIZE - cend % DBLOCK_SIZE;
522 /* Would have to waste space to re-align, add big factor, this
523 * case is a real loss (proportional to space wasted)! */
524 cbad = DBLOCK_SIZE * (DBLOCK_SIZE - cpos % DBLOCK_SIZE);
533 GNUNET_assert (best != -1);
535 perm[i] = perm[best];
537 cpos += sizes[perm[i]];
538 cpos = do_align (start, cpos);
544 * Finish building the directory. Frees the
545 * builder context and returns the directory
548 * @param bld directory to finish
549 * @param rsize set to the number of bytes needed
550 * @param rdata set to the encoded directory
551 * @return GNUNET_OK on success
554 GNUNET_FS_directory_builder_finish (struct GNUNET_FS_DirectoryBuilder *bld,
555 size_t * rsize, void **rdata)
563 struct BuilderEntry *pos;
564 struct BuilderEntry **bes;
571 size = strlen (GNUNET_DIRECTORY_MAGIC) + sizeof (uint32_t);
572 size += GNUNET_CONTAINER_meta_data_get_serialized_size (bld->meta);
578 sizes = GNUNET_malloc (bld->count * sizeof (size_t));
579 perm = GNUNET_malloc (bld->count * sizeof (unsigned int));
580 bes = GNUNET_malloc (bld->count * sizeof (struct BuilderEntry *));
582 for (i = 0; i < bld->count; i++)
589 block_align (size, bld->count, sizes, perm);
590 /* compute final size with alignment */
591 for (i = 0; i < bld->count; i++)
594 size += sizes[perm[i]];
595 size = do_align (psize, size);
599 data = GNUNET_malloc_large (size);
602 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
605 GNUNET_free_non_null (sizes);
606 GNUNET_free_non_null (perm);
607 GNUNET_free_non_null (bes);
608 return GNUNET_SYSERR;
611 memcpy (data, GNUNET_DIRECTORY_MAGIC, strlen (GNUNET_DIRECTORY_MAGIC));
612 off = strlen (GNUNET_DIRECTORY_MAGIC);
614 sptr = &data[off + sizeof (uint32_t)];
616 GNUNET_CONTAINER_meta_data_serialize (bld->meta, &sptr,
617 size - off - sizeof (uint32_t),
618 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
619 GNUNET_assert (ret != -1);
621 memcpy (&data[off], &big, sizeof (uint32_t));
622 off += sizeof (uint32_t) + ret;
623 for (j = 0; j < bld->count; j++)
628 off = do_align (psize, off);
629 memcpy (&data[off - sizes[i]], &(bes[i])[1], sizes[i]);
630 GNUNET_free (bes[i]);
632 GNUNET_free_non_null (sizes);
633 GNUNET_free_non_null (perm);
634 GNUNET_free_non_null (bes);
635 GNUNET_assert (off == size);
636 GNUNET_CONTAINER_meta_data_destroy (bld->meta);
642 /* end of fs_directory.c */