merged debian-sid branch and resolved conflicts
[ntk/apt.git] / apt-pkg / deb / debsrcrecords.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: debsrcrecords.cc,v 1.6 2004/03/17 05:58:54 mdz Exp $
4 /* ######################################################################
5
6 Debian Source Package Records - Parser implementation for Debian style
7 source indexes
8
9 ##################################################################### */
10 /*}}}*/
11 // Include Files /*{{{*/
12 #include <config.h>
13
14 #include <apt-pkg/deblistparser.h>
15 #include <apt-pkg/debsrcrecords.h>
16 #include <apt-pkg/error.h>
17 #include <apt-pkg/strutl.h>
18 #include <apt-pkg/configuration.h>
19 #include <apt-pkg/aptconfiguration.h>
20 #include <apt-pkg/hashes.h>
21
22 using std::max;
23 /*}}}*/
24
25 using std::string;
26
27 // SrcRecordParser::Binaries - Return the binaries field /*{{{*/
28 // ---------------------------------------------------------------------
29 /* This member parses the binaries field into a pair of class arrays and
30 returns a list of strings representing all of the components of the
31 binaries field. The returned array need not be freed and will be
32 reused by the next Binaries function call. This function is commonly
33 used during scanning to find the right package */
34 const char **debSrcRecordParser::Binaries()
35 {
36 const char *Start, *End;
37 if (Sect.Find("Binary", Start, End) == false)
38 return NULL;
39 for (; isspace(*Start) != 0; ++Start);
40 if (Start >= End)
41 return NULL;
42
43 StaticBinList.clear();
44 free(Buffer);
45 Buffer = strndup(Start, End - Start);
46
47 char* bin = Buffer;
48 do {
49 char* binStartNext = strchrnul(bin, ',');
50 char* binEnd = binStartNext - 1;
51 for (; isspace(*binEnd) != 0; --binEnd)
52 binEnd = '\0';
53 StaticBinList.push_back(bin);
54 if (*binStartNext != ',')
55 break;
56 *binStartNext = '\0';
57 for (bin = binStartNext + 1; isspace(*bin) != 0; ++bin);
58 } while (*bin != '\0');
59 StaticBinList.push_back(NULL);
60
61 return (const char **) &StaticBinList[0];
62 }
63 /*}}}*/
64 // SrcRecordParser::BuildDepends - Return the Build-Depends information /*{{{*/
65 // ---------------------------------------------------------------------
66 /* This member parses the build-depends information and returns a list of
67 package/version records representing the build dependency. The returned
68 array need not be freed and will be reused by the next call to this
69 function */
70 bool debSrcRecordParser::BuildDepends(std::vector<pkgSrcRecords::Parser::BuildDepRec> &BuildDeps,
71 bool const &ArchOnly, bool const &StripMultiArch)
72 {
73 unsigned int I;
74 const char *Start, *Stop;
75 BuildDepRec rec;
76 const char *fields[] = {"Build-Depends",
77 "Build-Depends-Indep",
78 "Build-Conflicts",
79 "Build-Conflicts-Indep"};
80
81 BuildDeps.clear();
82
83 for (I = 0; I < 4; I++)
84 {
85 if (ArchOnly && (I == 1 || I == 3))
86 continue;
87
88 if (Sect.Find(fields[I], Start, Stop) == false)
89 continue;
90
91 while (1)
92 {
93 Start = debListParser::ParseDepends(Start, Stop,
94 rec.Package,rec.Version,rec.Op,true, StripMultiArch);
95
96 if (Start == 0)
97 return _error->Error("Problem parsing dependency: %s", fields[I]);
98 rec.Type = I;
99
100 if (rec.Package != "")
101 BuildDeps.push_back(rec);
102
103 if (Start == Stop)
104 break;
105 }
106 }
107
108 return true;
109 }
110 /*}}}*/
111 // SrcRecordParser::Files - Return a list of files for this source /*{{{*/
112 // ---------------------------------------------------------------------
113 /* This parses the list of files and returns it, each file is required to have
114 a complete source package */
115 bool debSrcRecordParser::Files(std::vector<pkgSrcRecords::File> &List)
116 {
117 List.erase(List.begin(),List.end());
118
119 // map from the Hashsum field to the hashsum function,
120 // unfortunately this is not a 1:1 mapping from
121 // Hashes::SupporedHashes as e.g. Files is a historic name for the md5
122 const std::pair<const char*, const char*> SourceHashFields[] = {
123 std::make_pair( "Checksums-Sha512", "SHA512"),
124 std::make_pair( "Checksums-Sha256", "SHA256"),
125 std::make_pair( "Checksums-Sha1", "SHA1"),
126 std::make_pair( "Files", "MD5Sum"), // historic Name
127 };
128
129 for (unsigned int i=0;
130 i < sizeof(SourceHashFields)/sizeof(SourceHashFields[0]);
131 i++)
132 {
133 string Files = Sect.FindS(SourceHashFields[i].first);
134 if (Files.empty() == true)
135 continue;
136
137 // Stash the / terminated directory prefix
138 string Base = Sect.FindS("Directory");
139 if (Base.empty() == false && Base[Base.length()-1] != '/')
140 Base += '/';
141
142 std::vector<std::string> const compExts = APT::Configuration::getCompressorExtensions();
143
144 // Iterate over the entire list grabbing each triplet
145 const char *C = Files.c_str();
146 while (*C != 0)
147 {
148 pkgSrcRecords::File F;
149 string Size;
150
151 // Parse each of the elements
152 std::string RawHash;
153 if (ParseQuoteWord(C, RawHash) == false ||
154 ParseQuoteWord(C, Size) == false ||
155 ParseQuoteWord(C, F.Path) == false)
156 return _error->Error("Error parsing '%s' record",
157 SourceHashFields[i].first);
158 // assign full hash string
159 F.Hash = HashString(SourceHashFields[i].second, RawHash).toStr();
160 // API compat hack
161 if(SourceHashFields[i].second == "MD5Sum")
162 F.MD5Hash = RawHash;
163
164 // Parse the size and append the directory
165 F.Size = atoi(Size.c_str());
166 F.Path = Base + F.Path;
167
168 // Try to guess what sort of file it is we are getting.
169 string::size_type Pos = F.Path.length()-1;
170 while (1)
171 {
172 string::size_type Tmp = F.Path.rfind('.',Pos);
173 if (Tmp == string::npos)
174 break;
175 if (F.Type == "tar") {
176 // source v3 has extension 'debian.tar.*' instead of 'diff.*'
177 if (string(F.Path, Tmp+1, Pos-Tmp) == "debian")
178 F.Type = "diff";
179 break;
180 }
181 F.Type = string(F.Path,Tmp+1,Pos-Tmp);
182
183 if (std::find(compExts.begin(), compExts.end(), std::string(".").append(F.Type)) != compExts.end() ||
184 F.Type == "tar")
185 {
186 Pos = Tmp-1;
187 continue;
188 }
189
190 break;
191 }
192
193 List.push_back(F);
194 }
195 break;
196 }
197 return (List.size() > 0);
198 }
199 /*}}}*/
200 // SrcRecordParser::~SrcRecordParser - Destructor /*{{{*/
201 // ---------------------------------------------------------------------
202 /* */
203 debSrcRecordParser::~debSrcRecordParser()
204 {
205 delete[] Buffer;
206 }
207 /*}}}*/