]> git.saurik.com Git - apt.git/blame - apt-private/private-search.cc
tests: add epoch-packages to try clean with epochs
[apt.git] / apt-private / private-search.cc
CommitLineData
ee0167c4 1// Includes /*{{{*/
453b82a3
DK
2#include <config.h>
3
b9179170 4#include <apt-pkg/cachefile.h>
b9179170 5#include <apt-pkg/cacheset.h>
b9179170 6#include <apt-pkg/cmndline.h>
b9179170 7#include <apt-pkg/pkgrecords.h>
b9179170 8#include <apt-pkg/policy.h>
453b82a3
DK
9#include <apt-pkg/progress.h>
10#include <apt-pkg/cacheiterators.h>
11#include <apt-pkg/configuration.h>
12#include <apt-pkg/depcache.h>
13#include <apt-pkg/macros.h>
14#include <apt-pkg/pkgcache.h>
b9179170 15
453b82a3
DK
16#include <apt-private/private-cacheset.h>
17#include <apt-private/private-output.h>
18#include <apt-private/private-search.h>
631800b1 19#include <apt-private/private-show.h>
453b82a3
DK
20
21#include <string.h>
b9179170 22#include <iostream>
453b82a3 23#include <sstream>
b9179170 24#include <map>
453b82a3
DK
25#include <string>
26#include <utility>
b9179170 27
453b82a3 28#include <apti18n.h>
ee0167c4 29 /*}}}*/
b9179170 30
631800b1 31static bool FullTextSearch(CommandLine &CmdL) /*{{{*/
b9179170
MV
32{
33 pkgCacheFile CacheFile;
34 pkgCache *Cache = CacheFile.GetPkgCache();
35 pkgDepCache::Policy *Plcy = CacheFile.GetPolicy();
b9179170
MV
36 if (unlikely(Cache == NULL || Plcy == NULL))
37 return false;
38
859093da
DK
39 // Make sure there is at least one argument
40 unsigned int const NumPatterns = CmdL.FileSize() -1;
41 if (NumPatterns < 1)
42 return _error->Error(_("You must give at least one search pattern"));
43
44#define APT_FREE_PATTERNS() for (std::vector<regex_t>::iterator P = Patterns.begin(); \
45 P != Patterns.end(); ++P) { regfree(&(*P)); }
46
47 // Compile the regex pattern
48 std::vector<regex_t> Patterns;
49 for (unsigned int I = 0; I != NumPatterns; ++I)
50 {
51 regex_t pattern;
52 if (regcomp(&pattern, CmdL.FileList[I + 1], REG_EXTENDED | REG_ICASE | REG_NOSUB) != 0)
53 {
54 APT_FREE_PATTERNS();
55 return _error->Error("Regex compilation error");
56 }
57 Patterns.push_back(pattern);
58 }
59
b9179170 60 std::map<std::string, std::string> output_map;
b9179170
MV
61
62 LocalitySortedVersionSet bag;
1bef0dd5 63 OpTextProgress progress(*_config);
b9179170 64 progress.OverallProgress(0, 100, 50, _("Sorting"));
25594bb5 65 GetLocalitySortedVersionSet(CacheFile, &bag, &progress);
b9179170
MV
66 LocalitySortedVersionSet::iterator V = bag.begin();
67
68 progress.OverallProgress(50, 100, 50, _("Full Text Search"));
69 progress.SubProgress(bag.size());
206b6bb3 70 pkgRecords records(CacheFile);
1a68655d
DK
71
72 std::string format = "${color:highlight}${Package}${color:neutral}/${Origin} ${Version} ${Architecture}${ }${apt:Status}\n";
73 if (_config->FindB("APT::Cache::ShowFull",false) == false)
74 format += " ${Description}\n";
75 else
76 format += " ${LongDescription}\n";
77
631800b1 78 bool const NamesOnly = _config->FindB("APT::Cache::NamesOnly", false);
b9179170 79 int Done = 0;
25594bb5 80 std::vector<bool> PkgsDone(Cache->Head().PackageCount, false);
9ce3cfc9 81 for ( ;V != bag.end(); ++V)
b9179170
MV
82 {
83 if (Done%500 == 0)
84 progress.Progress(Done);
9ce3cfc9 85 ++Done;
859093da 86
206b6bb3 87 // we want to list each package only once
25594bb5
DK
88 pkgCache::PkgIterator const P = V.ParentPkg();
89 if (PkgsDone[P->ID] == true)
206b6bb3
DK
90 continue;
91
25594bb5 92 char const * const PkgName = P.Name();
b9179170 93 pkgCache::DescIterator Desc = V.TranslatedDescription();
ace5a062 94 std::string LongDesc = "";
fe4a5378
JAK
95 if (Desc.end() == false)
96 {
ace5a062
JAK
97 pkgRecords::Parser &parser = records.Lookup(Desc.FileList());
98 LongDesc = parser.LongDesc();
99 }
859093da 100
b9179170 101 bool all_found = true;
859093da
DK
102 for (std::vector<regex_t>::const_iterator pattern = Patterns.begin();
103 pattern != Patterns.end(); ++pattern)
b9179170 104 {
206b6bb3 105 if (regexec(&(*pattern), PkgName, 0, 0, 0) == 0)
859093da 106 continue;
206b6bb3 107 else if (NamesOnly == false && regexec(&(*pattern), LongDesc.c_str(), 0, 0, 0) == 0)
859093da
DK
108 continue;
109 // search patterns are AND, so one failing fails all
110 all_found = false;
111 break;
b9179170 112 }
859093da 113 if (all_found == true)
b9179170 114 {
25594bb5
DK
115 PkgsDone[P->ID] = true;
116 std::stringstream outs;
117 ListSingleVersion(CacheFile, records, V, outs, format);
118 output_map.insert(std::make_pair<std::string, std::string>(
119 PkgName, outs.str()));
b9179170
MV
120 }
121 }
859093da 122 APT_FREE_PATTERNS();
b9179170
MV
123 progress.Done();
124
125 // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status)
126 // output the sorted map
206b6bb3 127 std::map<std::string, std::string>::const_iterator K;
9ce3cfc9 128 for (K = output_map.begin(); K != output_map.end(); ++K)
b9179170
MV
129 std::cout << (*K).second << std::endl;
130
131 return true;
132}
ee0167c4 133 /*}}}*/
631800b1
DK
134// LocalitySort - Sort a version list by package file locality /*{{{*/
135static int LocalityCompare(const void * const a, const void * const b)
136{
137 pkgCache::VerFile const * const A = *(pkgCache::VerFile const * const * const)a;
138 pkgCache::VerFile const * const B = *(pkgCache::VerFile const * const * const)b;
139
140 if (A == 0 && B == 0)
141 return 0;
142 if (A == 0)
143 return 1;
144 if (B == 0)
145 return -1;
146
147 if (A->File == B->File)
148 return A->Offset - B->Offset;
149 return A->File - B->File;
150}
151void LocalitySort(pkgCache::VerFile ** const begin, unsigned long long const Count,size_t const Size)
152{
153 qsort(begin,Count,Size,LocalityCompare);
154}
155static void LocalitySort(pkgCache::DescFile ** const begin, unsigned long long const Count,size_t const Size)
156{
157 qsort(begin,Count,Size,LocalityCompare);
158}
159 /*}}}*/
160// Search - Perform a search /*{{{*/
161// ---------------------------------------------------------------------
162/* This searches the package names and package descriptions for a pattern */
163struct ExDescFile
164{
165 pkgCache::DescFile *Df;
166 pkgCache::VerIterator V;
167 map_id_t ID;
168};
169static bool Search(CommandLine &CmdL)
170{
171 bool const ShowFull = _config->FindB("APT::Cache::ShowFull",false);
172 unsigned int const NumPatterns = CmdL.FileSize() -1;
173
174 pkgCacheFile CacheFile;
175 pkgCache *Cache = CacheFile.GetPkgCache();
176 pkgDepCache::Policy *Plcy = CacheFile.GetPolicy();
177 if (unlikely(Cache == NULL || Plcy == NULL))
178 return false;
179
180 // Make sure there is at least one argument
181 if (NumPatterns < 1)
182 return _error->Error(_("You must give at least one search pattern"));
183
184 // Compile the regex pattern
185 regex_t *Patterns = new regex_t[NumPatterns];
186 memset(Patterns,0,sizeof(*Patterns)*NumPatterns);
187 for (unsigned I = 0; I != NumPatterns; I++)
188 {
189 if (regcomp(&Patterns[I],CmdL.FileList[I+1],REG_EXTENDED | REG_ICASE |
190 REG_NOSUB) != 0)
191 {
192 for (; I != 0; I--)
193 regfree(&Patterns[I]);
194 return _error->Error("Regex compilation error");
195 }
196 }
197
198 if (_error->PendingError() == true)
199 {
200 for (unsigned I = 0; I != NumPatterns; I++)
201 regfree(&Patterns[I]);
202 return false;
203 }
204
205 size_t const descCount = Cache->HeaderP->GroupCount + 1;
206 ExDescFile *DFList = new ExDescFile[descCount];
207 memset(DFList,0,sizeof(*DFList) * descCount);
208
209 bool *PatternMatch = new bool[descCount * NumPatterns];
210 memset(PatternMatch,false,sizeof(*PatternMatch) * descCount * NumPatterns);
211
212 // Map versions that we want to write out onto the VerList array.
213 bool const NamesOnly = _config->FindB("APT::Cache::NamesOnly",false);
214 for (pkgCache::GrpIterator G = Cache->GrpBegin(); G.end() == false; ++G)
215 {
216 size_t const PatternOffset = G->ID * NumPatterns;
217 size_t unmatched = 0, matched = 0;
218 for (unsigned I = 0; I < NumPatterns; ++I)
219 {
220 if (PatternMatch[PatternOffset + I] == true)
221 ++matched;
222 else if (regexec(&Patterns[I],G.Name(),0,0,0) == 0)
223 PatternMatch[PatternOffset + I] = true;
224 else
225 ++unmatched;
226 }
227
228 // already dealt with this package?
229 if (matched == NumPatterns)
230 continue;
231
232 // Doing names only, drop any that don't match..
233 if (NamesOnly == true && unmatched == NumPatterns)
234 continue;
235
236 // Find the proper version to use
237 pkgCache::PkgIterator P = G.FindPreferredPkg();
238 if (P.end() == true)
239 continue;
240 pkgCache::VerIterator V = Plcy->GetCandidateVer(P);
241 if (V.end() == false)
242 {
243 pkgCache::DescIterator const D = V.TranslatedDescription();
244 //FIXME: packages without a description can't be found
245 if (D.end() == true)
246 continue;
247 DFList[G->ID].Df = D.FileList();
248 DFList[G->ID].V = V;
249 DFList[G->ID].ID = G->ID;
250 }
251
252 if (unmatched == NumPatterns)
253 continue;
254
255 // Include all the packages that provide matching names too
256 for (pkgCache::PrvIterator Prv = P.ProvidesList() ; Prv.end() == false; ++Prv)
257 {
258 pkgCache::VerIterator V = Plcy->GetCandidateVer(Prv.OwnerPkg());
259 if (V.end() == true)
260 continue;
261
262 unsigned long id = Prv.OwnerPkg().Group()->ID;
263 pkgCache::DescIterator const D = V.TranslatedDescription();
264 //FIXME: packages without a description can't be found
265 if (D.end() == true)
266 continue;
267 DFList[id].Df = D.FileList();
268 DFList[id].V = V;
269 DFList[id].ID = id;
270
271 size_t const PrvPatternOffset = id * NumPatterns;
272 for (unsigned I = 0; I < NumPatterns; ++I)
273 PatternMatch[PrvPatternOffset + I] |= PatternMatch[PatternOffset + I];
274 }
275 }
276
277 LocalitySort(&DFList->Df, Cache->HeaderP->GroupCount, sizeof(*DFList));
278
279 // Create the text record parser
280 pkgRecords Recs(*Cache);
281 // Iterate over all the version records and check them
282 for (ExDescFile *J = DFList; J->Df != 0; ++J)
283 {
284 pkgRecords::Parser &P = Recs.Lookup(pkgCache::DescFileIterator(*Cache,J->Df));
285 size_t const PatternOffset = J->ID * NumPatterns;
286
287 if (NamesOnly == false)
288 {
289 std::string const LongDesc = P.LongDesc();
290 for (unsigned I = 0; I < NumPatterns; ++I)
291 {
292 if (PatternMatch[PatternOffset + I] == true)
293 continue;
294 else if (regexec(&Patterns[I],LongDesc.c_str(),0,0,0) == 0)
295 PatternMatch[PatternOffset + I] = true;
296 }
297 }
298
299 bool matchedAll = true;
300 for (unsigned I = 0; I < NumPatterns; ++I)
301 if (PatternMatch[PatternOffset + I] == false)
302 {
303 matchedAll = false;
304 break;
305 }
306
307 if (matchedAll == true)
308 {
309 if (ShowFull == true)
310 DisplayRecordV1(CacheFile, J->V, std::cout);
311 else
312 printf("%s - %s\n",P.Name().c_str(),P.ShortDesc().c_str());
313 }
314 }
315
316 delete [] DFList;
317 delete [] PatternMatch;
318 for (unsigned I = 0; I != NumPatterns; I++)
319 regfree(&Patterns[I]);
320 delete [] Patterns;
321 if (ferror(stdout))
322 return _error->Error("Write to stdout failed");
323 return true;
324}
325 /*}}}*/
326bool DoSearch(CommandLine &CmdL) /*{{{*/
327{
328 int const ShowVersion = _config->FindI("APT::Cache::Search::Version", 1);
329 if (ShowVersion <= 1)
330 return Search(CmdL);
331 return FullTextSearch(CmdL);
332}
333