Join with aliencode
[ntk/apt.git] / cmdline / apt-cdrom.cc
index 1839fda..c43029e 100644 (file)
@@ -1,8 +1,12 @@
 // -*- mode: cpp; mode: fold -*-
 // Description                                                         /*{{{*/
-// $Id: apt-cdrom.cc,v 1.1 1998/11/27 01:52:56 jgg Exp $
+// $Id: apt-cdrom.cc,v 1.36 2001/02/20 07:03:17 jgg Exp $
 /* ######################################################################
    
+   APT CDROM - Tool for handling APT's CDROM database.
+   
+   Currently the only option is 'add' which will take the current CD
+   in the drive and add it into the database.
    
    ##################################################################### */
                                                                        /*}}}*/
 #include <apt-pkg/cmndline.h>
 #include <apt-pkg/error.h>
 #include <apt-pkg/init.h>
-#include <apt-pkg/md5.h>
 #include <apt-pkg/fileutl.h>
 #include <apt-pkg/progress.h>
-#include <apt-pkg/tagfile.h>
-#include <strutl.h>
+#include <apt-pkg/cdromutl.h>
+#include <apt-pkg/strutl.h>
 #include <config.h>
+#include <apti18n.h>
+    
+#include "indexcopy.h"
 
 #include <iostream>
+#include <fstream>
 #include <vector>
 #include <algorithm>
-#include <sys/wait.h>
-#include <sys/errno.h>
-#include <sys/vfs.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <dirent.h>
 #include <stdio.h>
                                                                        /*}}}*/
 
-// UnmountCdrom - Unmount a cdrom                                      /*{{{*/
-// ---------------------------------------------------------------------
-/* */
-bool UnmountCdrom(string Path)
-{
-   int Child = fork();
-   if (Child < -1)
-      return _error->Errno("fork","Failed to fork");
-
-   // The child
-   if (Child == 0)
-   {
-      // Make all the fds /dev/null
-      for (int I = 0; I != 10;)
-        close(I);
-      for (int I = 0; I != 3;)
-        dup2(open("/dev/null",O_RDWR),I);
-      
-      const char *Args[10];
-      Args[0] = "umount";
-      Args[1] = Path.c_str();
-      Args[2] = 0;
-      execvp(Args[0],(char **)Args);      
-      exit(100);
-   }
-
-   // Wait for mount
-   int Status = 0;
-   while (waitpid(Child,&Status,0) != Child)
-   {
-      if (errno == EINTR)
-        continue;
-      return _error->Errno("waitpid","Couldn't wait for subprocess");
-   }
-   
-   // Check for an error code.
-   if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
-      return false;
-   return true;
-}
-                                                                       /*}}}*/
-// MountCdrom - Mount a cdrom                                          /*{{{*/
-// ---------------------------------------------------------------------
-/* We fork mount.. */
-bool MountCdrom(string Path)
-{
-   int Child = fork();
-   if (Child < -1)
-      return _error->Errno("fork","Failed to fork");
-
-   // The child
-   if (Child == 0)
-   {
-      // Make all the fds /dev/null
-      for (int I = 0; I != 10;)
-        close(I);      
-      for (int I = 0; I != 3;)
-        dup2(open("/dev/null",O_RDWR),I);
-      
-      const char *Args[10];
-      Args[0] = "mount";
-      Args[1] = Path.c_str();
-      Args[2] = 0;
-      execvp(Args[0],(char **)Args);      
-      exit(100);
-   }
-
-   // Wait for mount
-   int Status = 0;
-   while (waitpid(Child,&Status,0) != Child)
-   {
-      if (errno == EINTR)
-        continue;
-      return _error->Errno("waitpid","Couldn't wait for subprocess");
-   }
-   
-   // Check for an error code.
-   if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
-      return false;
-   return true;
-}
-                                                                       /*}}}*/
-// IdentCdrom - Generate a unique string for this CD                   /*{{{*/
-// ---------------------------------------------------------------------
-/* We convert everything we hash into a string, this prevents byte size/order
-   from effecting the outcome. */
-bool IdentCdrom(string CD,string &Res)
-{
-   MD5Summation Hash;
-
-   string StartDir = SafeGetCWD();
-   if (chdir(CD.c_str()) != 0)
-      return _error->Errno("chdir","Unable to change to %s",CD.c_str());
-   
-   DIR *D = opendir(".");
-   if (D == 0)
-      return _error->Errno("opendir","Unable to read %s",CD.c_str());
-      
-   // Run over the directory
-   char S[300];
-   for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
-   {
-      // Skip some files..
-      if (strcmp(Dir->d_name,".") == 0 ||
-         strcmp(Dir->d_name,"..") == 0)
-        continue;
-   
-      sprintf(S,"%lu",Dir->d_ino);
-      Hash.Add(S);
-      Hash.Add(Dir->d_name);
-   };
-   
-   chdir(StartDir.c_str());
-   closedir(D);
-   
-   // Some stats from the fsys
-   struct statfs Buf;
-   if (statfs(CD.c_str(),&Buf) != 0)
-      return _error->Errno("statfs","Failed to stat the cdrom");
-   
-   sprintf(S,"%u %u",Buf.f_blocks,Buf.f_bfree);
-   Hash.Add(S);
-   
-   Res = Hash.Result().Value();
-   return true;   
-}
-                                                                       /*}}}*/
-
-// 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.
    This speeds it up greatly as the majority of the size is in the
    binary-* sub dirs. */
