writer.cc 27 KB

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