]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/deb/dpkgpm.cc
reshuffle the pkgDPkgPM::ProcessDpkgStatusLine() some more to make it more robust
[apt.git] / apt-pkg / deb / dpkgpm.cc
index a5097a04fcef8b50799b8125db9d1b4f7f2c8c84..b5d3141653a25bc17af6f68bd6beefe7bcf6a854 100644 (file)
@@ -54,9 +54,15 @@ class pkgDPkgPMPrivate
 public:
    pkgDPkgPMPrivate() : stdin_is_dev_null(false), dpkgbuf_pos(0),
                        term_out(NULL), history_out(NULL), 
-                        last_reported_progress(0.0)
+                        last_reported_progress(0.0), nr_terminal_rows(0),
+                        fancy_progress_output(false)
    {
       dpkgbuf[0] = '\0';
+      if(_config->FindB("Dpkg::Progress-Fancy", false) == true)
+      {
+         fancy_progress_output = true;
+         _config->Set("DpkgPM::Progress", true);
+      }
    }
    bool stdin_is_dev_null;
    // the buffer we use for the dpkg status-fd reading
@@ -67,6 +73,8 @@ public:
    string dpkg_error;
 
    float last_reported_progress;
+   int nr_terminal_rows;
+   bool fancy_progress_output;
 };
 
 namespace