-bool FindPackages(string CD,vector<string> &List, int Depth = 0)
+bool FindPackages(string CD,vector<string> &List,vector<string> &SList,
+                 string &InfoDir,unsigned int Depth = 0)
 {
-   if (Depth >= 5)
+   static ino_t Inodes[9];
+   if (Depth >= 7)
       return true;
 
    if (CD[CD.length()-1] != '/')
       CD += '/';   
-   
+
    if (chdir(CD.c_str()) != 0)
       return _error->Errno("chdir","Unable to change to %s",CD.c_str());
 
+   // Look for a .disk subdirectory
+   struct stat Buf;
+   if (stat(".disk",&Buf) == 0)
+   {
+      if (InfoDir.empty() == true)
+        InfoDir = CD + ".disk/";
+   }
+
    /* Aha! We found some package files. We assume that everything under 
       this dir is controlled by those package files so we don't look down
       anymore */
-   struct stat Buf;
-   if (stat("Packages",&Buf) == 0 ||
-       stat("Packages.gz",&Buf) == 0)
+   if (stat("Packages",&Buf) == 0 || stat("Packages.gz",&Buf) == 0)
    {
       List.push_back(CD);
-      return true;
+      
+      // Continue down if thorough is given
+      if (_config->FindB("APT::CDROM::Thorough",false) == false)
+        return true;
    }
-
+   if (stat("Sources.gz",&Buf) == 0 || stat("Sources",&Buf) == 0)
+   {
+      SList.push_back(CD);
+      
+      // Continue down if thorough is given
+      if (_config->FindB("APT::CDROM::Thorough",false) == false)
+        return true;
+   }
+   
    DIR *D = opendir(".");
    if (D == 0)
       return _error->Errno("opendir","Unable to read %s",CD.c_str());
@@ -196,7 +91,8 @@ bool FindPackages(string CD,vector<string> &List, int Depth = 0)
       // Skip some files..
       if (strcmp(Dir->d_name,".") == 0 ||
          strcmp(Dir->d_name,"..") == 0 ||
-         strcmp(Dir->d_name,"source") == 0 ||
+         //strcmp(Dir->d_name,"source") == 0 ||
+         strcmp(Dir->d_name,".disk") == 0 ||
          strcmp(Dir->d_name,"experimental") == 0 ||
          strcmp(Dir->d_name,"binary-all") == 0)
         continue;
@@ -204,20 +100,27 @@ 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;
       
+      unsigned int I;
+      for (I = 0; I != Depth; I++)
+        if (Inodes[I] == Buf.st_ino)
+           break;
+      if (I != Depth)
+        continue;
+      
+      // Store the inodes weve seen
+      Inodes[Depth] = Buf.st_ino;
+
       // Descend
-      if (FindPackages(CD + Dir->d_name,List,Depth+1) == false)
+      if (FindPackages(CD + Dir->d_name,List,SList,InfoDir,Depth+1) == false)
         break;
 
       if (chdir(CD.c_str()) != 0)
-        return _error->Errno("chdir","Unable to change to ",CD.c_str());
+        return _error->Errno("chdir","Unable to change to %s",CD.c_str());
    };
 
    closedir(D);
