acquire-item.cc 47 KB

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