diff options
author | Arch Librarian <arch@canonical.com> | 2004-09-20 16:51:43 +0000 |
---|---|---|
committer | Arch Librarian <arch@canonical.com> | 2004-09-20 16:51:43 +0000 |
commit | 4dfaa253843978fd5877e7e2a4264ee207921872 (patch) | |
tree | 4b59323ef331703a15545308bd1d83037bc84d08 /cmdline | |
parent | 18444708fa7f67e7c7ab85719babcbfad774d123 (diff) |
Finished
Author: jgg
Date: 1998-11-28 00:00:36 GMT
Finished
Diffstat (limited to 'cmdline')
-rw-r--r-- | cmdline/apt-cdrom.cc | 580 |
1 files changed, 411 insertions, 169 deletions
diff --git a/cmdline/apt-cdrom.cc b/cmdline/apt-cdrom.cc index fe7d4d55a..8f2afd7d1 100644 --- a/cmdline/apt-cdrom.cc +++ b/cmdline/apt-cdrom.cc @@ -1,6 +1,6 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: apt-cdrom.cc,v 1.2 1998/11/27 04:49:42 jgg Exp $ +// $Id: apt-cdrom.cc,v 1.3 1998/11/28 00:00:36 jgg Exp $ /* ###################################################################### APT CDROM - Tool for handling APT's CDROM database. @@ -163,7 +163,7 @@ bool IdentCdrom(string CD,string &Res) } /*}}}*/ -// FindPackage - Find the package files on the CDROM /*{{{*/ +// FindPackages - Find the package files on the CDROM /*{{{*/ // --------------------------------------------------------------------- /* We look over the cdrom for package files. This is a recursive search that short circuits when it his a package file in the dir. @@ -171,7 +171,7 @@ bool IdentCdrom(string CD,string &Res) binary-* sub dirs. */ bool FindPackages(string CD,vector<string> &List, int Depth = 0) { - if (Depth >= 5) + if (Depth >= 7) return true; if (CD[CD.length()-1] != '/') @@ -209,10 +209,7 @@ bool FindPackages(string CD,vector<string> &List, int Depth = 0) // See if the name is a sub directory struct stat Buf; if (stat(Dir->d_name,&Buf) != 0) - { - _error->Errno("Stat","Stat failed for %s",Dir->d_name); - break; - } + continue; if (S_ISDIR(Buf.st_mode) == 0) continue; @@ -230,111 +227,6 @@ bool FindPackages(string CD,vector<string> &List, int Depth = 0) return !_error->PendingError(); } /*}}}*/ -// CopyPackages - Copy the package files from the CD /*{{{*/ -// --------------------------------------------------------------------- -/* */ -bool CopyPackages(string CDROM,string Name,vector<string> &List) -{ - OpTextProgress Progress; - - bool NoStat = _config->FindB("APT::CDROM::Fast",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++) - { - // 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; - 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"); - - // See if the file exists - if (NoStat == false) - { - struct stat Buf; - File = CDROM + File; - if (stat(File.c_str(),&Buf) != 0) - { - NotFound++; - continue; - } - - // Size match - if ((unsigned)Buf.st_size != Size) - { - WrongSize++; - continue; - } - } - - Packages++; - - // Copy it to the target package file - const char *Start; - const char *Stop; - Section.GetSection(Start,Stop); - if (Target.Write(Start,Stop-Start) == false) - return false; - } - - 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 (NotFound + WrongSize > 10) - cout << "Alot of package entires were discarded, perhaps this CD is funny?" << endl; -} - /*}}}*/ // DropBinaryArch - Dump dirs with a string like /binary-<foo>/ /*{{{*/ // --------------------------------------------------------------------- /* Here we drop everything that is not this machines arch */ @@ -445,56 +337,257 @@ bool DropRepeats(vector<string> &List) return true; } /*}}}*/ -// ConvertToSourceList - Takes the path list and converts it /*{{{*/ +// ConvertToSourceList - Convert a Path to a sourcelist entry /*{{{*/ // --------------------------------------------------------------------- -/* This looks at each element and decides if it can be expressed using - dists/ form or if it requires an absolute specficiation. It also - strips the leading CDROM path from the paths. */ -bool ConvertToSourcelist(string CD,vector<string> &List) +/* We look for things in dists/ notation and convert them to + <dist> <component> form otherwise it is left alone. This also strips + the CD path. */ +void ConvertToSourceList(string CD,string &Path) { char S[300]; sprintf(S,"binary-%s",_config->Find("Apt::Architecture").c_str()); + + // Strip the cdrom base path + Path = string(Path,CD.length()); + + // Too short to be a dists/ type + if (Path.length() < strlen("dists/")) + return; + + // Not a dists type. + if (stringcmp(Path.begin(),Path.begin()+strlen("dists/"),"dists/") != 0) + return; - sort(List.begin(),List.end()); + // Isolate the dist + string::size_type Slash = strlen("dists/"); + string::size_type Slash2 = Path.find('/',Slash + 1); + if (Slash2 == string::npos || Slash2 + 2 >= Path.length()) + return; + string Dist = string(Path,Slash,Slash2 - Slash); - // Convert to source list notation - for (vector<string>::iterator I = List.begin(); I != List.end(); I++) - { - // Strip the cdrom base path - *I = string(*I,CD.length()); + // Isolate the component + Slash = Path.find('/',Slash2+1); + if (Slash == string::npos || Slash + 2 >= Path.length()) + return; + string Comp = string(Path,Slash2+1,Slash - Slash2-1); - // Too short to be a dists/ type - if ((*I).length() < strlen("dists/")) - continue; + // Verify the trailing binar - bit + Slash2 = Path.find('/',Slash + 1); + if (Slash == string::npos) + return; + string Binary = string(Path,Slash+1,Slash2 - Slash-1); - // Not a dists type. - if (stringcmp((*I).begin(),(*I).begin()+strlen("dists/"),"dists/") != 0) - continue; + if (Binary != S) + return; + + Path = Dist + ' ' + Comp; +} + /*}}}*/ +// GrabFirst - Return the first Depth path components /*{{{*/ +// --------------------------------------------------------------------- +/* */ +bool GrabFirst(string Path,string &To,unsigned int Depth) +{ + string::size_type I = 0; + do + { + I = Path.find('/',I+1); + Depth--; + } + while (I != string::npos && Depth != 0); + + if (I == string::npos) + return false; - // Isolate the dist - string::size_type Slash = strlen("dists/"); - string::size_type Slash2 = (*I).find('/',Slash + 1); - if (Slash2 == string::npos || Slash2 + 2 >= (*I).length()) - continue; - string Dist = string(*I,Slash,Slash2 - Slash); + To = string(Path,0,I+1); + return true; +} + /*}}}*/ +// CopyPackages - Copy the package files from the CD /*{{{*/ +// --------------------------------------------------------------------- +/* */ +bool CopyPackages(string CDROM,string Name,vector<string> &List) +{ + OpTextProgress Progress; + + bool NoStat = _config->FindB("APT::CDROM::Fast",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; + } - // Isolate the component - Slash = (*I).find('/',Slash2+1); - if (Slash == string::npos || Slash + 2 >= (*I).length()) - continue; - string Comp = string(*I,Slash2+1,Slash - Slash2-1); + 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()); - // Verify the trailing binar - bit - Slash2 = (*I).find('/',Slash + 1); - if (Slash == string::npos) - continue; - string Binary = string(*I,Slash+1,Slash2 - Slash-1); + // Open the package file + FileFd Pkg(*I + "Packages",FileFd::ReadOnly); + pkgTagFile Parser(Pkg); + if (_error->PendingError() == true) + return false; - if (Binary != S) - continue; + // 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; - *I = Dist + ' ' + Comp; - } + // 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; + 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"); + + // See if the file exists + if (NoStat == false || Hits < 10) + { + struct stat Buf; + unsigned int Depth = 1; + string MyPrefix = Prefix; + while (1) + { + if (stat(string(CDROM + MyPrefix + File).c_str(),&Buf) != 0) + { + if (Prefix.empty() == true) + { + if (GrabFirst(OrigPath,MyPrefix,Depth++) == true) + continue; + } + + NotFound++; + Depth = 0; + break; + } + else + break; + } + + // Failed + if (Depth == 0) + continue; + + // Store the new prefix + if (Depth != 1) + Prefix = MyPrefix; + + // 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; + Section.GetSection(Start,Stop); + if (Target.Write(Start,Stop-Start) == false) + return false; + } + + 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; +} + /*}}}*/ + +// ReduceSourceList - Takes the path list and reduces it /*{{{*/ +// --------------------------------------------------------------------- +/* This takes the list of source list expressed entires and collects + similar ones to form a single entry for each dist */ +bool ReduceSourcelist(string CD,vector<string> &List) +{ + sort(List.begin(),List.end()); // Collect similar entries for (vector<string>::iterator I = List.begin(); I != List.end(); I++) @@ -503,19 +596,25 @@ bool ConvertToSourcelist(string CD,vector<string> &List) string::size_type Space = (*I).find(' '); if (Space == string::npos) continue; + string::size_type SSpace = (*I).find(' ',Space + 1); + if (SSpace == string::npos) + continue; - string Word1 = string(*I,0,Space); + string Word1 = string(*I,Space,SSpace-Space); for (vector<string>::iterator J = List.begin(); J != I; J++) { // Find a space.. string::size_type Space2 = (*J).find(' '); if (Space2 == string::npos) continue; + string::size_type SSpace2 = (*J).find(' ',Space2 + 1); + if (SSpace2 == string::npos) + continue; - if (string(*J,0,Space2) != Word1) + if (string(*J,Space2,SSpace2-Space2) != Word1) continue; - *J += string(*I,Space); + *J += string(*I,SSpace); *I = string(); } } @@ -532,14 +631,17 @@ bool ConvertToSourcelist(string CD,vector<string> &List) /*}}}*/ // WriteDatabase - Write the CDROM Database file /*{{{*/ // --------------------------------------------------------------------- -/* */ +/* We rewrite the configuration class associated with the cdrom database. */ bool WriteDatabase(Configuration &Cnf) { string DFile = _config->FindFile("Dir::State::cdroms"); - - ofstream Out(string(DFile + ".new").c_str()); + string NewFile = DFile + ".new"; + + unlink(NewFile.c_str()); + ofstream Out(NewFile.c_str()); if (!Out) - return _error->Error("Failed to open %s.new",DFile.c_str()); + return _error->Errno("ofstream::ofstream", + "Failed to open %s.new",DFile.c_str()); /* Write out all of the configuration directives by walking the configuration tree */ @@ -565,7 +667,7 @@ bool WriteDatabase(Configuration &Cnf) Out.close(); rename(DFile.c_str(),string(DFile + '~').c_str()); - if (rename(string(DFile + ".new").c_str(),DFile.c_str()) != 0) + if (rename(NewFile.c_str(),DFile.c_str()) != 0) return _error->Errno("rename","Failed to rename %s.new to %s", DFile.c_str(),DFile.c_str()); @@ -574,9 +676,100 @@ bool WriteDatabase(Configuration &Cnf) /*}}}*/ // WriteSourceList - Write an updated sourcelist /*{{{*/ // --------------------------------------------------------------------- -/* */ +/* This reads the old source list and copies it into the new one. It + appends the new CDROM entires just after the first block of comments. + This places them first in the file. It also removes any old entries + that were the same. */ bool WriteSourceList(string Name,vector<string> &List) { + string File = _config->FindFile("Dir::Etc::sourcelist"); + + // Open the stream for reading + ifstream F(File.c_str(),ios::in | ios::nocreate); + if (!F != 0) + return _error->Errno("ifstream::ifstream","Opening %s",File.c_str()); + + string NewFile = File + ".new"; + unlink(NewFile.c_str()); + ofstream Out(NewFile.c_str()); + if (!Out) + return _error->Errno("ofstream::ofstream", + "Failed to open %s.new",File.c_str()); + + // Create a short uri without the path + string ShortURI = "cdrom:" + Name + "/"; + + char Buffer[300]; + int CurLine = 0; + bool First = true; + while (F.eof() == false) + { + F.getline(Buffer,sizeof(Buffer)); + CurLine++; + _strtabexpand(Buffer,sizeof(Buffer)); + _strstrip(Buffer); + + // Comment or blank + if (Buffer[0] == '#' || Buffer[0] == 0) + { + Out << Buffer << endl; + continue; + } + + if (First == true) + { + for (vector<string>::iterator I = List.begin(); I != List.end(); I++) + { + string::size_type Space = (*I).find(' '); + if (Space == string::npos) + return _error->Error("Internal error"); + + Out << "deb \"cdrom:" << Name << "/" << string(*I,0,Space) << + "\" " << string(*I,Space+1) << endl; + } + } + First = false; + + // Grok it + string Type; + string URI; + char *C = Buffer; + if (ParseQuoteWord(C,Type) == false || + ParseQuoteWord(C,URI) == false) + { + Out << Buffer << endl; + continue; + } + + // Emit lines like this one + if (Type != "deb" || string(URI,0,ShortURI.length()) != ShortURI) + { + Out << Buffer << endl; + continue; + } + } + + // Just in case the file was empty + if (First == true) + { + for (vector<string>::iterator I = List.begin(); I != List.end(); I++) + { + string::size_type Space = (*I).find(' '); + if (Space == string::npos) + return _error->Error("Internal error"); + + Out << "deb \"cdrom:" << Name << "/" << string(*I,0,Space) << + "\" " << string(*I,Space+1) << endl; + } + } + + Out.close(); + + rename(File.c_str(),string(File + '~').c_str()); + if (rename(NewFile.c_str(),File.c_str()) != 0) + return _error->Errno("rename","Failed to rename %s.new to %s", + File.c_str(),File.c_str()); + return true; } /*}}}*/ @@ -608,7 +801,10 @@ string PromptLine(const char *Text) // DoAdd - Add a new CDROM /*{{{*/ // --------------------------------------------------------------------- -/* */ +/* This does the main add bit.. We show some status and things. The + sequence is to mount/umount the CD, Ident it then scan it for package + files and reduce that list. Then we copy over the package files and + verify them. Then rewrite the database files */ bool DoAdd(CommandLine &) { // Startup @@ -655,6 +851,15 @@ bool DoAdd(CommandLine &) if (FindPackages(CDROM,List) == false) return false; chdir(StartDir.c_str()); + + if (_config->FindB("Debug::aptcdrom",false) == true) + { + cout << "I found:" << endl; + for (vector<string>::iterator I = List.begin(); I != List.end(); I++) + { + cout << *I << endl; + } + } // Fix up the list DropBinaryArch(List); @@ -662,15 +867,41 @@ bool DoAdd(CommandLine &) cout << "Found " << List.size() << " package index files." << endl; if (List.size() == 0) - return _error->Error("Unable to locate any package files, perhaps this is not a debian CD-ROM"); + return _error->Error("Unable to locate any package files, perhaps this is not a Debian Disc"); // Check if the CD is in the database string Name; if (Database.Exists("CD::" + ID) == false || _config->FindB("APT::CDROM::Rename",false) == true) { - cout << "Please provide a name for this CD-ROM, such as 'Debian 2.1r1 Disk 1'"; - Name = PromptLine(""); + // Try to use the CDs label if at all possible + if (FileExists(CDROM + "/.disk/info") == true) + { + ifstream F(string(CDROM+ "/.disk/info").c_str()); + if (!F == 0) + getline(F,Name); + + if (Name.empty() == false) + { + cout << "Found label '" << Name << "'" << endl; + Database.Set("CD::" + ID + "::Label",Name); + } + } + + if (_config->FindB("APT::CDROM::Rename",false) == true || + Name.empty() == false) + { + cout << "Please provide a name for this Disc, such as 'Debian 2.1r1 Disk 1'"; + while (1) + { + Name = PromptLine(""); + if (Name.empty() == false && + Name.find('/') == string::npos) + break; + cout << "That is not a valid name, try again " << endl; + } + + } } else Name = Database.Find("CD::" + ID); @@ -681,20 +912,31 @@ bool DoAdd(CommandLine &) if (CopyPackages(CDROM,Name,List) == false) return false; - ConvertToSourcelist(CDROM,List); - - // Print the sourcelist entries - cout << "Source List entires for this Disc are:" << endl; - for (vector<string>::iterator I = List.begin(); I != List.end(); I++) - cout << "deb \"cdrom:" << Name << "/\" " << *I << endl; + ReduceSourcelist(CDROM,List); // Write the database and sourcelist if (_config->FindB("APT::cdrom::NoAct",false) == false) { if (WriteDatabase(Database) == false) return false; + + cout << "Writing new source list" << endl; + if (WriteSourceList(Name,List) == false) + return false; } - + + // Print the sourcelist entries + cout << "Source List entires for this Disc are:" << endl; + for (vector<string>::iterator I = List.begin(); I != List.end(); I++) + { + string::size_type Space = (*I).find(' '); + if (Space == string::npos) + return _error->Error("Internal error"); + + cout << "deb \"cdrom:" << Name << "/" << string(*I,0,Space) << + "\" " << string(*I,Space+1) << endl; + } + return true; } /*}}}*/ |