| 1 | // -*- mode: cpp; mode: fold -*- |
| 2 | // Description /*{{{*/ |
| 3 | // $Id: indexcopy.cc,v 1.10 2002/03/26 07:38:58 jgg Exp $ |
| 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 /*{{{*/ |
| 13 | #include<config.h> |
| 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> |
| 19 | #include <apt-pkg/aptconfiguration.h> |
| 20 | #include <apt-pkg/configuration.h> |
| 21 | #include <apt-pkg/tagfile.h> |
| 22 | #include <apt-pkg/metaindex.h> |
| 23 | #include <apt-pkg/cdrom.h> |
| 24 | #include <apt-pkg/gpgv.h> |
| 25 | #include <apt-pkg/hashes.h> |
| 26 | #include <apt-pkg/debmetaindex.h> |
| 27 | |
| 28 | #include <iostream> |
| 29 | #include <unistd.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include <sstream> |
| 35 | |
| 36 | #include "indexcopy.h" |
| 37 | #include <apti18n.h> |
| 38 | /*}}}*/ |
| 39 | |
| 40 | using namespace std; |
| 41 | |
| 42 | // IndexCopy::CopyPackages - Copy the package files from the CD /*{{{*/ |
| 43 | // --------------------------------------------------------------------- |
| 44 | /* */ |
| 45 | bool IndexCopy::CopyPackages(string CDROM,string Name,vector<string> &List, |
| 46 | pkgCdromStatus *log) |
| 47 | { |
| 48 | OpProgress *Progress = NULL; |
| 49 | if (List.empty() == true) |
| 50 | return true; |
| 51 | |
| 52 | if(log) |
| 53 | Progress = log->GetOpProgress(); |
| 54 | |
| 55 | bool NoStat = _config->FindB("APT::CDROM::Fast",false); |
| 56 | bool Debug = _config->FindB("Debug::aptcdrom",false); |
| 57 | |
| 58 | // Prepare the progress indicator |
| 59 | off_t TotalSize = 0; |
| 60 | std::vector<APT::Configuration::Compressor> const compressor = APT::Configuration::getCompressors(); |
| 61 | for (vector<string>::iterator I = List.begin(); I != List.end(); ++I) |
| 62 | { |
| 63 | struct stat Buf; |
| 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 | { |
| 69 | if (stat((file + c->Extension).c_str(), &Buf) != 0) |
| 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()); |
| 77 | TotalSize += Buf.st_size; |
| 78 | } |
| 79 | |
| 80 | off_t CurrentSize = 0; |
| 81 | unsigned int NotFound = 0; |
| 82 | unsigned int WrongSize = 0; |
| 83 | unsigned int Packages = 0; |
| 84 | for (vector<string>::iterator I = List.begin(); I != List.end(); ++I) |
| 85 | { |
| 86 | string OrigPath = string(*I,CDROM.length()); |
| 87 | |
| 88 | // Open the package file |
| 89 | FileFd Pkg(*I + GetFileName(), FileFd::ReadOnly, FileFd::Auto); |
| 90 | off_t const FileSize = Pkg.Size(); |
| 91 | |
| 92 | pkgTagFile Parser(&Pkg); |
| 93 | if (_error->PendingError() == true) |
| 94 | return false; |
| 95 | |
| 96 | // Open the output file |
| 97 | char S[400]; |
| 98 | snprintf(S,sizeof(S),"cdrom:[%s]/%s%s",Name.c_str(), |
| 99 | (*I).c_str() + CDROM.length(),GetFileName()); |
| 100 | string TargetF = _config->FindDir("Dir::State::lists") + "partial/"; |
| 101 | TargetF += URItoFileName(S); |
| 102 | FileFd Target; |
| 103 | if (_config->FindB("APT::CDROM::NoAct",false) == true) |
| 104 | { |
| 105 | TargetF = "/dev/null"; |
| 106 | Target.Open(TargetF,FileFd::WriteExists); |
| 107 | } else { |
| 108 | Target.Open(TargetF,FileFd::WriteAtomic); |
| 109 | } |
| 110 | if (_error->PendingError() == true) |
| 111 | return false; |
| 112 | |
| 113 | // Setup the progress meter |
| 114 | if(Progress) |
| 115 | Progress->OverallProgress(CurrentSize,TotalSize,FileSize, |
| 116 | string("Reading ") + Type() + " Indexes"); |
| 117 | |
| 118 | // Parse |
| 119 | if(Progress) |
| 120 | Progress->SubProgress(Pkg.Size()); |
| 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 | { |
| 128 | if(Progress) |
| 129 | Progress->Progress(Parser.Offset()); |
| 130 | string File; |
| 131 | unsigned long long Size; |
| 132 | if (GetFile(File,Size) == false) |
| 133 | return false; |
| 134 | |
| 135 | if (Chop != 0) |
| 136 | File = OrigPath + ChopDirs(File,Chop); |
| 137 | |
| 138 | // See if the file exists |
| 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 | } |
| 155 | |
| 156 | // Get the size |
| 157 | struct stat Buf; |
| 158 | if (stat((CDROM + Prefix + File).c_str(),&Buf) != 0 || |
| 159 | Buf.st_size == 0) |
| 160 | { |
| 161 | bool Mangled = false; |
| 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 | } |
| 171 | |
| 172 | if (Mangled == false || |
| 173 | stat((CDROM + Prefix + File).c_str(),&Buf) != 0) |
| 174 | { |
| 175 | if (Debug == true) |
| 176 | clog << "Missed(2): " << OrigFile << endl; |
| 177 | NotFound++; |
| 178 | continue; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // Size match |
| 183 | if ((unsigned long long)Buf.st_size != Size) |
| 184 | { |
| 185 | if (Debug == true) |
| 186 | clog << "Wrong Size: " << File << endl; |
| 187 | WrongSize++; |
| 188 | continue; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | Packages++; |
| 193 | Hits++; |
| 194 | |
| 195 | if (RewriteEntry(Target, File) == false) |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | if (Debug == true) |
| 200 | cout << " Processed by using Prefix '" << Prefix << "' and chop " << Chop << endl; |
| 201 | |
| 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"); |
| 210 | ChangeOwnerAndPermissionOfFile("CopyPackages", FinalF.c_str(), "root", "root", 0644); |
| 211 | } |
| 212 | |
| 213 | /* Mangle the source to be in the proper notation with |
| 214 | prefix dist [component] */ |
| 215 | *I = string(*I,Prefix.length()); |
| 216 | ConvertToSourceList(CDROM,*I); |
| 217 | *I = Prefix + ' ' + *I; |
| 218 | |
| 219 | CurrentSize += FileSize; |
| 220 | } |
| 221 | if(Progress) |
| 222 | Progress->Done(); |
| 223 | |
| 224 | // Some stats |
| 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) |
| 230 | ioprintf(msg, _("Wrote %i records with %i missing files.\n"), |
| 231 | Packages, NotFound); |
| 232 | else if (NotFound == 0 && WrongSize != 0) |
| 233 | ioprintf(msg, _("Wrote %i records with %i mismatched files\n"), |
| 234 | Packages, WrongSize); |
| 235 | if (NotFound != 0 && WrongSize != 0) |
| 236 | ioprintf(msg, _("Wrote %i records with %i missing files and %i mismatched files\n"), Packages, NotFound, WrongSize); |
| 237 | } |
| 238 | |
| 239 | if (Packages == 0) |
| 240 | _error->Warning("No valid records were found."); |
| 241 | |
| 242 | if (NotFound + WrongSize > 10) |
| 243 | _error->Warning("A lot of entries were discarded, something may be wrong.\n"); |
| 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); |
| 260 | |
| 261 | if (I == string::npos) |
| 262 | return string(); |
| 263 | |
| 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; |
| 280 | if (stat((CD + MyPrefix + File).c_str(),&Buf) != 0) |
| 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; |
| 309 | if (stat((Dir + File).c_str(),&Buf) != 0) |
| 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 |
| 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 | */ |
| 342 | void IndexCopy::ConvertToSourceList(string CD,string &Path) |
| 343 | { |
| 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. |
| 354 | if (stringcmp(Path.c_str(),Path.c_str()+strlen("dists/"),"dists/") != 0) |
| 355 | return; |
| 356 | |
| 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 |
| 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 | |
| 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") |
| 386 | continue; |
| 387 | |
| 388 | Path = Dist + ' ' + Comp; |
| 389 | return; |
| 390 | } |
| 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 | /*}}}*/ |
| 413 | // PackageCopy::GetFile - Get the file information from the section /*{{{*/ |
| 414 | // --------------------------------------------------------------------- |
| 415 | /* */ |
| 416 | bool PackageCopy::GetFile(string &File,unsigned long long &Size) |
| 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 /*{{{*/ |
| 426 | bool PackageCopy::RewriteEntry(FileFd &Target,string const &File) |
| 427 | { |
| 428 | std::vector<pkgTagSection::Tag> Changes; |
| 429 | Changes.push_back(pkgTagSection::Tag::Rewrite("Filename", File)); |
| 430 | |
| 431 | if (Section->Write(Target, TFRewritePackageOrder, Changes) == false) |
| 432 | return false; |
| 433 | return Target.Write("\n", 1); |
| 434 | } |
| 435 | /*}}}*/ |
| 436 | // SourceCopy::GetFile - Get the file information from the section /*{{{*/ |
| 437 | // --------------------------------------------------------------------- |
| 438 | /* */ |
| 439 | bool SourceCopy::GetFile(string &File,unsigned long long &Size) |
| 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 | |
| 450 | // Read the first file triplet |
| 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 |
| 462 | Size = strtoull(sSize.c_str(), NULL, 10); |
| 463 | File = Base + File; |
| 464 | return true; |
| 465 | } |
| 466 | /*}}}*/ |
| 467 | // SourceCopy::RewriteEntry - Rewrite the entry with a new filename /*{{{*/ |
| 468 | bool SourceCopy::RewriteEntry(FileFd &Target, std::string const &File) |
| 469 | { |
| 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) |
| 475 | return false; |
| 476 | return Target.Write("\n", 1); |
| 477 | } |
| 478 | /*}}}*/ |
| 479 | // SigVerify::Verify - Verify a files md5sum against its metaindex /*{{{*/ |
| 480 | bool SigVerify::Verify(string prefix, string file, metaIndex *MetaIndex) |
| 481 | { |
| 482 | const metaIndex::checkSum *Record = MetaIndex->Lookup(file); |
| 483 | bool const Debug = _config->FindB("Debug::aptcdrom",false); |
| 484 | |
| 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 ) |
| 489 | if(!RealFileExists(prefix+file)) |
| 490 | { |
| 491 | if (Debug == true) |
| 492 | cout << "Skipping nonexistent in " << prefix << " file " << file << std::endl; |
| 493 | return true; |
| 494 | } |
| 495 | |
| 496 | if (!Record) |
| 497 | { |
| 498 | _error->Warning(_("Can't find authentication record for: %s"), file.c_str()); |
| 499 | return false; |
| 500 | } |
| 501 | |
| 502 | if (!Record->Hashes.VerifyFile(prefix+file)) |
| 503 | { |
| 504 | _error->Warning(_("Hash mismatch for: %s"),file.c_str()); |
| 505 | return false; |
| 506 | } |
| 507 | |
| 508 | if(Debug == true) |
| 509 | { |
| 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; |
| 514 | } |
| 515 | |
| 516 | return true; |
| 517 | } |
| 518 | /*}}}*/ |
| 519 | bool SigVerify::CopyMetaIndex(string CDROM, string CDName, /*{{{*/ |
| 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; |
| 530 | Target.Open(TargetF,FileFd::WriteAtomic); |
| 531 | Rel.Open(prefix + file,FileFd::ReadOnly); |
| 532 | if (CopyFile(Rel,Target) == false || Target.Close() == false) |
| 533 | return _error->Error("Copying of '%s' for '%s' from '%s' failed", file.c_str(), CDName.c_str(), prefix.c_str()); |
| 534 | ChangeOwnerAndPermissionOfFile("CopyPackages", TargetF.c_str(), "root", "root", 0644); |
| 535 | |
| 536 | return true; |
| 537 | } |
| 538 | /*}}}*/ |
| 539 | bool SigVerify::CopyAndVerify(string CDROM,string Name,vector<string> &SigList, /*{{{*/ |
| 540 | vector<string> /*PkgList*/,vector<string> /*SrcList*/) |
| 541 | { |
| 542 | if (SigList.empty() == true) |
| 543 | return true; |
| 544 | |
| 545 | bool Debug = _config->FindB("Debug::aptcdrom",false); |
| 546 | |
| 547 | // Read all Release files |
| 548 | for (vector<string>::iterator I = SigList.begin(); I != SigList.end(); ++I) |
| 549 | { |
| 550 | if(Debug) |
| 551 | cout << "Signature verify for: " << *I << endl; |
| 552 | |
| 553 | metaIndex *MetaIndex = new debReleaseIndex("",""); |
| 554 | string prefix = *I; |
| 555 | |
| 556 | string const releasegpg = *I+"Release.gpg"; |
| 557 | string const release = *I+"Release"; |
| 558 | string const inrelease = *I+"InRelease"; |
| 559 | bool useInRelease = true; |
| 560 | |
| 561 | // a Release.gpg without a Release should never happen |
| 562 | if (RealFileExists(inrelease) == true) |
| 563 | ; |
| 564 | else if(RealFileExists(release) == false || RealFileExists(releasegpg) == false) |
| 565 | { |
| 566 | delete MetaIndex; |
| 567 | continue; |
| 568 | } |
| 569 | else |
| 570 | useInRelease = false; |
| 571 | |
| 572 | pid_t pid = ExecFork(); |
| 573 | if(pid < 0) { |
| 574 | _error->Error("Fork failed"); |
| 575 | return false; |
| 576 | } |
| 577 | if(pid == 0) |
| 578 | { |
| 579 | if (useInRelease == true) |
| 580 | ExecGPGV(inrelease, inrelease); |
| 581 | else |
| 582 | ExecGPGV(release, releasegpg); |
| 583 | } |
| 584 | |
| 585 | if(!ExecWait(pid, "gpgv")) { |
| 586 | _error->Warning("Signature verification failed for: %s", |
| 587 | (useInRelease ? inrelease.c_str() : releasegpg.c_str())); |
| 588 | // something went wrong, don't copy the Release.gpg |
| 589 | // FIXME: delete any existing gpg file? |
| 590 | delete MetaIndex; |
| 591 | continue; |
| 592 | } |
| 593 | |
| 594 | // Open the Release file and add it to the MetaIndex |
| 595 | std::string ErrorText; |
| 596 | if(MetaIndex->Load(release, &ErrorText) == false) |
| 597 | { |
| 598 | _error->Error("%s", ErrorText.c_str()); |
| 599 | return false; |
| 600 | } |
| 601 | |
| 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(); |
| 605 | for (vector<string>::iterator I = keys.begin(); I != keys.end(); ++I) |
| 606 | { |
| 607 | if(!Verify(prefix,*I, MetaIndex)) { |
| 608 | // something went wrong, don't copy the Release.gpg |
| 609 | // FIXME: delete any existing gpg file? |
| 610 | _error->Discard(); |
| 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 |
| 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 | } |
| 626 | } |
| 627 | |
| 628 | return true; |
| 629 | } |
| 630 | /*}}}*/ |
| 631 | // SigVerify::RunGPGV - deprecated wrapper calling ExecGPGV /*{{{*/ |
| 632 | APT_NORETURN bool SigVerify::RunGPGV(std::string const &File, std::string const &FileOut, |
| 633 | int const &statusfd, int fd[2]) { |
| 634 | ExecGPGV(File, FileOut, statusfd, fd); |
| 635 | } |
| 636 | APT_NORETURN bool SigVerify::RunGPGV(std::string const &File, std::string const &FileOut, |
| 637 | int const &statusfd) { |
| 638 | ExecGPGV(File, FileOut, statusfd); |
| 639 | } |
| 640 | /*}}}*/ |
| 641 | bool TranslationsCopy::CopyTranslations(string CDROM,string Name, /*{{{*/ |
| 642 | vector<string> &List, pkgCdromStatus *log) |
| 643 | { |
| 644 | OpProgress *Progress = NULL; |
| 645 | if (List.empty() == true) |
| 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 |
| 654 | off_t TotalSize = 0; |
| 655 | std::vector<APT::Configuration::Compressor> const compressor = APT::Configuration::getCompressors(); |
| 656 | for (vector<string>::iterator I = List.begin(); I != List.end(); ++I) |
| 657 | { |
| 658 | struct stat Buf; |
| 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 | { |
| 664 | if (stat((file + c->Extension).c_str(), &Buf) != 0) |
| 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()); |
| 672 | TotalSize += Buf.st_size; |
| 673 | } |
| 674 | |
| 675 | off_t CurrentSize = 0; |
| 676 | unsigned int NotFound = 0; |
| 677 | unsigned int WrongSize = 0; |
| 678 | unsigned int Packages = 0; |
| 679 | for (vector<string>::iterator I = List.begin(); I != List.end(); ++I) |
| 680 | { |
| 681 | // Open the package file |
| 682 | FileFd Pkg(*I, FileFd::ReadOnly, FileFd::Auto); |
| 683 | off_t const FileSize = Pkg.Size(); |
| 684 | |
| 685 | pkgTagFile Parser(&Pkg); |
| 686 | if (_error->PendingError() == true) |
| 687 | return false; |
| 688 | |
| 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); |
| 695 | FileFd Target; |
| 696 | if (_config->FindB("APT::CDROM::NoAct",false) == true) |
| 697 | { |
| 698 | TargetF = "/dev/null"; |
| 699 | Target.Open(TargetF,FileFd::WriteExists); |
| 700 | } else { |
| 701 | Target.Open(TargetF,FileFd::WriteAtomic); |
| 702 | } |
| 703 | if (_error->PendingError() == true) |
| 704 | return false; |
| 705 | |
| 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; |
| 718 | while (Parser.Step(Section) == true) |
| 719 | { |
| 720 | if(Progress) |
| 721 | Progress->Progress(Parser.Offset()); |
| 722 | |
| 723 | if (Section.Write(Target) == false || Target.Write("\n", 1) == false) |
| 724 | return false; |
| 725 | |
| 726 | Packages++; |
| 727 | Hits++; |
| 728 | } |
| 729 | |
| 730 | if (Debug == true) |
| 731 | cout << " Processed by using Prefix '" << Prefix << "' and chop " << endl; |
| 732 | |
| 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"); |
| 741 | ChangeOwnerAndPermissionOfFile("CopyTranslations", FinalF.c_str(), "root", "root", 0644); |
| 742 | } |
| 743 | |
| 744 | CurrentSize += FileSize; |
| 745 | } |
| 746 | if(Progress) |
| 747 | Progress->Done(); |
| 748 | |
| 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) |
| 755 | ioprintf(msg, _("Wrote %i records with %i missing files.\n"), |
| 756 | Packages, NotFound); |
| 757 | else if (NotFound == 0 && WrongSize != 0) |
| 758 | ioprintf(msg, _("Wrote %i records with %i mismatched files\n"), |
| 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 | } |
| 763 | |
| 764 | if (Packages == 0) |
| 765 | _error->Warning("No valid records were found."); |
| 766 | |
| 767 | if (NotFound + WrongSize > 10) |
| 768 | _error->Warning("A lot of entries were discarded, something may be wrong.\n"); |
| 769 | |
| 770 | return true; |
| 771 | } |
| 772 | /*}}}*/ |
| 773 | |
| 774 | IndexCopy::IndexCopy() : d(NULL) {} |
| 775 | APT_CONST IndexCopy::~IndexCopy() {} |
| 776 | |
| 777 | PackageCopy::PackageCopy() : IndexCopy(), d(NULL) {} |
| 778 | APT_CONST PackageCopy::~PackageCopy() {} |
| 779 | SourceCopy::SourceCopy() : IndexCopy(), d(NULL) {} |
| 780 | APT_CONST SourceCopy::~SourceCopy() {} |
| 781 | TranslationsCopy::TranslationsCopy() : d(NULL) {} |
| 782 | APT_CONST TranslationsCopy::~TranslationsCopy() {} |
| 783 | SigVerify::SigVerify() : d(NULL) {} |
| 784 | APT_CONST SigVerify::~SigVerify() {} |