// file : bpkg/pkg-disfigure.cxx -*- C++ -*- // copyright : Copyright (c) 2014-2016 Code Synthesis Ltd // license : MIT; see accompanying LICENSE file #include #include #include #include #include using namespace std; using namespace butl; namespace bpkg { void pkg_disfigure (const dir_path& c, const common_options& o, transaction& t, const shared_ptr& p) { assert (p->state == package_state::configured || p->state == package_state::broken); tracer trace ("pkg_disfigure"); database& db (t.database ()); tracer_guard tg (db, trace); // Check that we have no dependents. // if (p->state == package_state::configured) { using query = query; auto r (db.query (query::name == p->name)); if (!r.empty ()) { diag_record dr; dr << fail << "package " << p->name << " still has dependencies:"; for (const package_dependent& pd: r) { dr << info << "package " << pd.name; if (pd.constraint) dr << " on " << p->name << " " << *pd.constraint; } } } // Since we are no longer configured, clear the prerequisites list. // p->prerequisites.clear (); // Calculate package's src_root and out_root. // assert (p->src_root); // Must be set since unpacked. assert (p->out_root); // Must be set since configured. dir_path src_root (p->src_root->absolute () ? *p->src_root : c / *p->src_root); dir_path out_root (c / *p->out_root); // Always relative. l4 ([&]{trace << "src_root: " << src_root << ", " << "out_root: " << out_root;}); // Form the buildspec. // string bspec; if (p->state == package_state::configured) { bspec = "clean('" + out_root.string () + "/') " "disfigure('" + out_root.string () + "/')"; } else { // Why do we need to specify src_root? While it's unnecessary // for a completely configured package, here we disfigure a // partially configured one. // if (src_root == out_root) bspec = "disfigure('" + out_root.string () + "/')"; else bspec = "disfigure('" + src_root.string () + "/'@'" + out_root.string () + "/')"; } l4 ([&]{trace << "buildspec: " << bspec;}); // Disfigure. // try { if (exists (out_root)) run_b (o, c, bspec, true); // Run quiet. // Make sure the out directory is gone unless it is the same as src. // if (out_root != src_root && exists (out_root)) fail << "package output directory " << out_root << " still exists"; } catch (const failed&) { // If we failed to disfigure the package, set it to the broken // state. The user can then try to clean things up with pkg-purge. // p->state = package_state::broken; db.update (p); t.commit (); info << "package " << p->name << " is now broken; " << "use 'pkg-purge' to remove"; throw; } p->out_root = nullopt; p->state = package_state::unpacked; db.update (p); t.commit (); } int pkg_disfigure (const pkg_disfigure_options& o, cli::scanner& args) { tracer trace ("pkg_disfigure"); const dir_path& c (o.directory ()); l4 ([&]{trace << "configuration: " << c;}); if (!args.more ()) fail << "package name argument expected" << info << "run 'bpkg help pkg-disfigure' for more information"; string n (args.next ()); database db (open (c, trace)); transaction t (db.begin ()); shared_ptr p (db.find (n)); if (p == nullptr) fail << "package " << n << " does not exist in configuration " << c; if (p->state != package_state::configured) fail << "package " << n << " is " << p->state << info << "expected it to be configured"; l4 ([&]{trace << p->name << " " << p->version;}); pkg_disfigure (c, o, t, p); // Commits the transaction. if (verb) text << "disfigured " << p->name << " " << p->version; return 0; } }