diff options
-rw-r--r-- | bpkg/common.cli | 12 | ||||
-rw-r--r-- | bpkg/fetch.cxx | 44 |
2 files changed, 53 insertions, 3 deletions
diff --git a/bpkg/common.cli b/bpkg/common.cli index 45d13d3..45d2c55 100644 --- a/bpkg/common.cli +++ b/bpkg/common.cli @@ -119,6 +119,18 @@ namespace bpkg \cb{wget}, and \cb{fetch}." } + size_t --fetch-timeout + { + "<sec>", + "The fetch program timeout. While the exact semantics of the value + depends on the fetch program used, at a minimum it specifies in + seconds the maximum time that can be spent without any network + activity. Specifically, it is translated to the \cb{--max-time} + option for \cb{curl} and to the \cb{--timeout} option for \cb{wget} + and \cb{fetch}. See \cb{--fetch} for more information on the fetch + program." + } + strings --fetch-option { "<opt>", diff --git a/bpkg/fetch.cxx b/bpkg/fetch.cxx index 700e345..a4f238a 100644 --- a/bpkg/fetch.cxx +++ b/bpkg/fetch.cxx @@ -96,6 +96,7 @@ namespace bpkg static process start_wget (const path& prog, + const optional<size_t>& timeout, const strings& ops, const string& url, const path& out) @@ -140,6 +141,15 @@ namespace bpkg else if (verb > 3) args.push_back ("-d"); + // Set download timeout if requested. + // + string tm; + if (timeout) + { + tm = "--timeout=" + to_string (*timeout); + args.push_back (tm.c_str ()); + } + // Add extra options. The idea if that they may override what // we have set before this point but not after (like -O below). // @@ -219,6 +229,7 @@ namespace bpkg static process start_curl (const path& prog, + const optional<size_t>& timeout, const strings& ops, const string& url, const path& out) @@ -249,6 +260,16 @@ namespace bpkg else if (verb > 3) args.push_back ("-v"); + // Set download timeout if requested. + // + string tm; + if (timeout) + { + tm = to_string (*timeout); + args.push_back ("--max-time"); + args.push_back (tm.c_str ()); + } + // Add extra options. The idea is that they may override what // we have set before this point but not after. // @@ -334,6 +355,7 @@ namespace bpkg static process start_fetch (const path& prog, + const optional<size_t>& timeout, const strings& ops, const string& url, const path& out) @@ -358,6 +380,15 @@ namespace bpkg else if (verb > 3) args.push_back ("-v"); + // Set download timeout if requested. + // + string tm; + if (timeout) + { + tm = "--timeout=" + to_string (*timeout); + args.push_back (tm.c_str ()); + } + // Add extra options. The idea is that they may override what // we have set before this point but not after (like -o below). // @@ -485,8 +516,11 @@ namespace bpkg static process start (const common_options& o, const string& url, const path& out = path ()) { - process (*f) ( - const path&, const strings&, const string&, const path&) = nullptr; + process (*f) (const path&, + const optional<size_t>&, + const strings&, + const string&, + const path&) = nullptr; switch (check (o)) { @@ -495,9 +529,13 @@ namespace bpkg case fetch: f = &start_fetch; break; } + optional<size_t> timeout; + if (o.fetch_timeout_specified ()) + timeout = o.fetch_timeout (); + try { - return f (fetch_path, o.fetch_option (), url, out); + return f (fetch_path, timeout, o.fetch_option (), url, out); } catch (const process_error& e) { |