do not pollute namespace in the headers with using (Closes: #500198)
[ntk/apt.git] / apt-pkg / cdrom.cc
index 93deb49..392cd89 100644 (file)
@@ -1,5 +1,6 @@
 /*
  */
+#include<config.h>
 
 #include<apt-pkg/init.h>
 #include<apt-pkg/error.h>
@@ -10,8 +11,6 @@
 
 #include<sstream>
 #include<fstream>
-#include<config.h>
-#include<apti18n.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <dirent.h>
@@ -22,6 +21,8 @@
 
 #include "indexcopy.h"
 
+#include<apti18n.h>
+
 using namespace std;
 
 // FindPackages - Find the package files on the CDROM                  /*{{{*/
@@ -155,7 +156,11 @@ 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);
@@ -198,7 +203,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) {
@@ -229,7 +234,7 @@ bool pkgCdrom::DropBinaryArch(vector<string> &List)
       // Between Start and End is the architecture
       Start += 8;
       if ((End = strstr(Start,"/")) != 0 && Start != End &&
-          APT::Configuration::checkArchitecture(string(Start, --End)) == true)
+          APT::Configuration::checkArchitecture(string(Start, End)) == true)
         continue; // okay, architecture is accepted
 
       // not accepted -> Erase it
@@ -257,8 +262,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++)
@@ -304,7 +311,7 @@ void pkgCdrom::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++)
+   for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
    {
       // Find a space..
       string::size_type Space = (*I).find(' ');
@@ -316,7 +323,7 @@ void pkgCdrom::ReduceSourcelist(string CD,vector<string> &List)
 
       string Word1 = string(*I,Space,SSpace-Space);
       string Prefix = string(*I,0,Space);
-      for (vector<string>::iterator J = List.begin(); J != I; J++)
+      for (vector<string>::iterator J = List.begin(); J != I; ++J)
       {
         // Find a space..
         string::size_type Space2 = (*J).find(' ');
@@ -383,7 +390,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());
@@ -399,7 +406,7 @@ bool pkgCdrom::WriteDatabase(Configuration &Cnf)
    that were the same. */
 bool pkgCdrom::WriteSourceList(string Name,vector<string> &List,bool Source)
 {
-   if (List.size() == 0)
+   if (List.empty() == true)
       return true;
 
    string File = _config->FindFile("Dir::Etc::sourcelist");
@@ -449,7 +456,7 @@ bool pkgCdrom::WriteSourceList(string Name,vector<string> &List,bool Source)
 
       if (First == true)
       {
-        for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
+        for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
         {
            string::size_type Space = (*I).find(' ');
            if (Space == string::npos)
@@ -483,7 +490,7 @@ bool pkgCdrom::WriteSourceList(string Name,vector<string> &List,bool Source)
    // Just in case the file was empty
    if (First == true)
    {
-      for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
+      for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
       {
         string::size_type Space = (*I).find(' ');
         if (Space == string::npos)
@@ -509,11 +516,12 @@ 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());
@@ -523,7 +531,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.. "));
    
 
@@ -533,10 +541,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;
@@ -547,7 +557,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());
@@ -555,8 +566,10 @@ 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);
    }
 
@@ -568,11 +581,12 @@ 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());
@@ -592,11 +606,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
@@ -605,26 +620,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;
@@ -634,7 +652,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;
    }
 
@@ -643,13 +662,13 @@ bool pkgCdrom::Add(pkgCdromStatus *log)                                   /*{{{*/
    if (_config->FindB("Debug::aptcdrom",false) == true)
    {
       cout << "I found (binary):" << endl;
-      for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
+      for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
         cout << *I << endl;
       cout << "I found (source):" << endl;
-      for (vector<string>::iterator I = SourceList.begin(); I != SourceList.end(); I++)
+      for (vector<string>::iterator I = SourceList.begin(); I != SourceList.end(); ++I)
         cout << *I << endl;
       cout << "I found (Signatures):" << endl;
-      for (vector<string>::iterator I = SigList.begin(); I != SigList.end(); I++)
+      for (vector<string>::iterator I = SigList.begin(); I != SigList.end(); ++I)
         cout << *I << endl;
    }   
 
@@ -661,7 +680,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"), 
@@ -670,7 +689,7 @@ bool pkgCdrom::Add(pkgCdromStatus *log)                                     /*{{{*/
       log->Update(msg.str(), STEP_SCAN);
    }
 
-   if (List.size() == 0 && SourceList.size() == 0
+   if (List.empty() == true && SourceList.empty() == true
    {
       if (_config->FindB("APT::CDROM::NoMount",false) == false) 
         UnmountCdrom(CDROM);
@@ -694,11 +713,12 @@ bool pkgCdrom::Add(pkgCdromStatus *log)                                   /*{{{*/
         {
            // Escape special characters
            string::iterator J = Name.begin();
-           for (; J != Name.end(); J++)
+           for (; J != Name.end(); ++J)
               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());
@@ -710,7 +730,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);
@@ -738,18 +758,18 @@ bool pkgCdrom::Add(pkgCdromStatus *log)                                   /*{{{*/
 
    // Escape special characters
    string::iterator J = Name.begin();
-   for (; J != Name.end(); J++)
+   for (; J != Name.end(); ++J)
       if (*J == '"' || *J == ']' || *J == '[')
         *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;
@@ -774,19 +794,18 @@ 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++)
+   for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
    {
       string::size_type Space = (*I).find(' ');
       if (Space == string::npos)
@@ -796,7 +815,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;
@@ -804,7 +824,7 @@ bool pkgCdrom::Add(pkgCdromStatus *log)                                     /*{{{*/
       }
    }
 
-   for (vector<string>::iterator I = SourceList.begin(); I != SourceList.end(); I++)
+   for (vector<string>::iterator I = SourceList.begin(); I != SourceList.end(); ++I)
    {
       string::size_type Space = (*I).find(' ');
       if (Space == string::npos)
@@ -814,7 +834,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;
@@ -824,7 +844,8 @@ 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);
    }
 
@@ -854,6 +875,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");
@@ -867,8 +889,18 @@ pkgUdevCdromDevices::Dlopen()                                      /*{{{*/
    return true;
 }
                                                                        /*}}}*/
+                                                                        /*{{{*/
+// convenience interface, this will just call ScanForRemovable
 vector<CdromDevice>
-pkgUdevCdromDevices::Scan()                                             /*{{{*/
+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;
@@ -880,7 +912,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);
@@ -892,11 +928,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);