tolerate additional IPv4 address now available for gnunet.org
[oweals/gnunet.git] / src / fs / fs_directory.c
index f22480d5f54f64257c7d9321601e545264c23039..2dadc483542227aa406840a8dbe30ccf1404848e 100644 (file)
@@ -1,21 +1,21 @@
 /*
      This file is part of GNUnet.
-     (C) 2003, 2004, 2006, 2009 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2003, 2004, 2006, 2009 GNUnet e.V.
 
-     GNUnet is free software; you can redistribute it and/or modify
-     it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
+     GNUnet is free software: you can redistribute it and/or modify it
+     under the terms of the GNU Affero General Public License as published
+     by the Free Software Foundation, either version 3 of the License,
+     or (at your option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
+     Affero General Public License for more details.
+    
+     You should have received a copy of the GNU Affero General Public License
+     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-     You should have received a copy of the GNU General Public License
-     along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     SPDX-License-Identifier: AGPL3.0-or-later
 */
 
 /**
@@ -34,7 +34,7 @@
  */
 #include "platform.h"
 #include "gnunet_fs_service.h"
-#include "fs.h"
+#include "fs_api.h"
 
 /**
  * String that is used to indicate that a file
  * Does the meta-data claim that this is a directory?
  * Checks if the mime-type is that of a GNUnet directory.
  *
- * @return GNUNET_YES if it is, GNUNET_NO if it is not, GNUNET_SYSERR if
- *  we have no mime-type information (treat as 'GNUNET_NO')
+ * @return #GNUNET_YES if it is, #GNUNET_NO if it is not, #GNUNET_SYSERR if
+ *  we have no mime-type information (treat as #GNUNET_NO)
  */
 int
-GNUNET_FS_meta_data_test_for_directory (const struct GNUNET_CONTAINER_MetaData
-                                        *md)
+GNUNET_FS_meta_data_test_for_directory (const struct GNUNET_CONTAINER_MetaData *md)
 {
   char *mime;
   int ret;
 
   if (NULL == md)
     return GNUNET_SYSERR;
-  mime =
-      GNUNET_CONTAINER_meta_data_get_by_type (md, EXTRACTOR_METATYPE_MIMETYPE);
-  if (mime == NULL)
+  mime = GNUNET_CONTAINER_meta_data_get_by_type (md, EXTRACTOR_METATYPE_MIMETYPE);
+  if (NULL == mime)
     return GNUNET_SYSERR;
-  ret = (0 == strcmp (mime, GNUNET_FS_DIRECTORY_MIME)) ? GNUNET_YES : GNUNET_NO;
+  ret = (0 == strcasecmp (mime, GNUNET_FS_DIRECTORY_MIME)) ? GNUNET_YES : GNUNET_NO;
   GNUNET_free (mime);
   return ret;
 }
@@ -72,7 +70,7 @@ GNUNET_FS_meta_data_test_for_directory (const struct GNUNET_CONTAINER_MetaData
 /**
  * Set the MIMETYPE information for the given
  * metadata to "application/gnunet-directory".
- * 
+ *
  * @param md metadata to add mimetype to
  */
 void
@@ -88,11 +86,9 @@ GNUNET_FS_meta_data_make_directory (struct GNUNET_CONTAINER_MetaData *md)
     GNUNET_free (mime);
     return;
   }
-  GNUNET_CONTAINER_meta_data_insert (md,
-                                     "<gnunet>",
+  GNUNET_CONTAINER_meta_data_insert (md, "<gnunet>",
                                      EXTRACTOR_METATYPE_MIMETYPE,
-                                     EXTRACTOR_METAFORMAT_UTF8,
-                                     "text/plain",
+                                     EXTRACTOR_METAFORMAT_UTF8, "text/plain",
                                      GNUNET_FS_DIRECTORY_MIME,
                                      strlen (GNUNET_FS_DIRECTORY_MIME) + 1);
 }
@@ -126,7 +122,7 @@ struct GetFullDataClosure
  *        used in the main libextractor library and yielding
  *        meta data).
  * @param type libextractor-type describing the meta data
- * @param format basic format information about data 
+ * @param format basic format information about data
  * @param data_mime_type mime-type of data (not of the original file);
  *        can be NULL (if mime-type is not known)
  * @param data actual meta-data found
@@ -134,10 +130,8 @@ struct GetFullDataClosure
  * @return 0 to continue extracting, 1 to abort
  */
 static int
