apt-sortpkgs.cc 5.5 KB

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