writer.cc 29 KB

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