@@ -225,108 +128,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/";
-      FileFd Target(TargetF + URItoFileName(S),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 */
@@ -372,37 +173,50 @@ int Score(string Path)
 {
    int Res = 0;
    if (Path.find("stable/") != string::npos)
-      Res += 2;
+      Res += 29;
+   if (Path.find("/binary-") != string::npos)
+      Res += 20;
    if (Path.find("frozen/") != string::npos)
-      Res += 2;
+      Res += 28;
+   if (Path.find("unstable/") != string::npos)
+      Res += 27;
    if (Path.find("/dists/") != string::npos)
-      Res += 4;
+      Res += 40;
    if (Path.find("/main/") != string::npos)
-      Res += 2;
+      Res += 20;
    if (Path.find("/contrib/") != string::npos)
-      Res += 2;
+      Res += 20;
    if (Path.find("/non-free/") != string::npos)
-      Res += 2;
+      Res += 20;
    if (Path.find("/non-US/") != string::npos)
-      Res += 2;
+      Res += 20;
+   if (Path.find("/source/") != string::npos)
+      Res += 10;
+   if (Path.find("/debian/") != string::npos)
+      Res -= 10;
    return Res;
 }
                                                                        /*}}}*/
 // DropRepeats - Drop repeated files resulting from symlinks           /*{{{*/
 // ---------------------------------------------------------------------
 /* Here we go and stat every file that we found and strip dup inodes. */
-bool DropRepeats(vector<string> &List)
+bool DropRepeats(vector<string> &List,const char *Name)
 {
    // Get a list of all the inodes
    ino_t *Inodes = new ino_t[List.size()];
    for (unsigned int I = 0; I != List.size(); I++)
    {
       struct stat Buf;
-      if (stat(List[I].c_str(),&Buf) != 0)
-        _error->Errno("stat","Failed to stat %s",List[I].c_str());
+      if (stat((List[I] + Name).c_str(),&Buf) != 0 &&
+         stat((List[I] + Name + ".gz").c_str(),&Buf) != 0)
+        _error->Errno("stat","Failed to stat %s%s",List[I].c_str(),
+                      Name);
       Inodes[I] = Buf.st_ino;
    }
    
+   if (_error->PendingError() == true)
+      return false;
+   
    // Look for dups
    for (unsigned int I = 0; I != List.size(); I++)
    {
@@ -437,57 +251,15 @@ bool DropRepeats(vector<string> &List)
    return true;
 }
                                                                        /*}}}*/
-// ConvertToSourceList - Takes the path list and converts it           /*{{{*/
+
+// ReduceSourceList - Takes the path list and reduces it               /*{{{*/
 // ---------------------------------------------------------------------
-/* 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)
+/* This takes the list of source list expressed entires and collects
+   similar ones to form a single entry for each dist */
+void ReduceSourcelist(string CD,vector<string> &List)
 {
-   char S[300];
-   sprintf(S,"binary-%s",_config->Find("Apt::Architecture").c_str());
-
    sort(List.begin(),List.end());
    
-   // 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());
-   
-      // Too short to be a dists/ type
-      if ((*I).length() < strlen("dists/"))
-        continue;
-   
-      // Not a dists type.
-      if (stringcmp((*I).begin(),(*I).begin()+strlen("dists/"),"dists/") != 0)
-        continue;
-
-      // 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);
-
-      // 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);
-      
-      // Verify the trailing binar - bit
-      Slash2 = (*I).find('/',Slash + 1);
-      if (Slash == string::npos)
-        continue;
-      string Binary = string(*I,Slash+1,Slash2 - Slash-1);
-      
-      if (Binary != S)
-        continue;
-      
-      *I = Dist + ' ' + Comp;
-   }
-   
    // Collect similar entries
    for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
    {
@@ -495,19 +267,28 @@ bool ConvertToSourcelist(string CD,vector<string> &List)
       string::size_type Space = (*I).find(' ');
       if (Space == string::npos)
         continue;
-      
-      string Word1 = string(*I,0,Space);
+      string::size_type SSpace = (*I).find(' ',Space + 1);
+      if (SSpace == string::npos)
+        continue;
+
+      string Word1 = string(*I,Space,SSpace-Space);
+      string Prefix = string(*I,0,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,0,Space2) != Prefix)
+           continue;
+        if (string(*J,Space2,SSpace2-Space2) != Word1)
            continue;
         
-        *J += string(*I,Space);
+        *J += string(*I,SSpace);
         *I = string();
       }
    }   
