rework the code in cdromutl.cc to make coverity (more) happy
[ntk/apt.git] / apt-pkg / cdrom.cc
index f5c19a4..a5668a5 100644 (file)
@@ -68,12 +68,11 @@ bool pkgCdrom::FindPackages(string CD,
    if (RealFileExists(".aptignr") == true)
       return true;
 
-
    /* Check _first_ for a signature file as apt-cdrom assumes that all files
       under a Packages/Source file are in control of that file and stops 
       the scanning
    */
-   if (RealFileExists("Release.gpg") == true)
+   if (RealFileExists("Release.gpg") == true || RealFileExists("InRelease") == true)
    {
       SigList.push_back(CD);
    }
@@ -270,6 +269,29 @@ bool pkgCdrom::DropBinaryArch(vector<string> &List)
       --I; // the next entry is at the same index after the erase
    }
    
+   return true;
+}
+                                                                       /*}}}*/
+// DropTranslation - Dump unwanted Translation-<lang> files            /*{{{*/
+// ---------------------------------------------------------------------
+/* Here we drop everything that is not configured in Acquire::Languages */
+bool pkgCdrom::DropTranslation(vector<string> &List)
+{
+   for (unsigned int I = 0; I < List.size(); I++)
+   {
+      const char *Start;
+      if ((Start = strstr(List[I].c_str(), "/Translation-")) == NULL)
+        continue;
+      Start += strlen("/Translation-");
+
+      if (APT::Configuration::checkLanguage(Start, true) == true)
+        continue;
+
+      // not accepted -> Erase it
+      List.erase(List.begin() + I);
+      --I; // the next entry is at the same index after the erase
+   }
+
    return true;
 }
                                                                        /*}}}*/
@@ -278,6 +300,7 @@ bool pkgCdrom::DropBinaryArch(vector<string> &List)
 /* Here we go and stat every file that we found and strip dup inodes. */
 bool pkgCdrom::DropRepeats(vector<string> &List,const char *Name)
 {
+   bool couldFindAllFiles = true;
    // Get a list of all the inodes
    ino_t *Inodes = new ino_t[List.size()];
    for (unsigned int I = 0; I != List.size(); ++I)
@@ -298,21 +321,22 @@ bool pkgCdrom::DropRepeats(vector<string> &List,const char *Name)
       }
 
       if (found == false)
-         _error->Errno("stat","Failed to stat %s%s",List[I].c_str(), Name);
+      {
+        _error->Errno("stat","Failed to stat %s%s",List[I].c_str(), Name);
+        couldFindAllFiles = false;
+        Inodes[I] = 0;
+      }
    }
 
-   if (_error->PendingError() == true) {
-      delete[] Inodes;
-      return false;
-   }
-   
    // Look for dups
    for (unsigned int I = 0; I != List.size(); I++)
    {
+      if (Inodes[I] == 0)
+        continue;
       for (unsigned int J = I+1; J < List.size(); J++)
       {
         // No match
-        if (Inodes[J] != Inodes[I])
+        if (Inodes[J] == 0 || Inodes[J] != Inodes[I])
            continue;
         
         // We score the two paths.. and erase one
@@ -338,7 +362,7 @@ bool pkgCdrom::DropRepeats(vector<string> &List,const char *Name)
         List.erase(List.begin()+I);
    }
    
-   return true;
+   return couldFindAllFiles;
 }
                                                                        /*}}}*/
 // ReduceSourceList - Takes the path list and reduces it               /*{{{*/
@@ -362,6 +386,7 @@ void pkgCdrom::ReduceSourcelist(string CD,vector<string> &List)
 
       string Word1 = string(*I,Space,SSpace-Space);
       string Prefix = string(*I,0,Space);
+      string Component = string(*I,SSpace);
       for (vector<string>::iterator J = List.begin(); J != I; ++J)
       {
         // Find a space..
@@ -376,9 +401,11 @@ void pkgCdrom::ReduceSourcelist(string CD,vector<string> &List)
            continue;
         if (string(*J,Space2,SSpace2-Space2) != Word1)
            continue;
-        
-        *J += string(*I,SSpace);
-        *I = string();
+
+        string Component2 = string(*J, SSpace2) + " ";
+        if (Component2.find(Component + " ") == std::string::npos)
+           *J += Component;
+        I->clear();
       }
    }   
 
@@ -408,28 +435,12 @@ bool pkgCdrom::WriteDatabase(Configuration &Cnf)
    
    /* 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;
-   }   
+   Cnf.Dump(Out, NULL, "%f \"%v\";\n", false);
 
    Out.close();
-   
-   link(DFile.c_str(),string(DFile + '~').c_str());
+
+   if (FileExists(DFile) == true)
+      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());
@@ -696,7 +707,8 @@ bool pkgCdrom::Add(pkgCdromStatus *log)                                     /*{{{*/
       return false;
    }
 
-   chdir(StartDir.c_str());
+   if (chdir(StartDir.c_str()) != 0)
+      return _error->Errno("chdir","Unable to change to %s", StartDir.c_str());
 
    if (_config->FindB("Debug::aptcdrom",false) == true)
    {
@@ -717,8 +729,16 @@ bool pkgCdrom::Add(pkgCdromStatus *log)                                    /*{{{*/
    DropBinaryArch(List);
    DropRepeats(List,"Packages");
    DropRepeats(SourceList,"Sources");
+   // FIXME: We ignore stat() errors here as we usually have only one of those in use
+   // This has little potencial to drop 'valid' stat() errors as we know that one of these
+   // files need to exist, but it would be better if we would check it here
+   _error->PushToStack();
    DropRepeats(SigList,"Release.gpg");
+   DropRepeats(SigList,"InRelease");
+   _error->RevertToStack();
    DropRepeats(TransList,"");
+   if (_config->FindB("APT::CDROM::DropTranslation", true) == true)
+      DropTranslation(TransList);
    if(log != NULL) {
       msg.str("");
       ioprintf(msg, _("Found %zu package indexes, %zu source indexes, "
@@ -809,6 +829,14 @@ bool pkgCdrom::Add(pkgCdromStatus *log)                                    /*{{{*/
       log->Update(msg.str());
       log->Update(_("Copying package lists..."), STEP_COPY);
    }
+
+   // check for existence and possibly create state directory for copying
+   string const listDir = _config->FindDir("Dir::State::lists");
+   string const partialListDir = listDir + "partial/";
+   if (CreateAPTDirectoryIfNeeded(_config->FindDir("Dir::State"), partialListDir) == false &&
+       CreateAPTDirectoryIfNeeded(listDir, partialListDir) == false)
+      return _error->Errno("cdrom", _("List directory %spartial is missing."), listDir.c_str());
+
    // 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;