From 04ea94103db92d6d27230794e14606547aacf670 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 24 Sep 2015 08:08:23 +0200 Subject: Factor and reuse manifest fetching code --- bpkg/fetch.cxx | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 bpkg/fetch.cxx (limited to 'bpkg/fetch.cxx') diff --git a/bpkg/fetch.cxx b/bpkg/fetch.cxx new file mode 100644 index 0000000..34445a8 --- /dev/null +++ b/bpkg/fetch.cxx @@ -0,0 +1,85 @@ +// file : bpkg/fetch.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#include + +#include + +#include +#include +#include + +#include + +#include + +using namespace std; +using namespace butl; + +namespace bpkg +{ + template + static M + fetch_file (const path& f) + { + if (!exists (f)) + fail << "file " << f << " does not exist"; + + try + { + ifstream ifs; + ifs.exceptions (ofstream::badbit | ofstream::failbit); + ifs.open (f.string ()); + + manifest_parser mp (ifs, f.string ()); + return M (mp); + } + catch (const manifest_parsing& e) + { + error (e.name, e.line, e.column) << e.description; + } + catch (const ifstream::failure&) + { + error << "unable to read from " << f; + } + + throw failed (); + } + + static const path repositories ("repositories"); + + repository_manifests + fetch_repositories (const dir_path& d) + { + return fetch_file (d / repositories); + } + + repository_manifests + fetch_repositories (const repository_location& rl) + { + assert (/*rl.remote () ||*/ rl.absolute ()); + + return rl.remote () + ? repository_manifests () + : fetch_file (rl.path () / repositories); + } + + static const path packages ("packages"); + + package_manifests + fetch_packages (const dir_path& d) + { + return fetch_file (d / packages); + } + + package_manifests + fetch_packages (const repository_location& rl) + { + assert (/*rl.remote () ||*/ rl.absolute ()); + + return rl.remote () + ? package_manifests () + : fetch_file (rl.path () / packages); + } +} -- cgit v1.1