@@ -508,56 +516,144 @@ void pkgDPkgPM::DoTerminalPty(int master)
 void pkgDPkgPM::ProcessDpkgStatusLine(int OutStatusFd, char *line)
 {
    bool const Debug = _config->FindB("Debug::pkgDPkgProgressReporting",false);
-   // the status we output
-   ostringstream status;
-
    if (Debug == true)
       std::clog << "got from dpkg '" << line << "'" << std::endl;
 
-
    /* dpkg sends strings like this:
       'status:   <pkg>: <pkg  qstate>'
       'status:   <pkg>:<arch>: <pkg  qstate>'
-      errors look like this:
-      'status: /var/cache/apt/archives/krecipes_0.8.1-0ubuntu1_i386.deb : error : trying to overwrite `/usr/share/doc/kde/HTML/en/krecipes/krectip.png', which is also in package krecipes-data 
-      and conffile-prompt like this
-      'status: conffile-prompt: conffile : 'current-conffile' 'new-conffile' useredited distedited
       
-      Newer versions of dpkg sent also:
-      'processing: install: pkg'
-      'processing: configure: pkg'
-      'processing: remove: pkg'
-      'processing: purge: pkg'
-      'processing: disappear: pkg'
-      'processing: trigproc: trigger'
-           
+      'processing: {install,configure,remove,purge,disappear,trigproc}: pkg'
+      'processing: {install,configure,remove,purge,disappear,trigproc}: trigger'
    */
+
    // we need to split on ": " (note the appended space) as the ':' is
    // part of the pkgname:arch information that dpkg sends
    // 
    // A dpkg error message may contain additional ":" (like
    //  "failed in buffer_write(fd) (10, ret=-1): backend dpkg-deb ..."
    // so we need to ensure to not split too much
-   std::vector<std::string> list = StringSplit(line, ": ", 3);
-   if(list.size() != 3)
+   std::vector<std::string> list = StringSplit(line, ": ", 4);
+   if(list.size() < 3)
    {
       if (Debug == true)
         std::clog << "ignoring line: not enough ':'" << std::endl;
       return;
    }
+
+   // build the (prefix, pkgname, action) tuple, position of this
+   // is different for "processing" or "status" messages
+   std::string prefix = APT::String::Strip(list[0]);
+   std::string pkgname;
+   std::string action_str;
+   ostringstream status;
+
+   // "processing" has the form "processing: action: pkg or trigger"
+   // with action = ["install", "configure", "remove", "purge", "disappear",
+   //                "trigproc"]
+   if (prefix == "processing")
+   {
+      pkgname = APT::String::Strip(list[2]);
+      action_str = APT::String::Strip(list[1]);
+
+      // this is what we support in the processing stage
+      if(action_str != "install" && action_str != "configure" &&
+         action_str != "remove" && action_str != "purge" && 
+         action_str != "purge")
+      {
+         if (Debug == true)
+            std::clog << "ignoring processing action: '" << action_str
+                      << "'" << std::endl;
+         return;
+      }
+   }
+   // "status" has the form: "status: pkg: state"
+   // with state in ["half-installed", "unpacked", "half-configured", 
+   //                "installed", "config-files", "not-installed"]
+   else if (prefix == "status")
+   {
+      pkgname = APT::String::Strip(list[1]);
+      action_str = APT::String::Strip(list[2]);
+   } else {
+      if (Debug == true)
+        std::clog << "unknown prefix '" << prefix << "'" << std::endl;
+      return;
+   }
+
+
+   /* handle the special cases first:
+
+      errors look like this:
+      'status: /var/cache/apt/archives/krecipes_0.8.1-0ubuntu1_i386.deb : error : trying to overwrite `/usr/share/doc/kde/HTML/en/krecipes/krectip.png', which is also in package krecipes-data 
+      and conffile-prompt like this
+      'status: conffile-prompt: conffile : 'current-conffile' 'new-conffile' useredited distedited
+   */
+   if (prefix == "status")
+   {
+      if(action_str == "error")
+      {
+         status << "pmerror:" << list[1]
+                << ":"  << (PackagesDone/float(PackagesTotal)*100.0) 
+                << ":" << list[3]
+                << endl;
+         if(OutStatusFd > 0)
+            FileFd::Write(OutStatusFd, status.str().c_str(), status.str().size());
+         if (Debug == true)
+            std::clog << "send: '" << status.str() << "'" << endl;
+         pkgFailures++;
+         WriteApportReport(list[1].c_str(), list[3].c_str());
+         return;
+      }
+      else if(action_str == "conffile")
+      {
+         status << "pmconffile:" << list[1]
+                << ":"  << (PackagesDone/float(PackagesTotal)*100.0) 
+                << ":" << list[3]
+                << endl;
+         if(OutStatusFd > 0)
+            FileFd::Write(OutStatusFd, status.str().c_str(), status.str().size());
+         if (Debug == true)
+            std::clog << "send: '" << status.str() << "'" << endl;
+         return;
+      }
+   }
+
+   // at this point we know that we should have a valid pkgname, so build all 
+   // the info from it
+
    // dpkg does not send always send "pkgname:arch" so we add it here if needed
-   std::string pkgname = list[1];
    if (pkgname.find(":") == std::string::npos)
    {
-      string const nativeArch = _config->Find("APT::Architecture");
-      pkgname = pkgname + ":" + nativeArch;
+      // find the package in the group that is in a touched by dpkg
+      // if there are multiple dpkg will send us a full pkgname:arch
+      pkgCache::GrpIterator Grp = Cache.FindGrp(pkgname);
+      if (Grp.end() == false) 
+      {
+          pkgCache::PkgIterator P = Grp.PackageList();
+          for (; P.end() != true; P = Grp.NextPkg(P))
+          {
+              if(Cache[P].Mode != pkgDepCache::ModeKeep)
+              {
+                  pkgname = P.FullName();
+                  break;
+              }
+          }
+      }
    }
+   
    const char* const pkg = pkgname.c_str();
-   const char* action = list[2].c_str();
+   const char* action = action_str.c_str();
+   std::string short_pkgname = StringSplit(pkgname, ":")[0];
+   std::string arch = "";
+   if (pkgname.find(":") != string::npos)
+      arch = StringSplit(pkgname, ":")[1];
+   std::string i18n_pkgname = pkgname;
+   if (arch.size() != 0)
+      strprintf(i18n_pkgname, "%s (%s)", short_pkgname.c_str(), arch.c_str());
 
    // 'processing' from dpkg looks like
    // 'processing: action: pkg'
-   if(strncmp(list[0].c_str(), "processing", strlen("processing")) == 0)
+   if(prefix == "processing")
    {
       char s[200];
       const char* const pkg_or_trigger = list[2].c_str();
@@ -586,67 +682,43 @@ void pkgDPkgPM::ProcessDpkgStatusLine(int OutStatusFd, char *line)
       if (strncmp(action, "disappear", strlen("disappear")) == 0)
         handleDisappearAction(pkg_or_trigger);
       return;
-   }
+   } 
 
