You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

796 lines
23 KiB

  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((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. if (_error->PendingError() == true)
  94. return false;
  95. FILE *TargetFl = fdopen(dup(Target.Fd()),"w");
  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((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((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((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((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(), NULL },
  392. { NULL, NULL, NULL }};
  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(), NULL },
  433. { NULL, NULL, NULL }};
  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. delete MetaIndex;
  537. continue;
  538. }
  539. // Open the Release file and add it to the MetaIndex
  540. if(!MetaIndex->Load(release))
  541. {
  542. _error->Error("%s",MetaIndex->ErrorText.c_str());
  543. return false;
  544. }
  545. // go over the Indexfiles and see if they verify
  546. // if so, remove them from our copy of the lists
  547. vector<string> keys = MetaIndex->MetaKeys();
  548. for (vector<string>::iterator I = keys.begin(); I != keys.end(); ++I)
  549. {
  550. if(!Verify(prefix,*I, MetaIndex)) {
  551. // something went wrong, don't copy the Release.gpg
  552. // FIXME: delete any existing gpg file?
  553. _error->Discard();
  554. continue;
  555. }
  556. }
  557. // we need a fresh one for the Release.gpg
  558. delete MetaIndex;
  559. // everything was fine, copy the Release and Release.gpg file
  560. if (useInRelease == true)
  561. CopyMetaIndex(CDROM, Name, prefix, "InRelease");
  562. else
  563. {
  564. CopyMetaIndex(CDROM, Name, prefix, "Release");
  565. CopyMetaIndex(CDROM, Name, prefix, "Release.gpg");
  566. }
  567. }
  568. return true;
  569. }
  570. /*}}}*/
  571. // SigVerify::RunGPGV - deprecated wrapper calling ExecGPGV /*{{{*/
  572. bool SigVerify::RunGPGV(std::string const &File, std::string const &FileOut,
  573. int const &statusfd, int fd[2]) {
  574. ExecGPGV(File, FileOut, statusfd, fd);
  575. return false;
  576. }
  577. bool SigVerify::RunGPGV(std::string const &File, std::string const &FileOut,
  578. int const &statusfd) {
  579. ExecGPGV(File, FileOut, statusfd);
  580. return false;
  581. }
  582. /*}}}*/
  583. bool TranslationsCopy::CopyTranslations(string CDROM,string Name, /*{{{*/
  584. vector<string> &List, pkgCdromStatus *log)
  585. {
  586. OpProgress *Progress = NULL;
  587. if (List.empty() == true)
  588. return true;
  589. if(log)
  590. Progress = log->GetOpProgress();
  591. bool Debug = _config->FindB("Debug::aptcdrom",false);
  592. // Prepare the progress indicator
  593. off_t TotalSize = 0;
  594. std::vector<APT::Configuration::Compressor> const compressor = APT::Configuration::getCompressors();
  595. for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
  596. {
  597. struct stat Buf;
  598. bool found = false;
  599. std::string file = *I;
  600. for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin();
  601. c != compressor.end(); ++c)
  602. {
  603. if (stat((file + c->Extension).c_str(), &Buf) != 0)
  604. continue;
  605. found = true;
  606. break;
  607. }
  608. if (found == false)
  609. return _error->Errno("stat", "Stat failed for %s", file.c_str());
  610. TotalSize += Buf.st_size;
  611. }
  612. off_t CurrentSize = 0;
  613. unsigned int NotFound = 0;
  614. unsigned int WrongSize = 0;
  615. unsigned int Packages = 0;
  616. for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
  617. {
  618. // Open the package file
  619. FileFd Pkg(*I, FileFd::ReadOnly, FileFd::Auto);
  620. off_t const FileSize = Pkg.Size();
  621. pkgTagFile Parser(&Pkg);
  622. if (_error->PendingError() == true)
  623. return false;
  624. // Open the output file
  625. char S[400];
  626. snprintf(S,sizeof(S),"cdrom:[%s]/%s",Name.c_str(),
  627. (*I).c_str() + CDROM.length());
  628. string TargetF = _config->FindDir("Dir::State::lists") + "partial/";
  629. TargetF += URItoFileName(S);
  630. FileFd Target;
  631. if (_config->FindB("APT::CDROM::NoAct",false) == true)
  632. {
  633. TargetF = "/dev/null";
  634. Target.Open(TargetF,FileFd::WriteExists);
  635. } else {
  636. Target.Open(TargetF,FileFd::WriteAtomic);
  637. }
  638. if (_error->PendingError() == true)
  639. return false;
  640. FILE *TargetFl = fdopen(dup(Target.Fd()),"w");
  641. if (TargetFl == 0)
  642. return _error->Errno("fdopen","Failed to reopen fd");
  643. // Setup the progress meter
  644. if(Progress)
  645. Progress->OverallProgress(CurrentSize,TotalSize,FileSize,
  646. string("Reading Translation Indexes"));
  647. // Parse
  648. if(Progress)
  649. Progress->SubProgress(Pkg.Size());
  650. pkgTagSection Section;
  651. this->Section = &Section;
  652. string Prefix;
  653. unsigned long Hits = 0;
  654. while (Parser.Step(Section) == true)
  655. {
  656. if(Progress)
  657. Progress->Progress(Parser.Offset());
  658. const char *Start;
  659. const char *Stop;
  660. Section.GetSection(Start,Stop);
  661. fwrite(Start,Stop-Start, 1, TargetFl);
  662. fputc('\n',TargetFl);
  663. Packages++;
  664. Hits++;
  665. }
  666. fclose(TargetFl);
  667. if (Debug == true)
  668. cout << " Processed by using Prefix '" << Prefix << "' and chop " << endl;
  669. if (_config->FindB("APT::CDROM::NoAct",false) == false)
  670. {
  671. // Move out of the partial directory
  672. Target.Close();
  673. string FinalF = _config->FindDir("Dir::State::lists");
  674. FinalF += URItoFileName(S);
  675. if (rename(TargetF.c_str(),FinalF.c_str()) != 0)
  676. return _error->Errno("rename","Failed to rename");
  677. }
  678. CurrentSize += FileSize;
  679. }
  680. if(Progress)
  681. Progress->Done();
  682. // Some stats
  683. if(log) {
  684. stringstream msg;
  685. if(NotFound == 0 && WrongSize == 0)
  686. ioprintf(msg, _("Wrote %i records.\n"), Packages);
  687. else if (NotFound != 0 && WrongSize == 0)
  688. ioprintf(msg, _("Wrote %i records with %i missing files.\n"),
  689. Packages, NotFound);
  690. else if (NotFound == 0 && WrongSize != 0)
  691. ioprintf(msg, _("Wrote %i records with %i mismatched files\n"),
  692. Packages, WrongSize);
  693. if (NotFound != 0 && WrongSize != 0)
  694. ioprintf(msg, _("Wrote %i records with %i missing files and %i mismatched files\n"), Packages, NotFound, WrongSize);
  695. }
  696. if (Packages == 0)
  697. _error->Warning("No valid records were found.");
  698. if (NotFound + WrongSize > 10)
  699. _error->Warning("A lot of entries were discarded, something may be wrong.\n");
  700. return true;
  701. }
  702. /*}}}*/