From 8049a4de3a5d77d28f9dbc6b2d712b280548a08a Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 25 Sep 2015 15:19:16 +0200 Subject: Implement archive fetching, complete pkg-fetch --- bpkg/pkg-fetch.cxx | 62 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 50 insertions(+), 12 deletions(-) (limited to 'bpkg/pkg-fetch.cxx') diff --git a/bpkg/pkg-fetch.cxx b/bpkg/pkg-fetch.cxx index 63dd31f..bfa6e54 100644 --- a/bpkg/pkg-fetch.cxx +++ b/bpkg/pkg-fetch.cxx @@ -6,6 +6,7 @@ #include +#include #include #include #include @@ -29,6 +30,8 @@ namespace bpkg level4 ([&]{trace << "configuration: " << c;}); database db (open (c, trace)); + transaction t (db.begin ()); + session s; path a; bool purge; @@ -52,21 +55,60 @@ namespace bpkg fail << "package name argument expected" << info << "run 'bpkg help pkg-fetch' for more information"; - string name (args.next ()); + string n (args.next ()); if (!args.more ()) fail << "package version argument expected" << info << "run 'bpkg help pkg-fetch' for more information"; - string ver (args.next ()); - - // TODO: + //@@ Same code as in pkg-status. Similar problem to repo_location; + // need a place for such utilities. // - // - Will need to use some kind or auto_rm/exception guard on - // fetched file. + version v; + { + const char* s (args.next ()); + try + { + v = version (s); + } + catch (const invalid_argument& e) + { + fail << "invalid package version '" << s << "': " << e.what (); + } + } + + if (db.query_value () == 0) + fail << "configuration " << c << " has no repositories" << + info << "use 'bpkg rep-add' to add a repository"; + + if (db.query_value () == 0) + fail << "configuration " << c << " has no available packages" << + info << "use 'bpkg rep-fetch' to fetch available packages list"; + + shared_ptr p ( + db.find (package_version_id (n, v))); + + if (p == nullptr) + fail << "package " << n << " " << v << " is not available"; + + // Pick a repository. Prefer local ones over the remote. // - - fail << "pkg-fetch " << name << " " << ver << " not yet implemented"; + const package_location* pl (&p->locations.front ()); + + for (const package_location& l: p->locations) + { + if (!l.repository.load ()->location.remote ()) + { + pl = &l; + break; + } + } + + if (verb > 1) + text << "fetching " << pl->location.leaf () << " " + << "from " << pl->repository->name (); + + a = fetch_archive (o, pl->repository->location, pl->location, c); purge = true; } @@ -79,10 +121,6 @@ namespace bpkg const auto& n (m.name); - // Database time. - // - transaction t (db.begin ()); - // See if this package already exists in this configuration. // if (shared_ptr p = db.find (n)) -- cgit v1.1