@@ -522,6 +303,161 @@ 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");
+   string NewFile = DFile + ".new";
+   
+   unlink(NewFile.c_str());
+   ofstream Out(NewFile.c_str());
+   if (!Out)
+      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 */
+   const Configuration::Item *Top = Cnf.Tree(0);
+   for (; Top != 0;)
+   {
+      // Print the config entry
+      if (Top->Value.empty() == false)
+        Out <<  Top->FullTag() + " \"" << Top->Value << "\";" << endl;
+      
+      if (Top->Child != 0)
+      {
+        Top = Top->Child;
+        continue;
+      }
+      
+      while (Top != 0 && Top->Next == 0)
+        Top = Top->Parent;
+      if (Top != 0)
+        Top = Top->Next;
+   }   
+
+   Out.close();
+   
+   rename(DFile.c_str(),string(DFile + '~').c_str());
+   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());
+
+   return true;
+}
+                                                                       /*}}}*/
+// 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,bool Source)
+{
+   if (List.size() == 0)
+      return true;
+
+   string File = _config->FindFile("Dir::Etc::sourcelist");
+
+   // Open the stream for reading
+   ifstream F((FileExists(File)?File.c_str():"/dev/null"),
+             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 + "]/";   
+   string ShortURI2 = "cdrom:" + Name + "/";     // For Compatibility
+
+   const char *Type;
+   if (Source == true)
+      Type = "deb-src";
+   else
+      Type = "deb";
+   
+   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 << Type << " cdrom:[" << Name << "]/" << string(*I,0,Space) <<
+              " " << string(*I,Space+1) << endl;
+        }
+      }
+      First = false;
+      
+      // Grok it
+      string cType;
+      string URI;
+      const char *C = Buffer;
+      if (ParseQuoteWord(C,cType) == false ||
+         ParseQuoteWord(C,URI) == false)
+      {
+        Out << Buffer << endl;
+        continue;
+      }
+
+      // Emit lines like this one
+      if (cType != Type || (string(URI,0,ShortURI.length()) != ShortURI &&
+         string(URI,0,ShortURI.length()) != ShortURI2))
+      {
+        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;
+}
+                                                                       /*}}}*/
 
 // Prompt - Simple prompt                                              /*{{{*/
 // ---------------------------------------------------------------------
@@ -550,13 +486,19 @@ 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
    string CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
-   cout << "Using CD-ROM mount point " << CDROM << endl;
+   if (CDROM[0] == '.')
+      CDROM= SafeGetCWD() + '/' + CDROM;
    
+   cout << "Using CD-ROM mount point " << CDROM << endl;
+      
    // Read the database
    Configuration Database;
    string DFile = _config->FindFile("Dir::State::cdroms");
@@ -572,63 +514,202 @@ bool DoAdd(CommandLine &)
    {
       cout << "Unmounting CD-ROM" << endl;
       UnmountCdrom(CDROM);
-   
+
       // Mount the new CDROM
-      Prompt("Please insert a CD-ROM and press any key");
+      Prompt("Please insert a Disc in the drive and press enter");
       cout << "Mounting CD-ROM" << endl;
       if (MountCdrom(CDROM) == false)
-      {
-        cout << "Failed to mount the cdrom." << endl;
-        return false;
-      }
+        return _error->Error("Failed to mount the cdrom.");
    }
    
    // Hash the CD to get an ID
-   cout << "Indentifying.. " << flush;
+   cout << "Identifying.. " << flush;
    string ID;
    if (IdentCdrom(CDROM,ID) == false)
+   {
+      cout << endl;
       return false;
+   }
+   
    cout << '[' << ID << ']' << endl;
 
    cout << "Scanning Disc for index files..  " << flush;
    // Get the CD structure
    vector<string> List;
+   vector<string> sList;
    string StartDir = SafeGetCWD();
-   if (FindPackages(CDROM,List) == false)
+   string InfoDir;
+   if (FindPackages(CDROM,List,sList,InfoDir) == false)
+   {
+      cout << endl;
       return false;
+   }
+   
    chdir(StartDir.c_str());
+
+   if (_config->FindB("Debug::aptcdrom",false) == true)
+   {
+      cout << "I found (binary):" << endl;
+      for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
+        cout << *I << endl;
+      cout << "I found (source):" << endl;
+      for (vector<string>::iterator I = sList.begin(); I != sList.end(); I++)
+        cout << *I << endl;
+   }   
    
    // Fix up the list
    DropBinaryArch(List);
-   DropRepeats(List);
-   cout << "Found " << List.size() << " package index files." << endl;
+   DropRepeats(List,"Packages");
+   DropRepeats(sList,"Sources");
+   cout << "Found " << List.size() << " package indexes and " << sList.size() << 
+      " source indexes." << endl;
 
