acquire-item.cc 47 KB

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