acquire-item.cc 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acquire-item.cc,v 1.46.2.9 2004/01/16 18:51:11 mdz Exp $
  4. /* ######################################################################
  5. Acquire Item - Item to acquire
  6. Each item can download to exactly one file at a time. This means you
  7. cannot create an item that fetches two uri's to two files at the same
  8. time. The pkgAcqIndex class creates a second class upon instantiation
  9. to fetch the other index files because of this.
  10. ##################################################################### */
  11. /*}}}*/
  12. // Include Files /*{{{*/
  13. #ifdef __GNUG__
  14. #pragma implementation "apt-pkg/acquire-item.h"
  15. #endif
  16. #include <apt-pkg/acquire-item.h>
  17. #include <apt-pkg/configuration.h>
  18. #include <apt-pkg/sourcelist.h>
  19. #include <apt-pkg/vendorlist.h>
  20. #include <apt-pkg/error.h>
  21. #include <apt-pkg/strutl.h>
  22. #include <apt-pkg/fileutl.h>
  23. #include <apt-pkg/md5.h>
  24. #include <apt-pkg/sha1.h>
  25. #include <apt-pkg/tagfile.h>
  26. #include <apti18n.h>
  27. #include <sys/stat.h>
  28. #include <unistd.h>
  29. #include <errno.h>
  30. #include <string>
  31. #include <sstream>
  32. #include <stdio.h>
  33. /*}}}*/
  34. using namespace std;
  35. // Acquire::Item::Item - Constructor /*{{{*/
  36. // ---------------------------------------------------------------------
  37. /* */
  38. pkgAcquire::Item::Item(pkgAcquire *Owner) : Owner(Owner), FileSize(0),
  39. PartialSize(0), Mode(0), ID(0), Complete(false),
  40. Local(false), QueueCounter(0)
  41. {
  42. Owner->Add(this);
  43. Status = StatIdle;
  44. }
  45. /*}}}*/
  46. // Acquire::Item::~Item - Destructor /*{{{*/
  47. // ---------------------------------------------------------------------
  48. /* */
  49. pkgAcquire::Item::~Item()
  50. {
  51. Owner->Remove(this);
  52. }
  53. /*}}}*/
  54. // Acquire::Item::Failed - Item failed to download /*{{{*/
  55. // ---------------------------------------------------------------------
  56. /* We return to an idle state if there are still other queues that could
  57. fetch this object */
  58. void pkgAcquire::Item::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  59. {
  60. Status = StatIdle;
  61. ErrorText = LookupTag(Message,"Message");
  62. if (QueueCounter <= 1)
  63. {
  64. /* This indicates that the file is not available right now but might
  65. be sometime later. If we do a retry cycle then this should be
  66. retried [CDROMs] */
  67. if (Cnf->LocalOnly == true &&
  68. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  69. {
  70. Status = StatIdle;
  71. Dequeue();
  72. return;
  73. }
  74. Status = StatError;
  75. Dequeue();
  76. }
  77. }
  78. /*}}}*/
  79. // Acquire::Item::Start - Item has begun to download /*{{{*/
  80. // ---------------------------------------------------------------------
  81. /* Stash status and the file size. Note that setting Complete means
  82. sub-phases of the acquire process such as decompresion are operating */
  83. void pkgAcquire::Item::Start(string /*Message*/,unsigned long Size)
  84. {
  85. Status = StatFetching;
  86. if (FileSize == 0 && Complete == false)
  87. FileSize = Size;
  88. }
  89. /*}}}*/
  90. // Acquire::Item::Done - Item downloaded OK /*{{{*/
  91. // ---------------------------------------------------------------------
  92. /* */
  93. void pkgAcquire::Item::Done(string Message,unsigned long Size,string,
  94. pkgAcquire::MethodConfig *Cnf)
  95. {
  96. // We just downloaded something..
  97. string FileName = LookupTag(Message,"Filename");
  98. if (Complete == false && FileName == DestFile)
  99. {
  100. if (Owner->Log != 0)
  101. Owner->Log->Fetched(Size,atoi(LookupTag(Message,"Resume-Point","0").c_str()));
  102. }
  103. if (FileSize == 0)
  104. FileSize= Size;
  105. Status = StatDone;
  106. ErrorText = string();
  107. Owner->Dequeue(this);
  108. }
  109. /*}}}*/
  110. // Acquire::Item::Rename - Rename a file /*{{{*/
  111. // ---------------------------------------------------------------------
  112. /* This helper function is used by alot of item methods as thier final
  113. step */
  114. void pkgAcquire::Item::Rename(string From,string To)
  115. {
  116. if (rename(From.c_str(),To.c_str()) != 0)
  117. {
  118. char S[300];
  119. snprintf(S,sizeof(S),_("rename failed, %s (%s -> %s)."),strerror(errno),
  120. From.c_str(),To.c_str());
  121. Status = StatError;
  122. ErrorText = S;
  123. }
  124. }
  125. /*}}}*/
  126. // AcqDiffIndex::AcqDiffIndex - Constructor
  127. // ---------------------------------------------------------------------
  128. /* Get the DiffIndex file first and see if there are patches availabe
  129. * If so, create a pkgAcqIndexDiffs fetcher that will get and apply the
  130. * patches. If anything goes wrong in that process, it will fall back to
  131. * the original packages file
  132. */
  133. pkgAcqDiffIndex::pkgAcqDiffIndex(pkgAcquire *Owner,
  134. string URI,string URIDesc,string ShortDesc,
  135. string ExpectedMD5)
  136. : Item(Owner), RealURI(URI), ExpectedMD5(ExpectedMD5), Description(URIDesc)
  137. {
  138. Debug = _config->FindB("Debug::pkgAcquire::Diffs",false);
  139. Desc.Description = URIDesc + "/DiffIndex";
  140. Desc.Owner = this;
  141. Desc.ShortDesc = ShortDesc;
  142. Desc.URI = URI + ".diff/Index";
  143. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  144. DestFile += URItoFileName(URI) + string(".DiffIndex");
  145. if(Debug)
  146. std::clog << "pkgAcqDiffIndex: " << Desc.URI << std::endl;
  147. // look for the current package file
  148. CurrentPackagesFile = _config->FindDir("Dir::State::lists");
  149. CurrentPackagesFile += URItoFileName(RealURI);
  150. if(!FileExists(CurrentPackagesFile) ||
  151. !_config->FindB("Acquire::Diffs",true)) {
  152. // we don't have a pkg file or we don't want to queue
  153. if(Debug)
  154. std::clog << "No index file or canceld by user" << std::endl;
  155. Failed("", NULL);
  156. return;
  157. }
  158. if(Debug) {
  159. std::clog << "pkgAcqIndexDiffs::pkgAcqIndexDiffs(): "
  160. << CurrentPackagesFile << std::endl;
  161. }
  162. QueueURI(Desc);
  163. }
  164. // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
  165. // ---------------------------------------------------------------------
  166. /* The only header we use is the last-modified header. */
  167. string pkgAcqDiffIndex::Custom600Headers()
  168. {
  169. string Final = _config->FindDir("Dir::State::lists");
  170. Final += URItoFileName(RealURI) + string(".IndexDiff");
  171. if(Debug)
  172. std::clog << "Custom600Header-IMS: " << Final << std::endl;
  173. struct stat Buf;
  174. if (stat(Final.c_str(),&Buf) != 0)
  175. return "\nIndex-File: true";
  176. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  177. }
  178. bool pkgAcqDiffIndex::ParseDiffIndex(string IndexDiffFile)
  179. {
  180. if(Debug)
  181. std::clog << "pkgAcqIndexDiffs::ParseIndexDiff() " << IndexDiffFile
  182. << std::endl;
  183. pkgTagSection Tags;
  184. string ServerSha1;
  185. vector<DiffInfo> available_patches;
  186. FileFd Fd(IndexDiffFile,FileFd::ReadOnly);
  187. pkgTagFile TF(&Fd);
  188. if (_error->PendingError() == true)
  189. return false;
  190. if(TF.Step(Tags) == true)
  191. {
  192. string local_sha1;
  193. bool found = false;
  194. DiffInfo d;
  195. string size;
  196. string tmp = Tags.FindS("SHA1-Current");
  197. std::stringstream ss(tmp);
  198. ss >> ServerSha1;
  199. FileFd fd(CurrentPackagesFile, FileFd::ReadOnly);
  200. SHA1Summation SHA1;
  201. SHA1.AddFD(fd.Fd(), fd.Size());
  202. local_sha1 = string(SHA1.Result());
  203. if(local_sha1 == ServerSha1) {
  204. if(Debug)
  205. std::clog << "Package file is up-to-date" << std::endl;
  206. // set found to true, this will queue a pkgAcqIndexDiffs with
  207. // a empty availabe_patches
  208. found = true;
  209. } else {
  210. if(Debug)
  211. std::clog << "SHA1-Current: " << ServerSha1 << std::endl;
  212. // check the historie and see what patches we need
  213. string history = Tags.FindS("SHA1-History");
  214. std::stringstream hist(history);
  215. while(hist >> d.sha1 >> size >> d.file) {
  216. d.size = atoi(size.c_str());
  217. // read until the first match is found
  218. if(d.sha1 == local_sha1)
  219. found=true;
  220. // from that point on, we probably need all diffs
  221. if(found) {
  222. if(Debug)
  223. std::clog << "Need to get diff: " << d.file << std::endl;
  224. available_patches.push_back(d);
  225. }
  226. }
  227. }
  228. // no information how to get the patches, bail out
  229. if(!found) {
  230. if(Debug)
  231. std::clog << "Can't find a patch in the index file" << std::endl;
  232. // Failed will queue a big package file
  233. Failed("", NULL);
  234. } else {
  235. // queue the diffs
  236. new pkgAcqIndexDiffs(Owner, RealURI, Description, Desc.ShortDesc,
  237. ExpectedMD5, available_patches);
  238. Complete = false;
  239. Status = StatDone;
  240. Dequeue();
  241. return true;
  242. }
  243. }
  244. return false;
  245. }
  246. void pkgAcqDiffIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  247. {
  248. if(Debug)
  249. std::clog << "pkgAcqDiffIndex failed: " << Desc.URI << std::endl
  250. << "Falling back to normal index file aquire" << std::endl;
  251. new pkgAcqIndex(Owner, RealURI, Description, Desc.ShortDesc,
  252. ExpectedMD5);
  253. Complete = false;
  254. Status = StatDone;
  255. Dequeue();
  256. }
  257. void pkgAcqDiffIndex::Done(string Message,unsigned long Size,string Md5Hash,
  258. pkgAcquire::MethodConfig *Cnf)
  259. {
  260. if(Debug)
  261. std::clog << "pkgAcqDiffIndex::Done(): " << Desc.URI << std::endl;
  262. Item::Done(Message,Size,Md5Hash,Cnf);
  263. string FinalFile;
  264. FinalFile = _config->FindDir("Dir::State::lists")+URItoFileName(RealURI);
  265. // sucess in downloading the index
  266. // rename the index
  267. FinalFile += string(".IndexDiff");
  268. if(Debug)
  269. std::clog << "Renaming: " << DestFile << " -> " << FinalFile
  270. << std::endl;
  271. Rename(DestFile,FinalFile);
  272. chmod(FinalFile.c_str(),0644);
  273. DestFile = FinalFile;
  274. if(!ParseDiffIndex(DestFile))
  275. return Failed("", NULL);
  276. Complete = true;
  277. Status = StatDone;
  278. Dequeue();
  279. return;
  280. }
  281. // AcqIndexDiffs::AcqIndexDiffs - Constructor
  282. // ---------------------------------------------------------------------
  283. /* The package diff is added to the queue. one object is constructed
  284. * for each diff and the index
  285. */
  286. pkgAcqIndexDiffs::pkgAcqIndexDiffs(pkgAcquire *Owner,
  287. string URI,string URIDesc,string ShortDesc,
  288. string ExpectedMD5, vector<DiffInfo> diffs)
  289. : Item(Owner), RealURI(URI), ExpectedMD5(ExpectedMD5),
  290. available_patches(diffs)
  291. {
  292. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  293. DestFile += URItoFileName(URI);
  294. Debug = _config->FindB("Debug::pkgAcquire::Diffs",false);
  295. Desc.Description = URIDesc;
  296. Desc.Owner = this;
  297. Desc.ShortDesc = ShortDesc;
  298. if(available_patches.size() == 0) {
  299. // we are done (yeah!)
  300. Finish(true);
  301. } else {
  302. // get the next diff
  303. State = StateFetchDiff;
  304. QueueNextDiff();
  305. }
  306. }
  307. void pkgAcqIndexDiffs::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  308. {
  309. if(Debug)
  310. std::clog << "pkgAcqIndexDiffs failed: " << Desc.URI << std::endl
  311. << "Falling back to normal index file aquire" << std::endl;
  312. new pkgAcqIndex(Owner, RealURI, Description,Desc.ShortDesc,
  313. ExpectedMD5);
  314. Finish();
  315. }
  316. // helper that cleans the item out of the fetcher queue
  317. void pkgAcqIndexDiffs::Finish(bool allDone)
  318. {
  319. // we restore the original name, this is required, otherwise
  320. // the file will be cleaned
  321. if(allDone) {
  322. DestFile = _config->FindDir("Dir::State::lists");
  323. DestFile += URItoFileName(RealURI);
  324. // do the final md5sum checking
  325. MD5Summation sum;
  326. FileFd Fd(DestFile, FileFd::ReadOnly);
  327. sum.AddFD(Fd.Fd(), Fd.Size());
  328. Fd.Close();
  329. string MD5 = (string)sum.Result();
  330. if (!ExpectedMD5.empty() && MD5 != ExpectedMD5)
  331. {
  332. Status = StatAuthError;
  333. ErrorText = _("MD5Sum mismatch");
  334. Rename(DestFile,DestFile + ".FAILED");
  335. Dequeue();
  336. return;
  337. }
  338. // this is for the "real" finish
  339. Complete = true;
  340. Status = StatDone;
  341. Dequeue();
  342. if(Debug)
  343. std::clog << "\n\nallDone: " << DestFile << "\n" << std::endl;
  344. return;
  345. }
  346. if(Debug)
  347. std::clog << "Finishing: " << Desc.URI << std::endl;
  348. Complete = false;
  349. Status = StatDone;
  350. Dequeue();
  351. return;
  352. }
  353. bool pkgAcqIndexDiffs::QueueNextDiff()
  354. {
  355. // calc sha1 of the just patched file
  356. string FinalFile = _config->FindDir("Dir::State::lists");
  357. FinalFile += URItoFileName(RealURI);
  358. FileFd fd(FinalFile, FileFd::ReadOnly);
  359. SHA1Summation SHA1;
  360. SHA1.AddFD(fd.Fd(), fd.Size());
  361. string local_sha1 = string(SHA1.Result());
  362. if(Debug)
  363. std::clog << "QueueNextDiff: "
  364. << FinalFile << " (" << local_sha1 << ")"<<std::endl;
  365. // remove all patches until the next matching patch is found
  366. // this requires the Index file to be ordered
  367. for(vector<DiffInfo>::iterator I=available_patches.begin();
  368. available_patches.size() > 0 && I != available_patches.end()
  369. && (*I).sha1 != local_sha1;
  370. I++) {
  371. available_patches.erase(I);
  372. }
  373. // error checking and falling back if no patch was found
  374. if(available_patches.size() == 0) {
  375. Failed("", NULL);
  376. return false;
  377. }
  378. // queue the right diff
  379. Desc.URI = string(RealURI) + ".diff/" + available_patches[0].file + ".gz";
  380. Desc.Description = available_patches[0].file + string(".pdiff");
  381. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  382. DestFile += URItoFileName(RealURI + ".diff/" + available_patches[0].file);
  383. if(Debug)
  384. std::clog << "pkgAcqIndexDiffs::QueueNextDiff(): " << Desc.URI << std::endl;
  385. QueueURI(Desc);
  386. return true;
  387. }
  388. void pkgAcqIndexDiffs::Done(string Message,unsigned long Size,string Md5Hash,
  389. pkgAcquire::MethodConfig *Cnf)
  390. {
  391. if(Debug)
  392. std::clog << "pkgAcqIndexDiffs::Done(): " << Desc.URI << std::endl;
  393. Item::Done(Message,Size,Md5Hash,Cnf);
  394. string FinalFile;
  395. FinalFile = _config->FindDir("Dir::State::lists")+URItoFileName(RealURI);
  396. // sucess in downloading a diff, enter ApplyDiff state
  397. if(State == StateFetchDiff)
  398. {
  399. if(Debug)
  400. std::clog << "Sending to gzip method: " << FinalFile << std::endl;
  401. string FileName = LookupTag(Message,"Filename");
  402. State = StateUnzipDiff;
  403. Desc.URI = "gzip:" + FileName;
  404. DestFile += ".decomp";
  405. QueueURI(Desc);
  406. Mode = "gzip";
  407. return;
  408. }
  409. // sucess in downloading a diff, enter ApplyDiff state
  410. if(State == StateUnzipDiff)
  411. {
  412. // rred excepts the patch as $FinalFile.ed
  413. Rename(DestFile,FinalFile+".ed");
  414. if(Debug)
  415. std::clog << "Sending to rred method: " << FinalFile << std::endl;
  416. State = StateApplyDiff;
  417. Desc.URI = "rred:" + FinalFile;
  418. QueueURI(Desc);
  419. Mode = "rred";
  420. return;
  421. }
  422. // success in download/apply a diff, queue next (if needed)
  423. if(State == StateApplyDiff)
  424. {
  425. // remove the just applied patch
  426. available_patches.erase(available_patches.begin());
  427. // move into place
  428. if(Debug)
  429. {
  430. std::clog << "Moving patched file in place: " << std::endl
  431. << DestFile << " -> " << FinalFile << std::endl;
  432. }
  433. Rename(DestFile,FinalFile);
  434. // see if there is more to download
  435. if(available_patches.size() > 0) {
  436. new pkgAcqIndexDiffs(Owner, RealURI, Description, Desc.ShortDesc,
  437. ExpectedMD5, available_patches);
  438. return Finish();
  439. } else
  440. return Finish(true);
  441. }
  442. }
  443. // AcqIndex::AcqIndex - Constructor /*{{{*/
  444. // ---------------------------------------------------------------------
  445. /* The package file is added to the queue and a second class is
  446. instantiated to fetch the revision file */
  447. pkgAcqIndex::pkgAcqIndex(pkgAcquire *Owner,
  448. string URI,string URIDesc,string ShortDesc,
  449. string ExpectedMD5, string comprExt)
  450. : Item(Owner), RealURI(URI), ExpectedMD5(ExpectedMD5)
  451. {
  452. Decompression = false;
  453. Erase = false;
  454. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  455. DestFile += URItoFileName(URI);
  456. if(comprExt.empty())
  457. {
  458. // autoselect the compression method
  459. if(FileExists("/usr/bin/bzip2"))
  460. CompressionExtension = ".bz2";
  461. else
  462. CompressionExtension = ".gz";
  463. } else {
  464. CompressionExtension = comprExt;
  465. }
  466. Desc.URI = URI + CompressionExtension;
  467. Desc.Description = URIDesc;
  468. Desc.Owner = this;
  469. Desc.ShortDesc = ShortDesc;
  470. QueueURI(Desc);
  471. }
  472. /*}}}*/
  473. // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
  474. // ---------------------------------------------------------------------
  475. /* The only header we use is the last-modified header. */
  476. string pkgAcqIndex::Custom600Headers()
  477. {
  478. string Final = _config->FindDir("Dir::State::lists");
  479. Final += URItoFileName(RealURI);
  480. struct stat Buf;
  481. if (stat(Final.c_str(),&Buf) != 0)
  482. return "\nIndex-File: true";
  483. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  484. }
  485. /*}}}*/
  486. void pkgAcqIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  487. {
  488. // no .bz2 found, retry with .gz
  489. if(Desc.URI.substr(Desc.URI.size()-3,Desc.URI.size()-1) == "bz2") {
  490. Desc.URI = Desc.URI.substr(0,Desc.URI.size()-3) + "gz";
  491. // retry with a gzip one
  492. new pkgAcqIndex(Owner, RealURI, Desc.Description,Desc.ShortDesc,
  493. ExpectedMD5, string(".gz"));
  494. Status = StatDone;
  495. Complete = false;
  496. Dequeue();
  497. return;
  498. }
  499. Item::Failed(Message,Cnf);
  500. }
  501. // AcqIndex::Done - Finished a fetch /*{{{*/
  502. // ---------------------------------------------------------------------
  503. /* This goes through a number of states.. On the initial fetch the
  504. method could possibly return an alternate filename which points
  505. to the uncompressed version of the file. If this is so the file
  506. is copied into the partial directory. In all other cases the file
  507. is decompressed with a gzip uri. */
  508. void pkgAcqIndex::Done(string Message,unsigned long Size,string MD5,
  509. pkgAcquire::MethodConfig *Cfg)
  510. {
  511. Item::Done(Message,Size,MD5,Cfg);
  512. if (Decompression == true)
  513. {
  514. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  515. {
  516. std::cerr << std::endl << RealURI << ": Computed MD5: " << MD5;
  517. std::cerr << " Expected MD5: " << ExpectedMD5 << std::endl;
  518. }
  519. if (MD5.empty())
  520. {
  521. MD5Summation sum;
  522. FileFd Fd(DestFile, FileFd::ReadOnly);
  523. sum.AddFD(Fd.Fd(), Fd.Size());
  524. Fd.Close();
  525. MD5 = (string)sum.Result();
  526. }
  527. if (!ExpectedMD5.empty() && MD5 != ExpectedMD5)
  528. {
  529. Status = StatAuthError;
  530. ErrorText = _("MD5Sum mismatch");
  531. Rename(DestFile,DestFile + ".FAILED");
  532. return;
  533. }
  534. // Done, move it into position
  535. string FinalFile = _config->FindDir("Dir::State::lists");
  536. FinalFile += URItoFileName(RealURI);
  537. Rename(DestFile,FinalFile);
  538. chmod(FinalFile.c_str(),0644);
  539. /* We restore the original name to DestFile so that the clean operation
  540. will work OK */
  541. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  542. DestFile += URItoFileName(RealURI);
  543. // Remove the compressed version.
  544. if (Erase == true)
  545. unlink(DestFile.c_str());
  546. return;
  547. }
  548. Erase = false;
  549. Complete = true;
  550. // Handle the unzipd case
  551. string FileName = LookupTag(Message,"Alt-Filename");
  552. if (FileName.empty() == false)
  553. {
  554. // The files timestamp matches
  555. if (StringToBool(LookupTag(Message,"Alt-IMS-Hit"),false) == true)
  556. return;
  557. Decompression = true;
  558. Local = true;
  559. DestFile += ".decomp";
  560. Desc.URI = "copy:" + FileName;
  561. QueueURI(Desc);
  562. Mode = "copy";
  563. return;
  564. }
  565. FileName = LookupTag(Message,"Filename");
  566. if (FileName.empty() == true)
  567. {
  568. Status = StatError;
  569. ErrorText = "Method gave a blank filename";
  570. }
  571. // The files timestamp matches
  572. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
  573. return;
  574. if (FileName == DestFile)
  575. Erase = true;
  576. else
  577. Local = true;
  578. string compExt = Desc.URI.substr(Desc.URI.size()-3,Desc.URI.size()-1);
  579. char *decompProg;
  580. if(compExt == "bz2")
  581. decompProg = "bzip2";
  582. else if(compExt == ".gz")
  583. decompProg = "gzip";
  584. else {
  585. _error->Error("Unsupported extension: %s", compExt.c_str());
  586. return;
  587. }
  588. Decompression = true;
  589. DestFile += ".decomp";
  590. Desc.URI = string(decompProg) + ":" + FileName;
  591. QueueURI(Desc);
  592. Mode = decompProg;
  593. }
  594. // AcqIndexTrans::pkgAcqIndexTrans - Constructor /*{{{*/
  595. // ---------------------------------------------------------------------
  596. /* The Translation file is added to the queue */
  597. pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcquire *Owner,
  598. string URI,string URIDesc,string ShortDesc) :
  599. pkgAcqIndex(Owner, URI, URIDesc, ShortDesc, "", "")
  600. {
  601. }
  602. /*}}}*/
  603. // AcqIndexTrans::Failed - Silence failure messages for missing files /*{{{*/
  604. // ---------------------------------------------------------------------
  605. /* */
  606. void pkgAcqIndexTrans::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  607. {
  608. if (Cnf->LocalOnly == true ||
  609. StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
  610. {
  611. // Ignore this
  612. Status = StatDone;
  613. Complete = false;
  614. Dequeue();
  615. return;
  616. }
  617. Item::Failed(Message,Cnf);
  618. }
  619. /*}}}*/
  620. pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire *Owner,
  621. string URI,string URIDesc,string ShortDesc,
  622. string MetaIndexURI, string MetaIndexURIDesc,
  623. string MetaIndexShortDesc,
  624. const vector<IndexTarget*>* IndexTargets,
  625. indexRecords* MetaIndexParser) :
  626. Item(Owner), RealURI(URI), MetaIndexURI(MetaIndexURI),
  627. MetaIndexURIDesc(MetaIndexURIDesc), MetaIndexShortDesc(MetaIndexShortDesc)
  628. {
  629. this->MetaIndexParser = MetaIndexParser;
  630. this->IndexTargets = IndexTargets;
  631. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  632. DestFile += URItoFileName(URI);
  633. // remove any partial downloaded sig-file. it may confuse proxies
  634. // and is too small to warrant a partial download anyway
  635. unlink(DestFile.c_str());
  636. // Create the item
  637. Desc.Description = URIDesc;
  638. Desc.Owner = this;
  639. Desc.ShortDesc = ShortDesc;
  640. Desc.URI = URI;
  641. string Final = _config->FindDir("Dir::State::lists");
  642. Final += URItoFileName(RealURI);
  643. struct stat Buf;
  644. if (stat(Final.c_str(),&Buf) == 0)
  645. {
  646. // File was already in place. It needs to be re-verified
  647. // because Release might have changed, so Move it into partial
  648. Rename(Final,DestFile);
  649. // unlink the file and do not try to use I-M-S and Last-Modified
  650. // if the users proxy is broken
  651. if(_config->FindB("Acquire::BrokenProxy", false) == true) {
  652. std::cerr << "forcing re-get of the signature file as requested" << std::endl;
  653. unlink(DestFile.c_str());
  654. }
  655. }
  656. QueueURI(Desc);
  657. }
  658. /*}}}*/
  659. // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
  660. // ---------------------------------------------------------------------
  661. /* The only header we use is the last-modified header. */
  662. string pkgAcqMetaSig::Custom600Headers()
  663. {
  664. struct stat Buf;
  665. if (stat(DestFile.c_str(),&Buf) != 0)
  666. return "\nIndex-File: true";
  667. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  668. }
  669. void pkgAcqMetaSig::Done(string Message,unsigned long Size,string MD5,
  670. pkgAcquire::MethodConfig *Cfg)
  671. {
  672. Item::Done(Message,Size,MD5,Cfg);
  673. string FileName = LookupTag(Message,"Filename");
  674. if (FileName.empty() == true)
  675. {
  676. Status = StatError;
  677. ErrorText = "Method gave a blank filename";
  678. return;
  679. }
  680. if (FileName != DestFile)
  681. {
  682. // We have to copy it into place
  683. Local = true;
  684. Desc.URI = "copy:" + FileName;
  685. QueueURI(Desc);
  686. return;
  687. }
  688. Complete = true;
  689. // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
  690. new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
  691. DestFile, IndexTargets, MetaIndexParser);
  692. }
  693. /*}}}*/
  694. void pkgAcqMetaSig::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  695. {
  696. // Delete any existing sigfile, so that this source isn't
  697. // mistakenly trusted
  698. string Final = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
  699. unlink(Final.c_str());
  700. // if we get a timeout if fail
  701. if(LookupTag(Message,"FailReason") == "Timeout" ||
  702. LookupTag(Message,"FailReason") == "TmpResolveFailure") {
  703. Item::Failed(Message,Cnf);
  704. return;
  705. }
  706. // queue a pkgAcqMetaIndex with no sigfile
  707. new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
  708. "", IndexTargets, MetaIndexParser);
  709. if (Cnf->LocalOnly == true ||
  710. StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
  711. {
  712. // Ignore this
  713. Status = StatDone;
  714. Complete = false;
  715. Dequeue();
  716. return;
  717. }
  718. Item::Failed(Message,Cnf);
  719. }
  720. pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire *Owner,
  721. string URI,string URIDesc,string ShortDesc,
  722. string SigFile,
  723. const vector<struct IndexTarget*>* IndexTargets,
  724. indexRecords* MetaIndexParser) :
  725. Item(Owner), RealURI(URI), SigFile(SigFile)
  726. {
  727. this->AuthPass = false;
  728. this->MetaIndexParser = MetaIndexParser;
  729. this->IndexTargets = IndexTargets;
  730. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  731. DestFile += URItoFileName(URI);
  732. // Create the item
  733. Desc.Description = URIDesc;
  734. Desc.Owner = this;
  735. Desc.ShortDesc = ShortDesc;
  736. Desc.URI = URI;
  737. QueueURI(Desc);
  738. }
  739. /*}}}*/
  740. // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
  741. // ---------------------------------------------------------------------
  742. /* The only header we use is the last-modified header. */
  743. string pkgAcqMetaIndex::Custom600Headers()
  744. {
  745. string Final = _config->FindDir("Dir::State::lists");
  746. Final += URItoFileName(RealURI);
  747. struct stat Buf;
  748. if (stat(Final.c_str(),&Buf) != 0)
  749. return "\nIndex-File: true";
  750. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  751. }
  752. void pkgAcqMetaIndex::Done(string Message,unsigned long Size,string MD5,
  753. pkgAcquire::MethodConfig *Cfg)
  754. {
  755. Item::Done(Message,Size,MD5,Cfg);
  756. // MetaIndexes are done in two passes: one to download the
  757. // metaindex with an appropriate method, and a second to verify it
  758. // with the gpgv method
  759. if (AuthPass == true)
  760. {
  761. AuthDone(Message);
  762. }
  763. else
  764. {
  765. RetrievalDone(Message);
  766. if (!Complete)
  767. // Still more retrieving to do
  768. return;
  769. if (SigFile == "")
  770. {
  771. // There was no signature file, so we are finished. Download
  772. // the indexes without verification.
  773. QueueIndexes(false);
  774. }
  775. else
  776. {
  777. // There was a signature file, so pass it to gpgv for
  778. // verification
  779. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  780. std::cerr << "Metaindex acquired, queueing gpg verification ("
  781. << SigFile << "," << DestFile << ")\n";
  782. AuthPass = true;
  783. Desc.URI = "gpgv:" + SigFile;
  784. QueueURI(Desc);
  785. Mode = "gpgv";
  786. }
  787. }
  788. }
  789. void pkgAcqMetaIndex::RetrievalDone(string Message)
  790. {
  791. // We have just finished downloading a Release file (it is not
  792. // verified yet)
  793. string FileName = LookupTag(Message,"Filename");
  794. if (FileName.empty() == true)
  795. {
  796. Status = StatError;
  797. ErrorText = "Method gave a blank filename";
  798. return;
  799. }
  800. if (FileName != DestFile)
  801. {
  802. Local = true;
  803. Desc.URI = "copy:" + FileName;
  804. QueueURI(Desc);
  805. return;
  806. }
  807. Complete = true;
  808. string FinalFile = _config->FindDir("Dir::State::lists");
  809. FinalFile += URItoFileName(RealURI);
  810. // The files timestamp matches
  811. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == false)
  812. {
  813. // Move it into position
  814. Rename(DestFile,FinalFile);
  815. }
  816. DestFile = FinalFile;
  817. }
  818. void pkgAcqMetaIndex::AuthDone(string Message)
  819. {
  820. // At this point, the gpgv method has succeeded, so there is a
  821. // valid signature from a key in the trusted keyring. We
  822. // perform additional verification of its contents, and use them
  823. // to verify the indexes we are about to download
  824. if (!MetaIndexParser->Load(DestFile))
  825. {
  826. Status = StatAuthError;
  827. ErrorText = MetaIndexParser->ErrorText;
  828. return;
  829. }
  830. if (!VerifyVendor())
  831. {
  832. return;
  833. }
  834. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  835. std::cerr << "Signature verification succeeded: "
  836. << DestFile << std::endl;
  837. // Download further indexes with verification
  838. QueueIndexes(true);
  839. // Done, move signature file into position
  840. string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
  841. URItoFileName(RealURI) + ".gpg";
  842. Rename(SigFile,VerifiedSigFile);
  843. chmod(VerifiedSigFile.c_str(),0644);
  844. }
  845. void pkgAcqMetaIndex::QueueIndexes(bool verify)
  846. {
  847. for (vector <struct IndexTarget*>::const_iterator Target = IndexTargets->begin();
  848. Target != IndexTargets->end();
  849. Target++)
  850. {
  851. string ExpectedIndexMD5;
  852. if (verify)
  853. {
  854. const indexRecords::checkSum *Record = MetaIndexParser->Lookup((*Target)->MetaKey);
  855. if (!Record)
  856. {
  857. Status = StatAuthError;
  858. ErrorText = "Unable to find expected entry "
  859. + (*Target)->MetaKey + " in Meta-index file (malformed Release file?)";
  860. return;
  861. }
  862. ExpectedIndexMD5 = Record->MD5Hash;
  863. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  864. {
  865. std::cerr << "Queueing: " << (*Target)->URI << std::endl;
  866. std::cerr << "Expected MD5: " << ExpectedIndexMD5 << std::endl;
  867. }
  868. if (ExpectedIndexMD5.empty())
  869. {
  870. Status = StatAuthError;
  871. ErrorText = "Unable to find MD5 sum for "
  872. + (*Target)->MetaKey + " in Meta-index file";
  873. return;
  874. }
  875. }
  876. // Queue Packages file
  877. new pkgAcqDiffIndex(Owner, (*Target)->URI, (*Target)->Description,
  878. (*Target)->ShortDesc, ExpectedIndexMD5);
  879. }
  880. }
  881. bool pkgAcqMetaIndex::VerifyVendor()
  882. {
  883. // // Maybe this should be made available from above so we don't have
  884. // // to read and parse it every time?
  885. // pkgVendorList List;
  886. // List.ReadMainList();
  887. // const Vendor* Vndr = NULL;
  888. // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
  889. // {
  890. // string::size_type pos = (*I).find("VALIDSIG ");
  891. // if (_config->FindB("Debug::Vendor", false))
  892. // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
  893. // << std::endl;
  894. // if (pos != std::string::npos)
  895. // {
  896. // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
  897. // if (_config->FindB("Debug::Vendor", false))
  898. // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
  899. // std::endl;
  900. // Vndr = List.FindVendor(Fingerprint) != "";
  901. // if (Vndr != NULL);
  902. // break;
  903. // }
  904. // }
  905. string Transformed = MetaIndexParser->GetExpectedDist();
  906. if (Transformed == "../project/experimental")
  907. {
  908. Transformed = "experimental";
  909. }
  910. string::size_type pos = Transformed.rfind('/');
  911. if (pos != string::npos)
  912. {
  913. Transformed = Transformed.substr(0, pos);
  914. }
  915. if (Transformed == ".")
  916. {
  917. Transformed = "";
  918. }
  919. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  920. {
  921. std::cerr << "Got Codename: " << MetaIndexParser->GetDist() << std::endl;
  922. std::cerr << "Expecting Dist: " << MetaIndexParser->GetExpectedDist() << std::endl;
  923. std::cerr << "Transformed Dist: " << Transformed << std::endl;
  924. }
  925. if (MetaIndexParser->CheckDist(Transformed) == false)
  926. {
  927. // This might become fatal one day
  928. // Status = StatAuthError;
  929. // ErrorText = "Conflicting distribution; expected "
  930. // + MetaIndexParser->GetExpectedDist() + " but got "
  931. // + MetaIndexParser->GetDist();
  932. // return false;
  933. if (!Transformed.empty())
  934. {
  935. _error->Warning("Conflicting distribution: %s (expected %s but got %s)",
  936. Desc.Description.c_str(),
  937. Transformed.c_str(),
  938. MetaIndexParser->GetDist().c_str());
  939. }
  940. }
  941. return true;
  942. }
  943. /*}}}*/
  944. // pkgAcqMetaIndex::Failed - no Release file present or no signature
  945. // file present /*{{{*/
  946. // ---------------------------------------------------------------------
  947. /* */
  948. void pkgAcqMetaIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  949. {
  950. if (AuthPass == true)
  951. {
  952. // gpgv method failed
  953. _error->Warning("GPG error: %s: %s",
  954. Desc.Description.c_str(),
  955. LookupTag(Message,"Message").c_str());
  956. }
  957. // No Release file was present, or verification failed, so fall
  958. // back to queueing Packages files without verification
  959. QueueIndexes(false);
  960. }
  961. /*}}}*/
  962. // AcqArchive::AcqArchive - Constructor /*{{{*/
  963. // ---------------------------------------------------------------------
  964. /* This just sets up the initial fetch environment and queues the first
  965. possibilitiy */
  966. pkgAcqArchive::pkgAcqArchive(pkgAcquire *Owner,pkgSourceList *Sources,
  967. pkgRecords *Recs,pkgCache::VerIterator const &Version,
  968. string &StoreFilename) :
  969. Item(Owner), Version(Version), Sources(Sources), Recs(Recs),
  970. StoreFilename(StoreFilename), Vf(Version.FileList()),
  971. Trusted(false)
  972. {
  973. Retries = _config->FindI("Acquire::Retries",0);
  974. if (Version.Arch() == 0)
  975. {
  976. _error->Error(_("I wasn't able to locate a file for the %s package. "
  977. "This might mean you need to manually fix this package. "
  978. "(due to missing arch)"),
  979. Version.ParentPkg().Name());
  980. return;
  981. }
  982. /* We need to find a filename to determine the extension. We make the
  983. assumption here that all the available sources for this version share
  984. the same extension.. */
  985. // Skip not source sources, they do not have file fields.
  986. for (; Vf.end() == false; Vf++)
  987. {
  988. if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
  989. continue;
  990. break;
  991. }
  992. // Does not really matter here.. we are going to fail out below
  993. if (Vf.end() != true)
  994. {
  995. // If this fails to get a file name we will bomb out below.
  996. pkgRecords::Parser &Parse = Recs->Lookup(Vf);
  997. if (_error->PendingError() == true)
  998. return;
  999. // Generate the final file name as: package_version_arch.foo
  1000. StoreFilename = QuoteString(Version.ParentPkg().Name(),"_:") + '_' +
  1001. QuoteString(Version.VerStr(),"_:") + '_' +
  1002. QuoteString(Version.Arch(),"_:.") +
  1003. "." + flExtension(Parse.FileName());
  1004. }
  1005. // check if we have one trusted source for the package. if so, switch
  1006. // to "TrustedOnly" mode
  1007. for (pkgCache::VerFileIterator i = Version.FileList(); i.end() == false; i++)
  1008. {
  1009. pkgIndexFile *Index;
  1010. if (Sources->FindIndex(i.File(),Index) == false)
  1011. continue;
  1012. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  1013. {
  1014. std::cerr << "Checking index: " << Index->Describe()
  1015. << "(Trusted=" << Index->IsTrusted() << ")\n";
  1016. }
  1017. if (Index->IsTrusted()) {
  1018. Trusted = true;
  1019. break;
  1020. }
  1021. }
  1022. // "allow-unauthenticated" restores apts old fetching behaviour
  1023. // that means that e.g. unauthenticated file:// uris are higher
  1024. // priority than authenticated http:// uris
  1025. if (_config->FindB("APT::Get::AllowUnauthenticated",false) == true)
  1026. Trusted = false;
  1027. // Select a source
  1028. if (QueueNext() == false && _error->PendingError() == false)
  1029. _error->Error(_("I wasn't able to locate file for the %s package. "
  1030. "This might mean you need to manually fix this package."),
  1031. Version.ParentPkg().Name());
  1032. }
  1033. /*}}}*/
  1034. // AcqArchive::QueueNext - Queue the next file source /*{{{*/
  1035. // ---------------------------------------------------------------------
  1036. /* This queues the next available file version for download. It checks if
  1037. the archive is already available in the cache and stashs the MD5 for
  1038. checking later. */
  1039. bool pkgAcqArchive::QueueNext()
  1040. {
  1041. for (; Vf.end() == false; Vf++)
  1042. {
  1043. // Ignore not source sources
  1044. if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
  1045. continue;
  1046. // Try to cross match against the source list
  1047. pkgIndexFile *Index;
  1048. if (Sources->FindIndex(Vf.File(),Index) == false)
  1049. continue;
  1050. // only try to get a trusted package from another source if that source
  1051. // is also trusted
  1052. if(Trusted && !Index->IsTrusted())
  1053. continue;
  1054. // Grab the text package record
  1055. pkgRecords::Parser &Parse = Recs->Lookup(Vf);
  1056. if (_error->PendingError() == true)
  1057. return false;
  1058. string PkgFile = Parse.FileName();
  1059. MD5 = Parse.MD5Hash();
  1060. if (PkgFile.empty() == true)
  1061. return _error->Error(_("The package index files are corrupted. No Filename: "
  1062. "field for package %s."),
  1063. Version.ParentPkg().Name());
  1064. Desc.URI = Index->ArchiveURI(PkgFile);
  1065. Desc.Description = Index->ArchiveInfo(Version);
  1066. Desc.Owner = this;
  1067. Desc.ShortDesc = Version.ParentPkg().Name();
  1068. // See if we already have the file. (Legacy filenames)
  1069. FileSize = Version->Size;
  1070. string FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile);
  1071. struct stat Buf;
  1072. if (stat(FinalFile.c_str(),&Buf) == 0)
  1073. {
  1074. // Make sure the size matches
  1075. if ((unsigned)Buf.st_size == Version->Size)
  1076. {
  1077. Complete = true;
  1078. Local = true;
  1079. Status = StatDone;
  1080. StoreFilename = DestFile = FinalFile;
  1081. return true;
  1082. }
  1083. /* Hmm, we have a file and its size does not match, this means it is
  1084. an old style mismatched arch */
  1085. unlink(FinalFile.c_str());
  1086. }
  1087. // Check it again using the new style output filenames
  1088. FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename);
  1089. if (stat(FinalFile.c_str(),&Buf) == 0)
  1090. {
  1091. // Make sure the size matches
  1092. if ((unsigned)Buf.st_size == Version->Size)
  1093. {
  1094. Complete = true;
  1095. Local = true;
  1096. Status = StatDone;
  1097. StoreFilename = DestFile = FinalFile;
  1098. return true;
  1099. }
  1100. /* Hmm, we have a file and its size does not match, this shouldnt
  1101. happen.. */
  1102. unlink(FinalFile.c_str());
  1103. }
  1104. DestFile = _config->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename);
  1105. // Check the destination file
  1106. if (stat(DestFile.c_str(),&Buf) == 0)
  1107. {
  1108. // Hmm, the partial file is too big, erase it
  1109. if ((unsigned)Buf.st_size > Version->Size)
  1110. unlink(DestFile.c_str());
  1111. else
  1112. PartialSize = Buf.st_size;
  1113. }
  1114. // Create the item
  1115. Local = false;
  1116. Desc.URI = Index->ArchiveURI(PkgFile);
  1117. Desc.Description = Index->ArchiveInfo(Version);
  1118. Desc.Owner = this;
  1119. Desc.ShortDesc = Version.ParentPkg().Name();
  1120. QueueURI(Desc);
  1121. Vf++;
  1122. return true;
  1123. }
  1124. return false;
  1125. }
  1126. /*}}}*/
  1127. // AcqArchive::Done - Finished fetching /*{{{*/
  1128. // ---------------------------------------------------------------------
  1129. /* */
  1130. void pkgAcqArchive::Done(string Message,unsigned long Size,string Md5Hash,
  1131. pkgAcquire::MethodConfig *Cfg)
  1132. {
  1133. Item::Done(Message,Size,Md5Hash,Cfg);
  1134. // Check the size
  1135. if (Size != Version->Size)
  1136. {
  1137. Status = StatError;
  1138. ErrorText = _("Size mismatch");
  1139. return;
  1140. }
  1141. // Check the md5
  1142. if (Md5Hash.empty() == false && MD5.empty() == false)
  1143. {
  1144. if (Md5Hash != MD5)
  1145. {
  1146. Status = StatError;
  1147. ErrorText = _("MD5Sum mismatch");
  1148. if(FileExists(DestFile))
  1149. Rename(DestFile,DestFile + ".FAILED");
  1150. return;
  1151. }
  1152. }
  1153. // Grab the output filename
  1154. string FileName = LookupTag(Message,"Filename");
  1155. if (FileName.empty() == true)
  1156. {
  1157. Status = StatError;
  1158. ErrorText = "Method gave a blank filename";
  1159. return;
  1160. }
  1161. Complete = true;
  1162. // Reference filename
  1163. if (FileName != DestFile)
  1164. {
  1165. StoreFilename = DestFile = FileName;
  1166. Local = true;
  1167. return;
  1168. }
  1169. // Done, move it into position
  1170. string FinalFile = _config->FindDir("Dir::Cache::Archives");
  1171. FinalFile += flNotDir(StoreFilename);
  1172. Rename(DestFile,FinalFile);
  1173. StoreFilename = DestFile = FinalFile;
  1174. Complete = true;
  1175. }
  1176. /*}}}*/
  1177. // AcqArchive::Failed - Failure handler /*{{{*/
  1178. // ---------------------------------------------------------------------
  1179. /* Here we try other sources */
  1180. void pkgAcqArchive::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  1181. {
  1182. ErrorText = LookupTag(Message,"Message");
  1183. /* We don't really want to retry on failed media swaps, this prevents
  1184. that. An interesting observation is that permanent failures are not
  1185. recorded. */
  1186. if (Cnf->Removable == true &&
  1187. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  1188. {
  1189. // Vf = Version.FileList();
  1190. while (Vf.end() == false) Vf++;
  1191. StoreFilename = string();
  1192. Item::Failed(Message,Cnf);
  1193. return;
  1194. }
  1195. if (QueueNext() == false)
  1196. {
  1197. // This is the retry counter
  1198. if (Retries != 0 &&
  1199. Cnf->LocalOnly == false &&
  1200. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  1201. {
  1202. Retries--;
  1203. Vf = Version.FileList();
  1204. if (QueueNext() == true)
  1205. return;
  1206. }
  1207. StoreFilename = string();
  1208. Item::Failed(Message,Cnf);
  1209. }
  1210. }
  1211. /*}}}*/
  1212. // AcqArchive::IsTrusted - Determine whether this archive comes from a
  1213. // trusted source /*{{{*/
  1214. // ---------------------------------------------------------------------
  1215. bool pkgAcqArchive::IsTrusted()
  1216. {
  1217. return Trusted;
  1218. }
  1219. // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
  1220. // ---------------------------------------------------------------------
  1221. /* */
  1222. void pkgAcqArchive::Finished()
  1223. {
  1224. if (Status == pkgAcquire::Item::StatDone &&
  1225. Complete == true)
  1226. return;
  1227. StoreFilename = string();
  1228. }
  1229. /*}}}*/
  1230. // AcqFile::pkgAcqFile - Constructor /*{{{*/
  1231. // ---------------------------------------------------------------------
  1232. /* The file is added to the queue */
  1233. pkgAcqFile::pkgAcqFile(pkgAcquire *Owner,string URI,string MD5,
  1234. unsigned long Size,string Dsc,string ShortDesc) :
  1235. Item(Owner), Md5Hash(MD5)
  1236. {
  1237. Retries = _config->FindI("Acquire::Retries",0);
  1238. DestFile = flNotDir(URI);
  1239. // Create the item
  1240. Desc.URI = URI;
  1241. Desc.Description = Dsc;
  1242. Desc.Owner = this;
  1243. // Set the short description to the archive component
  1244. Desc.ShortDesc = ShortDesc;
  1245. // Get the transfer sizes
  1246. FileSize = Size;
  1247. struct stat Buf;
  1248. if (stat(DestFile.c_str(),&Buf) == 0)
  1249. {
  1250. // Hmm, the partial file is too big, erase it
  1251. if ((unsigned)Buf.st_size > Size)
  1252. unlink(DestFile.c_str());
  1253. else
  1254. PartialSize = Buf.st_size;
  1255. }
  1256. QueueURI(Desc);
  1257. }
  1258. /*}}}*/
  1259. // AcqFile::Done - Item downloaded OK /*{{{*/
  1260. // ---------------------------------------------------------------------
  1261. /* */
  1262. void pkgAcqFile::Done(string Message,unsigned long Size,string MD5,
  1263. pkgAcquire::MethodConfig *Cnf)
  1264. {
  1265. // Check the md5
  1266. if (Md5Hash.empty() == false && MD5.empty() == false)
  1267. {
  1268. if (Md5Hash != MD5)
  1269. {
  1270. Status = StatError;
  1271. ErrorText = "MD5Sum mismatch";
  1272. Rename(DestFile,DestFile + ".FAILED");
  1273. return;
  1274. }
  1275. }
  1276. Item::Done(Message,Size,MD5,Cnf);
  1277. string FileName = LookupTag(Message,"Filename");
  1278. if (FileName.empty() == true)
  1279. {
  1280. Status = StatError;
  1281. ErrorText = "Method gave a blank filename";
  1282. return;
  1283. }
  1284. Complete = true;
  1285. // The files timestamp matches
  1286. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
  1287. return;
  1288. // We have to copy it into place
  1289. if (FileName != DestFile)
  1290. {
  1291. Local = true;
  1292. if (_config->FindB("Acquire::Source-Symlinks",true) == false ||
  1293. Cnf->Removable == true)
  1294. {
  1295. Desc.URI = "copy:" + FileName;
  1296. QueueURI(Desc);
  1297. return;
  1298. }
  1299. // Erase the file if it is a symlink so we can overwrite it
  1300. struct stat St;
  1301. if (lstat(DestFile.c_str(),&St) == 0)
  1302. {
  1303. if (S_ISLNK(St.st_mode) != 0)
  1304. unlink(DestFile.c_str());
  1305. }
  1306. // Symlink the file
  1307. if (symlink(FileName.c_str(),DestFile.c_str()) != 0)
  1308. {
  1309. ErrorText = "Link to " + DestFile + " failure ";
  1310. Status = StatError;
  1311. Complete = false;
  1312. }
  1313. }
  1314. }
  1315. /*}}}*/
  1316. // AcqFile::Failed - Failure handler /*{{{*/
  1317. // ---------------------------------------------------------------------
  1318. /* Here we try other sources */
  1319. void pkgAcqFile::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  1320. {
  1321. ErrorText = LookupTag(Message,"Message");
  1322. // This is the retry counter
  1323. if (Retries != 0 &&
  1324. Cnf->LocalOnly == false &&
  1325. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  1326. {
  1327. Retries--;
  1328. QueueURI(Desc);
  1329. return;
  1330. }
  1331. Item::Failed(Message,Cnf);
  1332. }
  1333. /*}}}*/