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 * Iterate over all entries in a directory. Note that directories
101 * are structured such that it is possible to iterate over the
102 * individual blocks as well as over the entire directory. Thus
103 * a client can call this function on the buffer in the
104 * GNUNET_FS_ProgressCallback. Also, directories can optionally
105 * include the contents of (small) files embedded in the directory
106 * itself; for those files, the processor may be given the
107 * contents of the file directly by this function.
110 * Note that this function maybe called on parts of directories. Thus
111 * parser errors should not be reported _at all_ (with GNUNET_break).
112 * Still, if some entries can be recovered despite these parsing
113 * errors, the function should try to do this.
115 * @param size number of bytes in data
116 * @param data pointer to the beginning of the directory
117 * @param offset offset of data in the directory
118 * @param dep function to call on each entry
119 * @param dep_cls closure for dep
122 GNUNET_FS_directory_list_contents (size_t size,
125 GNUNET_FS_DirectoryEntryProcessor dep,
128 const char *cdata = data;
135 struct GNUNET_FS_Uri *uri;
136 struct GNUNET_CONTAINER_MetaData *md;
141 (size >= 8 + sizeof (uint32_t)) &&
142 (0 == memcmp (cdata, GNUNET_FS_DIRECTORY_MAGIC, 8)) )
144 memcpy (&mdSize, &cdata[8], sizeof (uint32_t));
145 mdSize = ntohl (mdSize);
146 if (mdSize > size - 8 - sizeof (uint32_t))
149 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
150 _("Not a GNUnet directory.\n"));
153 md = GNUNET_CONTAINER_meta_data_deserialize (&cdata[8 +
159 return; /* malformed ! */
167 GNUNET_CONTAINER_meta_data_destroy (md);
168 pos = 8 + sizeof (uint32_t) + mdSize;
172 /* find end of URI */
173 if (cdata[pos] == '\0')
175 /* URI is never empty, must be end of block,
176 skip to next alignment */
178 ((pos / DBLOCK_SIZE) + 1) * DBLOCK_SIZE;
181 /* if we were already aligned, still skip a block! */
182 align += DBLOCK_SIZE;
187 /* malformed - or partial download... */
192 while ((epos < size) && (cdata[epos] != '\0'))
195 return; /* malformed - or partial download */
197 uri = GNUNET_FS_uri_parse (&cdata[pos], &emsg);
202 pos--; /* go back to '\0' to force going to next alignment */
205 if (GNUNET_FS_uri_test_ksk (uri))
207 GNUNET_FS_uri_destroy (uri);
209 return; /* illegal in directory! */
212 memcpy (&mdSize, &cdata[pos], sizeof (uint32_t));
213 mdSize = ntohl (mdSize);
214 pos += sizeof (uint32_t);
215 if (pos + mdSize > size)
217 GNUNET_FS_uri_destroy (uri);
218 return; /* malformed - or partial download */
221 md = GNUNET_CONTAINER_meta_data_deserialize (&cdata[pos], mdSize);
224 GNUNET_FS_uri_destroy (uri);
226 return; /* malformed ! */
229 filename = GNUNET_CONTAINER_meta_data_get_by_type (md,
230 EXTRACTOR_METATYPE_FILENAME);
231 file_data = GNUNET_CONTAINER_meta_data_get_by_type (md,
232 EXTRACTOR_METATYPE_GNUNET_FULL_DATA);
238 (file_data != NULL) ? strlen(file_data) : 0,
240 GNUNET_free_non_null (file_data);
241 GNUNET_free_non_null (filename);
242 GNUNET_CONTAINER_meta_data_destroy (md);
243 GNUNET_FS_uri_destroy (uri);
248 * Entries in the directory (builder).
253 * This is a linked list.
255 struct BuilderEntry *next;
258 * Length of this entry.
264 * Internal state of a directory builder.
266 struct GNUNET_FS_DirectoryBuilder
269 * Meta-data for the directory itself.
271 struct GNUNET_CONTAINER_MetaData *meta;
274 * Head of linked list of entries.
276 struct BuilderEntry *head;
279 * Number of entires in the directory.
286 * Create a directory builder.
288 * @param mdir metadata for the directory
290 struct GNUNET_FS_DirectoryBuilder *
291 GNUNET_FS_directory_builder_create (const struct GNUNET_CONTAINER_MetaData *mdir)
293 struct GNUNET_FS_DirectoryBuilder *ret;
295 ret = GNUNET_malloc(sizeof(struct GNUNET_FS_DirectoryBuilder));
297 ret->meta = GNUNET_CONTAINER_meta_data_duplicate (mdir);
299 ret->meta = GNUNET_CONTAINER_meta_data_create ();
300 GNUNET_FS_meta_data_make_directory (ret->meta);
306 * Add an entry to a directory.
308 * @param bld directory to extend
309 * @param uri uri of the entry (must not be a KSK)
310 * @param md metadata of the entry
311 * @param data raw data of the entry, can be NULL, otherwise
312 * data must point to exactly the number of bytes specified
313 * by the uri which must be of type LOC or CHK
316 GNUNET_FS_directory_builder_add (struct GNUNET_FS_DirectoryBuilder *bld,
317 const struct GNUNET_FS_Uri *uri,
318 const struct GNUNET_CONTAINER_MetaData *md,
321 struct GNUNET_FS_Uri *curi;
322 struct BuilderEntry *e;
332 struct GNUNET_CONTAINER_MetaData *meta;
333 const struct GNUNET_CONTAINER_MetaData *meta_use;
335 GNUNET_assert (! GNUNET_FS_uri_test_ksk (uri));
337 if (GNUNET_FS_uri_test_chk (uri))
338 fsize = GNUNET_FS_uri_chk_get_file_size (uri);
341 curi = GNUNET_FS_uri_loc_get_uri (uri);
342 fsize = GNUNET_FS_uri_chk_get_file_size (curi);
343 GNUNET_FS_uri_destroy (curi);
346 fsize = 0; /* not given */
347 if (fsize > MAX_INLINE_SIZE)
348 fsize = 0; /* too large */
349 uris = GNUNET_FS_uri_to_string (uri);
350 slen = strlen (uris) + 1;
352 GNUNET_CONTAINER_meta_data_get_serialized_size (md);
357 meta = GNUNET_CONTAINER_meta_data_duplicate (md);
358 GNUNET_CONTAINER_meta_data_insert (meta,
360 EXTRACTOR_METATYPE_GNUNET_FULL_DATA,
361 EXTRACTOR_METAFORMAT_BINARY,
366 GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
367 if ( (slen + sizeof (uint32_t) + mdxs - 1) / DBLOCK_SIZE ==
368 (slen + sizeof (uint32_t) + mds - 1) / DBLOCK_SIZE)
370 /* adding full data would not cause us to cross
371 additional blocks, so add it! */
377 if (mds > GNUNET_MAX_MALLOC_CHECKED / 2)
378 mds = GNUNET_MAX_MALLOC_CHECKED / 2;
379 e = GNUNET_malloc (sizeof(struct BuilderEntry) +
380 slen + mds + sizeof (uint32_t));
382 memcpy (ser, uris, slen);
384 sptr = &ser[slen + sizeof(uint32_t)];
385 ret = GNUNET_CONTAINER_meta_data_serialize (meta_use,
388 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
390 GNUNET_CONTAINER_meta_data_destroy (meta);
396 memcpy (&ser[slen], &big, sizeof (uint32_t));
397 e->len = slen + sizeof (uint32_t) + mds;
405 * Given the start and end position of a block of
406 * data, return the end position of that data
407 * after alignment to the DBLOCK_SIZE.
410 do_align (size_t start_position,
415 align = (end_position / DBLOCK_SIZE) * DBLOCK_SIZE;
416 if ((start_position < align) && (end_position > align))
417 return align + end_position - start_position;
423 * Compute a permuation of the blocks to
424 * minimize the cost of alignment. Greedy packer.
426 * @param start starting position for the first block
427 * @param count size of the two arrays
428 * @param sizes the sizes of the individual blocks
429 * @param perm the permutation of the blocks (updated)
432 block_align (size_t start,
448 for (i = 0; i < count; i++)
451 badness = 0x7FFFFFFF;
453 for (j = i; j < count; j++)
456 cend = cpos + sizes[cval];
457 if (cpos % DBLOCK_SIZE == 0)
459 /* prefer placing the largest blocks first */
460 cbad = -(cend % DBLOCK_SIZE);
464 if (cpos / DBLOCK_SIZE ==
467 /* Data fits into the same block! Prefer small left-overs! */
469 DBLOCK_SIZE - cend % DBLOCK_SIZE;
473 /* Would have to waste space to re-align, add big factor, this
474 case is a real loss (proportional to space wasted)! */
476 DBLOCK_SIZE * (DBLOCK_SIZE -
488 perm[i] = perm[best];
490 cpos += sizes[perm[i]];
491 cpos = do_align (start, cpos);
497 * Finish building the directory. Frees the
498 * builder context and returns the directory
501 * @param bld directory to finish
502 * @param rsize set to the number of bytes needed
503 * @param rdata set to the encoded directory
504 * @return GNUNET_OK on success
507 GNUNET_FS_directory_builder_finish (struct GNUNET_FS_DirectoryBuilder *bld,
517 struct BuilderEntry *pos;
518 struct BuilderEntry **bes;
525 size = 8 + sizeof (uint32_t);
526 size += GNUNET_CONTAINER_meta_data_get_serialized_size (bld->meta);
532 sizes = GNUNET_malloc (bld->count * sizeof (size_t));
533 perm = GNUNET_malloc (bld->count * sizeof (unsigned int));
534 bes = GNUNET_malloc (bld->count * sizeof (struct BuilderEntry *));
536 for (i = 0; i < bld->count; i++)
547 /* compute final size with alignment */
548 for (i = 0; i < bld->count; i++)
551 size += sizes[perm[i]];
552 size = do_align (psize, size);
556 data = GNUNET_malloc_large (size);
559 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
563 return GNUNET_SYSERR;
566 memcpy (data, GNUNET_DIRECTORY_MAGIC, 8);
569 sptr = &data[off + sizeof (uint32_t)];
570 ret = GNUNET_CONTAINER_meta_data_serialize (bld->meta,
572 size - off - sizeof (uint32_t),
573 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
574 GNUNET_assert (ret != -1);
576 memcpy (&data[8], &big, sizeof (uint32_t));
577 off += sizeof (uint32_t) + ret;
578 for (j = 0; j < bld->count; j++)
583 off = do_align (psize, off);
584 memcpy (&data[off - sizes[i]],
587 GNUNET_free (bes[i]);
589 GNUNET_free_non_null (sizes);
590 GNUNET_free_non_null (perm);
591 GNUNET_free_non_null (bes);
592 GNUNET_assert (off == size);
593 GNUNET_CONTAINER_meta_data_destroy (bld->meta);
599 /* end of fs_directory.c */