int CHttpDownloader::verifyAndGetNextPiece(CFile& file, IDownload* download) { //verify file by md5 if pieces.size == 0 if((download->pieces.empty()) && (download->hash!=NULL) && (download->hash->isSet())) { HashMD5 md5=HashMD5(); file.Hash(md5); if (md5.compare(download->hash)) { LOG_INFO("md5 correct: %s", md5.toString().c_str()); download->state=IDownload::STATE_FINISHED; showProcess(download, true); return -1; } else { LOG_ERROR("md5 sum missmatch %s %s", download->hash->toString().c_str(), md5.toString().c_str()); } } HashSHA1 sha1=HashSHA1(); unsigned alreadyDl=0; for(unsigned i=0; i<download->pieces.size(); i++ ) { //find first not downloaded piece showProcess(download, false); if (download->pieces[i].state==IDownload::STATE_FINISHED) { alreadyDl++; continue; } else if (download->pieces[i].state==IDownload::STATE_NONE) { if ((download->pieces[i].sha->isSet()) && (!file.IsNewFile())) { //reuse piece, if checksum is fine file.Hash(sha1, i); // LOG("bla %s %s", sha1.toString().c_str(), download.pieces[i].sha->toString().c_str()); if (sha1.compare(download->pieces[i].sha)) { // LOG_DEBUG("piece %d has already correct checksum, reusing", i); download->pieces[i].state=IDownload::STATE_FINISHED; showProcess(download, true); alreadyDl++; continue; } } return i; } } if (!download->pieces.empty()) { download->state=IDownload::STATE_FINISHED; showProcess(download, true); } return -1; }
// fonction de hachage // In: Clair c ----> Out: Empreinte void Contexte::h(string c, unsigned char *d) { unsigned char *toCrypt = (unsigned char *) c.c_str(); HashMD5(toCrypt, c.length(), d); }
bool CSdp::download() { if (downloaded) //allow download only once of the same sdp return true; filename=fileSystem->getSpringDir() + PATH_DELIMITER+"packages"+PATH_DELIMITER; LOG_DEBUG("%s",filename.c_str()); if (!fileSystem->directoryExists(filename)) { fileSystem->createSubdirs(filename); } int count=0; filename += this->md5 + ".sdp"; const std::string tmpFile = filename + ".tmp"; std::list<FileData*> files; bool rename = false; if (!fileSystem->fileExists(filename)) { //.sdp isn't avaiable, download it IDownload dl(tmpFile); dl.addMirror(url + "/packages/" + this->md5 + ".sdp"); httpDownload->download(&dl); fileSystem->parseSdp(tmpFile,files); //parse downloaded file rename = true; } else { fileSystem->parseSdp(filename,files); //parse downloaded file } std::list<FileData*>::iterator it; HashMD5 md5= HashMD5(); FileData tmp=FileData(); int i=0; for(it = files.begin(); it!=files.end(); ++it) { //check which file are available on local disk -> create list of files to download i++; md5.Set((*it)->md5, sizeof((*it)->md5)); std::string file; fileSystem->getPoolFilename(md5.toString(), file); if (!fileSystem->fileExists(file)) { //add non-existing files to download list count++; (*it)->download=true; } else { (*it)->download=false; } if (i%10==0) { LOG_DEBUG("\r%d/%d checked",i,(int)files.size()); } } LOG_DEBUG("\r%d/%d need to download %d files",i,(unsigned int)files.size(),count); std::string root = fileSystem->getSpringDir(); root += PATH_DELIMITER; root += "pool"; root += PATH_DELIMITER; if (!createPoolDirs(root)) { LOG_ERROR("Creating pool directories failed"); count = 0; } if (count>0) { downloaded=downloadStream(this->url+"/streamer.cgi?"+this->md5,files); LOG_DEBUG("Sucessfully downloaded %d files: %s %s",count,shortname.c_str(),name.c_str()); } else { LOG_DEBUG("Already downloaded: %s", shortname.c_str()); downloaded=true; } for(it = files.begin(); it!=files.end(); ++it) { //free memory delete *it; } if ((rename) && (!fileSystem->Rename(tmpFile, filename))) { LOG_ERROR("Couldn't rename %s to %s", tmpFile.c_str(), filename.c_str()); } return downloaded; }