@@ -111,26 +111,27 @@ maybe_file(Req, Prefix, Dir) ->
111111 nothing
112112 end .
113113
114+
115+ % % OTP_RELEASE macro was introduced in 21, `filename:safe_relative_path/1' in
116+ % % 19.3.
117+ -ifdef (OTP_RELEASE ).
118+ -ifdef (? OTP_RELEASE >= 21 ).
119+ safe_relative_path (Path ) ->
120+ filename :safe_relative_path (Path ).
121+ -endif .
122+ -else .
123+
114124% % @doc Backport of `filename:safe_relative_path/1' from 19.3. This code was
115125% % lifted from:
116126% % https://github.com/erlang/otp/blob/master/lib/stdlib/src/filename.erl#L811
117127-spec safe_relative_path (file :name_all ()) -> unsafe | file :name_all ().
118128safe_relative_path (Path ) ->
119- % % prefer the OTP version of `safe_relative_path/1' if it exists
120- case lists :member ({safe_relative_path , 1 },
121- filename :module_info (exports )) of
122- true ->
123- % % to suppress xref warnings on 19.2 and lower
124- Mod = filename ,
125- Mod :safe_relative_path (Path );
126- false ->
127- case filename :pathtype (Path ) of
128- relative ->
129- Cs0 = filename :split (Path ),
130- safe_relative_path_1 (Cs0 , []);
131- _ ->
132- unsafe
133- end
129+ case filename :pathtype (Path ) of
130+ relative ->
131+ Cs0 = filename :split (Path ),
132+ safe_relative_path_1 (Cs0 , []);
133+ _ ->
134+ unsafe
134135 end .
135136
136137safe_relative_path_1 ([" ." |T ], Acc ) ->
@@ -152,35 +153,4 @@ climb(_, []) ->
152153 unsafe ;
153154climb (T , [_ |Acc ]) ->
154155 safe_relative_path_1 (T , Acc ).
155-
156-
157- -ifdef (TEST ).
158- -include_lib (" eunit/include/eunit.hrl" ).
159-
160- safe_relative_path_test () ->
161- % % binary args
162- ? assertMatch ([], safe_relative_path (<<>>)),
163- ? assertMatch (unsafe , safe_relative_path (<<" /" >>)),
164- ? assertMatch (unsafe , safe_relative_path (<<" /root" >>)),
165- ? assertMatch (unsafe , safe_relative_path (<<" .." >>)),
166- ? assertMatch (unsafe , safe_relative_path (<<" dir/../../dir" >>)),
167- ? assertMatch (<<" dir" >>, safe_relative_path (<<" dir" >>)),
168- ? assertMatch (<<" dir" >>, safe_relative_path (<<" dir/../dir" >>)),
169- ? assertMatch (<<" dir/sub" >>, safe_relative_path (<<" dir/sub/../sub" >>)),
170- ? assertMatch (<<" dir/sub" >>, safe_relative_path (<<" dir/./sub" >>)),
171- ? assertMatch (<<" dir/sub" >>, safe_relative_path (<<" dir/././../dir/././sub" >>)),
172-
173- % % string args
174- % % (these are here for completeness since `filename:safe_relative_path'
175- % % supports string arguments
176- ? assertMatch ([], safe_relative_path (" " )),
177- ? assertMatch (unsafe , safe_relative_path (" /" )),
178- ? assertMatch (unsafe , safe_relative_path (" /root" )),
179- ? assertMatch (unsafe , safe_relative_path (" .." )),
180- ? assertMatch (unsafe , safe_relative_path (" dir/../../dir" )),
181- ? assertMatch (" dir" , safe_relative_path (" dir" )),
182- ? assertMatch (" dir" , safe_relative_path (" dir/../dir" )),
183- ? assertMatch (" dir/sub" , safe_relative_path (" dir/sub/../sub" )),
184- ? assertMatch (" dir/sub" , safe_relative_path (" dir/./sub" )),
185- ? assertMatch (" dir/sub" , safe_relative_path (" dir/././../dir/././sub" )).
186156-endif .
0 commit comments