- OpTextProgress Progress;
-
- bool NoStat = _config->FindB("APT::CDROM::Fast",false);
- bool Debug = _config->FindB("Debug::aptcdrom",false);
-
- // Prepare the progress indicator
- unsigned long TotalSize = 0;
- for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
- {
- struct stat Buf;
- if (stat(string(*I + "Packages").c_str(),&Buf) != 0)
- return _error->Errno("stat","Stat failed for %s",
- string(*I + "Packages").c_str());
- TotalSize += Buf.st_size;
- }
-
- unsigned long CurrentSize = 0;
- unsigned int NotFound = 0;
- unsigned int WrongSize = 0;
- unsigned int Packages = 0;
- for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
- {
- string OrigPath = string(*I,CDROM.length());
-
- // Open the package file
- FileFd Pkg(*I + "Packages",FileFd::ReadOnly);
- pkgTagFile Parser(Pkg);
- if (_error->PendingError() == true)
- return false;
-
- // Open the output file
- char S[400];
- sprintf(S,"cdrom:%s/%sPackages",Name.c_str(),(*I).c_str() + CDROM.length());
- string TargetF = _config->FindDir("Dir::State::lists") + "partial/";
- TargetF += URItoFileName(S);
- if (_config->FindB("APT::CDROM::NoAct",false) == true)
- TargetF = "/dev/null";
- FileFd Target(TargetF,FileFd::WriteEmpty);
- if (_error->PendingError() == true)
- return false;
-
- // Setup the progress meter
- Progress.OverallProgress(CurrentSize,TotalSize,Pkg.Size(),
- "Reading Package Lists");
-
- // Parse
- Progress.SubProgress(Pkg.Size());
- pkgTagSection Section;
- string Prefix;
- unsigned long Hits = 0;
- unsigned long Chop = 0;
- while (Parser.Step(Section) == true)
- {
- Progress.Progress(Parser.Offset());
-
- string File = Section.FindS("Filename");
- unsigned long Size = Section.FindI("Size");
- if (File.empty() || Size == 0)
- return _error->Error("Cannot find filename or size tag");
-
- if (Chop != 0)
- File = OrigPath + ChopDirs(File,Chop);
-
- // See if the file exists
- if (NoStat == false || Hits < 10)
- {
- // Attempt to fix broken structure
- if (Hits == 0)
- {
- if (ReconstructPrefix(Prefix,OrigPath,CDROM,File) == false &&
- ReconstructChop(Chop,*I,File) == false)
- {
- NotFound++;
- continue;
- }
- if (Chop != 0)
- File = OrigPath + ChopDirs(File,Chop);
- }
-
- // Get the size
- struct stat Buf;
- if (stat(string(CDROM + Prefix + File).c_str(),&Buf) != 0)
- {
- NotFound++;
- continue;
- }
-
- // Size match
- if ((unsigned)Buf.st_size != Size)
- {
- WrongSize++;
- continue;
- }
- }
-
- Packages++;
- Hits++;
-
- // Copy it to the target package file
- const char *Start;
- const char *Stop;
- if (Chop != 0)
- {
- // Mangle the output filename
- const char *Filename;
- Section.Find("Filename",Filename,Stop);
-
- /* We need to rewrite the filename field so we emit
- all fields except the filename file and rewrite that one */
- for (unsigned int I = 0; I != Section.Count(); I++)
- {
- Section.Get(Start,Stop,I);
- if (Start <= Filename && Stop > Filename)
- {
- char S[500];
- sprintf(S,"Filename: %s\n",File.c_str());
- if (I + 1 == Section.Count())
- strcat(S,"\n");
- if (Target.Write(S,strlen(S)) == false)
- return false;
- }
- else
- if (Target.Write(Start,Stop-Start) == false)
- return false;
- }
- if (Target.Write("\n",1) == false)
- return false;
- }
- else
- {
- Section.GetSection(Start,Stop);
- if (Target.Write(Start,Stop-Start) == false)
- return false;
- }
- }
-
- if (Debug == true)
- cout << " Processed by using Prefix '" << Prefix << "' and chop " << Chop << endl;
-
- if (_config->FindB("APT::CDROM::NoAct",false) == false)
- {
- // Move out of the partial directory
- Target.Close();
- string FinalF = _config->FindDir("Dir::State::lists");
- FinalF += URItoFileName(S);
- if (rename(TargetF.c_str(),FinalF.c_str()) != 0)
- return _error->Errno("rename","Failed to rename");
-
- // Copy the release file
- sprintf(S,"cdrom:%s/%sRelease",Name.c_str(),(*I).c_str() + CDROM.length());
- string TargetF = _config->FindDir("Dir::State::lists") + "partial/";
- TargetF += URItoFileName(S);
- if (FileExists(*I + "Release") == true)
- {
- FileFd Target(TargetF,FileFd::WriteEmpty);
- FileFd Rel(*I + "Release",FileFd::ReadOnly);
- if (_error->PendingError() == true)
- return false;
-
- if (CopyFile(Rel,Target) == false)
- return false;
- }
- else
- {
- // Empty release file
- FileFd Target(TargetF,FileFd::WriteEmpty);
- }
-
- // Rename the release file
- FinalF = _config->FindDir("Dir::State::lists");
- FinalF += URItoFileName(S);
- if (rename(TargetF.c_str(),FinalF.c_str()) != 0)
- return _error->Errno("rename","Failed to rename");
- }
-
- /* Mangle the source to be in the proper notation with
- prefix dist [component] */
- *I = string(*I,Prefix.length());
- ConvertToSourceList(CDROM,*I);
- *I = Prefix + ' ' + *I;
-
- CurrentSize += Pkg.Size();
- }
- Progress.Done();
-
- // Some stats
- cout << "Wrote " << Packages << " package records" ;
- if (NotFound != 0)
- cout << " with " << NotFound << " missing files";
- if (NotFound != 0 && WrongSize != 0)
- cout << " and";
- if (WrongSize != 0)
- cout << " with " << WrongSize << " mismatched files";
- cout << '.' << endl;
-
- if (Packages == 0)
- return _error->Error("No valid package records were found.");
-
- if (NotFound + WrongSize > 10)
- cout << "Alot of package entires were discarded, perhaps this CD is funny?" << endl;
-