use 'best' hash for source authentication
[ntk/apt.git] / apt-pkg / deb / debsrcrecords.cc
CommitLineData
11e7af84
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
c33b707e 3// $Id: debsrcrecords.cc,v 1.6 2004/03/17 05:58:54 mdz Exp $
11e7af84
AL
4/* ######################################################################
5
6 Debian Source Package Records - Parser implementation for Debian style
7 source indexes
8
9 ##################################################################### */
10 /*}}}*/
11// Include Files /*{{{*/
ea542140
DK
12#include <config.h>
13
b2e465d6 14#include <apt-pkg/deblistparser.h>
11e7af84
AL
15#include <apt-pkg/debsrcrecords.h>
16#include <apt-pkg/error.h>
36f610f1 17#include <apt-pkg/strutl.h>
b0e1a43f 18#include <apt-pkg/aptconfiguration.h>
453b82a3
DK
19#include <apt-pkg/srcrecords.h>
20#include <apt-pkg/tagfile.h>
4ab24e53 21
453b82a3
DK
22#include <ctype.h>
23#include <stdlib.h>
24#include <string.h>
25#include <algorithm>
26#include <string>
27#include <vector>
11e7af84
AL
28 /*}}}*/
29
453b82a3 30using std::max;
8f3ba4e8
DK
31using std::string;
32
11e7af84
AL
33// SrcRecordParser::Binaries - Return the binaries field /*{{{*/
34// ---------------------------------------------------------------------
35/* This member parses the binaries field into a pair of class arrays and
36 returns a list of strings representing all of the components of the
37 binaries field. The returned array need not be freed and will be
b2e465d6
AL
38 reused by the next Binaries function call. This function is commonly
39 used during scanning to find the right package */
11e7af84
AL
40const char **debSrcRecordParser::Binaries()
41{
39fb1e24
DK
42 const char *Start, *End;
43 if (Sect.Find("Binary", Start, End) == false)
44 return NULL;
45 for (; isspace(*Start) != 0; ++Start);
46 if (Start >= End)
47 return NULL;
48
49 StaticBinList.clear();
50 free(Buffer);
51 Buffer = strndup(Start, End - Start);
c33b707e 52
39fb1e24
DK
53 char* bin = Buffer;
54 do {
55 char* binStartNext = strchrnul(bin, ',');
56 char* binEnd = binStartNext - 1;
57 for (; isspace(*binEnd) != 0; --binEnd)
58 binEnd = '\0';
59 StaticBinList.push_back(bin);
60 if (*binStartNext != ',')
61 break;
62 *binStartNext = '\0';
63 for (bin = binStartNext + 1; isspace(*bin) != 0; ++bin);
64 } while (*bin != '\0');
65 StaticBinList.push_back(NULL);
c33b707e 66
e788a834 67 return &StaticBinList[0];
b2e465d6
AL
68}
69 /*}}}*/
70// SrcRecordParser::BuildDepends - Return the Build-Depends information /*{{{*/
71// ---------------------------------------------------------------------
72/* This member parses the build-depends information and returns a list of
73 package/version records representing the build dependency. The returned
74 array need not be freed and will be reused by the next call to this
75 function */
8f3ba4e8 76bool debSrcRecordParser::BuildDepends(std::vector<pkgSrcRecords::Parser::BuildDepRec> &BuildDeps,
41c81fd8 77 bool const &ArchOnly, bool const &StripMultiArch)
b2e465d6
AL
78{
79 unsigned int I;
80 const char *Start, *Stop;
81 BuildDepRec rec;
82 const char *fields[] = {"Build-Depends",
83 "Build-Depends-Indep",
84 "Build-Conflicts",
85 "Build-Conflicts-Indep"};
86
87 BuildDeps.clear();
11e7af84 88
b2e465d6 89 for (I = 0; I < 4; I++)
11e7af84 90 {
45430cbf
AL
91 if (ArchOnly && (I == 1 || I == 3))
92 continue;
93
b2e465d6
AL
94 if (Sect.Find(fields[I], Start, Stop) == false)
95 continue;
11e7af84 96
b2e465d6
AL
97 while (1)
98 {
99 Start = debListParser::ParseDepends(Start, Stop,
565ded7b 100 rec.Package,rec.Version,rec.Op,true,StripMultiArch,true);
b2e465d6
AL
101
102 if (Start == 0)
103 return _error->Error("Problem parsing dependency: %s", fields[I]);
104 rec.Type = I;
105
106 if (rec.Package != "")
107 BuildDeps.push_back(rec);
108
109 if (Start == Stop)
110 break;
111 }
11e7af84
AL
112 }
113
b2e465d6 114 return true;
11e7af84
AL
115}
116 /*}}}*/
36f610f1
AL
117// SrcRecordParser::Files - Return a list of files for this source /*{{{*/
118// ---------------------------------------------------------------------
119/* This parses the list of files and returns it, each file is required to have
120 a complete source package */
3a2b39ee 121bool debSrcRecordParser::Files(std::vector<pkgSrcRecords::File> &F)
36f610f1 122{
3a2b39ee
DK
123 std::vector<pkgSrcRecords::File2> F2;
124 if (Files2(F2) == false)
36f610f1 125 return false;
3a2b39ee
DK
126 for (std::vector<pkgSrcRecords::File2>::const_iterator f2 = F2.begin(); f2 != F2.end(); ++f2)
127 {
128 pkgSrcRecords::File2 f;
129#if __GNUC__ >= 4
130 #pragma GCC diagnostic push
131 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
132#endif
133 f.MD5Hash = f2->MD5Hash;
134 f.Size = f2->Size;
135#if __GNUC__ >= 4
136 #pragma GCC diagnostic pop
137#endif
138 f.Path = f2->Path;
139 f.Type = f2->Type;
140 F.push_back(f);
141 }
142 return true;
143}
144bool debSrcRecordParser::Files2(std::vector<pkgSrcRecords::File2> &List)
145{
146 List.clear();
36f610f1
AL
147
148 // Stash the / terminated directory prefix
36375005 149 string Base = Sect.FindS("Directory");
36f610f1
AL
150 if (Base.empty() == false && Base[Base.length()-1] != '/')
151 Base += '/';
b0e1a43f
DK
152
153 std::vector<std::string> const compExts = APT::Configuration::getCompressorExtensions();
154
3a2b39ee
DK
155 for (char const * const * type = HashString::SupportedHashes(); *type != NULL; ++type)
156 {
157 // derive field from checksum type
158 std::string checksumField("Checksums-");
159 if (strcmp(*type, "MD5Sum") == 0)
160 checksumField = "Files"; // historic name for MD5 checksums
161 else
162 checksumField.append(*type);
163
164 string const Files = Sect.FindS(checksumField.c_str());
165 if (Files.empty() == true)
166 continue;
167
168 // Iterate over the entire list grabbing each triplet
169 const char *C = Files.c_str();
170 while (*C != 0)
b2e465d6 171 {
3a2b39ee
DK
172 string hash, size, path;
173
174 // Parse each of the elements
175 if (ParseQuoteWord(C, hash) == false ||
176 ParseQuoteWord(C, size) == false ||
177 ParseQuoteWord(C, path) == false)
178 return _error->Error("Error parsing file record in %s of source package %s", checksumField.c_str(), Package().c_str());
179
180 HashString const hashString(*type, hash);
181 if (Base.empty() == false)
182 path = Base + path;
183
184 // look if we have a record for this file already
185 std::vector<pkgSrcRecords::File2>::iterator file = List.begin();
186 for (; file != List.end(); ++file)
187 if (file->Path == path)
188 break;
189
190 // we have it already, store the new hash and be done
191 if (file != List.end())
b2e465d6 192 {
3a2b39ee
DK
193#if __GNUC__ >= 4
194 // set for compatibility only, so warn users not us
195 #pragma GCC diagnostic push
196 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
197#endif
198 if (checksumField == "Files")
199 file->MD5Hash = hash;
200#if __GNUC__ >= 4
201 #pragma GCC diagnostic pop
202#endif
203 // an error here indicates that we have two different hashes for the same file
204 if (file->Hashes.push_back(hashString) == false)
205 return _error->Error("Error parsing checksum in %s of source package %s", checksumField.c_str(), Package().c_str());
b2e465d6
AL
206 continue;
207 }
3a2b39ee
DK
208
209 // we haven't seen this file yet
210 pkgSrcRecords::File2 F;
211 F.Path = path;
212 F.FileSize = strtoull(size.c_str(), NULL, 10);
213 F.Hashes.push_back(hashString);
214
215#if __GNUC__ >= 4
216 // set for compatibility only, so warn users not us
217 #pragma GCC diagnostic push
218 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
219#endif
220 F.Size = F.FileSize;
221 if (checksumField == "Files")
222 F.MD5Hash = hash;
223#if __GNUC__ >= 4
224 #pragma GCC diagnostic pop
225#endif
226
227 // Try to guess what sort of file it is we are getting.
228 string::size_type Pos = F.Path.length()-1;
229 while (1)
230 {
231 string::size_type Tmp = F.Path.rfind('.',Pos);
232 if (Tmp == string::npos)
233 break;
234 if (F.Type == "tar") {
235 // source v3 has extension 'debian.tar.*' instead of 'diff.*'
236 if (string(F.Path, Tmp+1, Pos-Tmp) == "debian")
237 F.Type = "diff";
238 break;
239 }
240 F.Type = string(F.Path,Tmp+1,Pos-Tmp);
241
242 if (std::find(compExts.begin(), compExts.end(), std::string(".").append(F.Type)) != compExts.end() ||
243 F.Type == "tar")
244 {
245 Pos = Tmp-1;
246 continue;
247 }
248
249 break;
250 }
251 List.push_back(F);
b2e465d6 252 }
36f610f1 253 }
3a2b39ee 254
36f610f1
AL
255 return true;
256}
257 /*}}}*/
7a9f09bd
MV
258// SrcRecordParser::~SrcRecordParser - Destructor /*{{{*/
259// ---------------------------------------------------------------------
260/* */
261debSrcRecordParser::~debSrcRecordParser()
262{
7f48c4df
MV
263 // was allocated via strndup()
264 free(Buffer);
7a9f09bd
MV
265}
266 /*}}}*/