writer.cc 36 KB

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