Skip to content

Commit

Permalink
Merge pull request #5921 from dra27/fix-download-chars
Browse files Browse the repository at this point in the history
Fix downloading URLs with invalid characters
  • Loading branch information
kit-ty-kate authored May 13, 2024
2 parents e11ebbf + ef1863d commit df4a390
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions master_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ users)
## Lint

## Repository
* Fix download URLs containing invalid characters on Windows (e.g. the ? character in `?full_index=1`) [#5921 @dra27]

## Lock

Expand Down
9 changes: 9 additions & 0 deletions src/core/opamStd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,15 @@ module OpamSys = struct
if !called then invalid_arg "Just what do you think you're doing, Dave?";
called := true;
console := printer

let is_valid_basename_char =
if Sys.win32 then
function
| '\000'..'\031'
| '<' | '>' | ':' | '"' | '/' | '\\' | '|' | '?' | '*' -> false
| _ -> true
else
fun c -> c <> '\000' && c <> '/'
end


Expand Down
4 changes: 4 additions & 0 deletions src/core/opamStd.mli
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,10 @@ module Sys : sig
OS) *)
val path_sep: char

(** [true] if a character may be used in a filename, depending on the OS.
For example, on Windows, `:` and `?` can't be in the name. *)
val is_valid_basename_char: char -> bool

(** Splits a PATH-like variable separated with [path_sep]. More involved than
it seems, because there may be quoting on Windows. By default, it returns
the path cleaned (remove trailing, leading, contiguous delimiters).
Expand Down
12 changes: 11 additions & 1 deletion src/repository/opamDownload.ml
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,18 @@ let download_as ?quiet ?validate ~overwrite ?compress ?checksum url dst =
()

let download ?quiet ?validate ~overwrite ?compress ?checksum url dstdir =
let base =
let base = OpamUrl.basename url in
if Sys.win32 then
let f c =
if OpamStd.Sys.is_valid_basename_char c then c else '_'
in
String.map f base
else
base
in
let dst =
OpamFilename.(create dstdir (Base.of_string (OpamUrl.basename url)))
OpamFilename.(create dstdir (Base.of_string base))
in
download_as ?quiet ?validate ~overwrite ?compress ?checksum url dst @@|
fun () -> dst
Expand Down

0 comments on commit df4a390

Please sign in to comment.