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