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