]>
Commit | Line | Data |
---|---|---|
143abaeb AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
90f057fd | 3 | // $Id: indexcopy.cc,v 1.10 2002/03/26 07:38:58 jgg Exp $ |
143abaeb AL |
4 | /* ###################################################################### |
5 | ||
6 | Index Copying - Aid for copying and verifying the index files | |
7 | ||
8 | This class helps apt-cache reconstruct a damaged index files. | |
9 | ||
10 | ##################################################################### */ | |
11 | /*}}}*/ | |
12 | // Include Files /*{{{*/ | |
ea542140 | 13 | #include<config.h> |
143abaeb AL |
14 | |
15 | #include <apt-pkg/error.h> | |
16 | #include <apt-pkg/progress.h> | |
17 | #include <apt-pkg/strutl.h> | |
18 | #include <apt-pkg/fileutl.h> | |
78c9276d | 19 | #include <apt-pkg/aptconfiguration.h> |
143abaeb AL |
20 | #include <apt-pkg/configuration.h> |
21 | #include <apt-pkg/tagfile.h> | |
a75c6a6e | 22 | #include <apt-pkg/indexrecords.h> |
a75c6a6e | 23 | #include <apt-pkg/cdrom.h> |
453b82a3 DK |
24 | #include <apt-pkg/gpgv.h> |
25 | #include <apt-pkg/hashes.h> | |
143abaeb | 26 | |
90f057fd | 27 | #include <iostream> |
a75c6a6e | 28 | #include <sstream> |
143abaeb AL |
29 | #include <unistd.h> |
30 | #include <sys/stat.h> | |
31 | #include <stdio.h> | |
650faab0 | 32 | #include <stdlib.h> |
453b82a3 | 33 | #include <string.h> |
ea542140 DK |
34 | |
35 | #include "indexcopy.h" | |
36 | #include <apti18n.h> | |
143abaeb AL |
37 | /*}}}*/ |
38 | ||
076d01b0 AL |
39 | using namespace std; |
40 | ||
143abaeb AL |
41 | // IndexCopy::CopyPackages - Copy the package files from the CD /*{{{*/ |
42 | // --------------------------------------------------------------------- | |
43 | /* */ | |
a75c6a6e MZ |
44 | bool IndexCopy::CopyPackages(string CDROM,string Name,vector<string> &List, |
45 | pkgCdromStatus *log) | |
143abaeb | 46 | { |
a75c6a6e | 47 | OpProgress *Progress = NULL; |
f7f0d6c7 | 48 | if (List.empty() == true) |
143abaeb AL |
49 | return true; |
50 | ||
a75c6a6e MZ |
51 | if(log) |
52 | Progress = log->GetOpProgress(); | |
143abaeb AL |
53 | |
54 | bool NoStat = _config->FindB("APT::CDROM::Fast",false); | |
55 | bool Debug = _config->FindB("Debug::aptcdrom",false); | |
56 | ||
57 | // Prepare the progress indicator | |
650faab0 | 58 | off_t TotalSize = 0; |
78c9276d | 59 | std::vector<APT::Configuration::Compressor> const compressor = APT::Configuration::getCompressors(); |
f7f0d6c7 | 60 | for (vector<string>::iterator I = List.begin(); I != List.end(); ++I) |
143abaeb AL |
61 | { |
62 | struct stat Buf; | |
78c9276d DK |
63 | bool found = false; |
64 | std::string file = std::string(*I).append(GetFileName()); | |
65 | for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin(); | |
66 | c != compressor.end(); ++c) | |
67 | { | |
e788a834 | 68 | if (stat((file + c->Extension).c_str(), &Buf) != 0) |
78c9276d DK |
69 | continue; |
70 | found = true; | |
71 | break; | |
72 | } | |
73 | ||
74 | if (found == false) | |
75 | return _error->Errno("stat", "Stat failed for %s", file.c_str()); | |
143abaeb | 76 | TotalSize += Buf.st_size; |
78c9276d | 77 | } |
143abaeb | 78 | |
650faab0 | 79 | off_t CurrentSize = 0; |
143abaeb AL |
80 | unsigned int NotFound = 0; |
81 | unsigned int WrongSize = 0; | |
82 | unsigned int Packages = 0; | |
f7f0d6c7 | 83 | for (vector<string>::iterator I = List.begin(); I != List.end(); ++I) |
143abaeb AL |
84 | { |
85 | string OrigPath = string(*I,CDROM.length()); | |
143abaeb AL |
86 | |
87 | // Open the package file | |
144353a9 | 88 | FileFd Pkg(*I + GetFileName(), FileFd::ReadOnly, FileFd::Auto); |
699b209e | 89 | off_t const FileSize = Pkg.Size(); |
01366a44 | 90 | |
b2e465d6 | 91 | pkgTagFile Parser(&Pkg); |
143abaeb AL |
92 | if (_error->PendingError() == true) |
93 | return false; | |
94 | ||
95 | // Open the output file | |
96 | char S[400]; | |
20ebd488 AL |
97 | snprintf(S,sizeof(S),"cdrom:[%s]/%s%s",Name.c_str(), |
98 | (*I).c_str() + CDROM.length(),GetFileName()); | |
143abaeb AL |
99 | string TargetF = _config->FindDir("Dir::State::lists") + "partial/"; |
100 | TargetF += URItoFileName(S); | |
8deb53ab | 101 | FileFd Target; |
143abaeb | 102 | if (_config->FindB("APT::CDROM::NoAct",false) == true) |
8deb53ab | 103 | { |
143abaeb | 104 | TargetF = "/dev/null"; |
8deb53ab MV |
105 | Target.Open(TargetF,FileFd::WriteExists); |
106 | } else { | |
107 | Target.Open(TargetF,FileFd::WriteAtomic); | |
108 | } | |
143abaeb AL |
109 | if (_error->PendingError() == true) |
110 | return false; | |
88593886 | 111 | |
143abaeb | 112 | // Setup the progress meter |
a75c6a6e MZ |
113 | if(Progress) |
114 | Progress->OverallProgress(CurrentSize,TotalSize,FileSize, | |
115 | string("Reading ") + Type() + " Indexes"); | |
143abaeb AL |
116 | |
117 | // Parse | |
a75c6a6e MZ |
118 | if(Progress) |
119 | Progress->SubProgress(Pkg.Size()); | |
143abaeb AL |
120 | pkgTagSection Section; |
121 | this->Section = &Section; | |
122 | string Prefix; | |
123 | unsigned long Hits = 0; | |
124 | unsigned long Chop = 0; | |
125 | while (Parser.Step(Section) == true) | |
126 | { | |
a75c6a6e MZ |
127 | if(Progress) |
128 | Progress->Progress(Parser.Offset()); | |
143abaeb | 129 | string File; |
650faab0 | 130 | unsigned long long Size; |
143abaeb AL |
131 | if (GetFile(File,Size) == false) |
132 | return false; | |
88593886 | 133 | |
143abaeb AL |
134 | if (Chop != 0) |
135 | File = OrigPath + ChopDirs(File,Chop); | |
88593886 | 136 | |
143abaeb | 137 | // See if the file exists |
143abaeb AL |
138 | if (NoStat == false || Hits < 10) |
139 | { | |
140 | // Attempt to fix broken structure | |
141 | if (Hits == 0) | |
142 | { | |
143 | if (ReconstructPrefix(Prefix,OrigPath,CDROM,File) == false && | |
144 | ReconstructChop(Chop,*I,File) == false) | |
145 | { | |
146 | if (Debug == true) | |
147 | clog << "Missed: " << File << endl; | |
148 | NotFound++; | |
149 | continue; | |
150 | } | |
151 | if (Chop != 0) | |
152 | File = OrigPath + ChopDirs(File,Chop); | |
153 | } | |
88593886 | 154 | |
143abaeb AL |
155 | // Get the size |
156 | struct stat Buf; | |
88593886 | 157 | if (stat((CDROM + Prefix + File).c_str(),&Buf) != 0 || |
143abaeb AL |
158 | Buf.st_size == 0) |
159 | { | |
69c2ecbd | 160 | bool Mangled = false; |
143abaeb AL |
161 | // Attempt to fix busted symlink support for one instance |
162 | string OrigFile = File; | |
163 | string::size_type Start = File.find("binary-"); | |
164 | string::size_type End = File.find("/",Start+3); | |
165 | if (Start != string::npos && End != string::npos) | |
166 | { | |
167 | File.replace(Start,End-Start,"binary-all"); | |
168 | Mangled = true; | |
169 | } | |
88593886 | 170 | |
143abaeb | 171 | if (Mangled == false || |
e788a834 | 172 | stat((CDROM + Prefix + File).c_str(),&Buf) != 0) |
143abaeb AL |
173 | { |
174 | if (Debug == true) | |
175 | clog << "Missed(2): " << OrigFile << endl; | |
176 | NotFound++; | |
177 | continue; | |
88593886 DK |
178 | } |
179 | } | |
180 | ||
143abaeb | 181 | // Size match |
650faab0 | 182 | if ((unsigned long long)Buf.st_size != Size) |
143abaeb AL |
183 | { |
184 | if (Debug == true) | |
185 | clog << "Wrong Size: " << File << endl; | |
186 | WrongSize++; | |
187 | continue; | |
188 | } | |
189 | } | |
88593886 | 190 | |
143abaeb AL |
191 | Packages++; |
192 | Hits++; | |
88593886 DK |
193 | |
194 | if (RewriteEntry(Target, File) == false) | |
b2e465d6 | 195 | return false; |
143abaeb AL |
196 | } |
197 | ||
198 | if (Debug == true) | |
199 | cout << " Processed by using Prefix '" << Prefix << "' and chop " << Chop << endl; | |
88593886 | 200 | |
143abaeb AL |
201 | if (_config->FindB("APT::CDROM::NoAct",false) == false) |
202 | { | |
203 | // Move out of the partial directory | |
204 | Target.Close(); | |
205 | string FinalF = _config->FindDir("Dir::State::lists"); | |
206 | FinalF += URItoFileName(S); | |
207 | if (rename(TargetF.c_str(),FinalF.c_str()) != 0) | |
208 | return _error->Errno("rename","Failed to rename"); | |
d84da499 | 209 | ChangeOwnerAndPermissionOfFile("CopyPackages", FinalF.c_str(), "root", "root", 0644); |
143abaeb | 210 | } |
88593886 | 211 | |
143abaeb | 212 | /* Mangle the source to be in the proper notation with |
88593886 | 213 | prefix dist [component] */ |
143abaeb AL |
214 | *I = string(*I,Prefix.length()); |
215 | ConvertToSourceList(CDROM,*I); | |
216 | *I = Prefix + ' ' + *I; | |
88593886 | 217 | |
143abaeb | 218 | CurrentSize += FileSize; |
88593886 | 219 | } |
a75c6a6e MZ |
220 | if(Progress) |
221 | Progress->Done(); | |
88593886 | 222 | |
143abaeb | 223 | // Some stats |
a75c6a6e MZ |
224 | if(log) { |
225 | stringstream msg; | |
226 | if(NotFound == 0 && WrongSize == 0) | |
227 | ioprintf(msg, _("Wrote %i records.\n"), Packages); | |
228 | else if (NotFound != 0 && WrongSize == 0) | |
88593886 | 229 | ioprintf(msg, _("Wrote %i records with %i missing files.\n"), |
a75c6a6e MZ |
230 | Packages, NotFound); |
231 | else if (NotFound == 0 && WrongSize != 0) | |
88593886 | 232 | ioprintf(msg, _("Wrote %i records with %i mismatched files\n"), |
a75c6a6e MZ |
233 | Packages, WrongSize); |
234 | if (NotFound != 0 && WrongSize != 0) | |
db0db9fe | 235 | ioprintf(msg, _("Wrote %i records with %i missing files and %i mismatched files\n"), Packages, NotFound, WrongSize); |
a75c6a6e | 236 | } |
88593886 | 237 | |
143abaeb | 238 | if (Packages == 0) |
dd27443e AL |
239 | _error->Warning("No valid records were found."); |
240 | ||
143abaeb | 241 | if (NotFound + WrongSize > 10) |
46e39c8e | 242 | _error->Warning("A lot of entries were discarded, something may be wrong.\n"); |
143abaeb AL |
243 | |
244 | return true; | |
245 | } | |
246 | /*}}}*/ | |
247 | // IndexCopy::ChopDirs - Chop off the leading directory components /*{{{*/ | |
248 | // --------------------------------------------------------------------- | |
249 | /* */ | |
250 | string IndexCopy::ChopDirs(string Path,unsigned int Depth) | |
251 | { | |
252 | string::size_type I = 0; | |
253 | do | |
254 | { | |
255 | I = Path.find('/',I+1); | |
256 | Depth--; | |
257 | } | |
258 | while (I != string::npos && Depth != 0); | |
88593886 | 259 | |
143abaeb AL |
260 | if (I == string::npos) |
261 | return string(); | |
88593886 | 262 | |
143abaeb AL |
263 | return string(Path,I+1); |
264 | } | |
265 | /*}}}*/ | |
266 | // IndexCopy::ReconstructPrefix - Fix strange prefixing /*{{{*/ | |
267 | // --------------------------------------------------------------------- | |
268 | /* This prepends dir components from the path to the package files to | |
269 | the path to the deb until it is found */ | |
270 | bool IndexCopy::ReconstructPrefix(string &Prefix,string OrigPath,string CD, | |
271 | string File) | |
272 | { | |
273 | bool Debug = _config->FindB("Debug::aptcdrom",false); | |
274 | unsigned int Depth = 1; | |
275 | string MyPrefix = Prefix; | |
276 | while (1) | |
277 | { | |
278 | struct stat Buf; | |
e788a834 | 279 | if (stat((CD + MyPrefix + File).c_str(),&Buf) != 0) |
143abaeb AL |
280 | { |
281 | if (Debug == true) | |
282 | cout << "Failed, " << CD + MyPrefix + File << endl; | |
283 | if (GrabFirst(OrigPath,MyPrefix,Depth++) == true) | |
284 | continue; | |
285 | ||
286 | return false; | |
287 | } | |
288 | else | |
289 | { | |
290 | Prefix = MyPrefix; | |
291 | return true; | |
292 | } | |
293 | } | |
294 | return false; | |
295 | } | |
296 | /*}}}*/ | |
297 | // IndexCopy::ReconstructChop - Fixes bad source paths /*{{{*/ | |
298 | // --------------------------------------------------------------------- | |
299 | /* This removes path components from the filename and prepends the location | |
300 | of the package files until a file is found */ | |
301 | bool IndexCopy::ReconstructChop(unsigned long &Chop,string Dir,string File) | |
302 | { | |
303 | // Attempt to reconstruct the filename | |
304 | unsigned long Depth = 0; | |
305 | while (1) | |
306 | { | |
307 | struct stat Buf; | |
e788a834 | 308 | if (stat((Dir + File).c_str(),&Buf) != 0) |
143abaeb AL |
309 | { |
310 | File = ChopDirs(File,1); | |
311 | Depth++; | |
312 | if (File.empty() == false) | |
313 | continue; | |
314 | return false; | |
315 | } | |
316 | else | |
317 | { | |
318 | Chop = Depth; | |
319 | return true; | |
320 | } | |
321 | } | |
322 | return false; | |
323 | } | |
324 | /*}}}*/ | |
325 | // IndexCopy::ConvertToSourceList - Convert a Path to a sourcelist /*{{{*/ | |
326 | // --------------------------------------------------------------------- | |
327 | /* We look for things in dists/ notation and convert them to | |
328 | <dist> <component> form otherwise it is left alone. This also strips | |
0f770a0c AL |
329 | the CD path. |
330 | ||
331 | This implements a regex sort of like: | |
332 | (.*)/dists/([^/]*)/(.*)/binary-* | |
333 | ^ ^ ^- Component | |
334 | | |-------- Distribution | |
335 | |------------------- Path | |
336 | ||
337 | It was deciced to use only a single word for dist (rather than say | |
338 | unstable/non-us) to increase the chance that each CD gets a single | |
339 | line in sources.list. | |
340 | */ | |
143abaeb AL |
341 | void IndexCopy::ConvertToSourceList(string CD,string &Path) |
342 | { | |
143abaeb AL |
343 | // Strip the cdrom base path |
344 | Path = string(Path,CD.length()); | |
345 | if (Path.empty() == true) | |
346 | Path = "/"; | |
347 | ||
348 | // Too short to be a dists/ type | |
349 | if (Path.length() < strlen("dists/")) | |
350 | return; | |
351 | ||
352 | // Not a dists type. | |
076d01b0 | 353 | if (stringcmp(Path.c_str(),Path.c_str()+strlen("dists/"),"dists/") != 0) |
143abaeb | 354 | return; |
7834cb57 | 355 | |
143abaeb AL |
356 | // Isolate the dist |
357 | string::size_type Slash = strlen("dists/"); | |
358 | string::size_type Slash2 = Path.find('/',Slash + 1); | |
359 | if (Slash2 == string::npos || Slash2 + 2 >= Path.length()) | |
360 | return; | |
361 | string Dist = string(Path,Slash,Slash2 - Slash); | |
362 | ||
363 | // Isolate the component | |
0f770a0c AL |
364 | Slash = Slash2; |
365 | for (unsigned I = 0; I != 10; I++) | |
366 | { | |
367 | Slash = Path.find('/',Slash+1); | |
368 | if (Slash == string::npos || Slash + 2 >= Path.length()) | |
369 | return; | |
370 | string Comp = string(Path,Slash2+1,Slash - Slash2-1); | |
371 | ||
372 | // Verify the trailing binary- bit | |
373 | string::size_type BinSlash = Path.find('/',Slash + 1); | |
374 | if (Slash == string::npos) | |
375 | return; | |
376 | string Binary = string(Path,Slash+1,BinSlash - Slash-1); | |
377 | ||
d29a5330 DK |
378 | if (strncmp(Binary.c_str(), "binary-", strlen("binary-")) == 0) |
379 | { | |
380 | Binary.erase(0, strlen("binary-")); | |
381 | if (APT::Configuration::checkArchitecture(Binary) == false) | |
382 | continue; | |
383 | } | |
384 | else if (Binary != "source") | |
0f770a0c AL |
385 | continue; |
386 | ||
387 | Path = Dist + ' ' + Comp; | |
143abaeb | 388 | return; |
0f770a0c | 389 | } |
143abaeb AL |
390 | } |
391 | /*}}}*/ | |
392 | // IndexCopy::GrabFirst - Return the first Depth path components /*{{{*/ | |
393 | // --------------------------------------------------------------------- | |
394 | /* */ | |
395 | bool IndexCopy::GrabFirst(string Path,string &To,unsigned int Depth) | |
396 | { | |
397 | string::size_type I = 0; | |
398 | do | |
399 | { | |
400 | I = Path.find('/',I+1); | |
401 | Depth--; | |
402 | } | |
403 | while (I != string::npos && Depth != 0); | |
404 | ||
405 | if (I == string::npos) | |
406 | return false; | |
407 | ||
408 | To = string(Path,0,I+1); | |
409 | return true; | |
410 | } | |
411 | /*}}}*/ | |
143abaeb AL |
412 | // PackageCopy::GetFile - Get the file information from the section /*{{{*/ |
413 | // --------------------------------------------------------------------- | |
414 | /* */ | |
650faab0 | 415 | bool PackageCopy::GetFile(string &File,unsigned long long &Size) |
143abaeb AL |
416 | { |
417 | File = Section->FindS("Filename"); | |
418 | Size = Section->FindI("Size"); | |
419 | if (File.empty() || Size == 0) | |
420 | return _error->Error("Cannot find filename or size tag"); | |
421 | return true; | |
422 | } | |
423 | /*}}}*/ | |
424 | // PackageCopy::RewriteEntry - Rewrite the entry with a new filename /*{{{*/ | |
88593886 | 425 | bool PackageCopy::RewriteEntry(FileFd &Target,string const &File) |
143abaeb | 426 | { |
88593886 DK |
427 | string const Dir(File,0,File.rfind('/')); |
428 | std::vector<pkgTagSection::Tag> Changes; | |
429 | Changes.push_back(pkgTagSection::Tag::Rewrite("Filename", File)); | |
430 | ||
431 | if (Section->Write(Target, TFRewritePackageOrder, Changes) == false) | |
b2e465d6 | 432 | return false; |
88593886 | 433 | return Target.Write("\n", 1); |
143abaeb AL |
434 | } |
435 | /*}}}*/ | |
436 | // SourceCopy::GetFile - Get the file information from the section /*{{{*/ | |
437 | // --------------------------------------------------------------------- | |
438 | /* */ | |
650faab0 | 439 | bool SourceCopy::GetFile(string &File,unsigned long long &Size) |
143abaeb AL |
440 | { |
441 | string Files = Section->FindS("Files"); | |
442 | if (Files.empty() == true) | |
443 | return false; | |
444 | ||
445 | // Stash the / terminated directory prefix | |
446 | string Base = Section->FindS("Directory"); | |
447 | if (Base.empty() == false && Base[Base.length()-1] != '/') | |
448 | Base += '/'; | |
449 | ||
b2e465d6 | 450 | // Read the first file triplet |
143abaeb AL |
451 | const char *C = Files.c_str(); |
452 | string sSize; | |
453 | string MD5Hash; | |
454 | ||
455 | // Parse each of the elements | |
456 | if (ParseQuoteWord(C,MD5Hash) == false || | |
457 | ParseQuoteWord(C,sSize) == false || | |
458 | ParseQuoteWord(C,File) == false) | |
459 | return _error->Error("Error parsing file record"); | |
460 | ||
461 | // Parse the size and append the directory | |
650faab0 | 462 | Size = strtoull(sSize.c_str(), NULL, 10); |
143abaeb AL |
463 | File = Base + File; |
464 | return true; | |
465 | } | |
466 | /*}}}*/ | |
467 | // SourceCopy::RewriteEntry - Rewrite the entry with a new filename /*{{{*/ | |
88593886 | 468 | bool SourceCopy::RewriteEntry(FileFd &Target, std::string const &File) |
143abaeb | 469 | { |
88593886 DK |
470 | string const Dir(File,0,File.rfind('/')); |
471 | std::vector<pkgTagSection::Tag> Changes; | |
472 | Changes.push_back(pkgTagSection::Tag::Rewrite("Directory", Dir)); | |
473 | ||
474 | if (Section->Write(Target, TFRewriteSourceOrder, Changes) == false) | |
b2e465d6 | 475 | return false; |
88593886 | 476 | return Target.Write("\n", 1); |
143abaeb AL |
477 | } |
478 | /*}}}*/ | |
88593886 | 479 | // SigVerify::Verify - Verify a files md5sum against its metaindex /*{{{*/ |
a75c6a6e MZ |
480 | bool SigVerify::Verify(string prefix, string file, indexRecords *MetaIndex) |
481 | { | |
482 | const indexRecords::checkSum *Record = MetaIndex->Lookup(file); | |
12c7078f | 483 | bool const Debug = _config->FindB("Debug::aptcdrom",false); |
a75c6a6e | 484 | |
12c7078f DK |
485 | // we skip non-existing files in the verifcation of the Release file |
486 | // as non-existing files do not harm, but a warning scares people and | |
487 | // makes it hard to strip unneeded files from an ISO like uncompressed | |
488 | // indexes as it is done on the mirrors (see also LP: #255545 ) | |
8220213e | 489 | if(!RealFileExists(prefix+file)) |
8d357c52 | 490 | { |
12c7078f DK |
491 | if (Debug == true) |
492 | cout << "Skipping nonexistent in " << prefix << " file " << file << std::endl; | |
8d357c52 MV |
493 | return true; |
494 | } | |
495 | ||
12c7078f | 496 | if (!Record) |
a75c6a6e | 497 | { |
a1e42d1f | 498 | _error->Warning(_("Can't find authentication record for: %s"), file.c_str()); |
a75c6a6e MZ |
499 | return false; |
500 | } | |
501 | ||
b3501edb | 502 | if (!Record->Hashes.VerifyFile(prefix+file)) |
a75c6a6e | 503 | { |
a1e42d1f | 504 | _error->Warning(_("Hash mismatch for: %s"),file.c_str()); |
a75c6a6e MZ |
505 | return false; |
506 | } | |
507 | ||
12c7078f | 508 | if(Debug == true) |
a75c6a6e | 509 | { |
b3501edb DK |
510 | cout << "File: " << prefix+file << endl |
511 | << "Expected Hash " << endl; | |
512 | for (HashStringList::const_iterator hs = Record->Hashes.begin(); hs != Record->Hashes.end(); ++hs) | |
513 | std::cout << "\t- " << hs->toStr() << std::endl; | |
a75c6a6e MZ |
514 | } |
515 | ||
516 | return true; | |
517 | } | |
92fcbfc1 DK |
518 | /*}}}*/ |
519 | bool SigVerify::CopyMetaIndex(string CDROM, string CDName, /*{{{*/ | |
a75c6a6e MZ |
520 | string prefix, string file) |
521 | { | |
522 | char S[400]; | |
523 | snprintf(S,sizeof(S),"cdrom:[%s]/%s%s",CDName.c_str(), | |
524 | (prefix).c_str() + CDROM.length(),file.c_str()); | |
525 | string TargetF = _config->FindDir("Dir::State::lists"); | |
526 | TargetF += URItoFileName(S); | |
527 | ||
528 | FileFd Target; | |
529 | FileFd Rel; | |
22041bd2 | 530 | Target.Open(TargetF,FileFd::WriteAtomic); |
a75c6a6e | 531 | Rel.Open(prefix + file,FileFd::ReadOnly); |
d84da499 | 532 | if (CopyFile(Rel,Target) == false || Target.Close() == false) |
2128d3fc | 533 | return _error->Error("Copying of '%s' for '%s' from '%s' failed", file.c_str(), CDName.c_str(), prefix.c_str()); |
d84da499 | 534 | ChangeOwnerAndPermissionOfFile("CopyPackages", TargetF.c_str(), "root", "root", 0644); |
2128d3fc | 535 | |
a75c6a6e MZ |
536 | return true; |
537 | } | |
92fcbfc1 DK |
538 | /*}}}*/ |
539 | bool SigVerify::CopyAndVerify(string CDROM,string Name,vector<string> &SigList, /*{{{*/ | |
65512241 | 540 | vector<string> /*PkgList*/,vector<string> /*SrcList*/) |
a75c6a6e | 541 | { |
f7f0d6c7 | 542 | if (SigList.empty() == true) |
a75c6a6e MZ |
543 | return true; |
544 | ||
545 | bool Debug = _config->FindB("Debug::aptcdrom",false); | |
546 | ||
547 | // Read all Release files | |
f7f0d6c7 | 548 | for (vector<string>::iterator I = SigList.begin(); I != SigList.end(); ++I) |
a75c6a6e MZ |
549 | { |
550 | if(Debug) | |
551 | cout << "Signature verify for: " << *I << endl; | |
552 | ||
553 | indexRecords *MetaIndex = new indexRecords; | |
554 | string prefix = *I; | |
555 | ||
a319c4ee DK |
556 | string const releasegpg = *I+"Release.gpg"; |
557 | string const release = *I+"Release"; | |
212080b8 DK |
558 | string const inrelease = *I+"InRelease"; |
559 | bool useInRelease = true; | |
a319c4ee | 560 | |
a75c6a6e | 561 | // a Release.gpg without a Release should never happen |
212080b8 DK |
562 | if (RealFileExists(inrelease) == true) |
563 | ; | |
564 | else if(RealFileExists(release) == false || RealFileExists(releasegpg) == false) | |
cfb3d242 DK |
565 | { |
566 | delete MetaIndex; | |
a75c6a6e | 567 | continue; |
cfb3d242 | 568 | } |
212080b8 DK |
569 | else |
570 | useInRelease = false; | |
a75c6a6e | 571 | |
a75c6a6e MZ |
572 | pid_t pid = ExecFork(); |
573 | if(pid < 0) { | |
574 | _error->Error("Fork failed"); | |
575 | return false; | |
576 | } | |
cf440fac | 577 | if(pid == 0) |
212080b8 DK |
578 | { |
579 | if (useInRelease == true) | |
2f5b6151 | 580 | ExecGPGV(inrelease, inrelease); |
212080b8 | 581 | else |
2f5b6151 | 582 | ExecGPGV(release, releasegpg); |
212080b8 | 583 | } |
cf440fac | 584 | |
a75c6a6e MZ |
585 | if(!ExecWait(pid, "gpgv")) { |
586 | _error->Warning("Signature verification failed for: %s", | |
212080b8 | 587 | (useInRelease ? inrelease.c_str() : releasegpg.c_str())); |
a75c6a6e MZ |
588 | // something went wrong, don't copy the Release.gpg |
589 | // FIXME: delete any existing gpg file? | |
a9d6b0ad | 590 | delete MetaIndex; |
a75c6a6e MZ |
591 | continue; |
592 | } | |
593 | ||
594 | // Open the Release file and add it to the MetaIndex | |
a319c4ee | 595 | if(!MetaIndex->Load(release)) |
a75c6a6e | 596 | { |
9b5d79ec | 597 | _error->Error("%s",MetaIndex->ErrorText.c_str()); |
a75c6a6e MZ |
598 | return false; |
599 | } | |
600 | ||
601 | // go over the Indexfiles and see if they verify | |
602 | // if so, remove them from our copy of the lists | |
603 | vector<string> keys = MetaIndex->MetaKeys(); | |
f7f0d6c7 | 604 | for (vector<string>::iterator I = keys.begin(); I != keys.end(); ++I) |
a75c6a6e MZ |
605 | { |
606 | if(!Verify(prefix,*I, MetaIndex)) { | |
607 | // something went wrong, don't copy the Release.gpg | |
608 | // FIXME: delete any existing gpg file? | |
7efdcd3a | 609 | _error->Discard(); |
a75c6a6e MZ |
610 | continue; |
611 | } | |
612 | } | |
613 | ||
614 | // we need a fresh one for the Release.gpg | |
615 | delete MetaIndex; | |
616 | ||
617 | // everything was fine, copy the Release and Release.gpg file | |
212080b8 DK |
618 | if (useInRelease == true) |
619 | CopyMetaIndex(CDROM, Name, prefix, "InRelease"); | |
620 | else | |
621 | { | |
622 | CopyMetaIndex(CDROM, Name, prefix, "Release"); | |
623 | CopyMetaIndex(CDROM, Name, prefix, "Release.gpg"); | |
624 | } | |
a75c6a6e MZ |
625 | } |
626 | ||
cf440fac | 627 | return true; |
a319c4ee DK |
628 | } |
629 | /*}}}*/ | |
27cc55ee | 630 | // SigVerify::RunGPGV - deprecated wrapper calling ExecGPGV /*{{{*/ |
a02db58f | 631 | APT_NORETURN bool SigVerify::RunGPGV(std::string const &File, std::string const &FileOut, |
27cc55ee DK |
632 | int const &statusfd, int fd[2]) { |
633 | ExecGPGV(File, FileOut, statusfd, fd); | |
d3e8fbb3 | 634 | } |
a02db58f | 635 | APT_NORETURN bool SigVerify::RunGPGV(std::string const &File, std::string const &FileOut, |
27cc55ee DK |
636 | int const &statusfd) { |
637 | ExecGPGV(File, FileOut, statusfd); | |
d3e8fbb3 | 638 | } |
27cc55ee | 639 | /*}}}*/ |
92fcbfc1 DK |
640 | bool TranslationsCopy::CopyTranslations(string CDROM,string Name, /*{{{*/ |
641 | vector<string> &List, pkgCdromStatus *log) | |
22f8568d MV |
642 | { |
643 | OpProgress *Progress = NULL; | |
f7f0d6c7 | 644 | if (List.empty() == true) |
22f8568d MV |
645 | return true; |
646 | ||
647 | if(log) | |
648 | Progress = log->GetOpProgress(); | |
649 | ||
650 | bool Debug = _config->FindB("Debug::aptcdrom",false); | |
651 | ||
652 | // Prepare the progress indicator | |
650faab0 | 653 | off_t TotalSize = 0; |
78c9276d | 654 | std::vector<APT::Configuration::Compressor> const compressor = APT::Configuration::getCompressors(); |
f7f0d6c7 | 655 | for (vector<string>::iterator I = List.begin(); I != List.end(); ++I) |
22f8568d MV |
656 | { |
657 | struct stat Buf; | |
78c9276d DK |
658 | bool found = false; |
659 | std::string file = *I; | |
660 | for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin(); | |
661 | c != compressor.end(); ++c) | |
662 | { | |
e788a834 | 663 | if (stat((file + c->Extension).c_str(), &Buf) != 0) |
78c9276d DK |
664 | continue; |
665 | found = true; | |
666 | break; | |
667 | } | |
668 | ||
669 | if (found == false) | |
670 | return _error->Errno("stat", "Stat failed for %s", file.c_str()); | |
22f8568d | 671 | TotalSize += Buf.st_size; |
78c9276d | 672 | } |
22f8568d | 673 | |
650faab0 | 674 | off_t CurrentSize = 0; |
22f8568d MV |
675 | unsigned int NotFound = 0; |
676 | unsigned int WrongSize = 0; | |
677 | unsigned int Packages = 0; | |
f7f0d6c7 | 678 | for (vector<string>::iterator I = List.begin(); I != List.end(); ++I) |
69c2ecbd | 679 | { |
22f8568d | 680 | // Open the package file |
144353a9 | 681 | FileFd Pkg(*I, FileFd::ReadOnly, FileFd::Auto); |
699b209e DK |
682 | off_t const FileSize = Pkg.Size(); |
683 | ||
22f8568d MV |
684 | pkgTagFile Parser(&Pkg); |
685 | if (_error->PendingError() == true) | |
686 | return false; | |
88593886 | 687 | |
22f8568d MV |
688 | // Open the output file |
689 | char S[400]; | |
690 | snprintf(S,sizeof(S),"cdrom:[%s]/%s",Name.c_str(), | |
691 | (*I).c_str() + CDROM.length()); | |
692 | string TargetF = _config->FindDir("Dir::State::lists") + "partial/"; | |
693 | TargetF += URItoFileName(S); | |
d5da93b8 | 694 | FileFd Target; |
22f8568d | 695 | if (_config->FindB("APT::CDROM::NoAct",false) == true) |
d5da93b8 | 696 | { |
22f8568d | 697 | TargetF = "/dev/null"; |
d5da93b8 DK |
698 | Target.Open(TargetF,FileFd::WriteExists); |
699 | } else { | |
700 | Target.Open(TargetF,FileFd::WriteAtomic); | |
701 | } | |
22f8568d MV |
702 | if (_error->PendingError() == true) |
703 | return false; | |
88593886 | 704 | |
22f8568d MV |
705 | // Setup the progress meter |
706 | if(Progress) | |
707 | Progress->OverallProgress(CurrentSize,TotalSize,FileSize, | |
708 | string("Reading Translation Indexes")); | |
709 | ||
710 | // Parse | |
711 | if(Progress) | |
712 | Progress->SubProgress(Pkg.Size()); | |
713 | pkgTagSection Section; | |
714 | this->Section = &Section; | |
715 | string Prefix; | |
716 | unsigned long Hits = 0; | |
22f8568d MV |
717 | while (Parser.Step(Section) == true) |
718 | { | |
719 | if(Progress) | |
720 | Progress->Progress(Parser.Offset()); | |
721 | ||
88593886 DK |
722 | if (Section.Write(Target) == false || Target.Write("\n", 1) == false) |
723 | return false; | |
22f8568d MV |
724 | |
725 | Packages++; | |
726 | Hits++; | |
727 | } | |
22f8568d MV |
728 | |
729 | if (Debug == true) | |
91c03d37 | 730 | cout << " Processed by using Prefix '" << Prefix << "' and chop " << endl; |
88593886 | 731 | |
22f8568d MV |
732 | if (_config->FindB("APT::CDROM::NoAct",false) == false) |
733 | { | |
734 | // Move out of the partial directory | |
735 | Target.Close(); | |
736 | string FinalF = _config->FindDir("Dir::State::lists"); | |
737 | FinalF += URItoFileName(S); | |
738 | if (rename(TargetF.c_str(),FinalF.c_str()) != 0) | |
739 | return _error->Errno("rename","Failed to rename"); | |
d84da499 | 740 | ChangeOwnerAndPermissionOfFile("CopyTranslations", FinalF.c_str(), "root", "root", 0644); |
22f8568d | 741 | } |
88593886 | 742 | |
22f8568d | 743 | CurrentSize += FileSize; |
88593886 | 744 | } |
22f8568d MV |
745 | if(Progress) |
746 | Progress->Done(); | |
88593886 | 747 | |
22f8568d MV |
748 | // Some stats |
749 | if(log) { | |
750 | stringstream msg; | |
751 | if(NotFound == 0 && WrongSize == 0) | |
752 | ioprintf(msg, _("Wrote %i records.\n"), Packages); | |
753 | else if (NotFound != 0 && WrongSize == 0) | |
88593886 | 754 | ioprintf(msg, _("Wrote %i records with %i missing files.\n"), |
22f8568d MV |
755 | Packages, NotFound); |
756 | else if (NotFound == 0 && WrongSize != 0) | |
88593886 | 757 | ioprintf(msg, _("Wrote %i records with %i mismatched files\n"), |
22f8568d MV |
758 | Packages, WrongSize); |
759 | if (NotFound != 0 && WrongSize != 0) | |
760 | ioprintf(msg, _("Wrote %i records with %i missing files and %i mismatched files\n"), Packages, NotFound, WrongSize); | |
761 | } | |
88593886 | 762 | |
22f8568d MV |
763 | if (Packages == 0) |
764 | _error->Warning("No valid records were found."); | |
765 | ||
766 | if (NotFound + WrongSize > 10) | |
46e39c8e | 767 | _error->Warning("A lot of entries were discarded, something may be wrong.\n"); |
22f8568d MV |
768 | |
769 | return true; | |
770 | } | |
92fcbfc1 | 771 | /*}}}*/ |
862bafea | 772 | |
c8a4ce6c | 773 | IndexCopy::IndexCopy() {} |
9d653a6d | 774 | APT_CONST IndexCopy::~IndexCopy() {} |
c8a4ce6c DK |
775 | |
776 | PackageCopy::PackageCopy() : IndexCopy() {} | |
777 | APT_CONST PackageCopy::~PackageCopy() {} | |
778 | SourceCopy::SourceCopy() : IndexCopy() {} | |
779 | APT_CONST SourceCopy::~SourceCopy() {} | |
780 | TranslationsCopy::TranslationsCopy() {} | |
781 | APT_CONST TranslationsCopy::~TranslationsCopy() {} | |
782 | SigVerify::SigVerify() {} | |
783 | APT_CONST SigVerify::~SigVerify() {} |