-   if(strncmp(action,"error",strlen("error")) == 0)
+   if (prefix == "status")
    {
-      status << "pmerror:" << list[1]
-            << ":"  << (PackagesDone/float(PackagesTotal)*100.0) 
-            << ":" << list[3]
-            << endl;
-      if(OutStatusFd > 0)
-        FileFd::Write(OutStatusFd, status.str().c_str(), status.str().size());
-      if (Debug == true)
-        std::clog << "send: '" << status.str() << "'" << endl;
-      pkgFailures++;
-      WriteApportReport(list[1].c_str(), list[3].c_str());
-      return;
-   }
-   else if(strncmp(action,"conffile",strlen("conffile")) == 0)
-   {
-      status << "pmconffile:" << list[1]
-            << ":"  << (PackagesDone/float(PackagesTotal)*100.0) 
-            << ":" << list[3]
-            << endl;
-      if(OutStatusFd > 0)
-        FileFd::Write(OutStatusFd, status.str().c_str(), status.str().size());
-      if (Debug == true)
-        std::clog << "send: '" << status.str() << "'" << endl;
-      return;
-   }
-
-   vector<struct DpkgState> const &states = PackageOps[pkg];
-   const char *next_action = NULL;
-   if(PackageOpsDone[pkg] < states.size())
-      next_action = states[PackageOpsDone[pkg]].state;
-   // check if the package moved to the next dpkg state
-   if(next_action && (strcmp(action, next_action) == 0)) 
-   {
-      // only read the translation if there is actually a next
-      // action
-      const char *translation = _(states[PackageOpsDone[pkg]].str);
-      char s[200];
-      snprintf(s, sizeof(s), translation, pkg);
-
-      // we moved from one dpkg state to a new one, report that
-      PackageOpsDone[pkg]++;
-      PackagesDone++;
-      // build the status str
-      status << "pmstatus:" << pkg 
-            << ":"  << (PackagesDone/float(PackagesTotal)*100.0) 
-            << ":" << s
-            << endl;
-      if(_config->FindB("DPkgPM::Progress", false) == true)
-         SendTerminalProgress(PackagesDone/float(PackagesTotal)*100.0);
-
-      if(OutStatusFd > 0)
-        FileFd::Write(OutStatusFd, status.str().c_str(), status.str().size());
-      if (Debug == true)
-        std::clog << "send: '" << status.str() << "'" << endl;
+      vector<struct DpkgState> const &states = PackageOps[pkg];
+      const char *next_action = NULL;
+      if(PackageOpsDone[pkg] < states.size())
+         next_action = states[PackageOpsDone[pkg]].state;
+      // check if the package moved to the next dpkg state
+      if(next_action && (strcmp(action, next_action) == 0)) 
+      {
+         // only read the translation if there is actually a next
+         // action
+         const char *translation = _(states[PackageOpsDone[pkg]].str);
+         char s[200];
+         snprintf(s, sizeof(s), translation, short_pkgname.c_str());
+         
+         // we moved from one dpkg state to a new one, report that
+         PackageOpsDone[pkg]++;
+         PackagesDone++;
+         // build the status str
+         status << "pmstatus:" << short_pkgname
+                << ":"  << (PackagesDone/float(PackagesTotal)*100.0) 
+                << ":" << s
+                << endl;
+         if(_config->FindB("DPkgPM::Progress", false) == true)
+            SendTerminalProgress(PackagesDone/float(PackagesTotal)*100.0);
+         
+         if(OutStatusFd > 0)
+            FileFd::Write(OutStatusFd, status.str().c_str(), status.str().size());
+         if (Debug == true)
+            std::clog << "send: '" << status.str() << "'" << endl;
+      }
+      if (Debug == true) 
+         std::clog << "(parsed from dpkg) pkg: " << short_pkgname
+                   << " action: " << action << endl;
    }
-   if (Debug == true) 
-      std::clog << "(parsed from dpkg) pkg: " << pkg 
-               << " action: " << action << endl;
 }
                                                                        /*}}}*/
 // DPkgPM::handleDisappearAction                                       /*{{{*/
@@ -892,10 +964,37 @@ void pkgDPkgPM::SendTerminalProgress(float percentage)
    if(percentage < (d->last_reported_progress + reporting_steps))
       return;
 
-   // FIXME: use colors too
-   std::cout << "\r\n"
-             << "Progress: [" << std::setw(3) << int(percentage) << "%]"
-             << "\r\n";
+   std::string progress_str;
+   strprintf(progress_str, _("Progress: [%3i%%]"), (int)percentage);
+   if (d->fancy_progress_output)
+   {
+         int row = d->nr_terminal_rows;
+
+         static string save_cursor = "\033[s";
+         static string restore_cursor = "\033[u";
+
+         static string set_bg_color = "\033[42m"; // green
+         static string set_fg_color = "\033[30m"; // black
+
+         static string restore_bg =  "\033[49m";
+         static string restore_fg = "\033[39m";
+
+         std::cout << save_cursor
+            // move cursor position to last row
+                   << "\033[" << row << ";0f" 
+                   << set_bg_color
+                   << set_fg_color
+                   << progress_str
+                   << restore_cursor
+                   << restore_bg
+                   << restore_fg;
+   }
+   else
+   {
+         std::cout << progress_str << "\r\n";
+   }
+   std::flush(std::cout);
+                   
    d->last_reported_progress = percentage;
 }
                                                                        /*}}}*/
