writer.cc 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: writer.cc,v 1.14 2004/03/24 01:40:43 mdz Exp $
  4. /* ######################################################################
  5. Writer
  6. The file writer classes. These write various types of output, sources,
  7. packages and contents.
  8. ##################################################################### */
  9. /*}}}*/
  10. // Include Files /*{{{*/
  11. #include <config.h>
  12. #include <apt-pkg/configuration.h>
  13. #include <apt-pkg/deblistparser.h>
  14. #include <apt-pkg/error.h>
  15. #include <apt-pkg/fileutl.h>
  16. #include <apt-pkg/gpgv.h>
  17. #include <apt-pkg/hashes.h>
  18. #include <apt-pkg/md5.h>
  19. #include <apt-pkg/strutl.h>
  20. #include <apt-pkg/debfile.h>
  21. #include <apt-pkg/pkgcache.h>
  22. #include <apt-pkg/sha1.h>
  23. #include <apt-pkg/sha2.h>
  24. #include <apt-pkg/tagfile.h>
  25. #include <ctype.h>
  26. #include <fnmatch.h>
  27. #include <ftw.h>
  28. #include <locale.h>
  29. #include <string.h>
  30. #include <sys/stat.h>
  31. #include <sys/types.h>
  32. #include <unistd.h>
  33. #include <ctime>
  34. #include <iostream>
  35. #include <sstream>
  36. #include <memory>
  37. #include <utility>
  38. #include <algorithm>
  39. #include "apt-ftparchive.h"
  40. #include "writer.h"
  41. #include "cachedb.h"
  42. #include "multicompress.h"
  43. #include "byhash.h"
  44. #include <apti18n.h>
  45. /*}}}*/
  46. using namespace std;
  47. FTWScanner *FTWScanner::Owner;
  48. // ConfigToDoHashes - which hashes to generate /*{{{*/
  49. static void SingleConfigToDoHashes(unsigned int &DoHashes, std::string const &Conf, unsigned int const Flag)
  50. {
  51. if (_config->FindB(Conf, (DoHashes & Flag) == Flag) == true)
  52. DoHashes |= Flag;
  53. else
  54. DoHashes &= ~Flag;
  55. }
  56. static void ConfigToDoHashes(unsigned int &DoHashes, std::string const &Conf)
  57. {
  58. SingleConfigToDoHashes(DoHashes, Conf + "::MD5", Hashes::MD5SUM);
  59. SingleConfigToDoHashes(DoHashes, Conf + "::SHA1", Hashes::SHA1SUM);
  60. SingleConfigToDoHashes(DoHashes, Conf + "::SHA256", Hashes::SHA256SUM);
  61. SingleConfigToDoHashes(DoHashes, Conf + "::SHA512", Hashes::SHA512SUM);
  62. }
  63. /*}}}*/
  64. // FTWScanner::FTWScanner - Constructor /*{{{*/
  65. FTWScanner::FTWScanner(FileFd * const GivenOutput, string const &Arch, bool const IncludeArchAll)
  66. : Arch(Arch), IncludeArchAll(IncludeArchAll), DoHashes(~0)
  67. {
  68. if (GivenOutput == NULL)
  69. {
  70. Output = new FileFd;
  71. OwnsOutput = true;
  72. Output->OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly, false);
  73. }
  74. else
  75. {
  76. Output = GivenOutput;
  77. OwnsOutput = false;
  78. }
  79. ErrorPrinted = false;
  80. NoLinkAct = !_config->FindB("APT::FTPArchive::DeLinkAct",true);
  81. ConfigToDoHashes(DoHashes, "APT::FTPArchive");
  82. }
  83. /*}}}*/
  84. FTWScanner::~FTWScanner()
  85. {
  86. if (Output != NULL && OwnsOutput)
  87. delete Output;
  88. }
  89. // FTWScanner::Scanner - FTW Scanner /*{{{*/
  90. // ---------------------------------------------------------------------
  91. /* This is the FTW scanner, it processes each directory element in the
  92. directory tree. */
  93. int FTWScanner::ScannerFTW(const char *File,const struct stat * /*sb*/,int Flag)
  94. {
  95. if (Flag == FTW_DNR)
  96. {
  97. Owner->NewLine(1);
  98. ioprintf(c1out, _("W: Unable to read directory %s\n"), File);
  99. }
  100. if (Flag == FTW_NS)
  101. {
  102. Owner->NewLine(1);
  103. ioprintf(c1out, _("W: Unable to stat %s\n"), File);
  104. }
  105. if (Flag != FTW_F)
  106. return 0;
  107. return ScannerFile(File, true);
  108. }
  109. /*}}}*/
  110. // FTWScanner::ScannerFile - File Scanner /*{{{*/
  111. // ---------------------------------------------------------------------
  112. /* */
  113. int FTWScanner::ScannerFile(const char *File, bool const &ReadLink)
  114. {
  115. const char *LastComponent = strrchr(File, '/');
  116. char *RealPath = NULL;
  117. if (LastComponent == NULL)
  118. LastComponent = File;
  119. else
  120. LastComponent++;
  121. vector<string>::const_iterator I;
  122. for(I = Owner->Patterns.begin(); I != Owner->Patterns.end(); ++I)
  123. {
  124. if (fnmatch((*I).c_str(), LastComponent, 0) == 0)
  125. break;
  126. }
  127. if (I == Owner->Patterns.end())
  128. return 0;
  129. /* Process it. If the file is a link then resolve it into an absolute
  130. name.. This works best if the directory components the scanner are
  131. given are not links themselves. */
  132. char Jnk[2];
  133. Owner->OriginalPath = File;
  134. if (ReadLink &&
  135. readlink(File,Jnk,sizeof(Jnk)) != -1 &&
  136. (RealPath = realpath(File,NULL)) != 0)
  137. {
  138. Owner->DoPackage(RealPath);
  139. free(RealPath);
  140. }
  141. else
  142. Owner->DoPackage(File);
  143. if (_error->empty() == false)
  144. {
  145. // Print any errors or warnings found
  146. string Err;
  147. bool SeenPath = false;
  148. while (_error->empty() == false)
  149. {
  150. Owner->NewLine(1);
  151. bool const Type = _error->PopMessage(Err);
  152. if (Type == true)
  153. cerr << _("E: ") << Err << endl;
  154. else
  155. cerr << _("W: ") << Err << endl;
  156. if (Err.find(File) != string::npos)
  157. SeenPath = true;
  158. }
  159. if (SeenPath == false)
  160. cerr << _("E: Errors apply to file ") << "'" << File << "'" << endl;
  161. return 0;
  162. }
  163. return 0;
  164. }
  165. /*}}}*/
  166. // FTWScanner::RecursiveScan - Just scan a directory tree /*{{{*/
  167. // ---------------------------------------------------------------------
  168. /* */
  169. bool FTWScanner::RecursiveScan(string const &Dir)
  170. {
  171. char *RealPath = NULL;
  172. /* If noprefix is set then jam the scan root in, so we don't generate
  173. link followed paths out of control */
  174. if (InternalPrefix.empty() == true)
  175. {
  176. if ((RealPath = realpath(Dir.c_str(),NULL)) == 0)
  177. return _error->Errno("realpath",_("Failed to resolve %s"),Dir.c_str());
  178. InternalPrefix = RealPath;
  179. free(RealPath);
  180. }
  181. // Do recursive directory searching
  182. Owner = this;
  183. int const Res = ftw(Dir.c_str(),ScannerFTW,30);
  184. // Error treewalking?
  185. if (Res != 0)
  186. {
  187. if (_error->PendingError() == false)
  188. _error->Errno("ftw",_("Tree walking failed"));
  189. return false;
  190. }
  191. return true;
  192. }
  193. /*}}}*/
  194. // FTWScanner::LoadFileList - Load the file list from a file /*{{{*/
  195. // ---------------------------------------------------------------------
  196. /* This is an alternative to using FTW to locate files, it reads the list
  197. of files from another file. */
  198. bool FTWScanner::LoadFileList(string const &Dir, string const &File)
  199. {
  200. char *RealPath = NULL;
  201. /* If noprefix is set then jam the scan root in, so we don't generate
  202. link followed paths out of control */
  203. if (InternalPrefix.empty() == true)
  204. {
  205. if ((RealPath = realpath(Dir.c_str(),NULL)) == 0)
  206. return _error->Errno("realpath",_("Failed to resolve %s"),Dir.c_str());
  207. InternalPrefix = RealPath;
  208. free(RealPath);
  209. }
  210. Owner = this;
  211. FILE *List = fopen(File.c_str(),"r");
  212. if (List == 0)
  213. return _error->Errno("fopen",_("Failed to open %s"),File.c_str());
  214. /* We are a tad tricky here.. We prefix the buffer with the directory
  215. name, that way if we need a full path with just use line.. Sneaky and
  216. fully evil. */
  217. char Line[1000];
  218. char *FileStart;
  219. if (Dir.empty() == true || Dir.end()[-1] != '/')
  220. FileStart = Line + snprintf(Line,sizeof(Line),"%s/",Dir.c_str());
  221. else
  222. FileStart = Line + snprintf(Line,sizeof(Line),"%s",Dir.c_str());
  223. while (fgets(FileStart,sizeof(Line) - (FileStart - Line),List) != 0)
  224. {
  225. char *FileName = _strstrip(FileStart);
  226. if (FileName[0] == 0)
  227. continue;
  228. if (FileName[0] != '/')
  229. {
  230. if (FileName != FileStart)
  231. memmove(FileStart,FileName,strlen(FileStart));
  232. FileName = Line;
  233. }
  234. #if 0
  235. struct stat St;
  236. int Flag = FTW_F;
  237. if (stat(FileName,&St) != 0)
  238. Flag = FTW_NS;
  239. #endif
  240. if (ScannerFile(FileName, false) != 0)
  241. break;
  242. }
  243. fclose(List);
  244. return true;
  245. }
  246. /*}}}*/
  247. // FTWScanner::Delink - Delink symlinks /*{{{*/
  248. // ---------------------------------------------------------------------
  249. /* */
  250. bool FTWScanner::Delink(string &FileName,const char *OriginalPath,
  251. unsigned long long &DeLinkBytes,
  252. unsigned long long const &FileSize)
  253. {
  254. // See if this isn't an internaly prefix'd file name.
  255. if (InternalPrefix.empty() == false &&
  256. InternalPrefix.length() < FileName.length() &&
  257. stringcmp(FileName.begin(),FileName.begin() + InternalPrefix.length(),
  258. InternalPrefix.begin(),InternalPrefix.end()) != 0)
  259. {
  260. if (DeLinkLimit != 0 && DeLinkBytes/1024 < DeLinkLimit)
  261. {
  262. // Tidy up the display
  263. if (DeLinkBytes == 0)
  264. cout << endl;
  265. NewLine(1);
  266. ioprintf(c1out, _(" DeLink %s [%s]\n"), (OriginalPath + InternalPrefix.length()),
  267. SizeToStr(FileSize).c_str());
  268. c1out << flush;
  269. if (NoLinkAct == false)
  270. {
  271. char OldLink[400];
  272. if (readlink(OriginalPath,OldLink,sizeof(OldLink)) == -1)
  273. _error->Errno("readlink",_("Failed to readlink %s"),OriginalPath);
  274. else
  275. {
  276. if (RemoveFile("FTWScanner::Delink", OriginalPath))
  277. {
  278. if (link(FileName.c_str(),OriginalPath) != 0)
  279. {
  280. // Panic! Restore the symlink
  281. if (symlink(OldLink,OriginalPath) != 0)
  282. _error->Errno("symlink", "failed to restore symlink");
  283. return _error->Errno("link",_("*** Failed to link %s to %s"),
  284. FileName.c_str(),
  285. OriginalPath);
  286. }
  287. }
  288. }
  289. }
  290. DeLinkBytes += FileSize;
  291. if (DeLinkBytes/1024 >= DeLinkLimit)
  292. ioprintf(c1out, _(" DeLink limit of %sB hit.\n"), SizeToStr(DeLinkBytes).c_str());
  293. }
  294. FileName = OriginalPath;
  295. }
  296. return true;
  297. }
  298. /*}}}*/
  299. // FTWScanner::SetExts - Set extensions to support /*{{{*/
  300. // ---------------------------------------------------------------------
  301. /* */
  302. bool FTWScanner::SetExts(string const &Vals)
  303. {
  304. ClearPatterns();
  305. string::size_type Start = 0;
  306. while (Start <= Vals.length()-1)
  307. {
  308. string::size_type const Space = Vals.find(' ',Start);
  309. string::size_type const Length = ((Space == string::npos) ? Vals.length() : Space) - Start;
  310. if ( Arch.empty() == false )
  311. {
  312. AddPattern(string("*_") + Arch + Vals.substr(Start, Length));
  313. if (IncludeArchAll == true && Arch != "all")
  314. AddPattern(string("*_all") + Vals.substr(Start, Length));
  315. }
  316. else
  317. AddPattern(string("*") + Vals.substr(Start, Length));
  318. Start += Length + 1;
  319. }
  320. return true;
  321. }
  322. /*}}}*/
  323. // PackagesWriter::PackagesWriter - Constructor /*{{{*/
  324. // ---------------------------------------------------------------------
  325. /* */
  326. PackagesWriter::PackagesWriter(FileFd * const GivenOutput, TranslationWriter * const transWriter,
  327. string const &DB,string const &Overrides,string const &ExtOverrides,
  328. string const &Arch, bool const IncludeArchAll) :
  329. FTWScanner(GivenOutput, Arch, IncludeArchAll), Db(DB), Stats(Db.Stats), TransWriter(transWriter)
  330. {
  331. SetExts(".deb .udeb");
  332. DeLinkLimit = 0;
  333. // Process the command line options
  334. ConfigToDoHashes(DoHashes, "APT::FTPArchive::Packages");
  335. DoAlwaysStat = _config->FindB("APT::FTPArchive::AlwaysStat", false);
  336. DoContents = _config->FindB("APT::FTPArchive::Contents",true);
  337. NoOverride = _config->FindB("APT::FTPArchive::NoOverrideMsg",false);
  338. LongDescription = _config->FindB("APT::FTPArchive::LongDescription",true);
  339. if (Db.Loaded() == false)
  340. DoContents = false;
  341. // Read the override file
  342. if (Overrides.empty() == false && Over.ReadOverride(Overrides) == false)
  343. return;
  344. else
  345. NoOverride = true;
  346. if (ExtOverrides.empty() == false)
  347. Over.ReadExtraOverride(ExtOverrides);
  348. _error->DumpErrors();
  349. }
  350. /*}}}*/
  351. // PackagesWriter::DoPackage - Process a single package /*{{{*/
  352. // ---------------------------------------------------------------------
  353. /* This method takes a package and gets its control information and
  354. MD5, SHA1 and SHA256 then writes out a control record with the proper fields
  355. rewritten and the path/size/hash appended. */
  356. bool PackagesWriter::DoPackage(string FileName)
  357. {
  358. // Pull all the data we need form the DB
  359. if (Db.GetFileInfo(FileName,
  360. true, /* DoControl */
  361. DoContents,
  362. true, /* GenContentsOnly */
  363. false, /* DoSource */
  364. DoHashes, DoAlwaysStat) == false)
  365. {
  366. return false;
  367. }
  368. unsigned long long FileSize = Db.GetFileSize();
  369. if (Delink(FileName,OriginalPath,Stats.DeLinkBytes,FileSize) == false)
  370. return false;
  371. // Lookup the overide information
  372. pkgTagSection &Tags = Db.Control.Section;
  373. string Package = Tags.FindS("Package");
  374. string Architecture;
  375. // if we generate a Packages file for a given arch, we use it to
  376. // look for overrides. if we run in "simple" mode without the
  377. // "Architecures" variable in the config we use the architecure value
  378. // from the deb file
  379. if(Arch != "")
  380. Architecture = Arch;
  381. else
  382. Architecture = Tags.FindS("Architecture");
  383. unique_ptr<Override::Item> OverItem(Over.GetItem(Package,Architecture));
  384. if (Package.empty() == true)
  385. return _error->Error(_("Archive had no package field"));
  386. // If we need to do any rewriting of the header do it now..
  387. if (OverItem.get() == 0)
  388. {
  389. if (NoOverride == false)
  390. {
  391. NewLine(1);
  392. ioprintf(c1out, _(" %s has no override entry\n"), Package.c_str());
  393. }
  394. OverItem = unique_ptr<Override::Item>(new Override::Item);
  395. OverItem->FieldOverride["Section"] = Tags.FindS("Section");
  396. OverItem->Priority = Tags.FindS("Priority");
  397. }
  398. // Strip the DirStrip prefix from the FileName and add the PathPrefix
  399. string NewFileName;
  400. if (DirStrip.empty() == false &&
  401. FileName.length() > DirStrip.length() &&
  402. stringcmp(FileName.begin(),FileName.begin() + DirStrip.length(),
  403. DirStrip.begin(),DirStrip.end()) == 0)
  404. NewFileName = string(FileName.begin() + DirStrip.length(),FileName.end());
  405. else
  406. NewFileName = FileName;
  407. if (PathPrefix.empty() == false)
  408. NewFileName = flCombine(PathPrefix,NewFileName);
  409. /* Configuration says we don't want to include the long Description
  410. in the package file - instead we want to ship a separated file */
  411. string desc;
  412. if (LongDescription == false) {
  413. desc = Tags.FindS("Description").append("\n");
  414. OverItem->FieldOverride["Description"] = desc.substr(0, desc.find('\n')).c_str();
  415. }
  416. // This lists all the changes to the fields we are going to make.
  417. std::vector<pkgTagSection::Tag> Changes;
  418. std::string Size;
  419. strprintf(Size, "%llu", (unsigned long long) FileSize);
  420. Changes.push_back(pkgTagSection::Tag::Rewrite("Size", Size));
  421. for (HashStringList::const_iterator hs = Db.HashesList.begin(); hs != Db.HashesList.end(); ++hs)
  422. {
  423. if (hs->HashType() == "MD5Sum")
  424. Changes.push_back(pkgTagSection::Tag::Rewrite("MD5sum", hs->HashValue()));
  425. else if (hs->HashType() == "Checksum-FileSize")
  426. continue;
  427. else
  428. Changes.push_back(pkgTagSection::Tag::Rewrite(hs->HashType(), hs->HashValue()));
  429. }
  430. Changes.push_back(pkgTagSection::Tag::Rewrite("Filename", NewFileName));
  431. Changes.push_back(pkgTagSection::Tag::Rewrite("Priority", OverItem->Priority));
  432. Changes.push_back(pkgTagSection::Tag::Remove("Status"));
  433. Changes.push_back(pkgTagSection::Tag::Remove("Optional"));
  434. string DescriptionMd5;
  435. if (LongDescription == false) {
  436. MD5Summation descmd5;
  437. descmd5.Add(desc.c_str());
  438. DescriptionMd5 = descmd5.Result().Value();
  439. Changes.push_back(pkgTagSection::Tag::Rewrite("Description-md5", DescriptionMd5));
  440. if (TransWriter != NULL)
  441. TransWriter->DoPackage(Package, desc, DescriptionMd5);
  442. }
  443. // Rewrite the maintainer field if necessary
  444. bool MaintFailed;
  445. string NewMaint = OverItem->SwapMaint(Tags.FindS("Maintainer"),MaintFailed);
  446. if (MaintFailed == true)
  447. {
  448. if (NoOverride == false)
  449. {
  450. NewLine(1);
  451. ioprintf(c1out, _(" %s maintainer is %s not %s\n"),
  452. Package.c_str(), Tags.FindS("Maintainer").c_str(), OverItem->OldMaint.c_str());
  453. }
  454. }
  455. if (NewMaint.empty() == false)
  456. Changes.push_back(pkgTagSection::Tag::Rewrite("Maintainer", NewMaint));
  457. /* Get rid of the Optional tag. This is an ugly, ugly, ugly hack that
  458. dpkg-scanpackages does. Well sort of. dpkg-scanpackages just does renaming
  459. but dpkg does this append bit. So we do the append bit, at least that way the
  460. status file and package file will remain similar. There are other transforms
  461. but optional is the only legacy one still in use for some lazy reason. */
  462. string OptionalStr = Tags.FindS("Optional");
  463. if (OptionalStr.empty() == false)
  464. {
  465. if (Tags.FindS("Suggests").empty() == false)
  466. OptionalStr = Tags.FindS("Suggests") + ", " + OptionalStr;
  467. Changes.push_back(pkgTagSection::Tag::Rewrite("Suggests", OptionalStr));
  468. }
  469. for (map<string,string>::const_iterator I = OverItem->FieldOverride.begin();
  470. I != OverItem->FieldOverride.end(); ++I)
  471. Changes.push_back(pkgTagSection::Tag::Rewrite(I->first, I->second));
  472. // Rewrite and store the fields.
  473. if (Tags.Write(*Output, TFRewritePackageOrder, Changes) == false ||
  474. Output->Write("\n", 1) == false)
  475. return false;
  476. return Db.Finish();
  477. }
  478. /*}}}*/
  479. PackagesWriter::~PackagesWriter() /*{{{*/
  480. {
  481. }
  482. /*}}}*/
  483. // TranslationWriter::TranslationWriter - Constructor /*{{{*/
  484. // ---------------------------------------------------------------------
  485. /* Create a Translation-Master file for this Packages file */
  486. TranslationWriter::TranslationWriter(string const &File, string const &TransCompress,
  487. mode_t const &Permissions) : Comp(NULL), Output(NULL)
  488. {
  489. if (File.empty() == true)
  490. return;
  491. Comp = new MultiCompress(File, TransCompress, Permissions);
  492. Output = &Comp->Input;
  493. }
  494. /*}}}*/
  495. // TranslationWriter::DoPackage - Process a single package /*{{{*/
  496. // ---------------------------------------------------------------------
  497. /* Create a Translation-Master file for this Packages file */
  498. bool TranslationWriter::DoPackage(string const &Pkg, string const &Desc,
  499. string const &MD5)
  500. {
  501. if (Output == NULL)
  502. return true;
  503. // Different archs can include different versions and therefore
  504. // different descriptions - so we need to check for both name and md5.
  505. string const Record = Pkg + ":" + MD5;
  506. if (Included.find(Record) != Included.end())
  507. return true;
  508. std::string out;
  509. strprintf(out, "Package: %s\nDescription-md5: %s\nDescription-en: %s\n",
  510. Pkg.c_str(), MD5.c_str(), Desc.c_str());
  511. Output->Write(out.c_str(), out.length());
  512. Included.insert(Record);
  513. return true;
  514. }
  515. /*}}}*/
  516. // TranslationWriter::~TranslationWriter - Destructor /*{{{*/
  517. // ---------------------------------------------------------------------
  518. /* */
  519. TranslationWriter::~TranslationWriter()
  520. {
  521. if (Comp != NULL)
  522. delete Comp;
  523. }
  524. /*}}}*/
  525. // SourcesWriter::SourcesWriter - Constructor /*{{{*/
  526. // ---------------------------------------------------------------------
  527. /* */
  528. SourcesWriter::SourcesWriter(FileFd * const GivenOutput, string const &DB, string const &BOverrides,string const &SOverrides,
  529. string const &ExtOverrides) :
  530. FTWScanner(GivenOutput), Db(DB), Stats(Db.Stats)
  531. {
  532. AddPattern("*.dsc");
  533. DeLinkLimit = 0;
  534. Buffer = 0;
  535. BufSize = 0;
  536. // Process the command line options
  537. ConfigToDoHashes(DoHashes, "APT::FTPArchive::Sources");
  538. NoOverride = _config->FindB("APT::FTPArchive::NoOverrideMsg",false);
  539. DoAlwaysStat = _config->FindB("APT::FTPArchive::AlwaysStat", false);
  540. // Read the override file
  541. if (BOverrides.empty() == false && BOver.ReadOverride(BOverrides) == false)
  542. return;
  543. else
  544. NoOverride = true;
  545. // WTF?? The logic above: if we can't read binary overrides, don't even try
  546. // reading source overrides. if we can read binary overrides, then say there
  547. // are no overrides. THIS MAKES NO SENSE! -- ajt@d.o, 2006/02/28
  548. if (ExtOverrides.empty() == false)
  549. SOver.ReadExtraOverride(ExtOverrides);
  550. if (SOverrides.empty() == false && FileExists(SOverrides) == true)
  551. SOver.ReadOverride(SOverrides,true);
  552. }
  553. /*}}}*/
  554. // SourcesWriter::DoPackage - Process a single package /*{{{*/
  555. static std::string getDscHash(unsigned int const DoHashes,
  556. Hashes::SupportedHashes const DoIt, pkgTagSection &Tags, char const * const FieldName,
  557. HashString const * const Hash, unsigned long long Size, std::string FileName)
  558. {
  559. if ((DoHashes & DoIt) != DoIt || Tags.Exists(FieldName) == false || Hash == NULL)
  560. return "";
  561. std::ostringstream out;
  562. out << "\n " << Hash->HashValue() << " " << Size << " " << FileName
  563. << "\n " << Tags.FindS(FieldName);
  564. return out.str();
  565. }
  566. bool SourcesWriter::DoPackage(string FileName)
  567. {
  568. // Pull all the data we need form the DB
  569. if (Db.GetFileInfo(FileName,
  570. false, /* DoControl */
  571. false, /* DoContents */
  572. false, /* GenContentsOnly */
  573. true, /* DoSource */
  574. DoHashes, DoAlwaysStat) == false)
  575. {
  576. return false;
  577. }
  578. // we need to perform a "write" here (this is what finish is doing)
  579. // because the call to Db.GetFileInfo() in the loop will change
  580. // the "db cursor"
  581. Db.Finish();
  582. pkgTagSection Tags;
  583. if (Tags.Scan(Db.Dsc.Data.c_str(), Db.Dsc.Data.length()) == false)
  584. return _error->Error("Could not find a record in the DSC '%s'",FileName.c_str());
  585. if (Tags.Exists("Source") == false)
  586. return _error->Error("Could not find a Source entry in the DSC '%s'",FileName.c_str());
  587. Tags.Trim();
  588. // Lookup the overide information, finding first the best priority.
  589. string BestPrio;
  590. string Bins = Tags.FindS("Binary");
  591. char Buffer[Bins.length() + 1];
  592. unique_ptr<Override::Item> OverItem(nullptr);
  593. if (Bins.empty() == false)
  594. {
  595. strcpy(Buffer,Bins.c_str());
  596. // Ignore too-long errors.
  597. char *BinList[400];
  598. TokSplitString(',',Buffer,BinList,sizeof(BinList)/sizeof(BinList[0]));
  599. // Look at all the binaries
  600. unsigned char BestPrioV = pkgCache::State::Extra;
  601. for (unsigned I = 0; BinList[I] != 0; I++)
  602. {
  603. unique_ptr<Override::Item> Itm(BOver.GetItem(BinList[I]));
  604. if (Itm.get() == 0)
  605. continue;
  606. unsigned char NewPrioV = debListParser::GetPrio(Itm->Priority);
  607. if (NewPrioV < BestPrioV || BestPrio.empty() == true)
  608. {
  609. BestPrioV = NewPrioV;
  610. BestPrio = Itm->Priority;
  611. }
  612. if (OverItem.get() == 0)
  613. OverItem = std::move(Itm);
  614. }
  615. }
  616. // If we need to do any rewriting of the header do it now..
  617. if (OverItem.get() == 0)
  618. {
  619. if (NoOverride == false)
  620. {
  621. NewLine(1);
  622. ioprintf(c1out, _(" %s has no override entry\n"), Tags.FindS("Source").c_str());
  623. }
  624. OverItem.reset(new Override::Item);
  625. }
  626. struct stat St;
  627. if (stat(FileName.c_str(), &St) != 0)
  628. return _error->Errno("fstat","Failed to stat %s",FileName.c_str());
  629. unique_ptr<Override::Item> SOverItem(SOver.GetItem(Tags.FindS("Source")));
  630. // const unique_ptr<Override::Item> autoSOverItem(SOverItem);
  631. if (SOverItem.get() == 0)
  632. {
  633. ioprintf(c1out, _(" %s has no source override entry\n"), Tags.FindS("Source").c_str());
  634. SOverItem = unique_ptr<Override::Item>(BOver.GetItem(Tags.FindS("Source")));
  635. if (SOverItem.get() == 0)
  636. {
  637. ioprintf(c1out, _(" %s has no binary override entry either\n"), Tags.FindS("Source").c_str());
  638. SOverItem = unique_ptr<Override::Item>(new Override::Item);
  639. *SOverItem = *OverItem;
  640. }
  641. }
  642. // Add the dsc to the files hash list
  643. string const strippedName = flNotDir(FileName);
  644. std::string const Files = getDscHash(DoHashes, Hashes::MD5SUM, Tags, "Files", Db.HashesList.find("MD5Sum"), St.st_size, strippedName);
  645. std::string ChecksumsSha1 = getDscHash(DoHashes, Hashes::SHA1SUM, Tags, "Checksums-Sha1", Db.HashesList.find("SHA1"), St.st_size, strippedName);
  646. std::string ChecksumsSha256 = getDscHash(DoHashes, Hashes::SHA256SUM, Tags, "Checksums-Sha256", Db.HashesList.find("SHA256"), St.st_size, strippedName);
  647. std::string ChecksumsSha512 = getDscHash(DoHashes, Hashes::SHA512SUM, Tags, "Checksums-Sha512", Db.HashesList.find("SHA512"), St.st_size, strippedName);
  648. // Strip the DirStrip prefix from the FileName and add the PathPrefix
  649. string NewFileName;
  650. if (DirStrip.empty() == false &&
  651. FileName.length() > DirStrip.length() &&
  652. stringcmp(DirStrip,OriginalPath,OriginalPath + DirStrip.length()) == 0)
  653. NewFileName = string(OriginalPath + DirStrip.length());
  654. else
  655. NewFileName = OriginalPath;
  656. if (PathPrefix.empty() == false)
  657. NewFileName = flCombine(PathPrefix,NewFileName);
  658. string Directory = flNotFile(OriginalPath);
  659. string Package = Tags.FindS("Source");
  660. // Perform operation over all of the files
  661. string ParseJnk;
  662. const char *C = Files.c_str();
  663. char *RealPath = NULL;
  664. for (;isspace(*C); C++);
  665. while (*C != 0)
  666. {
  667. // Parse each of the elements
  668. if (ParseQuoteWord(C,ParseJnk) == false ||
  669. ParseQuoteWord(C,ParseJnk) == false ||
  670. ParseQuoteWord(C,ParseJnk) == false)
  671. return _error->Error("Error parsing file record");
  672. string OriginalPath = Directory + ParseJnk;
  673. // Add missing hashes to source files
  674. if (((DoHashes & Hashes::SHA1SUM) == Hashes::SHA1SUM && !Tags.Exists("Checksums-Sha1")) ||
  675. ((DoHashes & Hashes::SHA256SUM) == Hashes::SHA256SUM && !Tags.Exists("Checksums-Sha256")) ||
  676. ((DoHashes & Hashes::SHA512SUM) == Hashes::SHA512SUM && !Tags.Exists("Checksums-Sha512")))
  677. {
  678. if (Db.GetFileInfo(OriginalPath,
  679. false, /* DoControl */
  680. false, /* DoContents */
  681. false, /* GenContentsOnly */
  682. false, /* DoSource */
  683. DoHashes,
  684. DoAlwaysStat) == false)
  685. {
  686. return _error->Error("Error getting file info");
  687. }
  688. for (HashStringList::const_iterator hs = Db.HashesList.begin(); hs != Db.HashesList.end(); ++hs)
  689. {
  690. if (hs->HashType() == "MD5Sum" || hs->HashType() == "Checksum-FileSize")
  691. continue;
  692. char const * fieldname;
  693. std::string * out;
  694. if (hs->HashType() == "SHA1")
  695. {
  696. fieldname = "Checksums-Sha1";
  697. out = &ChecksumsSha1;
  698. }
  699. else if (hs->HashType() == "SHA256")
  700. {
  701. fieldname = "Checksums-Sha256";
  702. out = &ChecksumsSha256;
  703. }
  704. else if (hs->HashType() == "SHA512")
  705. {
  706. fieldname = "Checksums-Sha512";
  707. out = &ChecksumsSha512;
  708. }
  709. else
  710. {
  711. _error->Warning("Ignoring unknown Checksumtype %s in SourcesWriter::DoPackages", hs->HashType().c_str());
  712. continue;
  713. }
  714. if (Tags.Exists(fieldname) == true)
  715. continue;
  716. std::ostringstream streamout;
  717. streamout << "\n " << hs->HashValue() << " " << Db.GetFileSize() << " " << ParseJnk;
  718. out->append(streamout.str());
  719. }
  720. // write back the GetFileInfo() stats data
  721. Db.Finish();
  722. }
  723. // Perform the delinking operation
  724. char Jnk[2];
  725. if (readlink(OriginalPath.c_str(),Jnk,sizeof(Jnk)) != -1 &&
  726. (RealPath = realpath(OriginalPath.c_str(),NULL)) != 0)
  727. {
  728. string RP = RealPath;
  729. free(RealPath);
  730. if (Delink(RP,OriginalPath.c_str(),Stats.DeLinkBytes,St.st_size) == false)
  731. return false;
  732. }
  733. }
  734. Directory = flNotFile(NewFileName);
  735. if (Directory.length() > 2)
  736. Directory.erase(Directory.end()-1);
  737. // This lists all the changes to the fields we are going to make.
  738. // (5 hardcoded + checksums + maintainer + end marker)
  739. std::vector<pkgTagSection::Tag> Changes;
  740. Changes.push_back(pkgTagSection::Tag::Remove("Source"));
  741. Changes.push_back(pkgTagSection::Tag::Rewrite("Package", Package));
  742. if (Files.empty() == false)
  743. Changes.push_back(pkgTagSection::Tag::Rewrite("Files", Files));
  744. if (ChecksumsSha1.empty() == false)
  745. Changes.push_back(pkgTagSection::Tag::Rewrite("Checksums-Sha1", ChecksumsSha1));
  746. if (ChecksumsSha256.empty() == false)
  747. Changes.push_back(pkgTagSection::Tag::Rewrite("Checksums-Sha256", ChecksumsSha256));
  748. if (ChecksumsSha512.empty() == false)
  749. Changes.push_back(pkgTagSection::Tag::Rewrite("Checksums-Sha512", ChecksumsSha512));
  750. if (Directory != "./")
  751. Changes.push_back(pkgTagSection::Tag::Rewrite("Directory", Directory));
  752. Changes.push_back(pkgTagSection::Tag::Rewrite("Priority", BestPrio));
  753. Changes.push_back(pkgTagSection::Tag::Remove("Status"));
  754. // Rewrite the maintainer field if necessary
  755. bool MaintFailed;
  756. string NewMaint = OverItem->SwapMaint(Tags.FindS("Maintainer"), MaintFailed);
  757. if (MaintFailed == true)
  758. {
  759. if (NoOverride == false)
  760. {
  761. NewLine(1);
  762. ioprintf(c1out, _(" %s maintainer is %s not %s\n"), Package.c_str(),
  763. Tags.FindS("Maintainer").c_str(), OverItem->OldMaint.c_str());
  764. }
  765. }
  766. if (NewMaint.empty() == false)
  767. Changes.push_back(pkgTagSection::Tag::Rewrite("Maintainer", NewMaint.c_str()));
  768. for (map<string,string>::const_iterator I = SOverItem->FieldOverride.begin();
  769. I != SOverItem->FieldOverride.end(); ++I)
  770. Changes.push_back(pkgTagSection::Tag::Rewrite(I->first, I->second));
  771. // Rewrite and store the fields.
  772. if (Tags.Write(*Output, TFRewriteSourceOrder, Changes) == false ||
  773. Output->Write("\n", 1) == false)
  774. return false;
  775. Stats.Packages++;
  776. return true;
  777. }
  778. /*}}}*/
  779. // ContentsWriter::ContentsWriter - Constructor /*{{{*/
  780. // ---------------------------------------------------------------------
  781. /* */
  782. ContentsWriter::ContentsWriter(FileFd * const GivenOutput, string const &DB,
  783. string const &Arch, bool const IncludeArchAll) :
  784. FTWScanner(GivenOutput, Arch, IncludeArchAll), Db(DB), Stats(Db.Stats)
  785. {
  786. SetExts(".deb");
  787. }
  788. /*}}}*/
  789. // ContentsWriter::DoPackage - Process a single package /*{{{*/
  790. // ---------------------------------------------------------------------
  791. /* If Package is the empty string the control record will be parsed to
  792. determine what the package name is. */
  793. bool ContentsWriter::DoPackage(string FileName, string Package)
  794. {
  795. if (!Db.GetFileInfo(FileName,
  796. Package.empty(), /* DoControl */
  797. true, /* DoContents */
  798. false, /* GenContentsOnly */
  799. false, /* DoSource */
  800. 0, /* DoHashes */
  801. false /* checkMtime */))
  802. {
  803. return false;
  804. }
  805. // Parse the package name
  806. if (Package.empty() == true)
  807. {
  808. Package = Db.Control.Section.FindS("Package");
  809. }
  810. Db.Contents.Add(Gen,Package);
  811. return Db.Finish();
  812. }
  813. /*}}}*/
  814. // ContentsWriter::ReadFromPkgs - Read from a packages file /*{{{*/
  815. // ---------------------------------------------------------------------
  816. /* */
  817. bool ContentsWriter::ReadFromPkgs(string const &PkgFile,string const &PkgCompress)
  818. {
  819. MultiCompress Pkgs(PkgFile,PkgCompress,0,false);
  820. if (_error->PendingError() == true)
  821. return false;
  822. // Open the package file
  823. FileFd Fd;
  824. if (Pkgs.OpenOld(Fd) == false)
  825. return false;
  826. pkgTagFile Tags(&Fd);
  827. if (_error->PendingError() == true)
  828. return false;
  829. // Parse.
  830. pkgTagSection Section;
  831. while (Tags.Step(Section) == true)
  832. {
  833. string File = flCombine(Prefix,Section.FindS("FileName"));
  834. string Package = Section.FindS("Section");
  835. if (Package.empty() == false && Package.end()[-1] != '/')
  836. {
  837. Package += '/';
  838. Package += Section.FindS("Package");
  839. }
  840. else
  841. Package += Section.FindS("Package");
  842. DoPackage(File,Package);
  843. if (_error->empty() == false)
  844. {
  845. _error->Error("Errors apply to file '%s'",File.c_str());
  846. _error->DumpErrors();
  847. }
  848. }
  849. // Tidy the compressor
  850. Fd.Close();
  851. return true;
  852. }
  853. /*}}}*/
  854. // ReleaseWriter::ReleaseWriter - Constructor /*{{{*/
  855. // ---------------------------------------------------------------------
  856. /* */
  857. ReleaseWriter::ReleaseWriter(FileFd * const GivenOutput, string const &/*DB*/) : FTWScanner(GivenOutput)
  858. {
  859. if (_config->FindB("APT::FTPArchive::Release::Default-Patterns", true) == true)
  860. {
  861. AddPattern("Packages");
  862. AddPattern("Packages.*");
  863. AddPattern("Translation-*");
  864. AddPattern("Sources");
  865. AddPattern("Sources.*");
  866. AddPattern("Release");
  867. AddPattern("Contents-*");
  868. AddPattern("Index");
  869. AddPattern("icons-*.tar");
  870. AddPattern("icons-*.tar.*");
  871. AddPattern("Components-*.yml");
  872. AddPattern("Components-*.yml.*");
  873. AddPattern("md5sum.txt");
  874. }
  875. AddPatterns(_config->FindVector("APT::FTPArchive::Release::Patterns"));
  876. time_t const now = time(NULL);
  877. setlocale(LC_TIME, "C");
  878. char datestr[128];
  879. if (strftime(datestr, sizeof(datestr), "%a, %d %b %Y %H:%M:%S UTC",
  880. gmtime(&now)) == 0)
  881. {
  882. datestr[0] = '\0';
  883. }
  884. time_t const validuntil = now + _config->FindI("APT::FTPArchive::Release::ValidTime", 0);
  885. char validstr[128];
  886. if (now == validuntil ||
  887. strftime(validstr, sizeof(validstr), "%a, %d %b %Y %H:%M:%S UTC",
  888. gmtime(&validuntil)) == 0)
  889. {
  890. validstr[0] = '\0';
  891. }
  892. setlocale(LC_TIME, "");
  893. map<string,string> Fields;
  894. Fields["Origin"] = "";
  895. Fields["Label"] = "";
  896. Fields["Suite"] = "";
  897. Fields["Version"] = "";
  898. Fields["Codename"] = "";
  899. Fields["Date"] = datestr;
  900. Fields["Valid-Until"] = validstr;
  901. Fields["Architectures"] = "";
  902. Fields["Components"] = "";
  903. Fields["Description"] = "";
  904. Fields["Signed-By"] = "";
  905. if (_config->FindB("APT::FTPArchive::DoByHash", false) == true)
  906. Fields["Acquire-By-Hash"] = "true";
  907. for(map<string,string>::const_iterator I = Fields.begin();
  908. I != Fields.end();
  909. ++I)
  910. {
  911. string Config = string("APT::FTPArchive::Release::") + (*I).first;
  912. string Value = _config->Find(Config, (*I).second.c_str());
  913. if (Value == "")
  914. continue;
  915. std::string const out = I->first + ": " + Value + "\n";
  916. Output->Write(out.c_str(), out.length());
  917. }
  918. ConfigToDoHashes(DoHashes, "APT::FTPArchive::Release");
  919. }
  920. /*}}}*/
  921. // ReleaseWriter::DoPackage - Process a single package /*{{{*/
  922. // ---------------------------------------------------------------------
  923. bool ReleaseWriter::DoPackage(string FileName)
  924. {
  925. // Strip the DirStrip prefix from the FileName and add the PathPrefix
  926. string NewFileName;
  927. if (DirStrip.empty() == false &&
  928. FileName.length() > DirStrip.length() &&
  929. stringcmp(FileName.begin(),FileName.begin() + DirStrip.length(),
  930. DirStrip.begin(),DirStrip.end()) == 0)
  931. {
  932. NewFileName = string(FileName.begin() + DirStrip.length(),FileName.end());
  933. while (NewFileName[0] == '/')
  934. NewFileName = string(NewFileName.begin() + 1,NewFileName.end());
  935. }
  936. else
  937. NewFileName = FileName;
  938. if (PathPrefix.empty() == false)
  939. NewFileName = flCombine(PathPrefix,NewFileName);
  940. FileFd fd(FileName, FileFd::ReadOnly);
  941. if (!fd.IsOpen())
  942. {
  943. return false;
  944. }
  945. CheckSums[NewFileName].size = fd.Size();
  946. Hashes hs(DoHashes);
  947. hs.AddFD(fd);
  948. CheckSums[NewFileName].Hashes = hs.GetHashStringList();
  949. fd.Close();
  950. // FIXME: wrong layer in the code(?)
  951. // FIXME2: symlink instead of create a copy
  952. if (_config->FindB("APT::FTPArchive::DoByHash", false) == true)
  953. {
  954. std::string Input = FileName;
  955. HashStringList hsl = hs.GetHashStringList();
  956. for(HashStringList::const_iterator h = hsl.begin();
  957. h != hsl.end(); ++h)
  958. {
  959. if (!h->usable())
  960. continue;
  961. if (flNotDir(FileName) == "Release" || flNotDir(FileName) == "InRelease")
  962. continue;
  963. std::string ByHashOutputFile = GenByHashFilename(Input, *h);
  964. std::string ByHashOutputDir = flNotFile(ByHashOutputFile);
  965. if(!CreateDirectory(flNotFile(Input), ByHashOutputDir))
  966. return _error->Warning("can not create dir %s", flNotFile(ByHashOutputFile).c_str());
  967. // write new hashes
  968. FileFd In(Input, FileFd::ReadOnly);
  969. FileFd Out(ByHashOutputFile, FileFd::WriteEmpty);
  970. if(!CopyFile(In, Out))
  971. return _error->Warning("failed to copy %s %s", Input.c_str(), ByHashOutputFile.c_str());
  972. }
  973. }
  974. return true;
  975. }
  976. /*}}}*/
  977. // ReleaseWriter::Finish - Output the checksums /*{{{*/
  978. // ---------------------------------------------------------------------
  979. static void printChecksumTypeRecord(FileFd &Output, char const * const Type, map<string, ReleaseWriter::CheckSum> const &CheckSums)
  980. {
  981. {
  982. std::string out;
  983. strprintf(out, "%s:\n", Type);
  984. Output.Write(out.c_str(), out.length());
  985. }
  986. for(map<string,ReleaseWriter::CheckSum>::const_iterator I = CheckSums.begin();
  987. I != CheckSums.end(); ++I)
  988. {
  989. HashString const * const hs = I->second.Hashes.find(Type);
  990. if (hs == NULL)
  991. continue;
  992. std::string out;
  993. strprintf(out, " %s %16llu %s\n",
  994. hs->HashValue().c_str(),
  995. (*I).second.size,
  996. (*I).first.c_str());
  997. Output.Write(out.c_str(), out.length());
  998. }
  999. }
  1000. void ReleaseWriter::Finish()
  1001. {
  1002. if ((DoHashes & Hashes::MD5SUM) == Hashes::MD5SUM)
  1003. printChecksumTypeRecord(*Output, "MD5Sum", CheckSums);
  1004. if ((DoHashes & Hashes::SHA1SUM) == Hashes::SHA1SUM)
  1005. printChecksumTypeRecord(*Output, "SHA1", CheckSums);
  1006. if ((DoHashes & Hashes::SHA256SUM) == Hashes::SHA256SUM)
  1007. printChecksumTypeRecord(*Output, "SHA256", CheckSums);
  1008. if ((DoHashes & Hashes::SHA512SUM) == Hashes::SHA512SUM)
  1009. printChecksumTypeRecord(*Output, "SHA512", CheckSums);
  1010. // go by-hash cleanup
  1011. map<string,ReleaseWriter::CheckSum>::const_iterator prev = CheckSums.begin();
  1012. if (_config->FindB("APT::FTPArchive::DoByHash", false) == true)
  1013. {
  1014. for(map<string,ReleaseWriter::CheckSum>::const_iterator I = CheckSums.begin();
  1015. I != CheckSums.end(); ++I)
  1016. {
  1017. if (I->first == "Release" || I->first == "InRelease")
  1018. continue;
  1019. // keep iterating until we find a new subdir
  1020. if(flNotFile(I->first) == flNotFile(prev->first))
  1021. continue;
  1022. // clean that subdir up
  1023. int keepFiles = _config->FindI("APT::FTPArchive::By-Hash-Keep", 3);
  1024. // calculate how many compressors are used (the amount of files
  1025. // in that subdir generated for this run)
  1026. keepFiles *= std::distance(prev, I);
  1027. prev = I;
  1028. HashStringList hsl = prev->second.Hashes;
  1029. for(HashStringList::const_iterator h = hsl.begin();
  1030. h != hsl.end(); ++h)
  1031. {
  1032. if (!h->usable())
  1033. continue;
  1034. std::string RealFilename = DirStrip+"/"+prev->first;
  1035. std::string ByHashOutputFile = GenByHashFilename(RealFilename, *h);
  1036. DeleteAllButMostRecent(flNotFile(ByHashOutputFile), keepFiles);
  1037. }
  1038. }
  1039. }
  1040. }