aboutsummaryrefslogtreecommitdiff
path: root/bpkg/fetch.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2015-09-24 08:08:23 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2015-09-24 08:08:23 +0200
commit04ea94103db92d6d27230794e14606547aacf670 (patch)
treee6731caab1d64375e52525fd2dec8ae33cfb50d0 /bpkg/fetch.cxx
parent69c333f31373177f6809ef4532f9c0c73a1a8148 (diff)
Factor and reuse manifest fetching code
Diffstat (limited to 'bpkg/fetch.cxx')
-rw-r--r--bpkg/fetch.cxx85
1 files changed, 85 insertions, 0 deletions
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 <bpkg/fetch>
+
+#include <fstream>
+
+#include <butl/process>
+#include <butl/fdstream>
+#include <butl/filesystem>
+
+#include <bpkg/manifest-parser>
+
+#include <bpkg/diagnostics>
+
+using namespace std;
+using namespace butl;
+
+namespace bpkg
+{
+ template <typename M>
+ 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<repository_manifests> (d / repositories);
+ }
+
+ repository_manifests
+ fetch_repositories (const repository_location& rl)
+ {
+ assert (/*rl.remote () ||*/ rl.absolute ());
+
+ return rl.remote ()
+ ? repository_manifests ()
+ : fetch_file<repository_manifests> (rl.path () / repositories);
+ }
+
+ static const path packages ("packages");
+
+ package_manifests
+ fetch_packages (const dir_path& d)
+ {
+ return fetch_file<package_manifests> (d / packages);
+ }
+
+ package_manifests
+ fetch_packages (const repository_location& rl)
+ {
+ assert (/*rl.remote () ||*/ rl.absolute ());
+
+ return rl.remote ()
+ ? package_manifests ()
+ : fetch_file<package_manifests> (rl.path () / packages);
+ }
+}