apt-sortpkgs.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: apt-sortpkgs.cc,v 1.2 2001/02/20 07:03:17 jgg Exp $
  4. /* ######################################################################
  5. APT Sort Packages - Program to sort Package and Source files
  6. This program is quite simple, it just sorts the package files by
  7. package and sorts the fields inside by the internal APT sort order.
  8. Input is taken from a named file and sent to stdout.
  9. ##################################################################### */
  10. /*}}}*/
  11. // Include Files /*{{{*/
  12. #include <apt-pkg/tagfile.h>
  13. #include <apt-pkg/error.h>
  14. #include <apt-pkg/configuration.h>
  15. #include <apt-pkg/cmndline.h>
  16. #include <apt-pkg/init.h>
  17. #include <apt-pkg/strutl.h>
  18. #include <config.h>
  19. #include <apti18n.h>
  20. #include <vector>
  21. #include <algorithm>
  22. #include <unistd.h>
  23. /*}}}*/
  24. struct PkgName
  25. {
  26. string Name;
  27. string Ver;
  28. string Arch;
  29. unsigned long Offset;
  30. unsigned long Length;
  31. inline int Compare3(const PkgName &x) const
  32. {
  33. int A = stringcasecmp(Name,x.Name);
  34. if (A == 0)
  35. {
  36. A = stringcasecmp(Ver,x.Ver);
  37. if (A == 0)
  38. A = stringcasecmp(Arch,x.Arch);
  39. }
  40. return A;
  41. }
  42. bool operator <(const PkgName &x) const {return Compare3(x) < 0;};
  43. bool operator >(const PkgName &x) const {return Compare3(x) > 0;};
  44. bool operator ==(const PkgName &x) const {return Compare3(x) == 0;};
  45. };
  46. // DoIt - Sort a single file /*{{{*/
  47. // ---------------------------------------------------------------------
  48. /* */
  49. bool DoIt(string InFile)
  50. {
  51. FileFd Fd(InFile,FileFd::ReadOnly);
  52. pkgTagFile Tags(&Fd);
  53. if (_error->PendingError() == true)
  54. return false;
  55. // Parse.
  56. vector<PkgName> List;
  57. pkgTagSection Section;
  58. unsigned long Largest = 0;
  59. unsigned long Offset = Tags.Offset();
  60. bool Source = _config->FindB("APT::SortPkgs::Source",false);
  61. while (Tags.Step(Section) == true)
  62. {
  63. PkgName Tmp;
  64. /* Fetch the name, auto-detecting if this is a source file or a
  65. package file */
  66. Tmp.Name = Section.FindS("Package");
  67. Tmp.Ver = Section.FindS("Version");
  68. Tmp.Arch = Section.FindS("Architecture");
  69. if (Tmp.Name.empty() == true)
  70. return _error->Error(_("Unknown package record!"));
  71. Tmp.Offset = Offset;
  72. Tmp.Length = Section.size();
  73. if (Largest < Tmp.Length)
  74. Largest = Tmp.Length;
  75. List.push_back(Tmp);
  76. Offset = Tags.Offset();
  77. }
  78. if (_error->PendingError() == true)
  79. return false;
  80. // Sort it
  81. sort(List.begin(),List.end());
  82. const char **Order = TFRewritePackageOrder;
  83. if (Source == true)
  84. Order = TFRewriteSourceOrder;
  85. // Emit
  86. unsigned char *Buffer = new unsigned char[Largest+1];
  87. for (vector<PkgName>::iterator I = List.begin(); I != List.end(); I++)
  88. {
  89. // Read in the Record.
  90. if (Fd.Seek(I->Offset) == false || Fd.Read(Buffer,I->Length) == false)
  91. {
  92. delete [] Buffer;
  93. return false;
  94. }
  95. Buffer[I->Length] = '\n';
  96. if (Section.Scan((char *)Buffer,I->Length+1) == false)
  97. {
  98. delete [] Buffer;
  99. return _error->Error("Internal error, failed to scan buffer");
  100. }
  101. // Sort the section
  102. if (TFRewrite(stdout,Section,Order,0) == false)
  103. {
  104. delete [] Buffer;
  105. return _error->Error("Internal error, failed to sort fields");
  106. }
  107. fputc('\n',stdout);
  108. }
  109. delete [] Buffer;
  110. return true;
  111. }
  112. /*}}}*/
  113. // ShowHelp - Show the help text /*{{{*/
  114. // ---------------------------------------------------------------------
  115. /* */
  116. int ShowHelp()
  117. {
  118. ioprintf(cout,_("%s %s for %s %s compiled on %s %s\n"),PACKAGE,VERSION,
  119. COMMON_OS,COMMON_CPU,__DATE__,__TIME__);
  120. if (_config->FindB("version") == true)
  121. return 0;
  122. cout <<
  123. _("Usage: apt-sortpkgs [options] file1 [file2 ...]\n"
  124. "\n"
  125. "apt-sortpkgs is a simple tool to sort package files. The -s option is used\n"
  126. "to indicate what kind of file it is.\n"
  127. "\n"
  128. "Options:\n"
  129. " -h This help text\n"
  130. " -s Use source file sorting\n"
  131. " -c=? Read this configuration file\n"
  132. " -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp\n");
  133. return 0;
  134. }
  135. /*}}}*/
  136. int main(unsigned int argc,const char *argv[])
  137. {
  138. CommandLine::Args Args[] = {
  139. {'h',"help","help",0},
  140. {'v',"version","version",0},
  141. {'s',"source","APT::SortPkgs::Source",0},
  142. {'c',"config-file",0,CommandLine::ConfigFile},
  143. {'o',"option",0,CommandLine::ArbItem},
  144. {0,0,0,0}};
  145. // Parse the command line and initialize the package library
  146. CommandLine CmdL(Args,_config);
  147. if (pkgInitConfig(*_config) == false ||
  148. CmdL.Parse(argc,argv) == false ||
  149. pkgInitSystem(*_config,_system) == false)
  150. {
  151. _error->DumpErrors();
  152. return 100;
  153. }
  154. // See if the help should be shown
  155. if (_config->FindB("help") == true ||
  156. CmdL.FileSize() == 0)
  157. return ShowHelp();
  158. // Match the operation
  159. for (unsigned int I = 0; I != CmdL.FileSize(); I++)
  160. if (DoIt(CmdL.FileList[I]) == false)
  161. break;
  162. // Print any errors or warnings found during parsing
  163. if (_error->empty() == false)
  164. {
  165. bool Errors = _error->PendingError();
  166. _error->DumpErrors();
  167. return Errors == true?100:0;
  168. }
  169. return 0;
  170. }