]> git.saurik.com Git - apt.git/blobdiff - cmdline/apt-cdrom.cc
CD swapping support
[apt.git] / cmdline / apt-cdrom.cc
index a4b60e4574c85ef2f0d1081c0d228d4f45e718fc..67c3bc7e40d08fe3cfe58eb62844bbb64c7fe083 100644 (file)
@@ -1,6 +1,6 @@
 // -*- mode: cpp; mode: fold -*-
 // Description                                                         /*{{{*/
-// $Id: apt-cdrom.cc,v 1.11 1998/12/09 00:55:23 jgg Exp $
+// $Id: apt-cdrom.cc,v 1.26 1999/07/03 03:10:35 jgg Exp $
 /* ######################################################################
    
    APT CDROM - Tool for handling APT's CDROM database.
@@ -18,7 +18,7 @@
 #include <apt-pkg/progress.h>
 #include <apt-pkg/tagfile.h>
 #include <apt-pkg/cdromutl.h>
-#include <strutl.h>
+#include <apt-pkg/strutl.h>
 #include <config.h>
 
 #include <iostream>
@@ -38,7 +38,8 @@
    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, unsigned int Depth = 0)
+bool FindPackages(string CD,vector<string> &List,string &InfoDir,
+                 unsigned int Depth = 0)
 {
    static ino_t Inodes[9];
    if (Depth >= 7)
@@ -50,12 +51,18 @@ bool FindPackages(string CD,vector<string> &List, unsigned int Depth = 0)
    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) 
    {
       List.push_back(CD);
       
@@ -63,7 +70,7 @@ bool FindPackages(string CD,vector<string> &List, unsigned int Depth = 0)
       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());
@@ -91,14 +98,14 @@ bool FindPackages(string CD,vector<string> &List, unsigned int Depth = 0)
       for (I = 0; I != Depth; I++)
         if (Inodes[I] == Buf.st_ino)
            break;
-      if (Inodes[I] == Buf.st_ino)
+      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,InfoDir,Depth+1) == false)
         break;
 
       if (chdir(CD.c_str()) != 0)
@@ -156,6 +163,8 @@ int Score(string Path)
    int Res = 0;
    if (Path.find("stable/") != string::npos)
       Res += 2;
+   if (Path.find("/binary-") != string::npos)
+      Res += 2;
    if (Path.find("frozen/") != string::npos)
       Res += 2;
    if (Path.find("/dists/") != string::npos)
@@ -182,10 +191,13 @@ bool DropRepeats(vector<string> &List)
    {
       struct stat Buf;
       if (stat((List[I] + "Packages").c_str(),&Buf) != 0)
-        _error->Errno("stat","Failed to stat %s",List[I].c_str());
+        _error->Errno("stat","Failed to stat %sPackages",List[I].c_str());
       Inodes[I] = Buf.st_ino;
    }
    
+   if (_error->PendingError() == true)
+      return false;
+   
    // Look for dups
    for (unsigned int I = 0; I != List.size(); I++)
    {
@@ -436,6 +448,7 @@ bool CopyPackages(string CDROM,string Name,vector<string> &List)
            File = OrigPath + ChopDirs(File,Chop);
         
         // See if the file exists
+        bool Mangled = false;
         if (NoStat == false || Hits < 10)
         {
            // Attempt to fix broken structure
@@ -444,6 +457,8 @@ bool CopyPackages(string CDROM,string Name,vector<string> &List)
               if (ReconstructPrefix(Prefix,OrigPath,CDROM,File) == false &&
                   ReconstructChop(Chop,*I,File) == false)
               {
+                 if (Debug == true)
+                    clog << "Missed: " << File << endl;
                  NotFound++;
                  continue;
               }
@@ -453,15 +468,34 @@ bool CopyPackages(string CDROM,string Name,vector<string> &List)
            
            // Get the size
            struct stat Buf;
-           if (stat(string(CDROM + Prefix + File).c_str(),&Buf) != 0)
+           if (stat(string(CDROM + Prefix + File).c_str(),&Buf) != 0 || 
+               Buf.st_size == 0)
            {
-              NotFound++;
-              continue;
+              // Attempt to fix busted symlink support for one instance
+              string OrigFile = File;
+              string::size_type Start = File.find("binary-");
+              string::size_type End = File.find("/",Start+3);
+              if (Start != string::npos && End != string::npos)
+              {
+                 File.replace(Start,End-Start,"binary-all");
+                 Mangled = true;
+              }
+              
+              if (Mangled == false ||
+                  stat(string(CDROM + Prefix + File).c_str(),&Buf) != 0)
+              {
+                 if (Debug == true)
+                    clog << "Missed(2): " << OrigFile << endl;
+                 NotFound++;
+                 continue;
+              }               
            }       
                                            
            // Size match
            if ((unsigned)Buf.st_size != Size)
            {
+              if (Debug == true)
+                 clog << "Wrong Size: " << File << endl;
               WrongSize++;
               continue;
            }
@@ -473,7 +507,7 @@ bool CopyPackages(string CDROM,string Name,vector<string> &List)
         // Copy it to the target package file
         const char *Start;
         const char *Stop;
-        if (Chop != 0)
+        if (Chop != 0 || Mangled == true)
         {
            // Mangle the output filename
            const char *Filename;
@@ -494,8 +528,13 @@ bool CopyPackages(string CDROM,string Name,vector<string> &List)
                     return false;
               }
               else
+              {
                  if (Target.Write(Start,Stop-Start) == false)
-                    return false;                
+                    return false;
+                 if (Stop[-1] != '\n')
+                    if (Target.Write("\n",1) == false)
+                       return false;
+              }               
            }
            if (Target.Write("\n",1) == false)
               return false;
@@ -571,7 +610,7 @@ bool CopyPackages(string CDROM,string Name,vector<string> &List)
       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;
+      cout << "Alot of package entries were discarded, perhaps this CD is funny?" << endl;
 
    return true;
 }
@@ -827,13 +866,10 @@ bool DoAdd(CommandLine &)
       UnmountCdrom(CDROM);
 
       // Mount the new CDROM
-      Prompt("Please insert a Disc in the drive 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
@@ -851,7 +887,8 @@ bool DoAdd(CommandLine &)
    // Get the CD structure
    vector<string> List;
    string StartDir = SafeGetCWD();
-   if (FindPackages(CDROM,List) == false)
+   string InfoDir;
+   if (FindPackages(CDROM,List,InfoDir) == false)
    {
       cout << endl;
       return false;
@@ -882,9 +919,10 @@ bool DoAdd(CommandLine &)
        _config->FindB("APT::CDROM::Rename",false) == true)
    {
       // Try to use the CDs label if at all possible
-      if (FileExists(CDROM + "/.disk/info") == true)
+      if (InfoDir.empty() == false &&
+         FileExists(InfoDir + "/info") == true)
       {
-        ifstream F(string(CDROM+ "/.disk/info").c_str());
+        ifstream F(string(InfoDir + "/info").c_str());
         if (!F == 0)
            getline(F,Name);
 
@@ -903,15 +941,22 @@ bool DoAdd(CommandLine &)
         {
            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);
+   
+   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 '" << Name << "'" << endl;
    
@@ -933,7 +978,7 @@ bool DoAdd(CommandLine &)
    }
 
    // 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++)
    {
       string::size_type Space = (*I).find(' ');
@@ -943,7 +988,8 @@ bool DoAdd(CommandLine &)
       cout << "deb \"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;
    return true;
 }
                                                                        /*}}}*/
@@ -955,6 +1001,8 @@ int ShowHelp()
 {
    cout << PACKAGE << ' ' << VERSION << " for " << ARCHITECTURE <<
        " compiled on " << __DATE__ << "  " << __TIME__ << endl;
+   if (_config->FindB("version") == true)
+      return 100;
    
    cout << "Usage: apt-cdrom [options] command" << endl;
    cout << endl;
@@ -973,7 +1021,7 @@ int ShowHelp()
    cout << "  -f   Fast mode, don't check package files" << endl;
    cout << "  -a   Thorough scan mode" << endl;
    cout << "  -c=? Read this configuration file" << endl;
-   cout << "  -o=? Set an arbitary configuration option, ie -o dir::cache=/tmp" << endl;
+   cout << "  -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp" << endl;
    cout << "See fstab(5)" << endl;
    return 100;
 }
@@ -983,6 +1031,7 @@ 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},
@@ -1011,6 +1060,10 @@ int main(int argc,const char *argv[])
    if (_config->FindB("help") == 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);