Skip to content

Commit

Permalink
filename: add might_escape function
Browse files Browse the repository at this point in the history
  • Loading branch information
rjbou committed Aug 26, 2024
1 parent afe4c84 commit 6a025d4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions master_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,4 @@ users)
## opam-core
* `OpamStd.Env`: add `env_string_list` for parsing string list environment variables (comma separated) [#5682 @desumn]
* `OpamHash`: export `compare_kind` [#5561 @rjbou]
* `OpamFilename`: add `might_escape` to check if a path is escapable, ie contains `<sep>..<sep>` [#5561 @rjbou]
10 changes: 10 additions & 0 deletions src/core/opamFilename.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
(* *)
(**************************************************************************)

let might_escape ~sep path =
let sep =
match sep with
| `Unix -> Re.char '/'
| `Windows -> Re.alt Re.[ char '\\'; char '/' ]
| `Unspecified -> Re.str Filename.dir_sep
in
List.exists (String.equal Filename.parent_dir_name)
Re.(split (compile sep) path)

module Base = struct
include OpamStd.AbstractString

Expand Down
3 changes: 3 additions & 0 deletions src/core/opamFilename.mli
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
(** Higher level file and directory name manipulation AND file operations,
wrappers on OpamSystem using the filename type *)

(* Returns [true] if string contains '..' between directory separators *)
val might_escape: sep:[`Unix | `Windows | `Unspecified ] -> string -> bool

(** Basenames *)
module Base: sig
include OpamStd.ABSTRACT
Expand Down

0 comments on commit 6a025d4

Please sign in to comment.