コード例 #1
0
ファイル: FileManager.cpp プロジェクト: Zorvalt/D-LAN
QList< QSharedPointer<IChunk> > FileManager::getAllChunks(const Protos::Common::Entry& localEntry, const Common::Hashes& hashes) const
{
   for (QListIterator<Common::Hash> h(hashes); h.hasNext();)
   {
      // Chunks from different files, usually one chunk.
      QList< QSharedPointer<Chunk> > chunks = this->chunks.values(h.next());

      for (QListIterator< QSharedPointer<Chunk> > i(chunks); i.hasNext();)
      {
         QSharedPointer<Chunk> chunk = i.next();
         if (chunk->matchesEntry(localEntry)) // The name, the path and the size of the file are the same?
         {
            // We verify that all hashes of all chunks match the given hashes. If it's not the case, the files are not the same.
            QList< QSharedPointer<Chunk> > allChunks = chunk->getOtherChunks();
            if (allChunks.size() != hashes.size())
               return QList< QSharedPointer<IChunk> >();

            QList< QSharedPointer<IChunk> > ret;
            for (int j = 0; j < allChunks.size(); j++)
            {
               if (allChunks[j]->getHash() != hashes[j]) // Only one hashes doesn't match -> all the file doesn't match.
                  return QList< QSharedPointer<IChunk> >();
               ret << allChunks[j];
            }
            return ret;
         }
      }
   }

   return QList< QSharedPointer<IChunk> >();
}
コード例 #2
0
ファイル: File.cpp プロジェクト: Ummon/D-LAN
/**
  * The number of given hashes may not match the total number of chunk.
  */
void File::setHashes(const Common::Hashes& hashes)
{
   this->chunks.reserve(this->getNbChunks());
   for (int i = 0; i < this->getNbChunks(); i++)
   {
      int chunkKnownBytes = !this->isComplete() ? 0 : i == this->getNbChunks() - 1 && this->getSize() % Chunk::CHUNK_SIZE != 0 ? this->getSize() % Chunk::CHUNK_SIZE : Chunk::CHUNK_SIZE;

      if (i < hashes.size() && !hashes[i].isNull())
      {
         QSharedPointer<Chunk> chunk(new Chunk(this, i, chunkKnownBytes, hashes[i]));
         this->chunks << chunk;
         if (chunk->isComplete())
            this->cache->onChunkHashKnown(chunk);
      }
      else
         // If there is too few hashes then null hashes are added.
         this->chunks << QSharedPointer<Chunk>(new Chunk(this, i, chunkKnownBytes));
   }
}