logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/

utils.ex (2647B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 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. @repo_timeout Pleroma.Config.get([Pleroma.Repo, :timeout], 15_000)
  13. def compile_dir(dir) when is_binary(dir) do
  14. dir
  15. |> File.ls!()
  16. |> Enum.map(&Path.join(dir, &1))
  17. |> Kernel.ParallelCompiler.compile()
  18. end
  19. @doc """
  20. POSIX-compliant check if command is available in the system
  21. ## Examples
  22. iex> command_available?("git")
  23. true
  24. iex> command_available?("wrongcmd")
  25. false
  26. """
  27. @spec command_available?(String.t()) :: boolean()
  28. def command_available?(command) do
  29. case :os.find_executable(String.to_charlist(command)) do
  30. false -> false
  31. _ -> true
  32. end
  33. end
  34. @doc "creates the uniq temporary directory"
  35. @spec tmp_dir(String.t()) :: {:ok, String.t()} | {:error, :file.posix()}
  36. def tmp_dir(prefix \\ "") do
  37. sub_dir =
  38. [
  39. prefix,
  40. Timex.to_unix(Timex.now()),
  41. :os.getpid(),
  42. String.downcase(Integer.to_string(:rand.uniform(0x100000000), 36))
  43. ]
  44. |> Enum.join("-")
  45. tmp_dir = Path.join(System.tmp_dir!(), sub_dir)
  46. case File.mkdir(tmp_dir) do
  47. :ok -> {:ok, tmp_dir}
  48. error -> error
  49. end
  50. end
  51. @spec posix_error_message(atom()) :: binary()
  52. def posix_error_message(code) when code in @posix_error_codes do
  53. error_message = Gettext.dgettext(Pleroma.Web.Gettext, "posix_errors", "#{code}")
  54. "(POSIX error: #{error_message})"
  55. end
  56. def posix_error_message(_), do: ""
  57. @doc """
  58. Returns [timeout: integer] suitable for passing as an option to Repo functions.
  59. This function detects if the execution was triggered from IEx shell, Mix task, or
  60. ./bin/pleroma_ctl and sets the timeout to :infinity, else returns the default timeout value.
  61. """
  62. @spec query_timeout() :: [timeout: integer]
  63. def query_timeout do
  64. {parent, _, _, _} = Process.info(self(), :current_stacktrace) |> elem(1) |> Enum.fetch!(2)
  65. cond do
  66. parent |> to_string |> String.starts_with?("Elixir.Mix.Task") -> [timeout: :infinity]
  67. parent == :erl_eval -> [timeout: :infinity]
  68. true -> [timeout: @repo_timeout]
  69. end
  70. end
  71. end