From 6ef57ae2a96085ccb56e356e21e652cc11777ac0 Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Sat, 25 Jan 2014 10:46:42 +0100 Subject: [PATCH] pkgman: Add full-sync command * BSolver/LibsolvSolver: Add FullSync() method. It uses libsolv's SOLVER_DISTUPGRADE mode. * BPackageManager: Add FullSync() using the new solver mode. * pkgman: Add full-sync command. The new command is similar to the update command without arguments, just more aggressive, allowing downgrading or even removal of packages, to match the state of the repositories. Just like "update" it doesn't work properly yet. --- headers/os/package/solver/Solver.h | 1 + .../private/package/manager/PackageManager.h | 1 + src/bin/pkgman/Jamfile | 1 + src/bin/pkgman/command_full_sync.cpp | 97 +++++++++++++++++++ src/kits/package/manager/PackageManager.cpp | 20 ++++ src/kits/package/solver/LibsolvSolver.cpp | 23 +++++ src/kits/package/solver/LibsolvSolver.h | 1 + 7 files changed, 144 insertions(+) create mode 100644 src/bin/pkgman/command_full_sync.cpp diff --git a/headers/os/package/solver/Solver.h b/headers/os/package/solver/Solver.h index bc558c9e86..3a0520879a 100644 --- a/headers/os/package/solver/Solver.h +++ b/headers/os/package/solver/Solver.h @@ -72,6 +72,7 @@ public: bool installNotYetInstalled, const BSolverPackageSpecifier** _unmatched = NULL) = 0; + virtual status_t FullSync() = 0; virtual status_t VerifyInstallation(uint32 flags = 0) = 0; bool HasProblems() const diff --git a/headers/private/package/manager/PackageManager.h b/headers/private/package/manager/PackageManager.h index a4dc0f99aa..7b7644ee40 100644 --- a/headers/private/package/manager/PackageManager.h +++ b/headers/private/package/manager/PackageManager.h @@ -90,6 +90,7 @@ public: int packageCount); void Update(const BSolverPackageSpecifierList& packages); + void FullSync(); void VerifyInstallation(); diff --git a/src/bin/pkgman/Jamfile b/src/bin/pkgman/Jamfile index 3ff4b48179..d55cb50ef2 100644 --- a/src/bin/pkgman/Jamfile +++ b/src/bin/pkgman/Jamfile @@ -6,6 +6,7 @@ BinCommand pkgman : Command.cpp command_add_repo.cpp command_drop_repo.cpp + command_full_sync.cpp command_install.cpp command_list_repos.cpp command_update.cpp diff --git a/src/bin/pkgman/command_full_sync.cpp b/src/bin/pkgman/command_full_sync.cpp new file mode 100644 index 0000000000..4359b6a271 --- /dev/null +++ b/src/bin/pkgman/command_full_sync.cpp @@ -0,0 +1,97 @@ +/* + * Copyright 2013-2014, Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + * + * Authors: + * Ingo Weinhold + */ + + +#include +#include +#include + +#include "Command.h" +#include "pkgman.h" +#include "PackageManager.h" + + +// TODO: internationalization! + + +using namespace BPackageKit; +using namespace BPackageKit::BPrivate; + + +static const char* const kShortUsage = + " %command%\n" + " Synchronizes the installed packages with the repositories.\n"; + +static const char* const kLongUsage = + "Usage: %program% %command%\n" + "Synchronizes the installed packages with the repositories. The command\n" + "is similar to the \"update\" command, but more aggressive. It also\n" + "downgrades or removes packages, if necessary.\n" + "\n" + "Options:\n" + " -H, --home\n" + " Synchronizes the packages in the user's home directory. Default is\n" + " to synchronize the packages in the system directory.\n" + " -y\n" + " Non-interactive mode. Automatically confirm changes, but fail when\n" + " encountering problems.\n" + "\n"; + + +DEFINE_COMMAND(FullSyncCommand, "full-sync", kShortUsage, kLongUsage, + kCommandCategoryPackages) + + +int +FullSyncCommand::Execute(int argc, const char* const* argv) +{ + BPackageInstallationLocation location + = B_PACKAGE_INSTALLATION_LOCATION_SYSTEM; + bool interactive = true; + + while (true) { + static struct option sLongOptions[] = { + { "help", no_argument, 0, 'h' }, + { "home", no_argument, 0, 'H' }, + { 0, 0, 0, 0 } + }; + + opterr = 0; // don't print errors + int c = getopt_long(argc, (char**)argv, "hHy", sLongOptions, NULL); + if (c == -1) + break; + + switch (c) { + case 'h': + PrintUsageAndExit(false); + break; + + case 'H': + location = B_PACKAGE_INSTALLATION_LOCATION_HOME; + break; + + case 'y': + interactive = false; + break; + + default: + PrintUsageAndExit(true); + break; + } + } + + // no remaining arguments + if (optind < argc) + PrintUsageAndExit(true); + + // perform the sync + PackageManager packageManager(location, interactive); + packageManager.FullSync(); + + return 0; +} diff --git a/src/kits/package/manager/PackageManager.cpp b/src/kits/package/manager/PackageManager.cpp index 1c6379a5f0..95761bb777 100644 --- a/src/kits/package/manager/PackageManager.cpp +++ b/src/kits/package/manager/PackageManager.cpp @@ -288,6 +288,26 @@ BPackageManager::Update(const BSolverPackageSpecifierList& packages) } +void +BPackageManager::FullSync() +{ + Init(B_ADD_INSTALLED_REPOSITORIES | B_ADD_REMOTE_REPOSITORIES + | B_REFRESH_REPOSITORIES); + + // solve + status_t error = fSolver->FullSync(); + if (error != B_OK) + DIE(error, "failed to compute packages to synchronize"); + + _HandleProblems(); + + // install/uninstall packages + _AnalyzeResult(); + _ConfirmChanges(); + _ApplyPackageChanges(); +} + + void BPackageManager::VerifyInstallation() { diff --git a/src/kits/package/solver/LibsolvSolver.cpp b/src/kits/package/solver/LibsolvSolver.cpp index 12a4b6acae..55acea62d8 100644 --- a/src/kits/package/solver/LibsolvSolver.cpp +++ b/src/kits/package/solver/LibsolvSolver.cpp @@ -426,6 +426,29 @@ LibsolvSolver::Update(const BSolverPackageSpecifierList& packages, } +status_t +LibsolvSolver::FullSync() +{ + // add repositories to pool + status_t error = _AddRepositories(); + if (error != B_OK) + return error; + + // Init the job queue and specify that all packages shall be updated. + error = _InitJobQueue(); + if (error != B_OK) + return error; + + queue_push2(fJobs, SOLVER_SOLVABLE_ALL, 0); + + // set jobs' solver mode and solve + _SetJobsSolverMode(SOLVER_DISTUPGRADE); + + _InitSolver(); + return _Solve(); +} + + status_t LibsolvSolver::VerifyInstallation(uint32 flags) { diff --git a/src/kits/package/solver/LibsolvSolver.h b/src/kits/package/solver/LibsolvSolver.h index 10e4655724..ecb6168427 100644 --- a/src/kits/package/solver/LibsolvSolver.h +++ b/src/kits/package/solver/LibsolvSolver.h @@ -57,6 +57,7 @@ public: bool installNotYetInstalled, const BSolverPackageSpecifier** _unmatched = NULL); + virtual status_t FullSync(); virtual status_t VerifyInstallation(uint32 flags = 0); virtual int32 CountProblems() const;