]>
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> | |
5ad0096a | 22 | #include <apt-pkg/metaindex.h> |
a75c6a6e | 23 | #include <apt-pkg/cdrom.h> |
453b82a3 DK |
24 | #include <apt-pkg/gpgv.h> |
25 | #include <apt-pkg/hashes.h> | |
5ad0096a | 26 | #include <apt-pkg/debmetaindex.h> |
143abaeb | 27 | |
90f057fd | 28 | #include <iostream> |
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> |
ac7f8f79 | 34 | #include <sstream> |
ea542140 DK |
35 | |
36 | #include "indexcopy.h" | |
37 | #include <apti18n.h> | |
143abaeb AL |
38 | /*}}}*/ |
39 | ||
076d01b0 AL |
40 | using namespace std; |
41 | ||
143abaeb AL |
42 | // IndexCopy::CopyPackages - Copy the package files from the CD /*{{{*/ |
43 | // --------------------------------------------------------------------- | |
44 | /* */ | |
a75c6a6e MZ |
45 | bool IndexCopy::CopyPackages(string CDROM,string Name,vector<string> &List, |
46 | pkgCdromStatus *log) | |
143abaeb | 47 | { |
a75c6a6e | 48 | OpProgress *Progress = NULL; |
f7f0d6c7 | 49 | if (List.empty() == true) |
143abaeb AL |
50 | return true; |
51 | ||
a75c6a6e MZ |
52 | if(log) |
53 | Progress = log->GetOpProgress(); | |
143abaeb AL |
54 | |
55 | bool NoStat = _config->FindB("APT::CDROM::Fast",false); | |
56 | bool Debug = _config->FindB("Debug::aptcdrom",false); | |
57 | ||
58 | // Prepare the progress indicator | |
650faab0 | 59 | off_t TotalSize = 0; |
78c9276d | 60 | std::vector<APT::Configuration::Compressor> const compressor = APT::Configuration::getCompressors(); |
f7f0d6c7 | 61 | for (vector<string>::iterator I = List.begin(); I != List.end(); ++I) |
143abaeb AL |
62 | { |
63 | struct stat Buf; | |
78c9276d DK |
64 | bool found = false; |
65 | std::string file = std::string(*I).append(GetFileName()); | |
66 | for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin(); | |
67 | c != compressor.end(); ++c) | |
68 | { | |
e788a834 | 69 | if (stat((file + c->Extension).c_str(), &Buf) != 0) |
78c9276d DK |
70 | continue; |
71 | found = true; | |
72 | break; | |
73 | } | |
74 | ||
75 | if (found == false) | |
76 | return _error->Errno("stat", "Stat failed for %s", file.c_str()); | |
143abaeb | 77 | TotalSize += Buf.st_size; |
78c9276d | 78 | } |
143abaeb | 79 | |
650faab0 | 80 | off_t CurrentSize = 0; |
143abaeb AL |
81 | unsigned int NotFound = 0; |
82 | unsigned int WrongSize = 0; | |
83 | unsigned int Packages = 0; | |
f7f0d6c7 | 84 | for (vector<string>::iterator I = List.begin(); I != List.end(); ++I) |
143abaeb AL |
85 | { |
86 | string OrigPath = string(*I,CDROM.length()); | |
143abaeb AL |
87 | |
88 | // Open the package file | |
144353a9 | 89 | FileFd Pkg(*I + GetFileName(), FileFd::ReadOnly, FileFd::Auto); |
699b209e | 90 | off_t const FileSize = Pkg.Size(); |
01366a44 | 91 | |
b2e465d6 | 92 | pkgTagFile Parser(&Pkg); |
95278287 | 93 | if (Pkg.IsOpen() == false || Pkg.Failed()) |
143abaeb AL |
94 | return false; |
95 | ||
96 | // Open the output file | |
97 | char S[400]; | |
20ebd488 AL |
98 | snprintf(S,sizeof(S),"cdrom:[%s]/%s%s",Name.c_str(), |
99 | (*I).c_str() + CDROM.length(),GetFileName()); | |
143abaeb AL |
100 | string TargetF = _config->FindDir("Dir::State::lists") + "partial/"; |
101 | TargetF += URItoFileName(S); | |
8deb53ab | 102 | FileFd Target; |
143abaeb | 103 | if (_config->FindB("APT::CDROM::NoAct",false) == true) |
8deb53ab | 104 | { |
143abaeb | 105 | TargetF = "/dev/null"; |
8deb53ab MV |
106 | Target.Open(TargetF,FileFd::WriteExists); |
107 | } else { | |
108 | Target.Open(TargetF,FileFd::WriteAtomic); | |
109 | } | |
95278287 | 110 | if (Target.IsOpen() == false || Target.Failed()) |
143abaeb | 111 | return false; |
88593886 | 112 | |
143abaeb | 113 | // Setup the progress meter |
a75c6a6e MZ |
114 | if(Progress) |
115 | Progress->OverallProgress(CurrentSize,TotalSize,FileSize, | |
116 | string("Reading ") + Type() + " Indexes"); | |
143abaeb AL |
117 | |
118 | // Parse | |
a75c6a6e MZ |
119 | if(Progress) |
120 | Progress->SubProgress(Pkg.Size()); | |
143abaeb AL |
121 | pkgTagSection Section; |
122 | this->Section = &Section; | |
123 | string Prefix; | |
124 | unsigned long Hits = 0; | |
125 | unsigned long Chop = 0; | |
126 | while (Parser.Step(Section) == true) | |
127 | { | |
a75c6a6e MZ |
128 | if(Progress) |
129 | Progress->Progress(Parser.Offset()); | |
143abaeb | 130 | string File; |
650faab0 | 131 | unsigned long long Size; |
143abaeb AL |
132 | if (GetFile(File,Size) == false) |
133 | return false; | |
88593886 | 134 | |
143abaeb AL |
135 | if (Chop != 0) |
136 | File = OrigPath + ChopDirs(File,Chop); | |
88593886 | 137 | |
143abaeb | 138 | // See if the file exists |
143abaeb AL |
139 | if (NoStat == false || Hits < 10) |
140 | { | |
141 | // Attempt to fix broken structure | |
142 | if (Hits == 0) | |
143 | { | |
144 | if (ReconstructPrefix(Prefix,OrigPath,CDROM,File) == false && | |
145 | ReconstructChop(Chop,*I,File) == false) | |
146 | { | |
147 | if (Debug == true) | |
148 | clog << "Missed: " << File << endl; | |
149 | NotFound++; | |
150 | continue; | |
151 | } | |
152 | if (Chop != 0) | |
153 | File = OrigPath + ChopDirs(File,Chop); | |
154 | } | |
88593886 | 155 | |
143abaeb AL |
156 | // Get the size |
157 | struct stat Buf; | |
88593886 | 158 | if (stat((CDROM + Prefix + File).c_str(),&Buf) != 0 || |
143abaeb AL |
159 | Buf.st_size == 0) |
160 | { | |
69c2ecbd | 161 | bool Mangled = false; |
143abaeb AL |
162 | // Attempt to fix busted symlink support for one instance |
163 | string OrigFile = File; | |
164 | string::size_type Start = File.find("binary-"); | |
165 | string::size_type End = File.find("/",Start+3); | |
166 | if (Start != string::npos && End != string::npos) | |
167 | { | |
168 | File.replace(Start,End-Start,"binary-all"); | |
169 | Mangled = true; | |
170 | } | |
88593886 | 171 | |
143abaeb | 172 | if (Mangled == false || |
e788a834 | 173 | stat((CDROM + Prefix + File).c_str(),&Buf) != 0) |
143abaeb AL |
174 | { |
175 | if (Debug == true) | |
176 | clog << "Missed(2): " << OrigFile << endl; | |
177 | NotFound++; | |
178 | continue; | |
88593886 DK |
179 | } |
180 | } | |
181 | ||
143abaeb | 182 | // Size match |
650faab0 | 183 | if ((unsigned long long)Buf.st_size != Size) |
143abaeb AL |
184 | { |
185 | if (Debug == true) | |
186 | clog << "Wrong Size: " << File << endl; | |
187 | WrongSize++; | |
188 | continue; | |
189 | } | |
190 | } | |
88593886 | 191 | |
143abaeb AL |
192 | Packages++; |
193 | Hits++; | |
88593886 DK |
194 | |
195 | if (RewriteEntry(Target, File) == false) | |
b2e465d6 | 196 | return false; |
143abaeb AL |
197 | } |
198 | ||
199 | if (Debug == true) | |
200 | cout << " Processed by using Prefix '" << Prefix << "' and chop " << Chop << endl; | |
88593886 | 201 | |
143abaeb AL |
202 | if (_config->FindB("APT::CDROM::NoAct",false) == false) |
203 | { | |
204 | // Move out of the partial directory | |
205 | Target.Close(); | |
206 | string FinalF = _config->FindDir("Dir::State::lists"); | |
207 | FinalF += URItoFileName(S); | |
208 | if (rename(TargetF.c_str(),FinalF.c_str()) != 0) | |
209 | return _error->Errno("rename","Failed to rename"); | |
d84da499 | 210 | ChangeOwnerAndPermissionOfFile("CopyPackages", FinalF.c_str(), "root", "root", 0644); |
143abaeb | 211 | } |
88593886 | 212 | |
143abaeb | 213 | /* Mangle the source to be in the proper notation with |
88593886 | 214 | prefix dist [component] */ |
143abaeb AL |
215 | *I = string(*I,Prefix.length()); |
216 | ConvertToSourceList(CDROM,*I); | |
217 | *I = Prefix + ' ' + *I; | |
88593886 | 218 | |
143abaeb | 219 | CurrentSize += FileSize; |
88593886 | 220 | } |
a75c6a6e MZ |
221 | if(Progress) |
222 | Progress->Done(); | |
88593886 | 223 | |
143abaeb | 224 | // Some stats |
a75c6a6e MZ |
225 | if(log) { |
226 | stringstream msg; | |
227 | if(NotFound == 0 && WrongSize == 0) | |
228 | ioprintf(msg, _("Wrote %i records.\n"), Packages); | |
229 | else if (NotFound != 0 && WrongSize == 0) | |
88593886 | 230 | ioprintf(msg, _("Wrote %i records with %i missing files.\n"), |
a75c6a6e MZ |
231 | Packages, NotFound); |
232 | else if (NotFound == 0 && WrongSize != 0) | |
88593886 | 233 | ioprintf(msg, _("Wrote %i records with %i mismatched files\n"), |
a75c6a6e MZ |
234 | Packages, WrongSize); |
235 | if (NotFound != 0 && WrongSize != 0) | |
db0db9fe | 236 | ioprintf(msg, _("Wrote %i records with %i missing files and %i mismatched files\n"), Packages, NotFound, WrongSize); |
a75c6a6e | 237 | } |
88593886 | 238 | |
143abaeb | 239 | if (Packages == 0) |
dd27443e AL |
240 | _error->Warning("No valid records were found."); |
241 | ||
143abaeb | 242 | if (NotFound + WrongSize > 10) |
46e39c8e | 243 | _error->Warning("A lot of entries were discarded, something may be wrong.\n"); |
143abaeb AL |
244 | |
245 | return true; | |
246 | } | |
247 | /*}}}*/ | |
248 | // IndexCopy::ChopDirs - Chop off the leading directory components /*{{{*/ | |
249 | // --------------------------------------------------------------------- | |
250 | /* */ | |
251 | string IndexCopy::ChopDirs(string Path,unsigned int Depth) | |
252 | { | |
253 | string::size_type I = 0; | |
254 | do | |
255 | { | |
256 | I = Path.find('/',I+1); | |
257 | Depth--; | |
258 | } | |
259 | while (I != string::npos && Depth != 0); | |
88593886 | 260 | |
143abaeb AL |
261 | if (I == string::npos) |
262 | return string(); | |
88593886 | 263 | |
143abaeb AL |
264 | return string(Path,I+1); |
265 | } | |
266 | /*}}}*/ | |
267 | // IndexCopy::ReconstructPrefix - Fix strange prefixing /*{{{*/ | |
268 | // --------------------------------------------------------------------- | |
269 | /* This prepends dir components from the path to the package files to | |
270 | the path to the deb until it is found */ | |
271 | bool IndexCopy::ReconstructPrefix(string &Prefix,string OrigPath,string CD, | |
272 | string File) | |
273 | { | |
274 | bool Debug = _config->FindB("Debug::aptcdrom",false); | |
275 | unsigned int Depth = 1; | |
276 | string MyPrefix = Prefix; | |
277 | while (1) | |
278 | { | |
279 | struct stat Buf; | |
e788a834 | 280 | if (stat((CD + MyPrefix + File).c_str(),&Buf) != 0) |
143abaeb AL |
281 | { |
282 | if (Debug == true) | |
283 | cout << "Failed, " << CD + MyPrefix + File << endl; | |
284 | if (GrabFirst(OrigPath,MyPrefix,Depth++) == true) | |
285 | continue; | |
286 | ||
287 | return false; | |
288 | } | |
289 | else | |
290 | { | |
291 | Prefix = MyPrefix; | |
292 | return true; | |
293 | } | |
294 | } | |
295 | return false; | |
296 | } | |
297 | /*}}}*/ | |
298 | // IndexCopy::ReconstructChop - Fixes bad source paths /*{{{*/ | |
299 | // --------------------------------------------------------------------- | |
300 | /* This removes path components from the filename and prepends the location | |
301 | of the package files until a file is found */ | |
302 | bool IndexCopy::ReconstructChop(unsigned long &Chop,string Dir,string File) | |
303 | { | |
304 | // Attempt to reconstruct the filename | |
305 | unsigned long Depth = 0; | |
306 | while (1) | |
307 | { | |
308 | struct stat Buf; | |
e788a834 | 309 | if (stat((Dir + File).c_str(),&Buf) != 0) |
143abaeb AL |
310 | { |
311 | File = ChopDirs(File,1); | |
312 | Depth++; | |
313 | if (File.empty() == false) | |
314 | continue; | |
315 | return false; | |
316 | } | |
317 | else | |
318 | { | |
319 | Chop = Depth; | |
320 | return true; | |
321 | } | |
322 | } | |
323 | return false; | |
324 | } | |
325 | /*}}}*/ | |
326 | // IndexCopy::ConvertToSourceList - Convert a Path to a sourcelist /*{{{*/ | |
327 | // --------------------------------------------------------------------- | |
328 | /* We look for things in dists/ notation and convert them to | |
329 | <dist> <component> form otherwise it is left alone. This also strips | |
0f770a0c AL |
330 | the CD path. |
331 | ||
332 | This implements a regex sort of like: | |
333 | (.*)/dists/([^/]*)/(.*)/binary-* | |
334 | ^ ^ ^- Component | |
335 | | |-------- Distribution | |
336 | |------------------- Path | |
337 | ||
338 | It was deciced to use only a single word for dist (rather than say | |
339 | unstable/non-us) to increase the chance that each CD gets a single | |
340 | line in sources.list. | |
341 | */ | |
143abaeb AL |
342 | void IndexCopy::ConvertToSourceList(string CD,string &Path) |
343 | { | |
143abaeb AL |
344 | // Strip the cdrom base path |
345 | Path = string(Path,CD.length()); | |
346 | if (Path.empty() == true) | |
347 | Path = "/"; | |
348 | ||
349 | // Too short to be a dists/ type | |
350 | if (Path.length() < strlen("dists/")) | |
351 | return; | |
352 | ||
353 | // Not a dists type. | |
076d01b0 | 354 | if (stringcmp(Path.c_str(),Path.c_str()+strlen("dists/"),"dists/") != 0) |
143abaeb | 355 | return; |
7834cb57 | 356 | |
143abaeb AL |
357 | // Isolate the dist |
358 | string::size_type Slash = strlen("dists/"); | |
359 | string::size_type Slash2 = Path.find('/',Slash + 1); | |
360 | if (Slash2 == string::npos || Slash2 + 2 >= Path.length()) | |
361 | return; | |
362 | string Dist = string(Path,Slash,Slash2 - Slash); | |
363 | ||
364 | // Isolate the component | |
0f770a0c AL |
365 | Slash = Slash2; |
366 | for (unsigned I = 0; I != 10; I++) | |
367 | { | |
368 | Slash = Path.find('/',Slash+1); | |
369 | if (Slash == string::npos || Slash + 2 >= Path.length()) | |
370 | return; | |
371 | string Comp = string(Path,Slash2+1,Slash - Slash2-1); | |
372 | ||
373 | // Verify the trailing binary- bit | |
374 | string::size_type BinSlash = Path.find('/',Slash + 1); | |
375 | if (Slash == string::npos) | |
376 | return; | |
377 | string Binary = string(Path,Slash+1,BinSlash - Slash-1); | |
378 | ||
d29a5330 DK |
379 | if (strncmp(Binary.c_str(), "binary-", strlen("binary-")) == 0) |
380 | { | |
381 | Binary.erase(0, strlen("binary-")); | |
382 | if (APT::Configuration::checkArchitecture(Binary) == false) | |
383 | continue; | |
384 | } | |
385 | else if (Binary != "source") | |
0f770a0c AL |
386 | continue; |
387 | ||
388 | Path = Dist + ' ' + Comp; | |
143abaeb | 389 | return; |
0f770a0c | 390 | } |
143abaeb AL |
391 | } |
392 | /*}}}*/ | |
393 | // IndexCopy::GrabFirst - Return the first Depth path components /*{{{*/ | |
394 | // --------------------------------------------------------------------- | |
395 | /* */ | |
396 | bool IndexCopy::GrabFirst(string Path,string &To,unsigned int Depth) | |
397 | { | |
398 | string::size_type I = 0; | |
399 | do | |
400 | { | |
401 | I = Path.find('/',I+1); | |
402 | Depth--; | |
403 | } | |
404 | while (I != string::npos && Depth != 0); | |
405 | ||
406 | if (I == string::npos) | |
407 | return false; | |
408 | ||
409 | To = string(Path,0,I+1); | |
410 | return true; | |
411 | } | |
412 | /*}}}*/ | |
143abaeb AL |
413 | // PackageCopy::GetFile - Get the file information from the section /*{{{*/ |
414 | // --------------------------------------------------------------------- | |
415 | /* */ | |
650faab0 | 416 | bool PackageCopy::GetFile(string &File,unsigned long long &Size) |
143abaeb AL |
417 | { |
418 | File = Section->FindS("Filename"); | |
419 | Size = Section->FindI("Size"); | |
420 | if (File.empty() || Size == 0) | |
421 | return _error->Error("Cannot find filename or size tag"); | |
422 | return true; | |
423 | } | |
424 | /*}}}*/ | |
425 | // PackageCopy::RewriteEntry - Rewrite the entry with a new filename /*{{{*/ | |
88593886 | 426 | bool PackageCopy::RewriteEntry(FileFd &Target,string const &File) |
143abaeb | 427 | { |
88593886 DK |
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 /*{{{*/ |
5ad0096a | 480 | bool SigVerify::Verify(string prefix, string file, metaIndex *MetaIndex) |
a75c6a6e | 481 | { |
5ad0096a | 482 | const metaIndex::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) |
5ad0096a | 549 | { |
a75c6a6e MZ |
550 | if(Debug) |
551 | cout << "Signature verify for: " << *I << endl; | |
552 | ||
d03b947b | 553 | metaIndex *MetaIndex = new debReleaseIndex("","", {}); |
a75c6a6e MZ |
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 | |
5ad0096a DK |
595 | std::string ErrorText; |
596 | if(MetaIndex->Load(release, &ErrorText) == false) | |
a75c6a6e | 597 | { |
5ad0096a | 598 | _error->Error("%s", ErrorText.c_str()); |
a75c6a6e MZ |
599 | return false; |
600 | } | |
5ad0096a | 601 | |
a75c6a6e MZ |
602 | // go over the Indexfiles and see if they verify |
603 | // if so, remove them from our copy of the lists | |
604 | vector<string> keys = MetaIndex->MetaKeys(); | |
f7f0d6c7 | 605 | for (vector<string>::iterator I = keys.begin(); I != keys.end(); ++I) |
a75c6a6e MZ |
606 | { |
607 | if(!Verify(prefix,*I, MetaIndex)) { | |
608 | // something went wrong, don't copy the Release.gpg | |
609 | // FIXME: delete any existing gpg file? | |
7efdcd3a | 610 | _error->Discard(); |
a75c6a6e MZ |
611 | continue; |
612 | } | |
613 | } | |
614 | ||
615 | // we need a fresh one for the Release.gpg | |
616 | delete MetaIndex; | |
617 | ||
618 | // everything was fine, copy the Release and Release.gpg file | |
212080b8 DK |
619 | if (useInRelease == true) |
620 | CopyMetaIndex(CDROM, Name, prefix, "InRelease"); | |
621 | else | |
622 | { | |
623 | CopyMetaIndex(CDROM, Name, prefix, "Release"); | |
624 | CopyMetaIndex(CDROM, Name, prefix, "Release.gpg"); | |
625 | } | |
a75c6a6e MZ |
626 | } |
627 | ||
cf440fac | 628 | return true; |
a319c4ee DK |
629 | } |
630 | /*}}}*/ | |
27cc55ee | 631 | // SigVerify::RunGPGV - deprecated wrapper calling ExecGPGV /*{{{*/ |
a02db58f | 632 | APT_NORETURN bool SigVerify::RunGPGV(std::string const &File, std::string const &FileOut, |
27cc55ee DK |
633 | int const &statusfd, int fd[2]) { |
634 | ExecGPGV(File, FileOut, statusfd, fd); | |
d3e8fbb3 | 635 | } |
a02db58f | 636 | APT_NORETURN bool SigVerify::RunGPGV(std::string const &File, std::string const &FileOut, |
27cc55ee DK |
637 | int const &statusfd) { |
638 | ExecGPGV(File, FileOut, statusfd); | |
d3e8fbb3 | 639 | } |
27cc55ee | 640 | /*}}}*/ |
92fcbfc1 DK |
641 | bool TranslationsCopy::CopyTranslations(string CDROM,string Name, /*{{{*/ |
642 | vector<string> &List, pkgCdromStatus *log) | |
22f8568d MV |
643 | { |
644 | OpProgress *Progress = NULL; | |
f7f0d6c7 | 645 | if (List.empty() == true) |
22f8568d MV |
646 | return true; |
647 | ||
648 | if(log) | |
649 | Progress = log->GetOpProgress(); | |
650 | ||
651 | bool Debug = _config->FindB("Debug::aptcdrom",false); | |
652 | ||
653 | // Prepare the progress indicator | |
650faab0 | 654 | off_t TotalSize = 0; |
78c9276d | 655 | std::vector<APT::Configuration::Compressor> const compressor = APT::Configuration::getCompressors(); |
f7f0d6c7 | 656 | for (vector<string>::iterator I = List.begin(); I != List.end(); ++I) |
22f8568d MV |
657 | { |
658 | struct stat Buf; | |
78c9276d DK |
659 | bool found = false; |
660 | std::string file = *I; | |
661 | for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin(); | |
662 | c != compressor.end(); ++c) | |
663 | { | |
e788a834 | 664 | if (stat((file + c->Extension).c_str(), &Buf) != 0) |
78c9276d DK |
665 | continue; |
666 | found = true; | |
667 | break; | |
668 | } | |
669 | ||
670 | if (found == false) | |
671 | return _error->Errno("stat", "Stat failed for %s", file.c_str()); | |
22f8568d | 672 | TotalSize += Buf.st_size; |
78c9276d | 673 | } |
22f8568d | 674 | |
650faab0 | 675 | off_t CurrentSize = 0; |
22f8568d MV |
676 | unsigned int NotFound = 0; |
677 | unsigned int WrongSize = 0; | |
678 | unsigned int Packages = 0; | |
f7f0d6c7 | 679 | for (vector<string>::iterator I = List.begin(); I != List.end(); ++I) |
69c2ecbd | 680 | { |
22f8568d | 681 | // Open the package file |
144353a9 | 682 | FileFd Pkg(*I, FileFd::ReadOnly, FileFd::Auto); |
699b209e DK |
683 | off_t const FileSize = Pkg.Size(); |
684 | ||
22f8568d | 685 | pkgTagFile Parser(&Pkg); |
95278287 | 686 | if (Pkg.IsOpen() == false || Pkg.Failed()) |
22f8568d | 687 | return false; |
88593886 | 688 | |
22f8568d MV |
689 | // Open the output file |
690 | char S[400]; | |
691 | snprintf(S,sizeof(S),"cdrom:[%s]/%s",Name.c_str(), | |
692 | (*I).c_str() + CDROM.length()); | |
693 | string TargetF = _config->FindDir("Dir::State::lists") + "partial/"; | |
694 | TargetF += URItoFileName(S); | |
d5da93b8 | 695 | FileFd Target; |
22f8568d | 696 | if (_config->FindB("APT::CDROM::NoAct",false) == true) |
d5da93b8 | 697 | { |
22f8568d | 698 | TargetF = "/dev/null"; |
d5da93b8 DK |
699 | Target.Open(TargetF,FileFd::WriteExists); |
700 | } else { | |
701 | Target.Open(TargetF,FileFd::WriteAtomic); | |
702 | } | |
95278287 | 703 | if (Pkg.IsOpen() == false || Pkg.Failed()) |
22f8568d | 704 | return false; |
88593886 | 705 | |
22f8568d MV |
706 | // Setup the progress meter |
707 | if(Progress) | |
708 | Progress->OverallProgress(CurrentSize,TotalSize,FileSize, | |
709 | string("Reading Translation Indexes")); | |
710 | ||
711 | // Parse | |
712 | if(Progress) | |
713 | Progress->SubProgress(Pkg.Size()); | |
714 | pkgTagSection Section; | |
715 | this->Section = &Section; | |
716 | string Prefix; | |
717 | unsigned long Hits = 0; | |
22f8568d MV |
718 | while (Parser.Step(Section) == true) |
719 | { | |
720 | if(Progress) | |
721 | Progress->Progress(Parser.Offset()); | |
722 | ||
88593886 DK |
723 | if (Section.Write(Target) == false || Target.Write("\n", 1) == false) |
724 | return false; | |
22f8568d MV |
725 | |
726 | Packages++; | |
727 | Hits++; | |
728 | } | |
22f8568d MV |
729 | |
730 | if (Debug == true) | |
91c03d37 | 731 | cout << " Processed by using Prefix '" << Prefix << "' and chop " << endl; |
88593886 | 732 | |
22f8568d MV |
733 | if (_config->FindB("APT::CDROM::NoAct",false) == false) |
734 | { | |
735 | // Move out of the partial directory | |
736 | Target.Close(); | |
737 | string FinalF = _config->FindDir("Dir::State::lists"); | |
738 | FinalF += URItoFileName(S); | |
739 | if (rename(TargetF.c_str(),FinalF.c_str()) != 0) | |
740 | return _error->Errno("rename","Failed to rename"); | |
d84da499 | 741 | ChangeOwnerAndPermissionOfFile("CopyTranslations", FinalF.c_str(), "root", "root", 0644); |
22f8568d | 742 | } |
88593886 | 743 | |
22f8568d | 744 | CurrentSize += FileSize; |
88593886 | 745 | } |
22f8568d MV |
746 | if(Progress) |
747 | Progress->Done(); | |
88593886 | 748 | |
22f8568d MV |
749 | // Some stats |
750 | if(log) { | |
751 | stringstream msg; | |
752 | if(NotFound == 0 && WrongSize == 0) | |
753 | ioprintf(msg, _("Wrote %i records.\n"), Packages); | |
754 | else if (NotFound != 0 && WrongSize == 0) | |
88593886 | 755 | ioprintf(msg, _("Wrote %i records with %i missing files.\n"), |
22f8568d MV |
756 | Packages, NotFound); |
757 | else if (NotFound == 0 && WrongSize != 0) | |
88593886 | 758 | ioprintf(msg, _("Wrote %i records with %i mismatched files\n"), |
22f8568d MV |
759 | Packages, WrongSize); |
760 | if (NotFound != 0 && WrongSize != 0) | |
761 | ioprintf(msg, _("Wrote %i records with %i missing files and %i mismatched files\n"), Packages, NotFound, WrongSize); | |
762 | } | |
88593886 | 763 | |
22f8568d MV |
764 | if (Packages == 0) |
765 | _error->Warning("No valid records were found."); | |
766 | ||
767 | if (NotFound + WrongSize > 10) | |
46e39c8e | 768 | _error->Warning("A lot of entries were discarded, something may be wrong.\n"); |
22f8568d MV |
769 | |
770 | return true; | |
771 | } | |
92fcbfc1 | 772 | /*}}}*/ |
862bafea | 773 | |
2651f1c0 | 774 | IndexCopy::IndexCopy() : d(nullptr), Section(nullptr) {} |
9d653a6d | 775 | APT_CONST IndexCopy::~IndexCopy() {} |
c8a4ce6c | 776 | |
6c55f07a | 777 | PackageCopy::PackageCopy() : IndexCopy(), d(NULL) {} |
c8a4ce6c | 778 | APT_CONST PackageCopy::~PackageCopy() {} |
6c55f07a | 779 | SourceCopy::SourceCopy() : IndexCopy(), d(NULL) {} |
c8a4ce6c | 780 | APT_CONST SourceCopy::~SourceCopy() {} |
2651f1c0 | 781 | TranslationsCopy::TranslationsCopy() : d(nullptr), Section(nullptr) {} |
c8a4ce6c | 782 | APT_CONST TranslationsCopy::~TranslationsCopy() {} |
6c55f07a | 783 | SigVerify::SigVerify() : d(NULL) {} |
c8a4ce6c | 784 | APT_CONST SigVerify::~SigVerify() {} |