Guard files listing to not crash on emoji names (#2558)

This commit is contained in:
Jonatan Kłosko 2024-04-09 17:15:44 +02:00 committed by GitHub
parent 18ec115dbd
commit f4a29840bc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -46,7 +46,7 @@ defimpl Livebook.FileSystem, for: Livebook.FileSystem.Local do
def list(file_system, path, recursive) do
FileSystem.Utils.assert_dir_path!(path)
case File.ls(path) do
case file_ls(path) do
{:ok, filenames} ->
paths =
Enum.map(filenames, fn name ->
@ -73,6 +73,31 @@ defimpl Livebook.FileSystem, for: Livebook.FileSystem.Local do
end
end
defp file_ls(dir) do
# TODO: use File.ls/1 back once this is fixed in the OTP version
# that we require. See https://github.com/erlang/otp/issues/4779
#
# Erlang currently chokes on emoji directories on Windows, so we
# discard those
case :file.list_dir(dir) do
{:ok, names} ->
{:ok,
for(
name <- names,
string =
try do
IO.chardata_to_string(name)
rescue
_ -> nil
end,
do: string
)}
{:error, error} ->
{:error, error}
end
end
def read(_file_system, path) do
FileSystem.Utils.assert_regular_path!(path)