-   if (List.size() == 0)
-      return _error->Error("Unable to locate any package files, perhaps this is not a debian CD-ROM");
+   if (List.size() == 0 && sList.size() == 0)
+      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 (InfoDir.empty() == false &&
+         FileExists(InfoDir + "/info") == true)
+      {
+        ifstream F(string(InfoDir + "/info").c_str());
+        if (!F == 0)
+           getline(F,Name);
+
+        if (Name.empty() == false)
+        {
+           // Escape special characters
+           string::iterator J = Name.begin();
+           for (; J != Name.end(); J++)
+              if (*J == '"' || *J == ']' || *J == '[')
+                 *J = '_';
+           
+           cout << "Found label '" << Name << "'" << endl;
+           Database.Set("CD::" + ID + "::Label",Name);
+        }       
+      }
+      
+      if (_config->FindB("APT::CDROM::Rename",false) == true ||
+         Name.empty() == true)
+      {
+        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 &&
+               Name.find('[') == string::npos &&
+               Name.find(']') == string::npos)
+              break;
+           cout << "That is not a valid name, try again " << endl;
+        }       
+      }      
    }
    else
       Name = Database.Find("CD::" + ID);
-   cout << "This Disc is called '" << Name << "'" << endl;
+
+   // Escape special characters
+   string::iterator J = Name.begin();
+   for (; J != Name.end(); J++)
+      if (*J == '"' || *J == ']' || *J == '[')
+        *J = '_';
+   
+   Database.Set("CD::" + ID,Name);
+   cout << "This Disc is called:" << endl << " '" << Name << "'" << endl;
    
    // Copy the package files to the state directory
-   if (CopyPackages(CDROM,Name,List) == false)
+   PackageCopy Copy;
+   SourceCopy SrcCopy;
+   if (Copy.CopyPackages(CDROM,Name,List) == false ||
+       SrcCopy.CopyPackages(CDROM,Name,sList) == false)
       return false;
    
-   ConvertToSourcelist(CDROM,List);
+   ReduceSourcelist(CDROM,List);
+   ReduceSourcelist(CDROM,sList);
+
+   // 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) == false ||
+         WriteSourceList(Name,sList,true) == false)
+        return false;
+   }
 
    // Print the sourcelist entries
-   cout << "Source List entires for this Disc are:" << endl;
+   cout << "Source List entries for this Disc are:" << endl;
    for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
-      cout << "deb \"cdrom:" << Name << "/\" " << *I << endl;
+   {
+      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;
+   }
+
+   for (vector<string>::iterator I = sList.begin(); I != sList.end(); I++)
+   {
+      string::size_type Space = (*I).find(' ');
+      if (Space == string::npos)
+        return _error->Error("Internal error");
+
+      cout << "deb-src cdrom:[" << Name << "]/" << string(*I,0,Space) << 
+        " " << string(*I,Space+1) << endl;
+   }
+
+   cout << "Repeat this process for the rest of the CDs in your set." << endl;
+
+   // Unmount and finish
+   if (_config->FindB("APT::CDROM::NoMount",false) == false)
+      UnmountCdrom(CDROM);
+   
+   return true;
+}
+                                                                       /*}}}*/
+// DoIdent - Ident a CDROM                                             /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+bool DoIdent(CommandLine &)
+{
+   // Startup
+   string CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
+   if (CDROM[0] == '.')
+      CDROM= SafeGetCWD() + '/' + CDROM;
+   
+   cout << "Using CD-ROM mount point " << CDROM << endl;
+   cout << "Mounting CD-ROM" << endl;
+   if (MountCdrom(CDROM) == false)
+      return _error->Error("Failed to mount the cdrom.");
+   
+   // Hash the CD to get an ID
+   cout << "Identifying.. " << flush;
+   string ID;
+   if (IdentCdrom(CDROM,ID) == false)
+   {
+      cout << endl;
+      return false;
+   }
    
+   cout << '[' << ID << ']' << endl;
+
+   // Read the database
+   Configuration Database;
+   string DFile = _config->FindFile("Dir::State::cdroms");
+   if (FileExists(DFile) == true)
+   {
+      if (ReadConfigFile(Database,DFile) == false)
+        return _error->Error("Unable to read the cdrom database %s",
+                             DFile.c_str());
+   }
+   cout << "Stored Label: '" << Database.Find("CD::" + ID) << "'" << endl;
    return true;
 }
                                                                        /*}}}*/
