]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/cdrom.cc
fix crash when P.Arch() was used but the cache got remapped
[apt.git] / apt-pkg / cdrom.cc
index 517efa180c4cec3e5b9cafc82b7b4a4043e9832e..0ad1c69e5f520555f0ff874241f352e79547c903 100644 (file)
@@ -6,6 +6,8 @@
 #include<apt-pkg/cdromutl.h>
 #include<apt-pkg/strutl.h>
 #include<apt-pkg/cdrom.h>
+#include<apt-pkg/aptconfiguration.h>
+
 #include<sstream>
 #include<fstream>
 #include<config.h>
@@ -153,14 +155,18 @@ bool pkgCdrom::FindPackages(string CD,
         break;
 
       if (chdir(CD.c_str()) != 0)
-        return _error->Errno("chdir","Unable to change to %s",CD.c_str());
+      {
+        _error->Errno("chdir","Unable to change to %s", CD.c_str());
+        closedir(D);
+        return false;
+      }
    };
 
    closedir(D);
    
    return !_error->PendingError();
 }
-
+                                                                       /*}}}*/
 // Score - We compute a 'score' for a path                             /*{{{*/
 // ---------------------------------------------------------------------
 /* Paths are scored based on how close they come to what I consider
@@ -196,7 +202,7 @@ int pkgCdrom::Score(string Path)
    // a symlink gets a big penalty
    struct stat Buf;
    string statPath = flNotFile(Path);
-   string cdromPath = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
+   string cdromPath = _config->FindDir("Acquire::cdrom::mount");
    while(statPath != cdromPath && statPath != "./") {
       statPath.resize(statPath.size()-1);  // remove the trailing '/'
       if (lstat(statPath.c_str(),&Buf) == 0) {
@@ -210,46 +216,34 @@ int pkgCdrom::Score(string Path)
 
    return Res;
 }
-
                                                                        /*}}}*/
 // DropBinaryArch - Dump dirs with a string like /binary-<foo>/                /*{{{*/
 // ---------------------------------------------------------------------
 /* Here we drop everything that is not this machines arch */
 bool pkgCdrom::DropBinaryArch(vector<string> &List)
 {
-   char S[300];
-   snprintf(S,sizeof(S),"/binary-%s/",
-           _config->Find("Apt::Architecture").c_str());
-   
+
    for (unsigned int I = 0; I < List.size(); I++)
    {
       const char *Str = List[I].c_str();
-      
-      const char *Res;
-      if ((Res = strstr(Str,"/binary-")) == 0)
+      const char *Start, *End;
+      if ((Start = strstr(Str,"/binary-")) == 0)
         continue;
 
-      // Weird, remove it.
-      if (strlen(Res) < strlen(S))
-      {
-        List.erase(List.begin() + I);
-        I--;
-        continue;
-      }
-         
-      // See if it is our arch
-      if (stringcmp(Res,Res + strlen(S),S) == 0)
-        continue;
-      
-      // Erase it
+      // Between Start and End is the architecture
+      Start += 8;
+      if ((End = strstr(Start,"/")) != 0 && Start != End &&
+          APT::Configuration::checkArchitecture(string(Start, End)) == true)
+        continue; // okay, architecture is accepted
+
+      // not accepted -> Erase it
       List.erase(List.begin() + I);
-      I--;
+      --I; // the next entry is at the same index after the erase
    }
    
    return true;
 }
-
-
+                                                                       /*}}}*/
 // DropRepeats - Drop repeated files resulting from symlinks           /*{{{*/
 // ---------------------------------------------------------------------
 /* Here we go and stat every file that we found and strip dup inodes. */
@@ -267,8 +261,10 @@ bool pkgCdrom::DropRepeats(vector<string> &List,const char *Name)
       Inodes[I] = Buf.st_ino;
    }
    
-   if (_error->PendingError() == true)
+   if (_error->PendingError() == true) {
+      delete[] Inodes;
       return false;
+   }
    
    // Look for dups
    for (unsigned int I = 0; I != List.size(); I++)
@@ -291,7 +287,8 @@ bool pkgCdrom::DropRepeats(vector<string> &List,const char *Name)
         List[J] = string();
       }
    }  
+   delete[] Inodes;
+
    // Wipe erased entries
    for (unsigned int I = 0; I < List.size();)
    {
@@ -304,7 +301,6 @@ bool pkgCdrom::DropRepeats(vector<string> &List,const char *Name)
    return true;
 }
                                                                        /*}}}*/
