]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/deb/dpkgpm.cc
* apt-pkg/deb/dpkgpm.cc:
[apt.git] / apt-pkg / deb / dpkgpm.cc
index fe8fbca748b5fc05b63bda2be3ff86881983a58a..4b4bba696387aaa85928bdbe9d06a3c4015f48f2 100644 (file)
@@ -8,13 +8,11 @@
    ##################################################################### */
                                                                        /*}}}*/
 // Includes                                                            /*{{{*/
    ##################################################################### */
                                                                        /*}}}*/
 // Includes                                                            /*{{{*/
-#ifdef __GNUG__
-#pragma implementation "apt-pkg/dpkgpm.h"
-#endif
 #include <apt-pkg/dpkgpm.h>
 #include <apt-pkg/error.h>
 #include <apt-pkg/configuration.h>
 #include <apt-pkg/depcache.h>
 #include <apt-pkg/dpkgpm.h>
 #include <apt-pkg/error.h>
 #include <apt-pkg/configuration.h>
 #include <apt-pkg/depcache.h>
+#include <apt-pkg/pkgrecords.h>
 #include <apt-pkg/strutl.h>
 
 #include <unistd.h>
 #include <apt-pkg/strutl.h>
 
 #include <unistd.h>
@@ -37,7 +35,8 @@ using namespace std;
 // DPkgPM::pkgDPkgPM - Constructor                                     /*{{{*/
 // ---------------------------------------------------------------------
 /* */
 // DPkgPM::pkgDPkgPM - Constructor                                     /*{{{*/
 // ---------------------------------------------------------------------
 /* */
-pkgDPkgPM::pkgDPkgPM(pkgDepCache *Cache) : pkgPackageManager(Cache)
+pkgDPkgPM::pkgDPkgPM(pkgDepCache *Cache) 
+   : pkgPackageManager(Cache), pkgFailures(0)
 {
 }
                                                                        /*}}}*/
 {
 }
                                                                        /*}}}*/
@@ -355,28 +354,28 @@ bool pkgDPkgPM::Go(int OutStatusFd)
    static const struct DpkgState DpkgStatesOpMap[][5] = {
       // Install operation
       { 
    static const struct DpkgState DpkgStatesOpMap[][5] = {
       // Install operation
       { 
-        {"half-installed", _("Preparing %s")}, 
-        {"unpacked", _("Unpacking %s") }, 
+        {"half-installed", N_("Preparing %s")}, 
+        {"unpacked", N_("Unpacking %s") }, 
         {NULL, NULL}
       },
       // Configure operation
       { 
         {NULL, NULL}
       },
       // Configure operation
       { 
-        {"unpacked",_("Preparing to configure %s") },
-        {"half-configured", _("Configuring %s") },
-        { "installed", _("Installed %s")},
+        {"unpacked",N_("Preparing to configure %s") },
+        {"half-configured", N_("Configuring %s") },
+        { "installed", N_("Installed %s")},
         {NULL, NULL}
       },
       // Remove operation
       { 
         {NULL, NULL}
       },
       // Remove operation
       { 
-        {"half-configured", _("Preparing for removal of %s")},
-        {"half-installed", _("Removing %s")},
-        {"config-files",  _("Removed %s")},
+        {"half-configured", N_("Preparing for removal of %s")},
+        {"half-installed", N_("Removing %s")},
+        {"config-files",  N_("Removed %s")},
         {NULL, NULL}
       },
       // Purge operation
       { 
         {NULL, NULL}
       },
       // Purge operation
       { 
-        {"config-files", _("Preparing for remove with config %s")},
-        {"not-installed", _("Removed with config %s")},
+        {"config-files", N_("Preparing to completely remove %s")},
+        {"not-installed", N_("Completely removed %s")},
         {NULL, NULL}
       },
    };
         {NULL, NULL}
       },
    };
@@ -474,6 +473,8 @@ bool pkgDPkgPM::Go(int OutStatusFd)
         case Item::Install:
         Args[n++] = "--unpack";
         Size += strlen(Args[n-1]);
         case Item::Install:
         Args[n++] = "--unpack";
         Size += strlen(Args[n-1]);
