diff options
author | Björn Gustavsson <[email protected]> | 2017-02-24 10:09:21 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2017-02-24 10:09:21 +0100 |
commit | 133645d9a24e6a6d7123370ef332ca478598040e (patch) | |
tree | 7843f65b743222cc027090f2b94f8227f4d4a5ee /lib/stdlib/src/filename.erl | |
parent | 6e687ee0c10d1c680d214baffcef7631eef52bc3 (diff) | |
parent | 8c7a68f3808a8d52f5cfc297a249ca4ef2480238 (diff) | |
download | otp-133645d9a24e6a6d7123370ef332ca478598040e.tar.gz otp-133645d9a24e6a6d7123370ef332ca478598040e.tar.bz2 otp-133645d9a24e6a6d7123370ef332ca478598040e.zip |
Merge pull request #1350 from bjorng/bjorn/stdlib/safe-relative-path/OTP-14215
filename: Add safe_relative_path/1
OTP-14215
Diffstat (limited to 'lib/stdlib/src/filename.erl')
-rw-r--r-- | lib/stdlib/src/filename.erl | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/lib/stdlib/src/filename.erl b/lib/stdlib/src/filename.erl index c4586171ca..5d60b3837e 100644 --- a/lib/stdlib/src/filename.erl +++ b/lib/stdlib/src/filename.erl @@ -34,7 +34,8 @@ -export([absname/1, absname/2, absname_join/2, basename/1, basename/2, dirname/1, extension/1, join/1, join/2, pathtype/1, - rootname/1, rootname/2, split/1, nativename/1]). + rootname/1, rootname/2, split/1, nativename/1, + safe_relative_path/1]). -export([find_src/1, find_src/2, flatten/1]). -export([basedir/2, basedir/3]). @@ -750,6 +751,39 @@ separators() -> _ -> {false, false} end. +-spec safe_relative_path(Filename) -> 'unsafe' | SafeFilename when + Filename :: file:name_all(), + SafeFilename :: file:name_all(). + +safe_relative_path(Path) -> + case pathtype(Path) of + relative -> + Cs0 = split(Path), + safe_relative_path_1(Cs0, []); + _ -> + unsafe + end. + +safe_relative_path_1(["."|T], Acc) -> + safe_relative_path_1(T, Acc); +safe_relative_path_1([<<".">>|T], Acc) -> + safe_relative_path_1(T, Acc); +safe_relative_path_1([".."|T], Acc) -> + climb(T, Acc); +safe_relative_path_1([<<"..">>|T], Acc) -> + climb(T, Acc); +safe_relative_path_1([H|T], Acc) -> + safe_relative_path_1(T, [H|Acc]); +safe_relative_path_1([], []) -> + []; +safe_relative_path_1([], Acc) -> + join(lists:reverse(Acc)). + +climb(_, []) -> + unsafe; +climb(T, [_|Acc]) -> + safe_relative_path_1(T, Acc). + %% find_src(Module) -- |