Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.9KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Utils do
  5. @posix_error_codes ~w(
  6. eacces eagain ebadf ebadmsg ebusy edeadlk edeadlock edquot eexist efault
  7. efbig eftype eintr einval eio eisdir eloop emfile emlink emultihop
  8. enametoolong enfile enobufs enodev enolck enolink enoent enomem enospc
  9. enosr enostr enosys enotblk enotdir enotsup enxio eopnotsupp eoverflow
  10. eperm epipe erange erofs espipe esrch estale etxtbsy exdev
  11. )a
  12. def compile_dir(dir) when is_binary(dir) do
  13. dir
  14. |> File.ls!()
  15. |> Enum.map(&Path.join(dir, &1))
  16. |> Kernel.ParallelCompiler.compile()
  17. end
  18. @doc """
  19. POSIX-compliant check if command is available in the system
  20. ## Examples
  21. iex> command_available?("git")
  22. true
  23. iex> command_available?("wrongcmd")
  24. false
  25. """
  26. @spec command_available?(String.t()) :: boolean()
  27. def command_available?(command) do
  28. case :os.find_executable(String.to_charlist(command)) do
  29. false -> false
  30. _ -> true
  31. end
  32. end
  33. @doc "creates the uniq temporary directory"
  34. @spec tmp_dir(String.t()) :: {:ok, String.t()} | {:error, :file.posix()}
  35. def tmp_dir(prefix \\ "") do
  36. sub_dir =
  37. [
  38. prefix,
  39. Timex.to_unix(Timex.now()),
  40. :os.getpid(),
  41. String.downcase(Integer.to_string(:rand.uniform(0x100000000), 36))
  42. ]
  43. |> Enum.join("-")
  44. tmp_dir = Path.join(System.tmp_dir!(), sub_dir)
  45. case File.mkdir(tmp_dir) do
  46. :ok -> {:ok, tmp_dir}
  47. error -> error
  48. end
  49. end
  50. @spec posix_error_message(atom()) :: binary()
  51. def posix_error_message(code) when code in @posix_error_codes do
  52. error_message = Gettext.dgettext(Pleroma.Web.Gettext, "posix_errors", "#{code}")
  53. "(POSIX error: #{error_message})"
  54. end
  55. def posix_error_message(_), do: ""
  56. end