Esempio n. 1
0
bool CTorrent::InstallMetadata()
{
    CFile* pFile = GetFile();

    ASSERT(m_TorrentInfo);

    CPartMap* pPartMap = pFile->GetPartMap();
    if(pPartMap && !CompareSubFiles(pPartMap))
    {
        if(pFile->GetInspector()->BadMetaData(GetHash()))
            pPartMap = NULL;
        else
            return false;
    }

    if(m_pHash->Compare(pFile->GetMasterHash().data()))
        pFile->SetFileName(m_TorrentInfo->GetTorrentName());

    bool bOpenIO = false;
    CJoinedPartMap* pJoinedParts = qobject_cast<CJoinedPartMap*>(pPartMap);
    if((pPartMap == NULL && !pFile->IsComplete()) || (pJoinedParts && pJoinedParts->GetLinks().isEmpty()))
    {
        bOpenIO = true;
        if(!pPartMap)
            pFile->SetFileSize(m_TorrentInfo->GetTotalLength());

        SetupPartMap();

        if(!pPartMap && !pFile->IsPending())
            pFile->SetFilePath();
    }

    LoadPieceHashes();

    if(pFile->IsIncomplete() && pFile->GetMasterHash()->GetType() == HashTorrent)
        pFile->CleanUpHashes();

    // Note: if neo is the masterhash the IO is already opened
    if(bOpenIO && !pFile->IsPending())
        pFile->Resume();
    return true;
}
Esempio n. 2
0
bool CTorrent::LoadTorrentFromFile(const QByteArray& InfoHash)
{
    ASSERT(m_TorrentInfo == NULL);
    m_TorrentInfo = new CTorrentInfo(this);

    ASSERT(m_pHash.isNull());
    m_pHash = CFileHashPtr(new CFileHash(HashTorrent));
    m_pHash->SetHash(InfoHash);

    QString TorrentFile = QString(InfoHash.toHex()) + ".torrent";

    if(m_TorrentInfo->LoadTorrentFile(theCore->m_TorrentManager->GetTorrentDir() + TorrentFile))
    {
        if(m_TorrentInfo->GetInfoHash() == InfoHash)
        {
            CFile* pFile = GetFile();

            if(pFile->GetFileSize() == 0)
                pFile->SetFileSize(m_TorrentInfo->GetTotalLength());

            LoadPieceHashes();

            if(m_TorrentInfo->IsMultiFile() && !pFile->IsMultiFile())
            {
                CFileHashPtr pMasterHash = pFile->GetMasterHash();
                if(!pMasterHash.isNull() && pMasterHash->GetHash() == InfoHash)
                {
                    LogLine(LOG_DEBUG | LOG_ERROR, tr("The multi file %1 is missing its proper index, restoring form torrent").arg(pFile->GetFileName()));
                    InstallMetadata();
                }
            }

            if(!m_TorrentInfo->IsEmpty() && !pFile->IsComplete() && !pFile->GetPartMap())
                SetupPartMap();
        }
        else
        {
            LogLine(LOG_DEBUG | LOG_ERROR, tr("The torrent file %1 contains an invalid infohash").arg(TorrentFile));
            delete m_TorrentInfo;
            m_TorrentInfo = new CTorrentInfo(this);
            m_TorrentInfo->SetInfoHash(InfoHash);
        }
    }
    else
        m_TorrentInfo->SetInfoHash(InfoHash);
    theCore->m_TorrentManager->RegisterInfoHash(m_TorrentInfo->GetInfoHash());

    return true; // Note: that is always true even if we fail to load as we always wil be able to proceed one way or another
}