upload.c

来自「GNUnet是一个安全的点对点网络框架」· C语言 代码 · 共 461 行 · 第 1/2 页

C
461
字号
  fd = GNUNET_disk_file_open (ectx, filename, O_RDONLY | O_LARGEFILE);  if (fd == -1)    {      GNUNET_GE_LOG (ectx,                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,                     _("Cannot open file `%s': `%s'"), filename,                     STRERROR (errno));      GNUNET_client_connection_destroy (sock);      return GNUNET_SYSERR;    }  dblock =    GNUNET_malloc (sizeof (GNUNET_DatastoreValue) + GNUNET_ECRS_DBLOCK_SIZE +                   sizeof (GNUNET_EC_DBlock));  dblock->size =    htonl (sizeof (GNUNET_DatastoreValue) + GNUNET_ECRS_DBLOCK_SIZE +           sizeof (GNUNET_EC_DBlock));  dblock->anonymity_level = htonl (anonymityLevel);  dblock->priority = htonl (priority);  dblock->type = htonl (GNUNET_ECRS_BLOCKTYPE_DATA);  dblock->expiration_time = GNUNET_htonll (expirationTime);  db = (GNUNET_EC_DBlock *) & dblock[1];  db->type = htonl (GNUNET_ECRS_BLOCKTYPE_DATA);  iblocks =    GNUNET_malloc (sizeof (GNUNET_DatastoreValue *) * (treedepth + 1));  for (i = 0; i <= treedepth; i++)    {      iblocks[i] =        GNUNET_malloc (sizeof (GNUNET_DatastoreValue) +                       GNUNET_ECRS_IBLOCK_SIZE + sizeof (GNUNET_EC_DBlock));      iblocks[i]->size =        htonl (sizeof (GNUNET_DatastoreValue) + sizeof (GNUNET_EC_DBlock));      iblocks[i]->anonymity_level = htonl (anonymityLevel);      iblocks[i]->priority = htonl (priority);      iblocks[i]->type = htonl (GNUNET_ECRS_BLOCKTYPE_DATA);      iblocks[i]->expiration_time = GNUNET_htonll (expirationTime);      ((GNUNET_EC_DBlock *) & iblocks[i][1])->type =        htonl (GNUNET_ECRS_BLOCKTYPE_DATA);    }  pos = 0;  while (pos < filesize)    {      if (upcb != NULL)        upcb (filesize, pos, eta, upcbClosure);      if (tt != NULL)        if (GNUNET_OK != tt (ttClosure))          goto FAILURE;      size = GNUNET_ECRS_DBLOCK_SIZE;      if (size > filesize - pos)        {          size = filesize - pos;          memset (&db[1], 0, GNUNET_ECRS_DBLOCK_SIZE);        }      GNUNET_GE_ASSERT (ectx,                        sizeof (GNUNET_DatastoreValue) + size +                        sizeof (GNUNET_EC_DBlock) < GNUNET_MAX_BUFFER_SIZE);      dblock->size =        htonl (sizeof (GNUNET_DatastoreValue) + size +               sizeof (GNUNET_EC_DBlock));      if (size != READ (fd, &db[1], size))        {          GNUNET_GE_LOG_STRERROR_FILE (ectx,                                       GNUNET_GE_ERROR | GNUNET_GE_BULK |                                       GNUNET_GE_ADMIN | GNUNET_GE_USER,                                       "READ", filename);          goto FAILURE;        }      if (tt != NULL)        if (GNUNET_OK != tt (ttClosure))          goto FAILURE;      GNUNET_EC_file_block_get_key (db, size + sizeof (GNUNET_EC_DBlock),                                    &mchk.key);      GNUNET_EC_file_block_get_query (db, size + sizeof (GNUNET_EC_DBlock),                                      &mchk.query);#if DEBUG_UPLOAD      GNUNET_hash_to_enc (&mchk.query, &enc);      fprintf (stderr,               "Query for current block of size %u is `%s'\n", size,               (const char *) &enc);#endif      if (doIndex == GNUNET_YES)        {          if (GNUNET_SYSERR == GNUNET_FS_index (sock, &fileId, dblock, pos))            {              GNUNET_GE_LOG (ectx,                             GNUNET_GE_ERROR | GNUNET_GE_BULK |                             GNUNET_GE_USER,                             _                             ("Indexing data of file `%s' failed at position %llu.\n"),                             filename, pos);              goto FAILURE;            }        }      else        {          value = NULL;          if (GNUNET_OK !=              GNUNET_EC_file_block_encode (db,                                           size + sizeof (GNUNET_EC_DBlock),                                           &mchk.query, &value))            {              GNUNET_GE_BREAK (ectx, 0);              goto FAILURE;            }          GNUNET_GE_ASSERT (ectx, value != NULL);          *value = *dblock;     /* copy options! */          if ((doIndex == GNUNET_NO) &&              (GNUNET_OK != (ret = GNUNET_FS_insert (sock, value))))            {              GNUNET_GE_BREAK (ectx, ret == GNUNET_NO);              GNUNET_free (value);              goto FAILURE;            }          GNUNET_free (value);        }      pos += size;      now = GNUNET_get_time ();      if (pos > 0)        {          eta = (GNUNET_CronTime) (start +                                   (((double) (now - start) / (double) pos))                                   * (double) filesize);        }      if (GNUNET_OK != pushBlock (sock, &mchk, 0,       /* dblocks are on level 0 */                                  iblocks, priority, expirationTime))        goto FAILURE;    }  if (tt != NULL)    if (GNUNET_OK != tt (ttClosure))      goto FAILURE;#if DEBUG_UPLOAD  GNUNET_GE_LOG (ectx,                 GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,                 "Tree depth is %u, walking up tree.\n", treedepth);#endif  for (i = 0; i < treedepth; i++)    {      size = ntohl (iblocks[i]->size) - sizeof (GNUNET_DatastoreValue);      GNUNET_GE_ASSERT (ectx, size < GNUNET_MAX_BUFFER_SIZE);      if (size == sizeof (GNUNET_EC_DBlock))        {#if DEBUG_UPLOAD          GNUNET_GE_LOG (ectx,                         GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,                         "Level %u is empty\n", i);#endif          continue;        }      db = (GNUNET_EC_DBlock *) & iblocks[i][1];      GNUNET_EC_file_block_get_key (db, size, &mchk.key);#if DEBUG_UPLOAD      GNUNET_GE_LOG (ectx,                     GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,                     "Computing query for %u bytes content.\n", size);#endif      GNUNET_EC_file_block_get_query (db, size, &mchk.query);#if DEBUG_UPLOAD      IF_GELOG (ectx,                GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,                GNUNET_hash_to_enc (&mchk.query, &enc));      GNUNET_GE_LOG (ectx,                     GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,                     "Query for current block at level %u is `%s'.\n", i,                     &enc);#endif      if (GNUNET_OK != pushBlock (sock,                                  &mchk, i + 1, iblocks, priority,                                  expirationTime))        {          GNUNET_GE_BREAK (ectx, 0);          goto FAILURE;        }      GNUNET_EC_file_block_encode (db, size, &mchk.query, &value);      if (value == NULL)        {          GNUNET_GE_BREAK (ectx, 0);          goto FAILURE;        }      value->expiration_time = GNUNET_htonll (expirationTime);      value->priority = htonl (priority);      if ((doIndex != GNUNET_SYSERR) &&          (GNUNET_SYSERR == GNUNET_FS_insert (sock, value)))        {          GNUNET_GE_BREAK (ectx, 0);          GNUNET_free (value);          goto FAILURE;        }      GNUNET_free (value);      GNUNET_free (iblocks[i]);      iblocks[i] = NULL;    }#if DEBUG_UPLOAD  IF_GELOG (ectx,            GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,            GNUNET_hash_to_enc (&mchk.query, &enc));  GNUNET_GE_LOG (ectx, GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,                 "Query for top block is %s\n", &enc);#endif  /* build URI */  fid.file_length = GNUNET_htonll (filesize);  db = (GNUNET_EC_DBlock *) & iblocks[treedepth][1];  fid.chk = *(GNUNET_EC_ContentHashKey *) & (db[1]);  *uri = GNUNET_malloc (sizeof (URI));  (*uri)->type = chk;  (*uri)->data.fi = fid;  /* free resources */  GNUNET_free_non_null (iblocks[treedepth]);  GNUNET_free (iblocks);  GNUNET_free (dblock);  if (upcb != NULL)    upcb (filesize, filesize, eta, upcbClosure);  CLOSE (fd);  GNUNET_client_connection_destroy (sock);  return GNUNET_OK;FAILURE:  for (i = 0; i <= treedepth; i++)    GNUNET_free_non_null (iblocks[i]);  GNUNET_free (iblocks);  GNUNET_free (dblock);  CLOSE (fd);  GNUNET_client_connection_destroy (sock);  return GNUNET_SYSERR;}/* end of upload.c */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?