indexcopy.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: indexcopy.cc,v 1.10 2002/03/26 07:38:58 jgg Exp $
  4. /* ######################################################################
  5. Index Copying - Aid for copying and verifying the index files
  6. This class helps apt-cache reconstruct a damaged index files.
  7. ##################################################################### */
  8. /*}}}*/
  9. // Include Files /*{{{*/
  10. #include<config.h>
  11. #include <apt-pkg/error.h>
  12. #include <apt-pkg/progress.h>
  13. #include <apt-pkg/strutl.h>
  14. #include <apt-pkg/fileutl.h>
  15. #include <apt-pkg/aptconfiguration.h>
  16. #include <apt-pkg/configuration.h>
  17. #include <apt-pkg/tagfile.h>
  18. #include <apt-pkg/indexrecords.h>
  19. #include <apt-pkg/md5.h>
  20. #include <apt-pkg/cdrom.h>
  21. #include <iostream>
  22. #include <sstream>
  23. #include <unistd.h>
  24. #include <sys/stat.h>
  25. #include <sys/types.h>
  26. #include <fcntl.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include "indexcopy.h"
  30. #include <apti18n.h>
  31. /*}}}*/
  32. using namespace std;
  33. // IndexCopy::CopyPackages - Copy the package files from the CD /*{{{*/
  34. // ---------------------------------------------------------------------
  35. /* */
  36. bool IndexCopy::CopyPackages(string CDROM,string Name,vector<string> &List,
  37. pkgCdromStatus *log)
  38. {
  39. OpProgress *Progress = NULL;
  40. if (List.empty() == true)
  41. return true;
  42. if(log)
  43. Progress = log->GetOpProgress();
  44. bool NoStat = _config->FindB("APT::CDROM::Fast",false);
  45. bool Debug = _config->FindB("Debug::aptcdrom",false);
  46. // Prepare the progress indicator
  47. off_t TotalSize = 0;
  48. std::vector<APT::Configuration::Compressor> const compressor = APT::Configuration::getCompressors();
  49. for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
  50. {
  51. struct stat Buf;
  52. bool found = false;
  53. std::string file = std::string(*I).append(GetFileName());
  54. for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin();
  55. c != compressor.end(); ++c)
  56. {
  57. if (stat(std::string(file + c->Extension).c_str(), &Buf) != 0)
  58. continue;
  59. found = true;
  60. break;
  61. }
  62. if (found == false)
  63. return _error->Errno("stat", "Stat failed for %s", file.c_str());
  64. TotalSize += Buf.st_size;
  65. }
  66. off_t CurrentSize = 0;
  67. unsigned int NotFound = 0;
  68. unsigned int WrongSize = 0;
  69. unsigned int Packages = 0;
  70. for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
  71. {
  72. string OrigPath = string(*I,CDROM.length());
  73. // Open the package file
  74. FileFd Pkg(*I + GetFileName(), FileFd::ReadOnly, FileFd::Auto);
  75. off_t const FileSize = Pkg.Size();
  76. pkgTagFile Parser(&Pkg);
  77. if (_error->PendingError() == true)
  78. return false;
  79. // Open the output file
  80. char S[400];
  81. snprintf(S,sizeof(S),"cdrom:[%s]/%s%s",Name.c_str(),
  82. (*I).c_str() + CDROM.length(),GetFileName());
  83. string TargetF = _config->FindDir("Dir::State::lists") + "partial/";
  84. TargetF += URItoFileName(S);
  85. FileFd Target;
  86. if (_config->FindB("APT::CDROM::NoAct",false) == true)
  87. {
  88. TargetF = "/dev/null";
  89. Target.Open(TargetF,FileFd::WriteExists);
  90. } else {
  91. Target.Open(TargetF,FileFd::WriteAtomic);
  92. }
  93. FILE *TargetFl = fdopen(dup(Target.Fd()),"w");
  94. if (_error->PendingError() == true)
  95. return false;
  96. if (TargetFl == 0)
  97. return _error->Errno("fdopen","Failed to reopen fd");
  98. // Setup the progress meter
  99. if(Progress)
  100. Progress->OverallProgress(CurrentSize,TotalSize,FileSize,
  101. string("Reading ") + Type() + " Indexes");
  102. // Parse
  103. if(Progress)
  104. Progress->SubProgress(Pkg.Size());
  105. pkgTagSection Section;
  106. this->Section = &Section;
  107. string Prefix;
  108. unsigned long Hits = 0;
  109. unsigned long Chop = 0;
  110. while (Parser.Step(Section) == true)
  111. {
  112. if(Progress)
  113. Progress->Progress(Parser.Offset());
  114. string File;
  115. unsigned long long Size;
  116. if (GetFile(File,Size) == false)
  117. {
  118. fclose(TargetFl);
  119. return false;
  120. }
  121. if (Chop != 0)
  122. File = OrigPath + ChopDirs(File,Chop);
  123. // See if the file exists
  124. if (NoStat == false || Hits < 10)
  125. {
  126. // Attempt to fix broken structure
  127. if (Hits == 0)
  128. {
  129. if (ReconstructPrefix(Prefix,OrigPath,CDROM,File) == false &&
  130. ReconstructChop(Chop,*I,File) == false)
  131. {
  132. if (Debug == true)
  133. clog << "Missed: " << File << endl;
  134. NotFound++;
  135. continue;
  136. }
  137. if (Chop != 0)
  138. File = OrigPath + ChopDirs(File,Chop);
  139. }
  140. // Get the size
  141. struct stat Buf;
  142. if (stat(string(CDROM + Prefix + File).c_str(),&Buf) != 0 ||
  143. Buf.st_size == 0)
  144. {
  145. bool Mangled = false;
  146. // Attempt to fix busted symlink support for one instance
  147. string OrigFile = File;
  148. string::size_type Start = File.find("binary-");
  149. string::size_type End = File.find("/",Start+3);
  150. if (Start != string::npos && End != string::npos)
  151. {
  152. File.replace(Start,End-Start,"binary-all");
  153. Mangled = true;
  154. }
  155. if (Mangled == false ||
  156. stat(string(CDROM + Prefix + File).c_str(),&Buf) != 0)
  157. {
  158. if (Debug == true)
  159. clog << "Missed(2): " << OrigFile << endl;
  160. NotFound++;
  161. continue;
  162. }
  163. }
  164. // Size match
  165. if ((unsigned long long)Buf.st_size != Size)
  166. {
  167. if (Debug == true)
  168. clog << "Wrong Size: " << File << endl;
  169. WrongSize++;
  170. continue;
  171. }
  172. }
  173. Packages++;
  174. Hits++;
  175. if (RewriteEntry(TargetFl,File) == false)
  176. {
  177. fclose(TargetFl);
  178. return false;
  179. }
  180. }
  181. fclose(TargetFl);
  182. if (Debug == true)
  183. cout << " Processed by using Prefix '" << Prefix << "' and chop " << Chop << endl;
  184. if (_config->FindB("APT::CDROM::NoAct",false) == false)
  185. {
  186. // Move out of the partial directory
  187. Target.Close();
  188. string FinalF = _config->FindDir("Dir::State::lists");
  189. FinalF += URItoFileName(S);
  190. if (rename(TargetF.c_str(),FinalF.c_str()) != 0)
  191. return _error->Errno("rename","Failed to rename");
  192. }
  193. /* Mangle the source to be in the proper notation with
  194. prefix dist [component] */
  195. *I = string(*I,Prefix.length());
  196. ConvertToSourceList(CDROM,*I);
  197. *I = Prefix + ' ' + *I;
  198. CurrentSize += FileSize;
  199. }
  200. if(Progress)
  201. Progress->Done();
  202. // Some stats
  203. if(log) {
  204. stringstream msg;
  205. if(NotFound == 0 && WrongSize == 0)
  206. ioprintf(msg, _("Wrote %i records.\n"), Packages);
  207. else if (NotFound != 0 && WrongSize == 0)
  208. ioprintf(msg, _("Wrote %i records with %i missing files.\n"),
  209. Packages, NotFound);
  210. else if (NotFound == 0 && WrongSize != 0)
  211. ioprintf(msg, _("Wrote %i records with %i mismatched files\n"),
  212. Packages, WrongSize);
  213. if (NotFound != 0 && WrongSize != 0)
  214. ioprintf(msg, _("Wrote %i records with %i missing files and %i mismatched files\n"), Packages, NotFound, WrongSize);
  215. }
  216. if (Packages == 0)
  217. _error->Warning("No valid records were found.");
  218. if (NotFound + WrongSize > 10)
  219. _error->Warning("A lot of entries were discarded, something may be wrong.\n");
  220. return true;
  221. }
  222. /*}}}*/
  223. // IndexCopy::ChopDirs - Chop off the leading directory components /*{{{*/
  224. // ---------------------------------------------------------------------
  225. /* */
  226. string IndexCopy::ChopDirs(string Path,unsigned int Depth)
  227. {
  228. string::size_type I = 0;
  229. do
  230. {
  231. I = Path.find('/',I+1);
  232. Depth--;
  233. }
  234. while (I != string::npos && Depth != 0);
  235. if (I == string::npos)
  236. return string();
  237. return string(Path,I+1);
  238. }
  239. /*}}}*/
  240. // IndexCopy::ReconstructPrefix - Fix strange prefixing /*{{{*/
  241. // ---------------------------------------------------------------------
  242. /* This prepends dir components from the path to the package files to
  243. the path to the deb until it is found */
  244. bool IndexCopy::ReconstructPrefix(string &Prefix,string OrigPath,string CD,
  245. string File)
  246. {
  247. bool Debug = _config->FindB("Debug::aptcdrom",false);
  248. unsigned int Depth = 1;
  249. string MyPrefix = Prefix;
  250. while (1)
  251. {
  252. struct stat Buf;
  253. if (stat(string(CD + MyPrefix + File).c_str(),&Buf) != 0)
  254. {
  255. if (Debug == true)
  256. cout << "Failed, " << CD + MyPrefix + File << endl;
  257. if (GrabFirst(OrigPath,MyPrefix,Depth++) == true)
  258. continue;
  259. return false;
  260. }
  261. else
  262. {
  263. Prefix = MyPrefix;
  264. return true;
  265. }
  266. }
  267. return false;
  268. }
  269. /*}}}*/
  270. // IndexCopy::ReconstructChop - Fixes bad source paths /*{{{*/
  271. // ---------------------------------------------------------------------
  272. /* This removes path components from the filename and prepends the location
  273. of the package files until a file is found */
  274. bool IndexCopy::ReconstructChop(unsigned long &Chop,string Dir,string File)
  275. {
  276. // Attempt to reconstruct the filename
  277. unsigned long Depth = 0;
  278. while (1)
  279. {
  280. struct stat Buf;
  281. if (stat(string(Dir + File).c_str(),&Buf) != 0)
  282. {
  283. File = ChopDirs(File,1);
  284. Depth++;
  285. if (File.empty() == false)
  286. continue;
  287. return false;
  288. }
  289. else
  290. {
  291. Chop = Depth;
  292. return true;
  293. }
  294. }
  295. return false;
  296. }
  297. /*}}}*/
  298. // IndexCopy::ConvertToSourceList - Convert a Path to a sourcelist /*{{{*/
  299. // ---------------------------------------------------------------------
  300. /* We look for things in dists/ notation and convert them to
  301. <dist> <component> form otherwise it is left alone. This also strips
  302. the CD path.
  303. This implements a regex sort of like:
  304. (.*)/dists/([^/]*)/(.*)/binary-*
  305. ^ ^ ^- Component
  306. | |-------- Distribution
  307. |------------------- Path
  308. It was deciced to use only a single word for dist (rather than say
  309. unstable/non-us) to increase the chance that each CD gets a single
  310. line in sources.list.
  311. */
  312. void IndexCopy::ConvertToSourceList(string CD,string &Path)
  313. {
  314. // Strip the cdrom base path
  315. Path = string(Path,CD.length());
  316. if (Path.empty() == true)
  317. Path = "/";
  318. // Too short to be a dists/ type
  319. if (Path.length() < strlen("dists/"))
  320. return;
  321. // Not a dists type.
  322. if (stringcmp(Path.c_str(),Path.c_str()+strlen("dists/"),"dists/") != 0)
  323. return;
  324. // Isolate the dist
  325. string::size_type Slash = strlen("dists/");
  326. string::size_type Slash2 = Path.find('/',Slash + 1);
  327. if (Slash2 == string::npos || Slash2 + 2 >= Path.length())
  328. return;
  329. string Dist = string(Path,Slash,Slash2 - Slash);
  330. // Isolate the component
  331. Slash = Slash2;
  332. for (unsigned I = 0; I != 10; I++)
  333. {
  334. Slash = Path.find('/',Slash+1);
  335. if (Slash == string::npos || Slash + 2 >= Path.length())
  336. return;
  337. string Comp = string(Path,Slash2+1,Slash - Slash2-1);
  338. // Verify the trailing binary- bit
  339. string::size_type BinSlash = Path.find('/',Slash + 1);
  340. if (Slash == string::npos)
  341. return;
  342. string Binary = string(Path,Slash+1,BinSlash - Slash-1);
  343. if (strncmp(Binary.c_str(), "binary-", strlen("binary-")) == 0)
  344. {
  345. Binary.erase(0, strlen("binary-"));
  346. if (APT::Configuration::checkArchitecture(Binary) == false)
  347. continue;
  348. }
  349. else if (Binary != "source")
  350. continue;
  351. Path = Dist + ' ' + Comp;
  352. return;
  353. }
  354. }
  355. /*}}}*/
  356. // IndexCopy::GrabFirst - Return the first Depth path components /*{{{*/
  357. // ---------------------------------------------------------------------
  358. /* */
  359. bool IndexCopy::GrabFirst(string Path,string &To,unsigned int Depth)
  360. {
  361. string::size_type I = 0;
  362. do
  363. {
  364. I = Path.find('/',I+1);
  365. Depth--;
  366. }
  367. while (I != string::npos && Depth != 0);
  368. if (I == string::npos)
  369. return false;
  370. To = string(Path,0,I+1);
  371. return true;
  372. }
  373. /*}}}*/
  374. // PackageCopy::GetFile - Get the file information from the section /*{{{*/
  375. // ---------------------------------------------------------------------
  376. /* */
  377. bool PackageCopy::GetFile(string &File,unsigned long long &Size)
  378. {
  379. File = Section->FindS("Filename");
  380. Size = Section->FindI("Size");
  381. if (File.empty() || Size == 0)
  382. return _error->Error("Cannot find filename or size tag");
  383. return true;
  384. }
  385. /*}}}*/
  386. // PackageCopy::RewriteEntry - Rewrite the entry with a new filename /*{{{*/
  387. // ---------------------------------------------------------------------
  388. /* */
  389. bool PackageCopy::RewriteEntry(FILE *Target,string File)
  390. {
  391. TFRewriteData Changes[] = {{"Filename",File.c_str()},
  392. {}};
  393. if (TFRewrite(Target,*Section,TFRewritePackageOrder,Changes) == false)
  394. return false;
  395. fputc('\n',Target);
  396. return true;
  397. }
  398. /*}}}*/
  399. // SourceCopy::GetFile - Get the file information from the section /*{{{*/
  400. // ---------------------------------------------------------------------
  401. /* */
  402. bool SourceCopy::GetFile(string &File,unsigned long long &Size)
  403. {
  404. string Files = Section->FindS("Files");
  405. if (Files.empty() == true)
  406. return false;
  407. // Stash the / terminated directory prefix
  408. string Base = Section->FindS("Directory");
  409. if (Base.empty() == false && Base[Base.length()-1] != '/')
  410. Base += '/';
  411. // Read the first file triplet
  412. const char *C = Files.c_str();
  413. string sSize;
  414. string MD5Hash;
  415. // Parse each of the elements
  416. if (ParseQuoteWord(C,MD5Hash) == false ||
  417. ParseQuoteWord(C,sSize) == false ||
  418. ParseQuoteWord(C,File) == false)
  419. return _error->Error("Error parsing file record");
  420. // Parse the size and append the directory
  421. Size = strtoull(sSize.c_str(), NULL, 10);
  422. File = Base + File;
  423. return true;
  424. }
  425. /*}}}*/
  426. // SourceCopy::RewriteEntry - Rewrite the entry with a new filename /*{{{*/
  427. // ---------------------------------------------------------------------
  428. /* */
  429. bool SourceCopy::RewriteEntry(FILE *Target,string File)
  430. {
  431. string Dir(File,0,File.rfind('/'));
  432. TFRewriteData Changes[] = {{"Directory",Dir.c_str()},
  433. {}};
  434. if (TFRewrite(Target,*Section,TFRewriteSourceOrder,Changes) == false)
  435. return false;
  436. fputc('\n',Target);
  437. return true;
  438. }
  439. /*}}}*/
  440. // SigVerify::Verify - Verify a files md5sum against its metaindex /*{{{*/
  441. // ---------------------------------------------------------------------
  442. /* */
  443. bool SigVerify::Verify(string prefix, string file, indexRecords *MetaIndex)
  444. {
  445. const indexRecords::checkSum *Record = MetaIndex->Lookup(file);
  446. bool const Debug = _config->FindB("Debug::aptcdrom",false);
  447. // we skip non-existing files in the verifcation of the Release file
  448. // as non-existing files do not harm, but a warning scares people and
  449. // makes it hard to strip unneeded files from an ISO like uncompressed
  450. // indexes as it is done on the mirrors (see also LP: #255545 )
  451. if(!RealFileExists(prefix+file))
  452. {
  453. if (Debug == true)
  454. cout << "Skipping nonexistent in " << prefix << " file " << file << std::endl;
  455. return true;
  456. }
  457. if (!Record)
  458. {
  459. _error->Warning(_("Can't find authentication record for: %s"), file.c_str());
  460. return false;
  461. }
  462. if (!Record->Hash.VerifyFile(prefix+file))
  463. {
  464. _error->Warning(_("Hash mismatch for: %s"),file.c_str());
  465. return false;
  466. }
  467. if(Debug == true)
  468. {
  469. cout << "File: " << prefix+file << endl;
  470. cout << "Expected Hash " << Record->Hash.toStr() << endl;
  471. }
  472. return true;
  473. }
  474. /*}}}*/
  475. bool SigVerify::CopyMetaIndex(string CDROM, string CDName, /*{{{*/
  476. string prefix, string file)
  477. {
  478. char S[400];
  479. snprintf(S,sizeof(S),"cdrom:[%s]/%s%s",CDName.c_str(),
  480. (prefix).c_str() + CDROM.length(),file.c_str());
  481. string TargetF = _config->FindDir("Dir::State::lists");
  482. TargetF += URItoFileName(S);
  483. FileFd Target;
  484. FileFd Rel;
  485. Target.Open(TargetF,FileFd::WriteAtomic);
  486. Rel.Open(prefix + file,FileFd::ReadOnly);
  487. if (CopyFile(Rel,Target) == false)
  488. return _error->Error("Copying of '%s' for '%s' from '%s' failed", file.c_str(), CDName.c_str(), prefix.c_str());
  489. return true;
  490. }
  491. /*}}}*/
  492. bool SigVerify::CopyAndVerify(string CDROM,string Name,vector<string> &SigList, /*{{{*/
  493. vector<string> PkgList,vector<string> SrcList)
  494. {
  495. if (SigList.empty() == true)
  496. return true;
  497. bool Debug = _config->FindB("Debug::aptcdrom",false);
  498. // Read all Release files
  499. for (vector<string>::iterator I = SigList.begin(); I != SigList.end(); ++I)
  500. {
  501. if(Debug)
  502. cout << "Signature verify for: " << *I << endl;
  503. indexRecords *MetaIndex = new indexRecords;
  504. string prefix = *I;
  505. string const releasegpg = *I+"Release.gpg";
  506. string const release = *I+"Release";
  507. string const inrelease = *I+"InRelease";
  508. bool useInRelease = true;
  509. // a Release.gpg without a Release should never happen
  510. if (RealFileExists(inrelease) == true)
  511. ;
  512. else if(RealFileExists(release) == false || RealFileExists(releasegpg) == false)
  513. {
  514. delete MetaIndex;
  515. continue;
  516. }
  517. else
  518. useInRelease = false;
  519. pid_t pid = ExecFork();
  520. if(pid < 0) {
  521. _error->Error("Fork failed");
  522. return false;
  523. }
  524. if(pid == 0)
  525. {
  526. if (useInRelease == true)
  527. ExecGPGV(inrelease, inrelease);
  528. else
  529. ExecGPGV(release, releasegpg);
  530. }
  531. if(!ExecWait(pid, "gpgv")) {
  532. _error->Warning("Signature verification failed for: %s",
  533. (useInRelease ? inrelease.c_str() : releasegpg.c_str()));
  534. // something went wrong, don't copy the Release.gpg
  535. // FIXME: delete any existing gpg file?
  536. continue;
  537. }
  538. // Open the Release file and add it to the MetaIndex
  539. if(!MetaIndex->Load(release))
  540. {
  541. _error->Error("%s",MetaIndex->ErrorText.c_str());
  542. return false;
  543. }
  544. // go over the Indexfiles and see if they verify
  545. // if so, remove them from our copy of the lists
  546. vector<string> keys = MetaIndex->MetaKeys();
  547. for (vector<string>::iterator I = keys.begin(); I != keys.end(); ++I)
  548. {
  549. if(!Verify(prefix,*I, MetaIndex)) {
  550. // something went wrong, don't copy the Release.gpg
  551. // FIXME: delete any existing gpg file?
  552. _error->Discard();
  553. continue;
  554. }
  555. }
  556. // we need a fresh one for the Release.gpg
  557. delete MetaIndex;
  558. // everything was fine, copy the Release and Release.gpg file
  559. if (useInRelease == true)
  560. CopyMetaIndex(CDROM, Name, prefix, "InRelease");
  561. else
  562. {
  563. CopyMetaIndex(CDROM, Name, prefix, "Release");
  564. CopyMetaIndex(CDROM, Name, prefix, "Release.gpg");
  565. }
  566. }
  567. return true;
  568. }
  569. /*}}}*/
  570. // SigVerify::RunGPGV - deprecated wrapper calling ExecGPGV /*{{{*/
  571. bool SigVerify::RunGPGV(std::string const &File, std::string const &FileOut,
  572. int const &statusfd, int fd[2]) {
  573. ExecGPGV(File, FileOut, statusfd, fd);
  574. return false;
  575. };
  576. bool SigVerify::RunGPGV(std::string const &File, std::string const &FileOut,
  577. int const &statusfd) {
  578. ExecGPGV(File, FileOut, statusfd);
  579. return false;
  580. };
  581. /*}}}*/
  582. bool TranslationsCopy::CopyTranslations(string CDROM,string Name, /*{{{*/
  583. vector<string> &List, pkgCdromStatus *log)
  584. {
  585. OpProgress *Progress = NULL;
  586. if (List.empty() == true)
  587. return true;
  588. if(log)
  589. Progress = log->GetOpProgress();
  590. bool Debug = _config->FindB("Debug::aptcdrom",false);
  591. // Prepare the progress indicator
  592. off_t TotalSize = 0;
  593. std::vector<APT::Configuration::Compressor> const compressor = APT::Configuration::getCompressors();
  594. for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
  595. {
  596. struct stat Buf;
  597. bool found = false;
  598. std::string file = *I;
  599. for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin();
  600. c != compressor.end(); ++c)
  601. {
  602. if (stat(std::string(file + c->Extension).c_str(), &Buf) != 0)
  603. continue;
  604. found = true;
  605. break;
  606. }
  607. if (found == false)
  608. return _error->Errno("stat", "Stat failed for %s", file.c_str());
  609. TotalSize += Buf.st_size;
  610. }
  611. off_t CurrentSize = 0;
  612. unsigned int NotFound = 0;
  613. unsigned int WrongSize = 0;
  614. unsigned int Packages = 0;
  615. for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
  616. {
  617. // Open the package file
  618. FileFd Pkg(*I, FileFd::ReadOnly, FileFd::Auto);
  619. off_t const FileSize = Pkg.Size();
  620. pkgTagFile Parser(&Pkg);
  621. if (_error->PendingError() == true)
  622. return false;
  623. // Open the output file
  624. char S[400];
  625. snprintf(S,sizeof(S),"cdrom:[%s]/%s",Name.c_str(),
  626. (*I).c_str() + CDROM.length());
  627. string TargetF = _config->FindDir("Dir::State::lists") + "partial/";
  628. TargetF += URItoFileName(S);
  629. FileFd Target;
  630. if (_config->FindB("APT::CDROM::NoAct",false) == true)
  631. {
  632. TargetF = "/dev/null";
  633. Target.Open(TargetF,FileFd::WriteExists);
  634. } else {
  635. Target.Open(TargetF,FileFd::WriteAtomic);
  636. }
  637. if (_error->PendingError() == true)
  638. return false;
  639. FILE *TargetFl = fdopen(dup(Target.Fd()),"w");
  640. if (TargetFl == 0)
  641. return _error->Errno("fdopen","Failed to reopen fd");
  642. // Setup the progress meter
  643. if(Progress)
  644. Progress->OverallProgress(CurrentSize,TotalSize,FileSize,
  645. string("Reading Translation Indexes"));
  646. // Parse
  647. if(Progress)
  648. Progress->SubProgress(Pkg.Size());
  649. pkgTagSection Section;
  650. this->Section = &Section;
  651. string Prefix;
  652. unsigned long Hits = 0;
  653. while (Parser.Step(Section) == true)
  654. {
  655. if(Progress)
  656. Progress->Progress(Parser.Offset());
  657. const char *Start;
  658. const char *Stop;
  659. Section.GetSection(Start,Stop);
  660. fwrite(Start,Stop-Start, 1, TargetFl);
  661. fputc('\n',TargetFl);
  662. Packages++;
  663. Hits++;
  664. }
  665. fclose(TargetFl);
  666. if (Debug == true)
  667. cout << " Processed by using Prefix '" << Prefix << "' and chop " << endl;
  668. if (_config->FindB("APT::CDROM::NoAct",false) == false)
  669. {
  670. // Move out of the partial directory
  671. Target.Close();
  672. string FinalF = _config->FindDir("Dir::State::lists");
  673. FinalF += URItoFileName(S);
  674. if (rename(TargetF.c_str(),FinalF.c_str()) != 0)
  675. return _error->Errno("rename","Failed to rename");
  676. }
  677. CurrentSize += FileSize;
  678. }
  679. if(Progress)
  680. Progress->Done();
  681. // Some stats
  682. if(log) {
  683. stringstream msg;
  684. if(NotFound == 0 && WrongSize == 0)
  685. ioprintf(msg, _("Wrote %i records.\n"), Packages);
  686. else if (NotFound != 0 && WrongSize == 0)
  687. ioprintf(msg, _("Wrote %i records with %i missing files.\n"),
  688. Packages, NotFound);
  689. else if (NotFound == 0 && WrongSize != 0)
  690. ioprintf(msg, _("Wrote %i records with %i mismatched files\n"),
  691. Packages, WrongSize);
  692. if (NotFound != 0 && WrongSize != 0)
  693. ioprintf(msg, _("Wrote %i records with %i missing files and %i mismatched files\n"), Packages, NotFound, WrongSize);
  694. }
  695. if (Packages == 0)
  696. _error->Warning("No valid records were found.");
  697. if (NotFound + WrongSize > 10)
  698. _error->Warning("A lot of entries were discarded, something may be wrong.\n");
  699. return true;
  700. }
  701. /*}}}*/