implement cleaning locks (bnc#385967)
authorJosef Reidinger <jreidinger@suse.cz>
Fri, 9 May 2008 13:37:21 +0000 (13:37 +0000)
committerJosef Reidinger <jreidinger@suse.cz>
Fri, 9 May 2008 13:37:21 +0000 (13:37 +0000)
src/zypper-command.cc
src/zypper-command.h
src/zypper-locks-callbacks.h [new file with mode: 0644]
src/zypper.cc

index ae997c9..d6819df 100644 (file)
@@ -46,6 +46,7 @@ const ZypperCommand ZypperCommand::XML_LIST_UPDATES_PATCHES(ZypperCommand::XML_L
 const ZypperCommand ZypperCommand::ADD_LOCK(ZypperCommand::ADD_LOCK_e);
 const ZypperCommand ZypperCommand::REMOVE_LOCK(ZypperCommand::REMOVE_LOCK_e);
 const ZypperCommand ZypperCommand::LIST_LOCKS(ZypperCommand::LIST_LOCKS_e);
+const ZypperCommand ZypperCommand::CLEAN_LOCKS(ZypperCommand::CLEAN_LOCKS_e);
 
 const ZypperCommand ZypperCommand::HELP(ZypperCommand::HELP_e);
 const ZypperCommand ZypperCommand::SHELL(ZypperCommand::SHELL_e);
@@ -107,6 +108,7 @@ ZypperCommand::Command ZypperCommand::parse(const std::string & strval_r)
     _table["addlock"] = _table["al"] = _table["lock-add"] = _table["la"] = ZypperCommand::ADD_LOCK_e;
     _table["removelock"] = _table["rl"] = _table["lock-delete"] = _table["ld"] = ZypperCommand::REMOVE_LOCK_e;
     _table["locks"] = _table["ll"] = _table["lock-list"] = ZypperCommand::LIST_LOCKS_e;
+    _table["cleanlocks"] = _table["cl"] = _table["lock-clean"] = ZypperCommand::CLEAN_LOCKS_e;
 
     _table["help"] = _table["?"] = ZypperCommand::HELP_e;
     _table["shell"] = _table["sh"] = ZypperCommand::SHELL_e;
index 524108d..4047d55 100644 (file)
@@ -41,6 +41,7 @@ struct ZypperCommand
   static const ZypperCommand ADD_LOCK;
   static const ZypperCommand REMOVE_LOCK;
   static const ZypperCommand LIST_LOCKS;
+  static const ZypperCommand CLEAN_LOCKS;
 
   static const ZypperCommand HELP;
   static const ZypperCommand SHELL;
@@ -98,6 +99,7 @@ struct ZypperCommand
     ADD_LOCK_e,
     REMOVE_LOCK_e,
     LIST_LOCKS_e,
+    CLEAN_LOCKS_e,
 
     HELP_e,
     SHELL_e,
diff --git a/src/zypper-locks-callbacks.h b/src/zypper-locks-callbacks.h
new file mode 100644 (file)
index 0000000..b6ce144
--- /dev/null
@@ -0,0 +1,73 @@
+#ifndef LOCKS_ZYPPER_CALLBACKS_H
+#define LOCKS_ZYPPER_CALLBACKS_H
+
+#include <iostream>
+
+#include "zypp/base/Logger.h"
+#include "zypp/ZYppCallbacks.h"
+#include "zypp/PoolQuery.h"
+
+#include "zypper-prompt.h"
+#include "zypper-utils.h"
+#include "output/prompt.h"
+
+namespace zypp {
+
+  struct LocksSaveReportReceiver : public zypp::callback::ReceiveReport<zypp::SavingLocksReport>
+  {
+    virtual Action conflict( const PoolQuery& query, ConflictState state )
+    {
+      //TODO localize
+      if (state==SAME_RESULTS)
+      {
+         Zypper::instance()->out().error("this query have lock same results as lock which you want remove:");
+      }
+      else
+      {
+         Zypper::instance()->out().error("this query lock some of objects, which you want unlock:");
+      }
+
+      query.serialize(std::cout);
+
+      return read_bool_answer(PROMPT_YN_REMOVE_LOCK ,
+        "Do you want remove this lock?", true)? DELETE : IGNORE;
+    }
+  };
+
+  struct CleanLocksReportReceiver : public zypp::callback::ReceiveReport<zypp::CleanEmptyLocksReport>
+  {
+    virtual Action execute( const PoolQuery& query )
+    {
+      Zypper::instance()->out().error("this query doesn't lock anything:");
+
+      query.serialize(std::cout);
+
+      return read_bool_answer(PROMPT_YN_REMOVE_LOCK ,
+        "Do you want remove this lock?", true)? DELETE : IGNORE;
+    }
+  };
+
+
+
+}
+
+class LocksCallbacks {
+  private:
+    zypp::LocksSaveReportReceiver _saveReport;
+    zypp::CleanLocksReportReceiver _cleanReport;
+
+  public:
+    LocksCallbacks()
+    {
+      _saveReport.connect();
+      _cleanReport.connect();
+    }
+     
+    ~LocksCallbacks()
+    {
+      _saveReport.disconnect();
+      _cleanReport.disconnect();
+    }
+};
+
+#endif
index 7e8ee34..4d3405d 100644 (file)
@@ -29,6 +29,7 @@
 
 #include "zypp/sat/SolvAttr.h"
 #include "zypp/PoolQuery.h"
+#include "zypp/Locks.h"
 
 #include "zypp/target/rpm/RpmHeader.h" // for install <.rpmURI>
 
@@ -1695,6 +1696,28 @@ void Zypper::processCommandOptions()
     break;
   }
 
+  case ZypperCommand::CLEAN_LOCKS_e:
+  {
+    static struct option options[] =
+    {
+      {"help", no_argument, 0, 'h'},
+      {"only-duplicates", no_argument, 0, 'd' },
+      {"only-empty", no_argument, 0, 'e' },
+      {0, 0, 0, 0}
+    };
+    specific_options = options;
+    _command_help = ( //TODO localize
+      "clean locks (ll)\n"
+      "\n"
+      "Removes useless locks. Before removing locks which doesn't lock anything, ask user.\n"
+      "\n"
+      "  Command options:\n"
+      "-d, --only-duplicates     Clean only duplicate locks.\n"
+      "-e, --only-empty          Clean only locks which doesn't lock anything.\n"
+    );
+    break;
+  }
+
   case ZypperCommand::SHELL_QUIT_e:
   {
     static struct option quit_options[] = {
@@ -3131,6 +3154,30 @@ void Zypper::doCommand()
 
     break;
   }
+  
+  case ZypperCommand::CLEAN_LOCKS_e:
+  {
+    if (runningHelp()) { out().info(_command_help, Out::QUIET); return; }
+   
+    init_target(*this);
+    init_repos(*this);
+    if (exitCode() != ZYPPER_EXIT_OK)
+      return;
+    load_resolvables(*this);
+
+    Locks::instance().read();
+    Locks::size_type start = Locks::instance().size();
+    if ( !copts.count("only-duplicate") )
+      Locks::instance().removeEmpty();
+    if ( !copts.count("only-empty") )
+      Locks::instance().removeDuplicates();
+
+    Locks::instance().save();
+
+    out().info(str::form("removed locks: %lu",start-Locks::instance().size()));
+    
+    break;
+  }
 
   // -----------------------------( shell )------------------------------------