writer.cc 28 KB

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