+        Args[n++] = "--auto-deconfigure";
+        Size += strlen(Args[n-1]);
         break;
       }
       
         break;
       }
       
@@ -623,10 +624,23 @@ bool pkgDPkgPM::Go(int OutStatusFd)
            'status: conffile-prompt: conffile : 'current-conffile' 'new-conffile' useredited distedited
            
         */
            'status: conffile-prompt: conffile : 'current-conffile' 'new-conffile' useredited distedited
            
         */
-        char* list[4];
-        TokSplitString(':', line, list, 5);
+        char* list[5];
+        //        dpkg sends multiline error messages sometimes (see
+        //        #374195 for a example. we should support this by
+        //        either patching dpkg to not send multiline over the
+        //        statusfd or by rewriting the code here to deal with
+        //        it. for now we just ignore it and not crash
+        TokSplitString(':', line, list, sizeof(list)/sizeof(list[0]));
         char *pkg = list[1];
         char *action = _strstrip(list[2]);
         char *pkg = list[1];
         char *action = _strstrip(list[2]);
+        if( pkg == NULL || action == NULL) 
+        {
+           if (_config->FindB("Debug::pkgDPkgProgressReporting",false) == true)
+              std::clog << "ignoring line: not enough ':'" << std::endl;
+           // reset the line buffer
+           line[0]=0;
+           continue;
+        }
 
         if(strncmp(action,"error",strlen("error")) == 0)
         {
 
         if(strncmp(action,"error",strlen("error")) == 0)
         {
@@ -639,6 +653,8 @@ bool pkgDPkgPM::Go(int OutStatusFd)
            line[0]=0;
            if (_config->FindB("Debug::pkgDPkgProgressReporting",false) == true)
               std::clog << "send: '" << status.str() << "'" << endl;
            line[0]=0;
            if (_config->FindB("Debug::pkgDPkgProgressReporting",false) == true)
               std::clog << "send: '" << status.str() << "'" << endl;
+           pkgFailures++;
+           WriteApportReport(list[1], list[3]);
            continue;
         }
         if(strncmp(action,"conffile",strlen("conffile")) == 0)
            continue;
         }
         if(strncmp(action,"conffile",strlen("conffile")) == 0)
@@ -664,7 +680,7 @@ bool pkgDPkgPM::Go(int OutStatusFd)
         {
            // only read the translation if there is actually a next
            // action
         {
            // only read the translation if there is actually a next
            // action
-           const char *translation = states[PackageOpsDone[pkg]].str;
+           const char *translation = _(states[PackageOpsDone[pkg]].str);
            char s[200];
            snprintf(s, sizeof(s), translation, pkg);
 
            char s[200];
            snprintf(s, sizeof(s), translation, pkg);
 
@@ -698,14 +714,23 @@ bool pkgDPkgPM::Go(int OutStatusFd)
       // Check for an error code.
       if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
       {
       // Check for an error code.
       if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
       {
-        RunScripts("DPkg::Post-Invoke");
-        if (WIFSIGNALED(Status) != 0 && WTERMSIG(Status) == SIGSEGV)
-           return _error->Error("Sub-process %s received a segmentation fault.",Args[0]);
-
-        if (WIFEXITED(Status) != 0)
-           return _error->Error("Sub-process %s returned an error code (%u)",Args[0],WEXITSTATUS(Status));
+        // if it was set to "keep-dpkg-runing" then we won't return
+        // here but keep the loop going and just report it as a error
+        // for later
+        bool stopOnError = _config->FindB("Dpkg::StopOnError",true);
         
         
-        return _error->Error("Sub-process %s exited unexpectedly",Args[0]);
+        if(stopOnError)
+           RunScripts("DPkg::Post-Invoke");
+
+        if (WIFSIGNALED(Status) != 0 && WTERMSIG(Status) == SIGSEGV) 
+           _error->Error("Sub-process %s received a segmentation fault.",Args[0]);
+        else if (WIFEXITED(Status) != 0)
+           _error->Error("Sub-process %s returned an error code (%u)",Args[0],WEXITSTATUS(Status));
+        else 
+           _error->Error("Sub-process %s exited unexpectedly",Args[0]);
+
+        if(stopOnError)
+           return false;
       }      
    }
 
       }      
    }
 
