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 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., 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
63 GNUNET_CONTAINER_meta_data_get_by_type (md, EXTRACTOR_METATYPE_MIMETYPE);
66 ret = (0 == strcmp (mime, GNUNET_FS_DIRECTORY_MIME)) ? GNUNET_YES : GNUNET_NO;
73 * Set the MIMETYPE information for the given
74 * metadata to "application/gnunet-directory".
76 * @param md metadata to add mimetype to
79 GNUNET_FS_meta_data_make_directory (struct GNUNET_CONTAINER_MetaData *md)
84 GNUNET_CONTAINER_meta_data_get_by_type (md, EXTRACTOR_METATYPE_MIMETYPE);
87 GNUNET_break (0 == strcmp (mime, GNUNET_FS_DIRECTORY_MIME));
91 GNUNET_CONTAINER_meta_data_insert (md, "<gnunet>",
92 EXTRACTOR_METATYPE_MIMETYPE,
93 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
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, const char *plugin_name,
136 enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat format,
137 const char *data_mime_type, const char *data, size_t data_len)
139 struct GetFullDataClosure *gfdc = cls;
141 if (type == EXTRACTOR_METATYPE_GNUNET_FULL_DATA)
143 gfdc->size = data_len;
146 gfdc->data = GNUNET_malloc (data_len);
147 memcpy (gfdc->data, data, data_len);
156 * Iterate over all entries in a directory. Note that directories
157 * are structured such that it is possible to iterate over the
158 * individual blocks as well as over the entire directory. Thus
159 * a client can call this function on the buffer in the
160 * GNUNET_FS_ProgressCallback. Also, directories can optionally
161 * include the contents of (small) files embedded in the directory
162 * itself; for those files, the processor may be given the
163 * contents of the file directly by this function.
166 * Note that this function maybe called on parts of directories. Thus
167 * parser errors should not be reported _at all_ (with GNUNET_break).
168 * Still, if some entries can be recovered despite these parsing
169 * errors, the function should try to do this.
171 * @param size number of bytes in data
172 * @param data pointer to the beginning of the directory
173 * @param offset offset of data in the directory
174 * @param dep function to call on each entry
175 * @param dep_cls closure for dep
176 * @return GNUNET_OK if this could be a block in a directory,
177 * GNUNET_NO if this could be part of a directory (but not 100% OK)
178 * GNUNET_SYSERR if 'data' does not represent a directory
181 GNUNET_FS_directory_list_contents (size_t size, const void *data,
183 GNUNET_FS_DirectoryEntryProcessor dep,
186 struct GetFullDataClosure full_data;
187 const char *cdata = data;
193 struct GNUNET_FS_Uri *uri;
194 struct GNUNET_CONTAINER_MetaData *md;
198 ((size < 8 + sizeof (uint32_t)) ||
199 (0 != memcmp (cdata, GNUNET_FS_DIRECTORY_MAGIC, 8))))
200 return GNUNET_SYSERR;
204 memcpy (&mdSize, &cdata[8], sizeof (uint32_t));
205 mdSize = ntohl (mdSize);
206 if (mdSize > size - 8 - sizeof (uint32_t))
209 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
210 _("MAGIC mismatch. This is not a GNUnet directory.\n"));
211 return GNUNET_SYSERR;
213 md = GNUNET_CONTAINER_meta_data_deserialize (&cdata[8 + sizeof (uint32_t)],
218 return GNUNET_SYSERR; /* malformed ! */
220 dep (dep_cls, NULL, NULL, md, 0, NULL);
221 GNUNET_CONTAINER_meta_data_destroy (md);
222 pos = 8 + sizeof (uint32_t) + mdSize;
226 /* find end of URI */
227 if (cdata[pos] == '\0')
229 /* URI is never empty, must be end of block,
230 * skip to next alignment */
231 align = ((pos / DBLOCK_SIZE) + 1) * DBLOCK_SIZE;
234 /* if we were already aligned, still skip a block! */
235 align += DBLOCK_SIZE;
240 /* malformed - or partial download... */
245 while ((epos < size) && (cdata[epos] != '\0'))
248 return GNUNET_NO; /* malformed - or partial download */
250 uri = GNUNET_FS_uri_parse (&cdata[pos], &emsg);
255 pos--; /* go back to '\0' to force going to next alignment */
258 if (GNUNET_FS_uri_test_ksk (uri))
260 GNUNET_FS_uri_destroy (uri);
262 return GNUNET_NO; /* illegal in directory! */
265 memcpy (&mdSize, &cdata[pos], sizeof (uint32_t));
266 mdSize = ntohl (mdSize);
267 pos += sizeof (uint32_t);
268 if (pos + mdSize > size)
270 GNUNET_FS_uri_destroy (uri);
271 return GNUNET_NO; /* malformed - or partial download */
274 md = GNUNET_CONTAINER_meta_data_deserialize (&cdata[pos], mdSize);
277 GNUNET_FS_uri_destroy (uri);
279 return GNUNET_NO; /* malformed ! */
283 GNUNET_CONTAINER_meta_data_get_by_type (md,
284 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
286 full_data.data = NULL;
287 GNUNET_CONTAINER_meta_data_iterate (md, &find_full_data, &full_data);
290 dep (dep_cls, filename, uri, md, full_data.size, full_data.data);
292 GNUNET_free_non_null (full_data.data);
293 GNUNET_free_non_null (filename);
294 GNUNET_CONTAINER_meta_data_destroy (md);
295 GNUNET_FS_uri_destroy (uri);
301 * Entries in the directory (builder).
306 * This is a linked list.
308 struct BuilderEntry *next;
311 * Length of this entry.
317 * Internal state of a directory builder.
319 struct GNUNET_FS_DirectoryBuilder
322 * Meta-data for the directory itself.
324 struct GNUNET_CONTAINER_MetaData *meta;
327 * Head of linked list of entries.
329 struct BuilderEntry *head;
332 * Number of entires in the directory.
339 * Create a directory builder.
341 * @param mdir metadata for the directory
343 struct GNUNET_FS_DirectoryBuilder *
344 GNUNET_FS_directory_builder_create (const struct GNUNET_CONTAINER_MetaData
347 struct GNUNET_FS_DirectoryBuilder *ret;
349 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_DirectoryBuilder));
351 ret->meta = GNUNET_CONTAINER_meta_data_duplicate (mdir);
353 ret->meta = GNUNET_CONTAINER_meta_data_create ();
354 GNUNET_FS_meta_data_make_directory (ret->meta);
360 * Add an entry to a directory.
362 * @param bld directory to extend
363 * @param uri uri of the entry (must not be a KSK)
364 * @param md metadata of the entry
365 * @param data raw data of the entry, can be NULL, otherwise
366 * data must point to exactly the number of bytes specified
367 * by the uri which must be of type LOC or CHK
370 GNUNET_FS_directory_builder_add (struct GNUNET_FS_DirectoryBuilder *bld,
371 const struct GNUNET_FS_Uri *uri,
372 const struct GNUNET_CONTAINER_MetaData *md,
375 struct GNUNET_FS_Uri *curi;
376 struct BuilderEntry *e;
386 struct GNUNET_CONTAINER_MetaData *meta;
387 const struct GNUNET_CONTAINER_MetaData *meta_use;
389 GNUNET_assert (!GNUNET_FS_uri_test_ksk (uri));
392 GNUNET_assert (!GNUNET_FS_uri_test_sks (uri));
393 if (GNUNET_FS_uri_test_chk (uri))
395 fsize = GNUNET_FS_uri_chk_get_file_size (uri);
399 curi = GNUNET_FS_uri_loc_get_uri (uri);
400 GNUNET_assert (NULL != curi);
401 fsize = GNUNET_FS_uri_chk_get_file_size (curi);
402 GNUNET_FS_uri_destroy (curi);
407 fsize = 0; /* not given */
409 if (fsize > MAX_INLINE_SIZE)
410 fsize = 0; /* too large */
411 uris = GNUNET_FS_uri_to_string (uri);
412 slen = strlen (uris) + 1;
413 mds = GNUNET_CONTAINER_meta_data_get_serialized_size (md);
418 meta = GNUNET_CONTAINER_meta_data_duplicate (md);
419 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>",
420 EXTRACTOR_METATYPE_GNUNET_FULL_DATA,
421 EXTRACTOR_METAFORMAT_BINARY, NULL, data,
423 mdxs = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
424 if ((slen + sizeof (uint32_t) + mdxs - 1) / DBLOCK_SIZE ==
425 (slen + sizeof (uint32_t) + mds - 1) / DBLOCK_SIZE)
427 /* adding full data would not cause us to cross
428 * additional blocks, so add it! */
434 if (mds > GNUNET_MAX_MALLOC_CHECKED / 2)
435 mds = GNUNET_MAX_MALLOC_CHECKED / 2;
436 e = GNUNET_malloc (sizeof (struct BuilderEntry) + slen + mds +
438 ser = (char *) &e[1];
439 memcpy (ser, uris, slen);
441 sptr = &ser[slen + sizeof (uint32_t)];
443 GNUNET_CONTAINER_meta_data_serialize (meta_use, &sptr, mds,
444 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
446 GNUNET_CONTAINER_meta_data_destroy (meta);
452 memcpy (&ser[slen], &big, sizeof (uint32_t));
453 e->len = slen + sizeof (uint32_t) + mds;
461 * Given the start and end position of a block of
462 * data, return the end position of that data
463 * after alignment to the DBLOCK_SIZE.
466 do_align (size_t start_position, size_t end_position)
470 align = (end_position / DBLOCK_SIZE) * DBLOCK_SIZE;
471 if ((start_position < align) && (end_position > align))
472 return align + end_position - start_position;
478 * Compute a permuation of the blocks to
479 * minimize the cost of alignment. Greedy packer.
481 * @param start starting position for the first block
482 * @param count size of the two arrays
483 * @param sizes the sizes of the individual blocks
484 * @param perm the permutation of the blocks (updated)
487 block_align (size_t start, unsigned int count, const size_t * sizes,
501 for (i = 0; i < count; i++)
504 badness = 0x7FFFFFFF;
506 for (j = i; j < count; j++)
509 cend = cpos + sizes[cval];
510 if (cpos % DBLOCK_SIZE == 0)
512 /* prefer placing the largest blocks first */
513 cbad = -(cend % DBLOCK_SIZE);
517 if (cpos / DBLOCK_SIZE == cend / DBLOCK_SIZE)
519 /* Data fits into the same block! Prefer small left-overs! */
520 cbad = DBLOCK_SIZE - cend % DBLOCK_SIZE;
524 /* Would have to waste space to re-align, add big factor, this
525 * case is a real loss (proportional to space wasted)! */
526 cbad = DBLOCK_SIZE * (DBLOCK_SIZE - cpos % DBLOCK_SIZE);
535 GNUNET_assert (best != -1);
537 perm[i] = perm[best];
539 cpos += sizes[perm[i]];
540 cpos = do_align (start, cpos);
546 * Finish building the directory. Frees the
547 * builder context and returns the directory
550 * @param bld directory to finish
551 * @param rsize set to the number of bytes needed
552 * @param rdata set to the encoded directory
553 * @return GNUNET_OK on success
556 GNUNET_FS_directory_builder_finish (struct GNUNET_FS_DirectoryBuilder *bld,
557 size_t * rsize, void **rdata)
565 struct BuilderEntry *pos;
566 struct BuilderEntry **bes;
573 size = strlen (GNUNET_DIRECTORY_MAGIC) + sizeof (uint32_t);
574 size += GNUNET_CONTAINER_meta_data_get_serialized_size (bld->meta);
580 sizes = GNUNET_malloc (bld->count * sizeof (size_t));
581 perm = GNUNET_malloc (bld->count * sizeof (unsigned int));
582 bes = GNUNET_malloc (bld->count * sizeof (struct BuilderEntry *));
584 for (i = 0; i < bld->count; i++)
591 block_align (size, bld->count, sizes, perm);
592 /* compute final size with alignment */
593 for (i = 0; i < bld->count; i++)
596 size += sizes[perm[i]];
597 size = do_align (psize, size);
601 data = GNUNET_malloc_large (size);
604 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
607 GNUNET_free_non_null (sizes);
608 GNUNET_free_non_null (perm);
609 GNUNET_free_non_null (bes);
610 return GNUNET_SYSERR;
613 memcpy (data, GNUNET_DIRECTORY_MAGIC, strlen (GNUNET_DIRECTORY_MAGIC));
614 off = strlen (GNUNET_DIRECTORY_MAGIC);
616 sptr = &data[off + sizeof (uint32_t)];
618 GNUNET_CONTAINER_meta_data_serialize (bld->meta, &sptr,
619 size - off - sizeof (uint32_t),
620 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
621 GNUNET_assert (ret != -1);
623 memcpy (&data[off], &big, sizeof (uint32_t));
624 off += sizeof (uint32_t) + ret;
625 for (j = 0; j < bld->count; j++)
630 off = do_align (psize, off);
631 memcpy (&data[off - sizes[i]], &(bes[i])[1], sizes[i]);
632 GNUNET_free (bes[i]);
634 GNUNET_free_non_null (sizes);
635 GNUNET_free_non_null (perm);
636 GNUNET_free_non_null (bes);
637 GNUNET_assert (off == size);
638 GNUNET_CONTAINER_meta_data_destroy (bld->meta);
644 /* end of fs_directory.c */