-find_full_data (void *cls,
-                const char *plugin_name,
-                enum EXTRACTOR_MetaType type,
-                enum EXTRACTOR_MetaFormat format,
+find_full_data (void *cls, const char *plugin_name,
+                enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat format,
                 const char *data_mime_type, const char *data, size_t data_len)
 {
   struct GetFullDataClosure *gfdc = cls;
@@ -148,7 +142,7 @@ find_full_data (void *cls,
     if (data_len > 0)
     {
       gfdc->data = GNUNET_malloc (data_len);
-      memcpy (gfdc->data, data, data_len);
+      GNUNET_memcpy (gfdc->data, data, data_len);
     }
     return 1;
   }
@@ -176,10 +170,10 @@ find_full_data (void *cls,
  * @param data pointer to the beginning of the directory
  * @param offset offset of data in the directory
  * @param dep function to call on each entry
- * @param dep_cls closure for dep
- * @return GNUNET_OK if this could be a block in a directory,
- *         GNUNET_NO if this could be part of a directory (but not 100% OK)
- *         GNUNET_SYSERR if 'data' does not represent a directory
+ * @param dep_cls closure for @a dep
+ * @return #GNUNET_OK if this could be a block in a directory,
+ *         #GNUNET_NO if this could be part of a directory (but not 100% OK)
+ *         #GNUNET_SYSERR if @a data does not represent a directory
  */
 int
 GNUNET_FS_directory_list_contents (size_t size,
@@ -201,16 +195,16 @@ GNUNET_FS_directory_list_contents (size_t size,
 
   if ((offset == 0) &&
       ((size < 8 + sizeof (uint32_t)) ||
-       (0 != memcmp (cdata, GNUNET_FS_DIRECTORY_MAGIC, 8))))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                _("MAGIC mismatch.  This is not a GNUnet directory.\n"));
+       (0 != memcmp (cdata,
+                     GNUNET_FS_DIRECTORY_MAGIC,
+                     8))))
     return GNUNET_SYSERR;
-  }
   pos = offset;
   if (offset == 0)
   {
-    memcpy (&mdSize, &cdata[8], sizeof (uint32_t));
+    GNUNET_memcpy (&mdSize,
+                   &cdata[8],
+                   sizeof (uint32_t));
     mdSize = ntohl (mdSize);
     if (mdSize > size - 8 - sizeof (uint32_t))
     {
@@ -219,15 +213,19 @@ GNUNET_FS_directory_list_contents (size_t size,
                   _("MAGIC mismatch.  This is not a GNUnet directory.\n"));
       return GNUNET_SYSERR;
     }
-    md = GNUNET_CONTAINER_meta_data_deserialize (&cdata[8 +
-                                                        sizeof (uint32_t)],
+    md = GNUNET_CONTAINER_meta_data_deserialize (&cdata[8 + sizeof (uint32_t)],
                                                  mdSize);
     if (md == NULL)
     {
       GNUNET_break (0);
       return GNUNET_SYSERR;     /* malformed ! */
     }
-    dep (dep_cls, NULL, NULL, md, 0, NULL);
+    dep (dep_cls,
+         NULL,
+         NULL,
+         md,
+         0,
+         NULL);
     GNUNET_CONTAINER_meta_data_destroy (md);
     pos = 8 + sizeof (uint32_t) + mdSize;
   }
@@ -259,7 +257,7 @@ GNUNET_FS_directory_list_contents (size_t size,
 
     uri = GNUNET_FS_uri_parse (&cdata[pos], &emsg);
     pos = epos + 1;
-    if (uri == NULL)
+    if (NULL == uri)
     {
       GNUNET_free (emsg);
       pos--;                    /* go back to '\0' to force going to next alignment */
@@ -272,7 +270,9 @@ GNUNET_FS_directory_list_contents (size_t size,
       return GNUNET_NO;         /* illegal in directory! */
     }
 
-    memcpy (&mdSize, &cdata[pos], sizeof (uint32_t));
+    GNUNET_memcpy (&mdSize,
+                   &cdata[pos],
+                   sizeof (uint32_t));
     mdSize = ntohl (mdSize);
     pos += sizeof (uint32_t);
     if (pos + mdSize > size)
@@ -281,22 +281,31 @@ GNUNET_FS_directory_list_contents (size_t size,
       return GNUNET_NO;         /* malformed - or partial download */
     }
 
-    md = GNUNET_CONTAINER_meta_data_deserialize (&cdata[pos], mdSize);
-    if (md == NULL)
+    md = GNUNET_CONTAINER_meta_data_deserialize (&cdata[pos],
+                                                 mdSize);
+    if (NULL == md)
     {
       GNUNET_FS_uri_destroy (uri);
       GNUNET_break (0);
       return GNUNET_NO;         /* malformed ! */
     }
     pos += mdSize;
-    filename = GNUNET_CONTAINER_meta_data_get_by_type (md,
-                                                       EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
+    filename =
+        GNUNET_CONTAINER_meta_data_get_by_type (md,
+                                                EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
     full_data.size = 0;
     full_data.data = NULL;
-    GNUNET_CONTAINER_meta_data_iterate (md, &find_full_data, &full_data);
-    if (dep != NULL)
+    GNUNET_CONTAINER_meta_data_iterate (md,
+                                        &find_full_data,
+                                        &full_data);
+    if (NULL != dep)
     {
-      dep (dep_cls, filename, uri, md, full_data.size, full_data.data);
+      dep (dep_cls,
+           filename,
+           uri,
+           md,
+           full_data.size,
+           full_data.data);
     }
     GNUNET_free_non_null (full_data.data);
     GNUNET_free_non_null (filename);
@@ -346,7 +355,7 @@ struct GNUNET_FS_DirectoryBuilder
 
 /**
  * Create a directory builder.
- * 
+ *
  * @param mdir metadata for the directory
  */
 struct GNUNET_FS_DirectoryBuilder *
@@ -355,7 +364,7 @@ GNUNET_FS_directory_builder_create (const struct GNUNET_CONTAINER_MetaData
 {
   struct GNUNET_FS_DirectoryBuilder *ret;
 
-  ret = GNUNET_malloc (sizeof (struct GNUNET_FS_DirectoryBuilder));
+  ret = GNUNET_new (struct GNUNET_FS_DirectoryBuilder);
   if (mdir != NULL)
     ret->meta = GNUNET_CONTAINER_meta_data_duplicate (mdir);
   else
@@ -367,7 +376,7 @@ GNUNET_FS_directory_builder_create (const struct GNUNET_CONTAINER_MetaData
 
 /**
  * Add an entry to a directory.
- * 
+ *
  * @param bld directory to extend
  * @param uri uri of the entry (must not be a KSK)
  * @param md metadata of the entry
@@ -425,11 +434,10 @@ GNUNET_FS_directory_builder_add (struct GNUNET_FS_DirectoryBuilder *bld,
   if (fsize > 0)
   {
     meta = GNUNET_CONTAINER_meta_data_duplicate (md);
-    GNUNET_CONTAINER_meta_data_insert (meta,
-                                       "<gnunet>",
+    GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>",
                                        EXTRACTOR_METATYPE_GNUNET_FULL_DATA,
-                                       EXTRACTOR_METAFORMAT_BINARY,
-                                       NULL, data, fsize);
+                                       EXTRACTOR_METAFORMAT_BINARY, NULL, data,
+                                       fsize);
     mdxs = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
     if ((slen + sizeof (uint32_t) + mdxs - 1) / DBLOCK_SIZE ==
         (slen + sizeof (uint32_t) + mds - 1) / DBLOCK_SIZE)
@@ -443,16 +451,15 @@ GNUNET_FS_directory_builder_add (struct GNUNET_FS_DirectoryBuilder *bld,
 
   if (mds > GNUNET_MAX_MALLOC_CHECKED / 2)
     mds = GNUNET_MAX_MALLOC_CHECKED / 2;
-  e = GNUNET_malloc (sizeof (struct BuilderEntry) +
-                     slen + mds + sizeof (uint32_t));
+  e = GNUNET_malloc (sizeof (struct BuilderEntry) + slen + mds +
+                     sizeof (uint32_t));
   ser = (char *) &e[1];
-  memcpy (ser, uris, slen);
+  GNUNET_memcpy (ser, uris, slen);
   GNUNET_free (uris);
   sptr = &ser[slen + sizeof (uint32_t)];
-  ret = GNUNET_CONTAINER_meta_data_serialize (meta_use,
-                                              &sptr,
-                                              mds,
-                                              GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
+  ret =
+      GNUNET_CONTAINER_meta_data_serialize (meta_use, &sptr, mds,
+                                            GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
   if (NULL != meta)
     GNUNET_CONTAINER_meta_data_destroy (meta);
   if (ret == -1)
@@ -460,7 +467,7 @@ GNUNET_FS_directory_builder_add (struct GNUNET_FS_DirectoryBuilder *bld,
   else
     mds = ret;
   big = htonl (mds);
-  memcpy (&ser[slen], &big, sizeof (uint32_t));
+  GNUNET_memcpy (&ser[slen], &big, sizeof (uint32_t));
   e->len = slen + sizeof (uint32_t) + mds;
   e->next = bld->head;
   bld->head = e;
@@ -495,8 +502,8 @@ do_align (size_t start_position, size_t end_position)
  * @param perm the permutation of the blocks (updated)
  */
 static void
-block_align (size_t start,
-             unsigned int count, const size_t * sizes, unsigned int *perm)
+block_align (size_t start, unsigned int count, const size_t * sizes,
+             unsigned int *perm)
 {
   unsigned int i;
   unsigned int j;
@@ -561,11 +568,12 @@ block_align (size_t start,
  * @param bld directory to finish
  * @param rsize set to the number of bytes needed
  * @param rdata set to the encoded directory
- * @return GNUNET_OK on success
+ * @return #GNUNET_OK on success
  */
 int
 GNUNET_FS_directory_builder_finish (struct GNUNET_FS_DirectoryBuilder *bld,
-                                    size_t * rsize, void **rdata)
+                                    size_t * rsize,
+                                    void **rdata)
 {
   char *data;
   char *sptr;
@@ -588,9 +596,12 @@ GNUNET_FS_directory_builder_finish (struct GNUNET_FS_DirectoryBuilder *bld,
   bes = NULL;
   if (0 < bld->count)
   {
-    sizes = GNUNET_malloc (bld->count * sizeof (size_t));
-    perm = GNUNET_malloc (bld->count * sizeof (unsigned int));
-    bes = GNUNET_malloc (bld->count * sizeof (struct BuilderEntry *));
+    sizes = GNUNET_new_array (bld->count,
+                              size_t);
+    perm = GNUNET_new_array (bld->count,
+                             unsigned int);
+    bes = GNUNET_new_array (bld->count,
+                            struct BuilderEntry *);
     pos = bld->head;
     for (i = 0; i < bld->count; i++)
     {
@@ -612,7 +623,8 @@ GNUNET_FS_directory_builder_finish (struct GNUNET_FS_DirectoryBuilder *bld,
   data = GNUNET_malloc_large (size);
   if (data == NULL)
   {
-    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
+    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
+                         "malloc");
     *rsize = 0;
     *rdata = NULL;
     GNUNET_free_non_null (sizes);
@@ -621,17 +633,22 @@ GNUNET_FS_directory_builder_finish (struct GNUNET_FS_DirectoryBuilder *bld,
     return GNUNET_SYSERR;
   }
   *rdata = data;
-  memcpy (data, GNUNET_DIRECTORY_MAGIC, strlen (GNUNET_DIRECTORY_MAGIC));
+  GNUNET_memcpy (data,
+                 GNUNET_DIRECTORY_MAGIC,
+                 strlen (GNUNET_DIRECTORY_MAGIC));
   off = strlen (GNUNET_DIRECTORY_MAGIC);
 
   sptr = &data[off + sizeof (uint32_t)];
-  ret = GNUNET_CONTAINER_meta_data_serialize (bld->meta,
-                                              &sptr,
-                                              size - off - sizeof (uint32_t),
-                                              GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
+  ret =
+      GNUNET_CONTAINER_meta_data_serialize (bld->meta,
+                                            &sptr,
+                                            size - off - sizeof (uint32_t),
+                                            GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
   GNUNET_assert (ret != -1);
   big = htonl (ret);
-  memcpy (&data[off], &big, sizeof (uint32_t));
+  GNUNET_memcpy (&data[off],
+                 &big,
+                 sizeof (uint32_t));
   off += sizeof (uint32_t) + ret;
   for (j = 0; j < bld->count; j++)
   {
@@ -639,7 +656,7 @@ GNUNET_FS_directory_builder_finish (struct GNUNET_FS_DirectoryBuilder *bld,
     psize = off;
     off += sizes[i];
     off = do_align (psize, off);
-    memcpy (&data[off - sizes[i]], &(bes[i])[1], sizes[i]);
+    GNUNET_memcpy (&data[off - sizes[i]], &(bes[i])[1], sizes[i]);
     GNUNET_free (bes[i]);
   }
   GNUNET_free_non_null (sizes);