@@ -920,6 +1019,43 @@ static int racy_pselect(int nfds, fd_set *readfds, fd_set *writefds,
    return retval;
 }
 /*}}}*/
+
+void pkgDPkgPM::SetupTerminalScrollArea(int nr_rows)
+{
+     if(!d->fancy_progress_output)
+        return;
+
+     // scroll down a bit to avoid visual glitch when the screen
+     // area shrinks by one row
+     std::cout << "\n";
+         
+     // save cursor
+     std::cout << "\033[s";
+         
+     // set scroll region (this will place the cursor in the top left)
+     std::cout << "\033[1;" << nr_rows - 1 << "r";
+            
+     // restore cursor but ensure its inside the scrolling area
+     std::cout << "\033[u";
+     static const char *move_cursor_up = "\033[1A";
+     std::cout << move_cursor_up;
+     std::flush(std::cout);
+}
+
+void pkgDPkgPM::CleanupTerminal()
+{
+   // reset scroll area
+   SetupTerminalScrollArea(d->nr_terminal_rows + 1);
+   if(d->fancy_progress_output)
+   {
+      // override the progress line (sledgehammer)
+      static const char* clear_screen_below_cursor = "\033[J";
+      std::cout << clear_screen_below_cursor;
+      std::flush(std::cout);
+   }
+}
+
+
 // DPkgPM::Go - Run the sequence                                       /*{{{*/
 // ---------------------------------------------------------------------
 /* This globs the operations and calls dpkg 
@@ -1276,7 +1412,8 @@ bool pkgDPkgPM::Go(int OutStatusFd)
       _error->PushToStack();
       if (tcgetattr(STDOUT_FILENO, &tt) == 0)
       {
-        ioctl(0, TIOCGWINSZ, (char *)&win);
+        ioctl(1, TIOCGWINSZ, (char *)&win);
+         d->nr_terminal_rows = win.ws_row;
         if (openpty(&master, &slave, NULL, &tt, &win) < 0)
         {
            _error->Errno("openpty", _("Can not write log (%s)"), _("Is /dev/pts mounted?"));
@@ -1318,11 +1455,12 @@ bool pkgDPkgPM::Go(int OutStatusFd)
                << endl;
         FileFd::Write(OutStatusFd, status.str().c_str(), status.str().size());
       }
+
       Child = ExecFork();
-      
       // This is the child
       if (Child == 0)
       {
+
         if(slave >= 0 && master >= 0) 
         {
            setsid();
@@ -1339,7 +1477,7 @@ bool pkgDPkgPM::Go(int OutStatusFd)
 
         if (chdir(_config->FindDir("DPkg::Run-Directory","/").c_str()) != 0)
            _exit(100);
-        
+
         if (_config->FindB("DPkg::FlushSTDIN",true) == true && isatty(STDIN_FILENO))
         {
            int Flags,dummy;
@@ -1355,6 +1493,9 @@ bool pkgDPkgPM::Go(int OutStatusFd)
            if (fcntl(STDIN_FILENO,F_SETFL,Flags & (~(long)O_NONBLOCK)) < 0)
               _exit(100);
         }
+         // setup terminal
+         SetupTerminalScrollArea(d->nr_terminal_rows);
+         SendTerminalProgress(PackagesDone/float(PackagesTotal)*100.0);
 
         /* No Job Control Stop Env is a magic dpkg var that prevents it
            from using sigstop */
@@ -1479,6 +1620,7 @@ bool pkgDPkgPM::Go(int OutStatusFd)
         if(stopOnError) 
         {
            CloseLog();
+            CleanupTerminal();
            return false;
         }
       }      
@@ -1488,7 +1630,9 @@ bool pkgDPkgPM::Go(int OutStatusFd)
    // dpkg is done at this point
    if(_config->FindB("DPkgPM::Progress", false) == true)
       SendTerminalProgress(100);
-   
+
+   CleanupTerminal();
+
    if (pkgPackageManager::SigINTStop)
        _error->Warning(_("Operation was interrupted before it could finish"));