@@ -722,3 +747,94 @@ void pkgDPkgPM::Reset()
    List.erase(List.begin(),List.end());
 }
                                                                        /*}}}*/
    List.erase(List.begin(),List.end());
 }
                                                                        /*}}}*/
+// pkgDpkgPM::WriteApportReport - write out error report pkg failure   /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+void pkgDPkgPM::WriteApportReport(const char *pkgpath, const char *errormsg) 
+{
+   string pkgname, reportfile, srcpkgname, pkgver, arch;
+   string::size_type pos;
+   FILE *report;
+
+   if (_config->FindB("Dpkg::ApportFailureReport",true) == false)
+      return;
+
+   // only report the first error if we are in StopOnError=false mode
+   // to prevent bogus reports
+   if((_config->FindB("Dpkg::StopOnError",true) == false) && pkgFailures > 1)
+      return;
+
+   // get the pkgname and reportfile
+   pkgname = flNotDir(pkgpath);
+   pos = pkgname.rfind('_');
+   if(pos != string::npos)
+      pkgname = string(pkgname, 0, pos);
+
+   // find the package versin and source package name
+   pkgCache::PkgIterator Pkg = Cache.FindPkg(pkgname);
+   if (Pkg.end() == true)
+      return;
+   pkgCache::VerIterator Ver = Cache.GetCandidateVer(Pkg);
+   pkgver = Ver.VerStr();
+   if (Ver.end() == true)
+      return;
+   pkgRecords Recs(Cache);
+   pkgRecords::Parser &Parse = Recs.Lookup(Ver.FileList());
+   srcpkgname = Parse.SourcePkg();
+   if(srcpkgname.empty())
+      srcpkgname = pkgname;
+
+   // if the file exists already, we check:
+   // - if it was reported already (touched by apport). 
+   //   If not, we do nothing, otherwise
+   //    we overwrite it. This is the same behaviour as apport
+   // - if we have a report with the same pkgversion already
+   //   then we skip it
+   reportfile = flCombine("/var/crash",pkgname+".0.crash");
+   if(FileExists(reportfile))
+   {
+      struct stat buf;
+      char strbuf[255];
+
+      // check atime/mtime
+      stat(reportfile.c_str(), &buf);
+      if(buf.st_mtime > buf.st_atime)
+        return;
+
+      // check if the existing report is the same version
+      report = fopen(reportfile.c_str(),"r");
+      while(fgets(strbuf, sizeof(strbuf), report) != NULL)
+      {
+        if(strstr(strbuf,"Package:") == strbuf)
+        {
+           char pkgname[255], version[255];
+           if(sscanf(strbuf, "Package: %s %s", pkgname, version) == 2)
+              if(strcmp(pkgver.c_str(), version) == 0)
+              {
+                 fclose(report);
+                 return;
+              }
+        }
+      }
+      fclose(report);
+   }
+
+   // now write the report
+   arch = _config->Find("APT::Architecture");
+   report = fopen(reportfile.c_str(),"w");
+   if(report == NULL)
+      return;
+   if(_config->FindB("DPkgPM::InitialReportOnly",false) == true)
+      chmod(reportfile.c_str(), 0);
+   else
+      chmod(reportfile.c_str(), 0600);
+   fprintf(report, "ProblemType: Package\n");
+   fprintf(report, "Architecture: %s\n", arch.c_str());
+   time_t now = time(NULL);
+   fprintf(report, "Date: %s" , ctime(&now));
+   fprintf(report, "Package: %s %s\n", pkgname.c_str(), pkgver.c_str());
+   fprintf(report, "SourcePackage: %s\n", srcpkgname.c_str());
+   fprintf(report, "ErrorMessage:\n %s\n", errormsg);
+   fclose(report);
+}
+                                                                       /*}}}*/