]> git.saurik.com Git - apt.git/blob - cmdline/apt-sortpkgs.cc
Merge branch 'portability/freebsd'
[apt.git] / cmdline / apt-sortpkgs.cc
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
6 APT Sort Packages - Program to sort Package and Source files
7
8 This program is quite simple, it just sorts the package files by
9 package and sorts the fields inside by the internal APT sort order.
10 Input is taken from a named file and sent to stdout.
11
12 ##################################################################### */
13 /*}}}*/
14 // Include Files /*{{{*/
15 #include <config.h>
16
17 #include <apt-pkg/tagfile.h>
18 #include <apt-pkg/error.h>
19 #include <apt-pkg/configuration.h>
20 #include <apt-pkg/cmndline.h>
21 #include <apt-pkg/init.h>
22 #include <apt-pkg/strutl.h>
23 #include <apt-pkg/fileutl.h>
24 #include <apt-pkg/pkgsystem.h>
25
26 #include <apt-private/private-cmndline.h>
27 #include <apt-private/private-main.h>
28
29 #include <vector>
30 #include <algorithm>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <iostream>
34 #include <string>
35 #include <memory>
36
37 #include <apti18n.h>
38 /*}}}*/
39
40 using namespace std;
41
42 struct PkgName /*{{{*/
43 {
44 string Name;
45 string Ver;
46 string Arch;
47 unsigned long Offset;
48 unsigned long Length;
49
50 inline int Compare3(const PkgName &x) const
51 {
52 int A = stringcasecmp(Name,x.Name);
53 if (A == 0)
54 {
55 A = stringcasecmp(Ver,x.Ver);
56 if (A == 0)
57 A = stringcasecmp(Arch,x.Arch);
58 }
59 return A;
60 }
61
62 bool operator <(const PkgName &x) const {return Compare3(x) < 0;};
63 bool operator >(const PkgName &x) const {return Compare3(x) > 0;};
64 bool operator ==(const PkgName &x) const {return Compare3(x) == 0;};
65 };
66 /*}}}*/
67 // DoIt - Sort a single file /*{{{*/
68 // ---------------------------------------------------------------------
69 /* */
70 static bool DoIt(string InFile)
71 {
72 FileFd Fd(InFile,FileFd::ReadOnly);
73 pkgTagFile Tags(&Fd);
74 if (_error->PendingError() == true)
75 return false;
76
77 // Parse.
78 vector<PkgName> List;
79 pkgTagSection Section;
80 unsigned long Largest = 0;
81 unsigned long Offset = Tags.Offset();
82 bool Source = _config->FindB("APT::SortPkgs::Source",false);
83 while (Tags.Step(Section) == true)
84 {
85 PkgName Tmp;
86
87 /* Fetch the name, auto-detecting if this is a source file or a
88 package file */
89 Tmp.Name = Section.FindS("Package");
90 Tmp.Ver = Section.FindS("Version");
91 Tmp.Arch = Section.FindS("Architecture");
92
93 if (Tmp.Name.empty() == true)
94 return _error->Error(_("Unknown package record!"));
95
96 Tmp.Offset = Offset;
97 Tmp.Length = Section.size();
98 if (Largest < Tmp.Length)
99 Largest = Tmp.Length;
100
101 List.push_back(Tmp);
102
103 Offset = Tags.Offset();
104 }
105 if (_error->PendingError() == true)
106 return false;
107
108 // Sort it
109 sort(List.begin(),List.end());
110
111 const char **Order = TFRewritePackageOrder;
112 if (Source == true)
113 Order = TFRewriteSourceOrder;
114
115 // Emit
116 FileFd stdoutfd;
117 stdoutfd.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly, false);
118 auto const Buffer = std::unique_ptr<unsigned char[]>(new unsigned char[Largest+1]);
119 for (vector<PkgName>::iterator I = List.begin(); I != List.end(); ++I)
120 {
121 // Read in the Record.
122 if (Fd.Seek(I->Offset) == false || Fd.Read(Buffer.get(),I->Length) == false)
123 return false;
124
125 Buffer[I->Length] = '\n';
126 if (Section.Scan((char *)Buffer.get(),I->Length+1) == false)
127 return _error->Error("Internal error, failed to scan buffer");
128
129 // Sort the section
130 if (Section.Write(stdoutfd, Order) == false || stdoutfd.Write("\n", 1) == false)
131 return _error->Error("Internal error, failed to sort fields");
132 }
133 return true;
134 }
135 /*}}}*/
136 static bool ShowHelp(CommandLine &) /*{{{*/
137 {
138 std::cout <<
139 _("Usage: apt-sortpkgs [options] file1 [file2 ...]\n"
140 "\n"
141 "apt-sortpkgs is a simple tool to sort package information files.\n"
142 "By default it sorts by binary package information, but the -s option\n"
143 "can be used to switch to source package ordering instead.\n");
144 return true;
145 }
146 /*}}}*/
147 static std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
148 {
149 return {
150 {nullptr, nullptr, nullptr}
151 };
152 }
153 /*}}}*/
154 int main(int argc,const char *argv[]) /*{{{*/
155 {
156 CommandLine CmdL;
157 ParseCommandLine(CmdL, APT_CMD::APT_SORTPKG, &_config, &_system, argc, argv, &ShowHelp, &GetCommands);
158
159 // Match the operation
160 for (unsigned int I = 0; I != CmdL.FileSize(); I++)
161 if (DoIt(CmdL.FileList[I]) == false)
162 break;
163
164 return DispatchCommandLine(CmdL, {});
165 }
166 /*}}}*/