-
 // ReduceSourceList - Takes the path list and reduces it               /*{{{*/
 // ---------------------------------------------------------------------
 /* This takes the list of source list expressed entires and collects
@@ -393,7 +389,7 @@ bool pkgCdrom::WriteDatabase(Configuration &Cnf)
 
    Out.close();
    
-   rename(DFile.c_str(),string(DFile + '~').c_str());
+   link(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());
@@ -513,18 +509,18 @@ bool pkgCdrom::WriteSourceList(string Name,vector<string> &List,bool Source)
    
    return true;
 }
-
-
-bool pkgCdrom::Ident(string &ident, pkgCdromStatus *log)
+                                                                       /*}}}*/
+bool pkgCdrom::Ident(string &ident, pkgCdromStatus *log)               /*{{{*/
 {
    stringstream msg;
 
    // Startup
-   string CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
+   string CDROM = _config->FindDir("Acquire::cdrom::mount");
    if (CDROM[0] == '.')
       CDROM= SafeGetCWD() + '/' + CDROM;
 
-   if(log) {
+   if (log != NULL)
+   {
       msg.str("");
       ioprintf(msg, _("Using CD-ROM mount point %s\nMounting CD-ROM\n"),
                      CDROM.c_str());
@@ -534,7 +530,7 @@ bool pkgCdrom::Ident(string &ident, pkgCdromStatus *log)
       return _error->Error("Failed to mount the cdrom.");
 
    // Hash the CD to get an ID
-   if(log) 
+   if (log != NULL)
       log->Update(_("Identifying.. "));
    
 
@@ -544,10 +540,12 @@ bool pkgCdrom::Ident(string &ident, pkgCdromStatus *log)
       return false;
    }
 
-   msg.str("");
-   ioprintf(msg, "[%s]\n",ident.c_str());
-   log->Update(msg.str());
-
+   if (log != NULL)
+   {
+      msg.str("");
+      ioprintf(msg, "[%s]\n",ident.c_str());
+      log->Update(msg.str());
+   }
 
    // Read the database
    Configuration Database;
@@ -558,7 +556,8 @@ bool pkgCdrom::Ident(string &ident, pkgCdromStatus *log)
         return _error->Error("Unable to read the cdrom database %s",
                              DFile.c_str());
    }
-   if(log) {
+   if (log != NULL)
+   {
       msg.str("");
       ioprintf(msg, _("Stored label: %s\n"),
       Database.Find("CD::"+ident).c_str());
@@ -566,25 +565,27 @@ bool pkgCdrom::Ident(string &ident, pkgCdromStatus *log)
    }
 
    // Unmount and finish
-   if (_config->FindB("APT::CDROM::NoMount",false) == false) {
-      log->Update(_("Unmounting CD-ROM...\n"), STEP_LAST);
+   if (_config->FindB("APT::CDROM::NoMount",false) == false)
+   {
+      if (log != NULL)
+        log->Update(_("Unmounting CD-ROM...\n"), STEP_LAST);
       UnmountCdrom(CDROM);
    }
 
    return true;
 }
-
-
-bool pkgCdrom::Add(pkgCdromStatus *log)
+                                                                       /*}}}*/
+bool pkgCdrom::Add(pkgCdromStatus *log)                                        /*{{{*/
 {
    stringstream msg;
 
    // Startup
-   string CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
+   string CDROM = _config->FindDir("Acquire::cdrom::mount");
    if (CDROM[0] == '.')
       CDROM= SafeGetCWD() + '/' + CDROM;
    
-   if(log) {
+   if(log != NULL)
+   {
       log->SetTotal(STEP_LAST);
       msg.str("");
       ioprintf(msg, _("Using CD-ROM mount point %s\n"), CDROM.c_str());
@@ -604,11 +605,12 @@ bool pkgCdrom::Add(pkgCdromStatus *log)
    // Unmount the CD and get the user to put in the one they want
    if (_config->FindB("APT::CDROM::NoMount",false) == false)
    {
-      if(log)
+      if(log != NULL)
         log->Update(_("Unmounting CD-ROM\n"), STEP_UNMOUNT);
       UnmountCdrom(CDROM);
 
-      if(log) {
+      if(log != NULL)
+      {
         log->Update(_("Waiting for disc...\n"), STEP_WAIT);
         if(!log->ChangeCdrom()) {
            // user aborted
@@ -617,26 +619,29 @@ bool pkgCdrom::Add(pkgCdromStatus *log)
       }
 
       // Mount the new CDROM
-      log->Update(_("Mounting CD-ROM...\n"), STEP_MOUNT);
+      if(log != NULL)
+        log->Update(_("Mounting CD-ROM...\n"), STEP_MOUNT);
+
       if (MountCdrom(CDROM) == false)
         return _error->Error("Failed to mount the cdrom.");
    }
    
    // Hash the CD to get an ID
-   if(log)
+   if(log != NULL)
       log->Update(_("Identifying.. "), STEP_IDENT);
    string ID;
    if (IdentCdrom(CDROM,ID) == false)
    {
-      log->Update("\n");
+      if (log != NULL)
+        log->Update("\n");
       return false;
    }
-   if(log) 
+   if(log != NULL)
+   {
       log->Update("["+ID+"]\n");
-
-   if(log) 
       log->Update(_("Scanning disc for index files..\n"),STEP_SCAN);
-   
+   }
+
    // Get the CD structure
    vector<string> List;
    vector<string> SourceList;
@@ -646,7 +651,8 @@ bool pkgCdrom::Add(pkgCdromStatus *log)
    string InfoDir;
    if (FindPackages(CDROM,List,SourceList, SigList,TransList,InfoDir,log) == false)
    {
-      log->Update("\n");
+      if (log != NULL)
+        log->Update("\n");
       return false;
    }
 
@@ -673,7 +679,7 @@ bool pkgCdrom::Add(pkgCdromStatus *log)
    DropRepeats(SourceList,"Sources");
    DropRepeats(SigList,"Release.gpg");
    DropRepeats(TransList,"");
-   if(log) {
+   if(log != NULL) {
       msg.str("");
       ioprintf(msg, _("Found %zu package indexes, %zu source indexes, "
                      "%zu translation indexes and %zu signatures\n"), 
@@ -710,7 +716,8 @@ bool pkgCdrom::Add(pkgCdromStatus *log)
               if (*J == '"' || *J == ']' || *J == '[')
                  *J = '_';
            
-           if(log) {
+           if(log != NULL)
+           {
               msg.str("");
               ioprintf(msg, _("Found label '%s'\n"), Name.c_str());
               log->Update(msg.str());
@@ -722,7 +729,7 @@ bool pkgCdrom::Add(pkgCdromStatus *log)
       if (_config->FindB("APT::CDROM::Rename",false) == true ||
          Name.empty() == true)
       {
-        if(!log
+        if(log == NULL
          {
            if (_config->FindB("APT::CDROM::NoMount",false) == false) 
               UnmountCdrom(CDROM);
@@ -755,13 +762,13 @@ bool pkgCdrom::Add(pkgCdromStatus *log)
         *J = '_';
    
    Database.Set("CD::" + ID,Name);
-   if(log) {
+   if(log != NULL)
+   {
       msg.str("");
       ioprintf(msg, _("This disc is called: \n'%s'\n"), Name.c_str());
       log->Update(msg.str());
+      log->Update(_("Copying package lists..."), STEP_COPY);
    }
-
-   log->Update(_("Copying package lists..."), STEP_COPY);
    // take care of the signatures and copy them if they are ok
    // (we do this before PackageCopy as it modifies "List" and "SourceList")
    SigVerify SignVerify;
@@ -786,16 +793,15 @@ bool pkgCdrom::Add(pkgCdromStatus *log)
       if (WriteDatabase(Database) == false)
         return false;
       
-      if(log) {
+      if(log != NULL)
         log->Update(_("Writing new source list\n"), STEP_WRITE);
-      }
       if (WriteSourceList(Name,List,false) == false ||
          WriteSourceList(Name,SourceList,true) == false)
         return false;
    }
 
    // Print the sourcelist entries
-   if(log
+   if(log != NULL)
       log->Update(_("Source list entries for this disc are:\n"));
 
    for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
@@ -808,7 +814,8 @@ bool pkgCdrom::Add(pkgCdromStatus *log)
         return _error->Error("Internal error");
       }
 
-      if(log) {
+      if(log != NULL)
+      {
         msg.str("");
         msg << "deb cdrom:[" << Name << "]/" << string(*I,0,Space) << 
            " " << string(*I,Space+1) << endl;
@@ -826,7 +833,7 @@ bool pkgCdrom::Add(pkgCdromStatus *log)
         return _error->Error("Internal error");
       }
 
-      if(log) {
+      if(log != NULL) {
         msg.str("");
         msg << "deb-src cdrom:[" << Name << "]/" << string(*I,0,Space) << 
            " " << string(*I,Space+1) << endl;
@@ -834,27 +841,30 @@ bool pkgCdrom::Add(pkgCdromStatus *log)
       }
    }
 
-   
-
    // Unmount and finish
    if (_config->FindB("APT::CDROM::NoMount",false) == false) {
-      log->Update(_("Unmounting CD-ROM...\n"), STEP_LAST);
+      if (log != NULL)
+        log->Update(_("Unmounting CD-ROM...\n"), STEP_LAST);
       UnmountCdrom(CDROM);
    }
 
    return true;
 }
-
-
-pkgUdevCdromDevices::pkgUdevCdromDevices() 
-  : libudev_handle(NULL)
+                                                                       /*}}}*/
+pkgUdevCdromDevices::pkgUdevCdromDevices()                                     /*{{{*/
+   : libudev_handle(NULL)
 {
 
 }
+                                                                       /*}}}*/
 
 bool
-pkgUdevCdromDevices::Dlopen()
+pkgUdevCdromDevices::Dlopen()                                          /*{{{*/
 {
+   // alread open
+   if(libudev_handle != NULL)
+      return true;
+
    // see if we can get libudev
    void *h = ::dlopen("libudev.so.0", RTLD_LAZY);
    if(h == NULL)
@@ -864,6 +874,7 @@ pkgUdevCdromDevices::Dlopen()
    libudev_handle = h;
    udev_new = (udev* (*)(void)) dlsym(h, "udev_new");
    udev_enumerate_add_match_property = (int (*)(udev_enumerate*, const char*, const char*))dlsym(h, "udev_enumerate_add_match_property");
+   udev_enumerate_add_match_sysattr = (int (*)(udev_enumerate*, const char*, const char*))dlsym(h, "udev_enumerate_add_match_sysattr");
    udev_enumerate_scan_devices = (int (*)(udev_enumerate*))dlsym(h, "udev_enumerate_scan_devices");
    udev_enumerate_get_list_entry = (udev_list_entry* (*)(udev_enumerate*))dlsym(h, "udev_enumerate_get_list_entry");
    udev_device_new_from_syspath = (udev_device* (*)(udev*, const char*))dlsym(h, "udev_device_new_from_syspath");
@@ -876,9 +887,19 @@ pkgUdevCdromDevices::Dlopen()
 
    return true;
 }
-
+                                                                       /*}}}*/
+                                                                        /*{{{*/
+// convenience interface, this will just call ScanForRemovable
 vector<CdromDevice>
 pkgUdevCdromDevices::Scan()
+{ 
+   bool CdromOnly = _config->FindB("APT::cdrom::CdromOnly", true);
+   return ScanForRemovable(CdromOnly); 
+};
+                                                                       /*}}}*/
+                                                                        /*{{{*/
+vector<CdromDevice>
+pkgUdevCdromDevices::ScanForRemovable(bool CdromOnly)
 {
    vector<CdromDevice> cdrom_devices;
    struct udev_enumerate *enumerate;
@@ -890,7 +911,11 @@ pkgUdevCdromDevices::Scan()
 
    udev_ctx = udev_new();
    enumerate = udev_enumerate_new (udev_ctx);
-   udev_enumerate_add_match_property(enumerate, "ID_CDROM", "1");
+   if (CdromOnly)
+      udev_enumerate_add_match_property(enumerate, "ID_CDROM", "1");
+   else {
+      udev_enumerate_add_match_sysattr(enumerate, "removable", "1");
+   }
 
    udev_enumerate_scan_devices (enumerate);
    devices = udev_enumerate_get_list_entry (enumerate);
@@ -902,11 +927,18 @@ pkgUdevCdromDevices::Scan()
       if (udevice == NULL)
         continue;
       const char* devnode = udev_device_get_devnode(udevice);
-      const char* mountpath = udev_device_get_property_value(udevice, "FSTAB_DIR");
+
+      // try fstab_dir first
+      string mountpath;
+      const char* mp = udev_device_get_property_value(udevice, "FSTAB_DIR");
+      if (mp)
+         mountpath = string(mp);
+      else
+         mountpath = FindMountPointForDevice(devnode);
 
       // fill in the struct
       cdrom.DeviceName = string(devnode);
-      if (mountpath) {
+      if (mountpath != "") {
         cdrom.MountPath = mountpath;
         string s = string(mountpath);
         cdrom.Mounted = IsMounted(s);
@@ -918,9 +950,11 @@ pkgUdevCdromDevices::Scan()
    } 
    return cdrom_devices;
 }
+                                                                       /*}}}*/
 
-
-pkgUdevCdromDevices::~pkgUdevCdromDevices()
+pkgUdevCdromDevices::~pkgUdevCdromDevices()                             /*{{{*/
 { 
-   dlclose(libudev_handle);
+   if (libudev_handle != NULL)
+      dlclose(libudev_handle);
 }
+                                                                       /*}}}*/