File size: 685 Bytes
8da2481
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
defmodule Plausible.File do
  @moduledoc """
  File helpers for Plausible.
  """

  @doc """
  Moves a file from one location to another.

  Tries renaming first, and falls back to copying and deleting the original.
  """
  @spec mv!(Path.t(), Path.t()) :: :ok
  def mv!(source, destination) do
    File.rename!(source, destination)
  rescue
    e in File.RenameError ->
      try do
        case e.reason do
          # fallback to cp/rm for cross-device moves
          # https://github.com/plausible/analytics/issues/4638
          :exdev -> File.cp!(source, destination)
          _ -> reraise(e, __STACKTRACE__)
        end
      after
        File.rm(source)
      end
  end
end