@@ -638,27 +719,33 @@ bool DoAdd(CommandLine &)
 /* */
 int ShowHelp()
 {
-   cout << PACKAGE << ' ' << VERSION << " for " << ARCHITECTURE <<
-       " compiled on " << __DATE__ << "  " << __TIME__ << endl;
-   
-   cout << "Usage: apt-cdrom [options] command" << endl;
-   cout << endl;
-   cout << "apt-cdrom is a tool to add CDROM's to APT's source list. The " << endl;
-   cout << "CDROM mount point and device information is taken from apt.conf" << endl;
-   cout << "and /etc/fstab." << endl;
-   cout << endl;
-   cout << "Commands:" << endl;
-   cout << "   add - Add a CDROM" << endl;
-   cout << endl;
-   cout << "Options:" << endl;
-   cout << "  -h   This help text" << endl;
-   cout << "  -d   CD-ROM mount point" << endl;
-   cout << "  -r   Rename a recognized CD-ROM" << endl;
-   cout << "  -m   No mounting" << endl;
-   cout << "  -c=? Read this configuration file" << endl;
-   cout << "  -o=? Set an arbitary configuration option, ie -o dir::cache=/tmp" << endl;
-   cout << "See fstab(5)" << endl;
-   return 100;
+   ioprintf(cout,_("%s %s for %s %s compiled on %s %s\n"),PACKAGE,VERSION,
+           COMMON_OS,COMMON_CPU,__DATE__,__TIME__);
+   if (_config->FindB("version") == true)
+      return 0;
+   
+   cout << 
+      "Usage: apt-cdrom [options] command\n"
+      "\n"
+      "apt-cdrom is a tool to add CDROM's to APT's source list. The\n"
+      "CDROM mount point and device information is taken from apt.conf\n"
+      "and /etc/fstab.\n"
+      "\n"
+      "Commands:\n"
+      "   add - Add a CDROM\n"
+      "   ident - Report the identity of a CDROM\n"
+      "\n"
+      "Options:\n"
+      "  -h   This help text\n"
+      "  -d   CD-ROM mount point\n"
+      "  -r   Rename a recognized CD-ROM\n"
+      "  -m   No mounting\n"
+      "  -f   Fast mode, don't check package files\n"
+      "  -a   Thorough scan mode\n"
+      "  -c=? Read this configuration file\n"
+      "  -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp\n"
+      "See fstab(5)\n";
+   return 0;
 }
                                                                        /*}}}*/
 
@@ -666,30 +753,41 @@ int main(int argc,const char *argv[])
 {
    CommandLine::Args Args[] = {
       {'h',"help","help",0},
+      {'v',"version","version",0},
       {'d',"cdrom","Acquire::cdrom::mount",CommandLine::HasArg},
       {'r',"rename","APT::CDROM::Rename",0},
       {'m',"no-mount","APT::CDROM::NoMount",0},
       {'f',"fast","APT::CDROM::Fast",0},
+      {'n',"just-print","APT::CDROM::NoAct",0},
+      {'n',"recon","APT::CDROM::NoAct",0},      
+      {'n',"no-act","APT::CDROM::NoAct",0},
+      {'a',"thorough","APT::CDROM::Thorough",0},
       {'c',"config-file",0,CommandLine::ConfigFile},
       {'o',"option",0,CommandLine::ArbItem},
       {0,0,0,0}};
    CommandLine::Dispatch Cmds[] = {
       {"add",&DoAdd},
+      {"ident",&DoIdent},
       {0,0}};
         
    // Parse the command line and initialize the package library
    CommandLine CmdL(Args,_config);
-   if (pkgInitialize(*_config) == false ||
-       CmdL.Parse(argc,argv) == false)
+   if (pkgInitConfig(*_config) == false ||
+       CmdL.Parse(argc,argv) == false ||
+       pkgInitSystem(*_config,_system) == false)
    {
       _error->DumpErrors();
       return 100;
    }
 
    // See if the help should be shown
-   if (_config->FindB("help") == true ||
+   if (_config->FindB("help") == true || _config->FindB("version") == true ||
        CmdL.FileSize() == 0)
       return ShowHelp();
+
+   // Deal with stdout not being a tty
+   if (ttyname(STDOUT_FILENO) == 0 && _config->FindI("quiet",0) < 1)
+      _config->Set("quiet","1");
    
    // Match the operation
    CmdL.DispatchArg(Cmds);