// file : bpkg/pkg-verify.cxx -*- C++ -*- // license : MIT; see accompanying LICENSE file #include #include // cout #include #include #include #include #include #include using namespace std; using namespace butl; namespace bpkg { vector pkg_verify (const common_options& co, manifest_parser& p, const path& what, int diag_level) { manifest_name_value nv (p.next ()); // Make sure this is the start and we support the version. // if (!nv.name.empty ()) throw manifest_parsing (p.name (), nv.name_line, nv.name_column, "start of package manifest expected"); if (nv.value != "1") throw manifest_parsing (p.name (), nv.value_line, nv.value_column, "unsupported format version"); vector r; // For the depends name, parse the value and if it contains the build2 or // bpkg constraints, verify that they are satisfied. // // Note that if the semantics of the depends value changes we may be // unable to parse some of them before we get to build2 or bpkg and issue // the user-friendly diagnostics. So we are going to ignore such depends // values. But that means that if the user made a mistake in build2/bpkg // then we will skip them as well. This, however, is not a problem since // the pre-parsed result will then be re-parsed (e.g., by the // package_manifest() constructor) which will diagnose any mistakes. // for (nv = p.next (); !nv.empty (); nv = p.next ()) { if (nv.name == "depends") try { dependency_alternatives da (nv.value); if (da.buildtime) { for (dependency& d: da) { const package_name& dn (d.name); if (dn != "build2" && dn != "bpkg") continue; if (da.size () != 1) { if (diag_level != 0) error (p.name (), nv.value_line, nv.value_column) << "alternatives in " << dn << " dependency"; throw failed (); } if (dn == "build2") { if (d.constraint && !satisfy_build2 (co, d)) { if (diag_level != 0) { diag_record dr (error); dr << "unable to satisfy constraint (" << d << ")"; if (!what.empty ()) dr << " for package " << what; dr << info << "available build2 version is " << build2_version; } throw failed (); } } else { if (d.constraint && !satisfy_bpkg (co, d)) { if (diag_level != 0) { diag_record dr (error); dr << "unable to satisfy constraint (" << d << ")"; if (!what.empty ()) dr << " for package " << what; dr << "available bpkg version is " << bpkg_version; } throw failed (); } } } } } catch (const invalid_argument&) {} // Ignore r.push_back (move (nv)); } // Make sure this is the end. // nv = p.next (); if (!nv.empty ()) throw manifest_parsing (p.name (), nv.name_line, nv.name_column, "single package manifest expected"); return r; } package_manifest pkg_verify (const common_options& co, const path& af, bool iu, bool ev, bool cd, int diag_level) try { dir_path pd (package_dir (af)); path mf (pd / manifest_file); // If the diag level is less than 2, we need to make tar not print any // diagnostics. There doesn't seem to be an option to suppress this and // the only way is to redirect stderr to something like /dev/null. // // If things go badly for tar and it starts spitting errors instead of the // manifest, the manifest parser will fail. But that's ok since we assume // that the child error is always the reason for the manifest parsing // failure. // pair pr (start_extract (co, af, mf, diag_level == 2)); auto wait = [&pr] () {return pr.second.wait () && pr.first.wait ();}; try { ifdstream is (move (pr.second.in_ofd), fdstream_mode::skip); manifest_parser mp (is, mf.string ()); package_manifest m (mp.name (), pkg_verify (co, mp, af, diag_level), iu, cd); is.close (); if (wait ()) { // Verify package archive/directory is -. // dir_path ed (m.name.string () + "-" + m.version.string ()); if (pd != ed) { if (diag_level != 0) error << "package archive/directory name mismatch in " << af << info << "extracted from archive '" << pd << "'" << info << "expected from manifest '" << ed << "'"; throw failed (); } // Expand the *-file manifest values, if requested. // if (ev) { m.load_files ( [&pd, &co, &af, diag_level] (const string& n, const path& p) { path f (pd / p); string s (extract (co, af, f, diag_level != 0)); if (s.empty ()) { if (diag_level != 0) error << n << " manifest value in package archive " << af << " references empty file " << f; throw failed (); } return s; }, iu); } return m; } // Child exited with an error, fall through. } // Ignore these exceptions if the child process exited with // an error status since that's the source of the failure. // catch (const manifest_parsing& e) { if (wait ()) { if (diag_level != 0) error (e.name, e.line, e.column) << e.description << info << "package archive " << af; throw failed (); } } catch (const io_error&) { if (wait ()) { if (diag_level != 0) error << "unable to extract " << mf << " from " << af; throw failed (); } } // We should only get here if the child exited with an error // status. // assert (!wait ()); // While it is reasonable to assuming the child process issued // diagnostics, tar, specifically, doesn't mention the archive // name. // if (diag_level == 2) error << af << " does not appear to be a bpkg package"; throw not_package (); } catch (const process_error& e) { // Note: this is not an "invalid package" case, so no diag check. // fail << "unable to extract manifest file from " << af << ": " << e << endf; } package_manifest pkg_verify (const common_options& co, const dir_path& d, bool iu, const function& tf, int diag_level) { // Parse the manifest. // path mf (d / manifest_file); if (!exists (mf)) { if (diag_level == 2) error << "no manifest file in package directory " << d; throw not_package (); } try { ifdstream ifs (mf); manifest_parser mp (ifs, mf.string ()); package_manifest m (mp.name (), pkg_verify (co, mp, d, diag_level), tf, iu); // We used to verify package directory is - but it is // not clear why we should enforce it in this case (i.e., the user // provides us with a package directory). // // dir_path ed (m.name + "-" + m.version.string ()); // // if (d.leaf () != ed) // { // if (diag_level != 0) // error << "invalid package directory name '" << d.leaf () << "'" << // info << "expected from manifest '" << ed << "'"; // // throw failed (); // } return m; } catch (const manifest_parsing& e) { if (diag_level != 0) error (e.name, e.line, e.column) << e.description; throw failed (); } catch (const io_error& e) { if (diag_level != 0) error << "unable to read from " << mf << ": " << e; throw failed (); } } int pkg_verify (const pkg_verify_options& o, cli::scanner& args) { tracer trace ("pkg_verify"); if (!args.more ()) fail << "archive path argument expected" << info << "run 'bpkg help pkg-verify' for more information"; path a (args.next ()); if (!exists (a)) fail << "archive file '" << a << "' does not exist"; l4 ([&]{trace << "archive: " << a;}); // If we were asked to run silent, don't yap about the reason // why the package is invalid. Just return the error status. // try { package_manifest m (pkg_verify (o, a, o.ignore_unknown (), o.deep () /* expand_values */, o.deep () /* complete_depends */, o.silent () ? 0 : 2)); if (o.manifest ()) { try { manifest_serializer s (cout, "stdout"); m.serialize (s); } catch (const manifest_serialization& e) { fail << "unable to serialize manifest: " << e.description; } catch (const io_error&) { fail << "unable to write to stdout"; } } else if (verb && !o.silent () && !o.no_result ()) text << "valid package " << m.name << " " << m.version; return 0; } catch (const failed& e) { return e.code; } } }