acquire-item.cc 42 KB

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