acquire-item.cc 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  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 ExpectedHash,
  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. return;
  572. Decompression = true;
  573. Local = true;
  574. DestFile += ".decomp";
  575. Desc.URI = "copy:" + FileName;
  576. QueueURI(Desc);
  577. Mode = "copy";
  578. return;
  579. }
  580. FileName = LookupTag(Message,"Filename");
  581. if (FileName.empty() == true)
  582. {
  583. Status = StatError;
  584. ErrorText = "Method gave a blank filename";
  585. }
  586. // The files timestamp matches
  587. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
  588. {
  589. unlink(FileName.c_str());
  590. return;
  591. }
  592. if (FileName == DestFile)
  593. Erase = true;
  594. else
  595. Local = true;
  596. string compExt = Desc.URI.substr(Desc.URI.size()-3);
  597. const char *decompProg;
  598. if(compExt == "bz2")
  599. decompProg = "bzip2";
  600. else if(compExt == ".gz")
  601. decompProg = "gzip";
  602. else {
  603. _error->Error("Unsupported extension: %s", compExt.c_str());
  604. return;
  605. }
  606. Decompression = true;
  607. DestFile += ".decomp";
  608. Desc.URI = string(decompProg) + ":" + FileName;
  609. QueueURI(Desc);
  610. Mode = decompProg;
  611. }
  612. // AcqIndexTrans::pkgAcqIndexTrans - Constructor /*{{{*/
  613. // ---------------------------------------------------------------------
  614. /* The Translation file is added to the queue */
  615. pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcquire *Owner,
  616. string URI,string URIDesc,string ShortDesc)
  617. : pkgAcqIndex(Owner, URI, URIDesc, ShortDesc, HashString(), "")
  618. {
  619. }
  620. /*}}}*/
  621. // AcqIndexTrans::Failed - Silence failure messages for missing files /*{{{*/
  622. // ---------------------------------------------------------------------
  623. /* */
  624. void pkgAcqIndexTrans::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  625. {
  626. if (Cnf->LocalOnly == true ||
  627. StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
  628. {
  629. // Ignore this
  630. Status = StatDone;
  631. Complete = false;
  632. Dequeue();
  633. return;
  634. }
  635. Item::Failed(Message,Cnf);
  636. }
  637. /*}}}*/
  638. pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire *Owner,
  639. string URI,string URIDesc,string ShortDesc,
  640. string MetaIndexURI, string MetaIndexURIDesc,
  641. string MetaIndexShortDesc,
  642. const vector<IndexTarget*>* IndexTargets,
  643. indexRecords* MetaIndexParser) :
  644. Item(Owner), RealURI(URI), MetaIndexURI(MetaIndexURI),
  645. MetaIndexURIDesc(MetaIndexURIDesc), MetaIndexShortDesc(MetaIndexShortDesc),
  646. MetaIndexParser(MetaIndexParser), IndexTargets(IndexTargets)
  647. {
  648. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  649. DestFile += URItoFileName(URI);
  650. // remove any partial downloaded sig-file in partial/.
  651. // it may confuse proxies and is too small to warrant a
  652. // partial download anyway
  653. unlink(DestFile.c_str());
  654. // Create the item
  655. Desc.Description = URIDesc;
  656. Desc.Owner = this;
  657. Desc.ShortDesc = ShortDesc;
  658. Desc.URI = URI;
  659. string Final = _config->FindDir("Dir::State::lists");
  660. Final += URItoFileName(RealURI);
  661. struct stat Buf;
  662. if (stat(Final.c_str(),&Buf) == 0)
  663. {
  664. // File was already in place. It needs to be re-verified
  665. // because Release might have changed, so Move it into partial
  666. Rename(Final,DestFile);
  667. }
  668. QueueURI(Desc);
  669. }
  670. /*}}}*/
  671. // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
  672. // ---------------------------------------------------------------------
  673. /* The only header we use is the last-modified header. */
  674. string pkgAcqMetaSig::Custom600Headers()
  675. {
  676. struct stat Buf;
  677. if (stat(DestFile.c_str(),&Buf) != 0)
  678. return "\nIndex-File: true";
  679. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  680. }
  681. void pkgAcqMetaSig::Done(string Message,unsigned long Size,string MD5,
  682. pkgAcquire::MethodConfig *Cfg)
  683. {
  684. Item::Done(Message,Size,MD5,Cfg);
  685. string FileName = LookupTag(Message,"Filename");
  686. if (FileName.empty() == true)
  687. {
  688. Status = StatError;
  689. ErrorText = "Method gave a blank filename";
  690. return;
  691. }
  692. if (FileName != DestFile)
  693. {
  694. // We have to copy it into place
  695. Local = true;
  696. Desc.URI = "copy:" + FileName;
  697. QueueURI(Desc);
  698. return;
  699. }
  700. Complete = true;
  701. // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
  702. new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
  703. DestFile, IndexTargets, MetaIndexParser);
  704. }
  705. /*}}}*/
  706. void pkgAcqMetaSig::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  707. {
  708. string Final = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
  709. // if we get a network error we fail gracefully
  710. if(Status == StatTransientNetworkError)
  711. {
  712. Item::Failed(Message,Cnf);
  713. // move the sigfile back on transient network failures
  714. if(FileExists(DestFile))
  715. Rename(DestFile,Final);
  716. // set the status back to , Item::Failed likes to reset it
  717. Status = pkgAcquire::Item::StatTransientNetworkError;
  718. return;
  719. }
  720. // Delete any existing sigfile when the acquire failed
  721. unlink(Final.c_str());
  722. // queue a pkgAcqMetaIndex with no sigfile
  723. new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
  724. "", IndexTargets, MetaIndexParser);
  725. if (Cnf->LocalOnly == true ||
  726. StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
  727. {
  728. // Ignore this
  729. Status = StatDone;
  730. Complete = false;
  731. Dequeue();
  732. return;
  733. }
  734. Item::Failed(Message,Cnf);
  735. }
  736. pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire *Owner,
  737. string URI,string URIDesc,string ShortDesc,
  738. string SigFile,
  739. const vector<struct IndexTarget*>* IndexTargets,
  740. indexRecords* MetaIndexParser) :
  741. Item(Owner), RealURI(URI), SigFile(SigFile), IndexTargets(IndexTargets),
  742. MetaIndexParser(MetaIndexParser), AuthPass(false), IMSHit(false)
  743. {
  744. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  745. DestFile += URItoFileName(URI);
  746. // Create the item
  747. Desc.Description = URIDesc;
  748. Desc.Owner = this;
  749. Desc.ShortDesc = ShortDesc;
  750. Desc.URI = URI;
  751. QueueURI(Desc);
  752. }
  753. /*}}}*/
  754. // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
  755. // ---------------------------------------------------------------------
  756. /* The only header we use is the last-modified header. */
  757. string pkgAcqMetaIndex::Custom600Headers()
  758. {
  759. string Final = _config->FindDir("Dir::State::lists");
  760. Final += URItoFileName(RealURI);
  761. struct stat Buf;
  762. if (stat(Final.c_str(),&Buf) != 0)
  763. return "\nIndex-File: true";
  764. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  765. }
  766. void pkgAcqMetaIndex::Done(string Message,unsigned long Size,string Hash,
  767. pkgAcquire::MethodConfig *Cfg)
  768. {
  769. Item::Done(Message,Size,Hash,Cfg);
  770. // MetaIndexes are done in two passes: one to download the
  771. // metaindex with an appropriate method, and a second to verify it
  772. // with the gpgv method
  773. if (AuthPass == true)
  774. {
  775. AuthDone(Message);
  776. }
  777. else
  778. {
  779. RetrievalDone(Message);
  780. if (!Complete)
  781. // Still more retrieving to do
  782. return;
  783. if (SigFile == "")
  784. {
  785. // There was no signature file, so we are finished. Download
  786. // the indexes without verification.
  787. QueueIndexes(false);
  788. }
  789. else
  790. {
  791. // There was a signature file, so pass it to gpgv for
  792. // verification
  793. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  794. std::cerr << "Metaindex acquired, queueing gpg verification ("
  795. << SigFile << "," << DestFile << ")\n";
  796. AuthPass = true;
  797. Desc.URI = "gpgv:" + SigFile;
  798. QueueURI(Desc);
  799. Mode = "gpgv";
  800. }
  801. }
  802. }
  803. void pkgAcqMetaIndex::RetrievalDone(string Message)
  804. {
  805. // We have just finished downloading a Release file (it is not
  806. // verified yet)
  807. string FileName = LookupTag(Message,"Filename");
  808. if (FileName.empty() == true)
  809. {
  810. Status = StatError;
  811. ErrorText = "Method gave a blank filename";
  812. return;
  813. }
  814. if (FileName != DestFile)
  815. {
  816. Local = true;
  817. Desc.URI = "copy:" + FileName;
  818. QueueURI(Desc);
  819. return;
  820. }
  821. // see if the download was a IMSHit
  822. IMSHit = StringToBool(LookupTag(Message,"IMS-Hit"),false);
  823. Complete = true;
  824. string FinalFile = _config->FindDir("Dir::State::lists");
  825. FinalFile += URItoFileName(RealURI);
  826. // If we get a IMS hit we can remove the empty file in partial
  827. // othersie we move the file in place
  828. if (IMSHit)
  829. unlink(DestFile.c_str());
  830. else
  831. Rename(DestFile,FinalFile);
  832. chmod(FinalFile.c_str(),0644);
  833. DestFile = FinalFile;
  834. }
  835. void pkgAcqMetaIndex::AuthDone(string Message)
  836. {
  837. // At this point, the gpgv method has succeeded, so there is a
  838. // valid signature from a key in the trusted keyring. We
  839. // perform additional verification of its contents, and use them
  840. // to verify the indexes we are about to download
  841. if (!MetaIndexParser->Load(DestFile))
  842. {
  843. Status = StatAuthError;
  844. ErrorText = MetaIndexParser->ErrorText;
  845. return;
  846. }
  847. if (!VerifyVendor(Message))
  848. {
  849. return;
  850. }
  851. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  852. std::cerr << "Signature verification succeeded: "
  853. << DestFile << std::endl;
  854. // Download further indexes with verification
  855. QueueIndexes(true);
  856. // Done, move signature file into position
  857. string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
  858. URItoFileName(RealURI) + ".gpg";
  859. Rename(SigFile,VerifiedSigFile);
  860. chmod(VerifiedSigFile.c_str(),0644);
  861. }
  862. void pkgAcqMetaIndex::QueueIndexes(bool verify)
  863. {
  864. for (vector <struct IndexTarget*>::const_iterator Target = IndexTargets->begin();
  865. Target != IndexTargets->end();
  866. Target++)
  867. {
  868. HashString ExpectedIndexHash;
  869. if (verify)
  870. {
  871. const indexRecords::checkSum *Record = MetaIndexParser->Lookup((*Target)->MetaKey);
  872. if (!Record)
  873. {
  874. Status = StatAuthError;
  875. ErrorText = "Unable to find expected entry "
  876. + (*Target)->MetaKey + " in Meta-index file (malformed Release file?)";
  877. return;
  878. }
  879. ExpectedIndexHash = Record->Hash;
  880. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  881. {
  882. std::cerr << "Queueing: " << (*Target)->URI << std::endl;
  883. std::cerr << "Expected Hash: " << ExpectedIndexHash.toStr() << std::endl;
  884. }
  885. if (ExpectedIndexHash.empty())
  886. {
  887. Status = StatAuthError;
  888. ErrorText = "Unable to find hash sum for "
  889. + (*Target)->MetaKey + " in Meta-index file";
  890. return;
  891. }
  892. }
  893. // Queue Packages file (either diff or full packages files, depending
  894. // on the users option)
  895. if(_config->FindB("Acquire::PDiffs",true) == true)
  896. new pkgAcqDiffIndex(Owner, (*Target)->URI, (*Target)->Description,
  897. (*Target)->ShortDesc, ExpectedIndexHash);
  898. else
  899. new pkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description,
  900. (*Target)->ShortDesc, ExpectedIndexHash);
  901. }
  902. }
  903. bool pkgAcqMetaIndex::VerifyVendor(string Message)
  904. {
  905. // // Maybe this should be made available from above so we don't have
  906. // // to read and parse it every time?
  907. // pkgVendorList List;
  908. // List.ReadMainList();
  909. // const Vendor* Vndr = NULL;
  910. // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
  911. // {
  912. // string::size_type pos = (*I).find("VALIDSIG ");
  913. // if (_config->FindB("Debug::Vendor", false))
  914. // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
  915. // << std::endl;
  916. // if (pos != std::string::npos)
  917. // {
  918. // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
  919. // if (_config->FindB("Debug::Vendor", false))
  920. // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
  921. // std::endl;
  922. // Vndr = List.FindVendor(Fingerprint) != "";
  923. // if (Vndr != NULL);
  924. // break;
  925. // }
  926. // }
  927. string::size_type pos;
  928. // check for missing sigs (that where not fatal because otherwise we had
  929. // bombed earlier)
  930. string missingkeys;
  931. string msg = _("There is no public key available for the "
  932. "following key IDs:\n");
  933. pos = Message.find("NO_PUBKEY ");
  934. if (pos != std::string::npos)
  935. {
  936. string::size_type start = pos+strlen("NO_PUBKEY ");
  937. string Fingerprint = Message.substr(start, Message.find("\n")-start);
  938. missingkeys += (Fingerprint);
  939. }
  940. if(!missingkeys.empty())
  941. _error->Warning("%s", string(msg+missingkeys).c_str());
  942. string Transformed = MetaIndexParser->GetExpectedDist();
  943. if (Transformed == "../project/experimental")
  944. {
  945. Transformed = "experimental";
  946. }
  947. pos = Transformed.rfind('/');
  948. if (pos != string::npos)
  949. {
  950. Transformed = Transformed.substr(0, pos);
  951. }
  952. if (Transformed == ".")
  953. {
  954. Transformed = "";
  955. }
  956. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  957. {
  958. std::cerr << "Got Codename: " << MetaIndexParser->GetDist() << std::endl;
  959. std::cerr << "Expecting Dist: " << MetaIndexParser->GetExpectedDist() << std::endl;
  960. std::cerr << "Transformed Dist: " << Transformed << std::endl;
  961. }
  962. if (MetaIndexParser->CheckDist(Transformed) == false)
  963. {
  964. // This might become fatal one day
  965. // Status = StatAuthError;
  966. // ErrorText = "Conflicting distribution; expected "
  967. // + MetaIndexParser->GetExpectedDist() + " but got "
  968. // + MetaIndexParser->GetDist();
  969. // return false;
  970. if (!Transformed.empty())
  971. {
  972. _error->Warning("Conflicting distribution: %s (expected %s but got %s)",
  973. Desc.Description.c_str(),
  974. Transformed.c_str(),
  975. MetaIndexParser->GetDist().c_str());
  976. }
  977. }
  978. return true;
  979. }
  980. /*}}}*/
  981. // pkgAcqMetaIndex::Failed - no Release file present or no signature
  982. // file present /*{{{*/
  983. // ---------------------------------------------------------------------
  984. /* */
  985. void pkgAcqMetaIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  986. {
  987. if (AuthPass == true)
  988. {
  989. // if we fail the authentication but got the file via a IMS-Hit
  990. // this means that the file wasn't downloaded and that it might be
  991. // just stale (server problem, proxy etc). we delete what we have
  992. // queue it again without i-m-s
  993. // alternatively we could just unlink the file and let the user try again
  994. if (IMSHit)
  995. {
  996. Complete = false;
  997. Local = false;
  998. AuthPass = false;
  999. unlink(DestFile.c_str());
  1000. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  1001. DestFile += URItoFileName(RealURI);
  1002. Desc.URI = RealURI;
  1003. QueueURI(Desc);
  1004. return;
  1005. }
  1006. // gpgv method failed
  1007. _error->Warning("GPG error: %s: %s",
  1008. Desc.Description.c_str(),
  1009. LookupTag(Message,"Message").c_str());
  1010. }
  1011. // No Release file was present, or verification failed, so fall
  1012. // back to queueing Packages files without verification
  1013. QueueIndexes(false);
  1014. }
  1015. /*}}}*/
  1016. // AcqArchive::AcqArchive - Constructor /*{{{*/
  1017. // ---------------------------------------------------------------------
  1018. /* This just sets up the initial fetch environment and queues the first
  1019. possibilitiy */
  1020. pkgAcqArchive::pkgAcqArchive(pkgAcquire *Owner,pkgSourceList *Sources,
  1021. pkgRecords *Recs,pkgCache::VerIterator const &Version,
  1022. string &StoreFilename) :
  1023. Item(Owner), Version(Version), Sources(Sources), Recs(Recs),
  1024. StoreFilename(StoreFilename), Vf(Version.FileList()),
  1025. Trusted(false)
  1026. {
  1027. Retries = _config->FindI("Acquire::Retries",0);
  1028. if (Version.Arch() == 0)
  1029. {
  1030. _error->Error(_("I wasn't able to locate a file for the %s package. "
  1031. "This might mean you need to manually fix this package. "
  1032. "(due to missing arch)"),
  1033. Version.ParentPkg().Name());
  1034. return;
  1035. }
  1036. /* We need to find a filename to determine the extension. We make the
  1037. assumption here that all the available sources for this version share
  1038. the same extension.. */
  1039. // Skip not source sources, they do not have file fields.
  1040. for (; Vf.end() == false; Vf++)
  1041. {
  1042. if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
  1043. continue;
  1044. break;
  1045. }
  1046. // Does not really matter here.. we are going to fail out below
  1047. if (Vf.end() != true)
  1048. {
  1049. // If this fails to get a file name we will bomb out below.
  1050. pkgRecords::Parser &Parse = Recs->Lookup(Vf);
  1051. if (_error->PendingError() == true)
  1052. return;
  1053. // Generate the final file name as: package_version_arch.foo
  1054. StoreFilename = QuoteString(Version.ParentPkg().Name(),"_:") + '_' +
  1055. QuoteString(Version.VerStr(),"_:") + '_' +
  1056. QuoteString(Version.Arch(),"_:.") +
  1057. "." + flExtension(Parse.FileName());
  1058. }
  1059. // check if we have one trusted source for the package. if so, switch
  1060. // to "TrustedOnly" mode
  1061. for (pkgCache::VerFileIterator i = Version.FileList(); i.end() == false; i++)
  1062. {
  1063. pkgIndexFile *Index;
  1064. if (Sources->FindIndex(i.File(),Index) == false)
  1065. continue;
  1066. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  1067. {
  1068. std::cerr << "Checking index: " << Index->Describe()
  1069. << "(Trusted=" << Index->IsTrusted() << ")\n";
  1070. }
  1071. if (Index->IsTrusted()) {
  1072. Trusted = true;
  1073. break;
  1074. }
  1075. }
  1076. // "allow-unauthenticated" restores apts old fetching behaviour
  1077. // that means that e.g. unauthenticated file:// uris are higher
  1078. // priority than authenticated http:// uris
  1079. if (_config->FindB("APT::Get::AllowUnauthenticated",false) == true)
  1080. Trusted = false;
  1081. // Select a source
  1082. if (QueueNext() == false && _error->PendingError() == false)
  1083. _error->Error(_("I wasn't able to locate file for the %s package. "
  1084. "This might mean you need to manually fix this package."),
  1085. Version.ParentPkg().Name());
  1086. }
  1087. /*}}}*/
  1088. // AcqArchive::QueueNext - Queue the next file source /*{{{*/
  1089. // ---------------------------------------------------------------------
  1090. /* This queues the next available file version for download. It checks if
  1091. the archive is already available in the cache and stashs the MD5 for
  1092. checking later. */
  1093. bool pkgAcqArchive::QueueNext()
  1094. {
  1095. for (; Vf.end() == false; Vf++)
  1096. {
  1097. // Ignore not source sources
  1098. if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
  1099. continue;
  1100. // Try to cross match against the source list
  1101. pkgIndexFile *Index;
  1102. if (Sources->FindIndex(Vf.File(),Index) == false)
  1103. continue;
  1104. // only try to get a trusted package from another source if that source
  1105. // is also trusted
  1106. if(Trusted && !Index->IsTrusted())
  1107. continue;
  1108. // Grab the text package record
  1109. pkgRecords::Parser &Parse = Recs->Lookup(Vf);
  1110. if (_error->PendingError() == true)
  1111. return false;
  1112. string PkgFile = Parse.FileName();
  1113. if(Parse.SHA256Hash() != "")
  1114. ExpectedHash = HashString("SHA256", Parse.SHA256Hash());
  1115. else if (Parse.SHA1Hash() != "")
  1116. ExpectedHash = HashString("SHA1", Parse.SHA1Hash());
  1117. else
  1118. ExpectedHash = HashString("MD5Sum", Parse.MD5Hash());
  1119. if (PkgFile.empty() == true)
  1120. return _error->Error(_("The package index files are corrupted. No Filename: "
  1121. "field for package %s."),
  1122. Version.ParentPkg().Name());
  1123. Desc.URI = Index->ArchiveURI(PkgFile);
  1124. Desc.Description = Index->ArchiveInfo(Version);
  1125. Desc.Owner = this;
  1126. Desc.ShortDesc = Version.ParentPkg().Name();
  1127. // See if we already have the file. (Legacy filenames)
  1128. FileSize = Version->Size;
  1129. string FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile);
  1130. struct stat Buf;
  1131. if (stat(FinalFile.c_str(),&Buf) == 0)
  1132. {
  1133. // Make sure the size matches
  1134. if ((unsigned)Buf.st_size == Version->Size)
  1135. {
  1136. Complete = true;
  1137. Local = true;
  1138. Status = StatDone;
  1139. StoreFilename = DestFile = FinalFile;
  1140. return true;
  1141. }
  1142. /* Hmm, we have a file and its size does not match, this means it is
  1143. an old style mismatched arch */
  1144. unlink(FinalFile.c_str());
  1145. }
  1146. // Check it again using the new style output filenames
  1147. FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename);
  1148. if (stat(FinalFile.c_str(),&Buf) == 0)
  1149. {
  1150. // Make sure the size matches
  1151. if ((unsigned)Buf.st_size == Version->Size)
  1152. {
  1153. Complete = true;
  1154. Local = true;
  1155. Status = StatDone;
  1156. StoreFilename = DestFile = FinalFile;
  1157. return true;
  1158. }
  1159. /* Hmm, we have a file and its size does not match, this shouldnt
  1160. happen.. */
  1161. unlink(FinalFile.c_str());
  1162. }
  1163. DestFile = _config->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename);
  1164. // Check the destination file
  1165. if (stat(DestFile.c_str(),&Buf) == 0)
  1166. {
  1167. // Hmm, the partial file is too big, erase it
  1168. if ((unsigned)Buf.st_size > Version->Size)
  1169. unlink(DestFile.c_str());
  1170. else
  1171. PartialSize = Buf.st_size;
  1172. }
  1173. // Create the item
  1174. Local = false;
  1175. Desc.URI = Index->ArchiveURI(PkgFile);
  1176. Desc.Description = Index->ArchiveInfo(Version);
  1177. Desc.Owner = this;
  1178. Desc.ShortDesc = Version.ParentPkg().Name();
  1179. QueueURI(Desc);
  1180. Vf++;
  1181. return true;
  1182. }
  1183. return false;
  1184. }
  1185. /*}}}*/
  1186. // AcqArchive::Done - Finished fetching /*{{{*/
  1187. // ---------------------------------------------------------------------
  1188. /* */
  1189. void pkgAcqArchive::Done(string Message,unsigned long Size,string CalcHash,
  1190. pkgAcquire::MethodConfig *Cfg)
  1191. {
  1192. Item::Done(Message,Size,CalcHash,Cfg);
  1193. // Check the size
  1194. if (Size != Version->Size)
  1195. {
  1196. Status = StatError;
  1197. ErrorText = _("Size mismatch");
  1198. return;
  1199. }
  1200. // Check the hash
  1201. if(ExpectedHash.toStr() != CalcHash)
  1202. {
  1203. Status = StatError;
  1204. ErrorText = _("Hash Sum mismatch");
  1205. if(FileExists(DestFile))
  1206. Rename(DestFile,DestFile + ".FAILED");
  1207. return;
  1208. }
  1209. // Grab the output filename
  1210. string FileName = LookupTag(Message,"Filename");
  1211. if (FileName.empty() == true)
  1212. {
  1213. Status = StatError;
  1214. ErrorText = "Method gave a blank filename";
  1215. return;
  1216. }
  1217. Complete = true;
  1218. // Reference filename
  1219. if (FileName != DestFile)
  1220. {
  1221. StoreFilename = DestFile = FileName;
  1222. Local = true;
  1223. return;
  1224. }
  1225. // Done, move it into position
  1226. string FinalFile = _config->FindDir("Dir::Cache::Archives");
  1227. FinalFile += flNotDir(StoreFilename);
  1228. Rename(DestFile,FinalFile);
  1229. StoreFilename = DestFile = FinalFile;
  1230. Complete = true;
  1231. }
  1232. /*}}}*/
  1233. // AcqArchive::Failed - Failure handler /*{{{*/
  1234. // ---------------------------------------------------------------------
  1235. /* Here we try other sources */
  1236. void pkgAcqArchive::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  1237. {
  1238. ErrorText = LookupTag(Message,"Message");
  1239. /* We don't really want to retry on failed media swaps, this prevents
  1240. that. An interesting observation is that permanent failures are not
  1241. recorded. */
  1242. if (Cnf->Removable == true &&
  1243. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  1244. {
  1245. // Vf = Version.FileList();
  1246. while (Vf.end() == false) Vf++;
  1247. StoreFilename = string();
  1248. Item::Failed(Message,Cnf);
  1249. return;
  1250. }
  1251. if (QueueNext() == false)
  1252. {
  1253. // This is the retry counter
  1254. if (Retries != 0 &&
  1255. Cnf->LocalOnly == false &&
  1256. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  1257. {
  1258. Retries--;
  1259. Vf = Version.FileList();
  1260. if (QueueNext() == true)
  1261. return;
  1262. }
  1263. StoreFilename = string();
  1264. Item::Failed(Message,Cnf);
  1265. }
  1266. }
  1267. /*}}}*/
  1268. // AcqArchive::IsTrusted - Determine whether this archive comes from a
  1269. // trusted source /*{{{*/
  1270. // ---------------------------------------------------------------------
  1271. bool pkgAcqArchive::IsTrusted()
  1272. {
  1273. return Trusted;
  1274. }
  1275. // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
  1276. // ---------------------------------------------------------------------
  1277. /* */
  1278. void pkgAcqArchive::Finished()
  1279. {
  1280. if (Status == pkgAcquire::Item::StatDone &&
  1281. Complete == true)
  1282. return;
  1283. StoreFilename = string();
  1284. }
  1285. /*}}}*/
  1286. // AcqFile::pkgAcqFile - Constructor /*{{{*/
  1287. // ---------------------------------------------------------------------
  1288. /* The file is added to the queue */
  1289. pkgAcqFile::pkgAcqFile(pkgAcquire *Owner,string URI,string Hash,
  1290. unsigned long Size,string Dsc,string ShortDesc,
  1291. const string &DestDir, const string &DestFilename) :
  1292. Item(Owner), ExpectedHash(Hash)
  1293. {
  1294. Retries = _config->FindI("Acquire::Retries",0);
  1295. if(!DestFilename.empty())
  1296. DestFile = DestFilename;
  1297. else if(!DestDir.empty())
  1298. DestFile = DestDir + "/" + flNotDir(URI);
  1299. else
  1300. DestFile = flNotDir(URI);
  1301. // Create the item
  1302. Desc.URI = URI;
  1303. Desc.Description = Dsc;
  1304. Desc.Owner = this;
  1305. // Set the short description to the archive component
  1306. Desc.ShortDesc = ShortDesc;
  1307. // Get the transfer sizes
  1308. FileSize = Size;
  1309. struct stat Buf;
  1310. if (stat(DestFile.c_str(),&Buf) == 0)
  1311. {
  1312. // Hmm, the partial file is too big, erase it
  1313. if ((unsigned)Buf.st_size > Size)
  1314. unlink(DestFile.c_str());
  1315. else
  1316. PartialSize = Buf.st_size;
  1317. }
  1318. QueueURI(Desc);
  1319. }
  1320. /*}}}*/
  1321. // AcqFile::Done - Item downloaded OK /*{{{*/
  1322. // ---------------------------------------------------------------------
  1323. /* */
  1324. void pkgAcqFile::Done(string Message,unsigned long Size,string CalcHash,
  1325. pkgAcquire::MethodConfig *Cnf)
  1326. {
  1327. Item::Done(Message,Size,CalcHash,Cnf);
  1328. // Check the hash
  1329. if(!ExpectedHash.empty() && ExpectedHash.toStr() != CalcHash)
  1330. {
  1331. Status = StatError;
  1332. ErrorText = "Hash Sum mismatch";
  1333. Rename(DestFile,DestFile + ".FAILED");
  1334. return;
  1335. }
  1336. string FileName = LookupTag(Message,"Filename");
  1337. if (FileName.empty() == true)
  1338. {
  1339. Status = StatError;
  1340. ErrorText = "Method gave a blank filename";
  1341. return;
  1342. }
  1343. Complete = true;
  1344. // The files timestamp matches
  1345. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
  1346. return;
  1347. // We have to copy it into place
  1348. if (FileName != DestFile)
  1349. {
  1350. Local = true;
  1351. if (_config->FindB("Acquire::Source-Symlinks",true) == false ||
  1352. Cnf->Removable == true)
  1353. {
  1354. Desc.URI = "copy:" + FileName;
  1355. QueueURI(Desc);
  1356. return;
  1357. }
  1358. // Erase the file if it is a symlink so we can overwrite it
  1359. struct stat St;
  1360. if (lstat(DestFile.c_str(),&St) == 0)
  1361. {
  1362. if (S_ISLNK(St.st_mode) != 0)
  1363. unlink(DestFile.c_str());
  1364. }
  1365. // Symlink the file
  1366. if (symlink(FileName.c_str(),DestFile.c_str()) != 0)
  1367. {
  1368. ErrorText = "Link to " + DestFile + " failure ";
  1369. Status = StatError;
  1370. Complete = false;
  1371. }
  1372. }
  1373. }
  1374. /*}}}*/
  1375. // AcqFile::Failed - Failure handler /*{{{*/
  1376. // ---------------------------------------------------------------------
  1377. /* Here we try other sources */
  1378. void pkgAcqFile::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  1379. {
  1380. ErrorText = LookupTag(Message,"Message");
  1381. // This is the retry counter
  1382. if (Retries != 0 &&
  1383. Cnf->LocalOnly == false &&
  1384. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  1385. {
  1386. Retries--;
  1387. QueueURI(Desc);
  1388. return;
  1389. }
  1390. Item::Failed(Message,Cnf);
  1391. }
  1392. /*}}}*/