From 9730f26a59b1c5bf844c056ac413448b2c96d86a Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 3 Aug 2015 18:35:03 +0200 Subject: Implement the completion of relative repository location to remote/absolute --- bpkg/manifest | 43 +++++++++++++++++++++++++++++++++++- bpkg/manifest.cxx | 66 +++++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 99 insertions(+), 10 deletions(-) (limited to 'bpkg') diff --git a/bpkg/manifest b/bpkg/manifest index e8c2714..0ec3a3a 100644 --- a/bpkg/manifest +++ b/bpkg/manifest @@ -302,14 +302,32 @@ namespace bpkg // repository_location () = default; + // Creates remote/absolute repository location. Throws invalid_argument + // if the location is a relative path. + // explicit repository_location (const std::string&); + // Creates a potentially relative repository location. If base is not + // empty, use it to complete the relative location to remote/absolute. + // Throws invalid_argument if base itself is relative or the resulting + // completed location is invalid. + // + repository_location (const std::string&, const repository_location& base); + + // Note that relative locations have no canonical name. + // const std::string& canonical_name () const noexcept {return canonical_name_;} + // There are 3 types of locations: remote, local absolute filesystem + // path and local relative filesystem path. Plus there is the special + // empty location. The following predicates can be used to determine + // what kind of location it is. Note that except for empty(), all the + // other predicates throw std::logic_error for an empty location. + // bool - empty () const noexcept {return canonical_name_.empty ();} + empty () const noexcept {return path_.empty ();} bool local () const @@ -320,6 +338,29 @@ namespace bpkg return host_.empty (); } + bool + remote () const + { + return !local (); + } + + bool + absolute () const + { + return local () && path_.absolute (); + } + + bool + relative () const + { + if (empty ()) + throw std::logic_error ("empty location"); + + // Note that in remote locations path is always absolute. + // + return path_.relative (); + } + const butl::dir_path& path () const { diff --git a/bpkg/manifest.cxx b/bpkg/manifest.cxx index 0393f6f..5e8da1a 100644 --- a/bpkg/manifest.cxx +++ b/bpkg/manifest.cxx @@ -748,14 +748,29 @@ namespace bpkg // repository_location // + // Location parameter type is fully qualified as compiler gets confused with + // string() member. + // + repository_location:: + repository_location (const std::string& l) + : repository_location (l, repository_location ()) // Delegate. + { + if (relative ()) + throw invalid_argument ("relative filesystem path"); + } + repository_location:: - repository_location (const std::string& l): port_ (0) + repository_location (const std::string& l, const repository_location& b) { - // Otherwise compiler gets confused with string() member. Same reason - // constructor parameter type is fully qualified. + // Otherwise compiler gets confused with string() member. // using std::string; + // Base repository location can not be a relative path. + // + if (!b.empty () && b.relative ()) + throw invalid_argument ("base relative filesystem path"); + if (::strncasecmp (l.c_str (), "http://", 7) == 0) { // Split location into host, port and path components. Calculate @@ -842,7 +857,9 @@ namespace bpkg // Chop the port, if present. // - if (pt != he) + if (pt == he) + port_ = 0; + else { unsigned long long n (++pt == he ? 0 : stoull (string (pt, he))); if (n == 0 || n > UINT16_MAX) @@ -874,8 +891,31 @@ namespace bpkg canonical_name_ += ':' + to_string (port_); } else + { path_ = dir_path (l); + if (path_.empty ()) + throw invalid_argument ("empty location"); + + // Complete if we are relative and have base. + // + if (!b.empty () && path_.relative ()) + { + // Convert the relative path location to an absolute or remote one. + // + host_ = b.host_; + port_ = b.port_; + path_ = b.path_ / path_; + + // Set canonical name to the base location canonical name host + // part. The path part of the canonical name is calculated below. + // + if (b.remote ()) + canonical_name_ = + b.canonical_name_.substr (0, b.canonical_name_.find ("/")); + } + } + // Normalize path to avoid different representations of the same location // and canonical name. So a/b/../c/1/x/../y and a/c/1/y to be considered // as same location. @@ -889,14 +929,19 @@ namespace bpkg throw invalid_argument ("invalid path"); } + // Finish calculating the canonical name, unless we are relative. + // + if (relative ()) + return; + // Search for the version path component preceeding canonical name // component. // - auto b (path_.rbegin ()), i (b), e (path_.rend ()); + auto rb (path_.rbegin ()), i (rb), re (path_.rend ()); // Find the version component. // - for (; i != e; ++i) + for (; i != re; ++i) { const string& c (*i); @@ -904,7 +949,7 @@ namespace bpkg break; } - if (i == e) + if (i == re) throw invalid_argument ("missing repository version"); // Validate the version. At the moment the only valid value is 1. @@ -914,7 +959,7 @@ namespace bpkg // Note: allow empty paths (e.g., http://stable.cppget.org/1/). // - string d (dir_path (b, i).posix_string ()); + string d (dir_path (rb, i).posix_string ()); if (!canonical_name_.empty () && !d.empty ()) // If we have host and dir. canonical_name_ += '/'; @@ -986,7 +1031,10 @@ namespace bpkg { try { - location = repository_location (move (v)); + // Call prerequisite repository location constructor, do not + // ammend relative path. + // + location = repository_location (move (v), repository_location ()); } catch (const invalid_argument& e) { -- cgit v1.1