Merge pull request 'Remove reference to pleroma-fox-tan-shy.png in COPYING' (#298...
[akkoma] / lib / pleroma / utils.ex
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
5 defmodule Pleroma.Utils do
6 @posix_error_codes ~w(
7 eacces eagain ebadf ebadmsg ebusy edeadlk edeadlock edquot eexist efault
8 efbig eftype eintr einval eio eisdir eloop emfile emlink emultihop
9 enametoolong enfile enobufs enodev enolck enolink enoent enomem enospc
10 enosr enostr enosys enotblk enotdir enotsup enxio eopnotsupp eoverflow
11 eperm epipe erange erofs espipe esrch estale etxtbsy exdev
12 )a
13
14 @repo_timeout Pleroma.Config.get([Pleroma.Repo, :timeout], 15_000)
15
16 def compile_dir(dir) when is_binary(dir) do
17 dir
18 |> elixir_files()
19 |> Kernel.ParallelCompiler.compile()
20 end
21
22 defp elixir_files(dir) when is_binary(dir) do
23 dir
24 |> File.ls!()
25 |> Enum.map(&Path.join(dir, &1))
26 |> Enum.flat_map(fn path ->
27 if File.dir?(path) do
28 elixir_files(path)
29 else
30 [path]
31 end
32 end)
33 |> Enum.filter(fn path -> String.ends_with?(path, ".ex") end)
34 end
35
36 @doc """
37 POSIX-compliant check if command is available in the system
38
39 ## Examples
40 iex> command_available?("git")
41 true
42 iex> command_available?("wrongcmd")
43 false
44
45 """
46 @spec command_available?(String.t()) :: boolean()
47 def command_available?(command) do
48 case :os.find_executable(String.to_charlist(command)) do
49 false -> false
50 _ -> true
51 end
52 end
53
54 @doc "creates the uniq temporary directory"
55 @spec tmp_dir(String.t()) :: {:ok, String.t()} | {:error, :file.posix()}
56 def tmp_dir(prefix \\ "") do
57 sub_dir =
58 [
59 prefix,
60 Timex.to_unix(Timex.now()),
61 :os.getpid(),
62 String.downcase(Integer.to_string(:rand.uniform(0x100000000), 36))
63 ]
64 |> Enum.join("-")
65
66 tmp_dir = Path.join(System.tmp_dir!(), sub_dir)
67
68 case File.mkdir(tmp_dir) do
69 :ok -> {:ok, tmp_dir}
70 error -> error
71 end
72 end
73
74 @spec posix_error_message(atom()) :: binary()
75 def posix_error_message(code) when code in @posix_error_codes do
76 error_message = Gettext.dgettext(Pleroma.Web.Gettext, "posix_errors", "#{code}")
77 "(POSIX error: #{error_message})"
78 end
79
80 def posix_error_message(_), do: ""
81
82 @doc """
83 Returns [timeout: integer] suitable for passing as an option to Repo functions.
84
85 This function detects if the execution was triggered from IEx shell, Mix task, or
86 ./bin/pleroma_ctl and sets the timeout to :infinity, else returns the default timeout value.
87 """
88 @spec query_timeout() :: [timeout: integer]
89 def query_timeout do
90 {parent, _, _, _} = Process.info(self(), :current_stacktrace) |> elem(1) |> Enum.fetch!(2)
91
92 cond do
93 parent |> to_string |> String.starts_with?("Elixir.Mix.Task") -> [timeout: :infinity]
94 parent == :erl_eval -> [timeout: :infinity]
95 true -> [timeout: @repo_timeout]
96 end
97 end
98 end