indexcopy.cc 23 KB

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