View Source Temp (temp v0.4.9)

Summary

Functions

Cleans up the temporary files tracked.

Returns {:ok, dir_path} where dir_path is the path is the path of the created temporary directory. Returns {:error, reason} if a failure occurs.

Same as mkdir/1, but raises an exception on failure. Otherwise, returns a temporary directory path.

Returns {:ok, fd, file_path} if no callback is passed, or {:ok, file_path} if callback is passed, where fd is the file descriptor of a temporary file and file_path is the path of the temporary file. When no callback is passed, the file descriptor should be closed. Returns {:error, reason} if a failure occurs.

Same as open/1, but raises an exception on failure.

Returns a {:ok, path} where path is a path that can be used freely in the system temporary directory, or {:error, reason} if it fails to get the system temporary directory.

Same as path/1, but raises an exception on failure. Otherwise, returns a temporary path.

Returns :ok when the tracking server used to track temporary files started properly.

Same as track/0, but raises an exception on failure. Otherwise, returns :ok

Add a file to the tracker, so that it will be removed automatically or on Temp.cleanup.

Return the paths currently tracked.

Types

@type options() :: nil | Path.t() | map()

Functions

Link to this function

cleanup(tracker \\ get_tracker!(), opts \\ [])

View Source
@spec cleanup(pid(), Keyword.t()) :: [Path.t()]

Cleans up the temporary files tracked.

@spec mkdir(options()) :: {:ok, Path.t()} | {:error, any()}

Returns {:ok, dir_path} where dir_path is the path is the path of the created temporary directory. Returns {:error, reason} if a failure occurs.

The directory is automatically tracked if tracking is enabled.

Options

See path/1.

@spec mkdir!(options()) :: Path.t() | no_return()

Same as mkdir/1, but raises an exception on failure. Otherwise, returns a temporary directory path.

Link to this function

open(options \\ nil, func \\ nil)

View Source
@spec open(options(), nil | (File.io_device() -> any())) ::
  {:ok, Path.t()} | {:ok, File.io_device(), Path.t()} | {:error, any()}

Returns {:ok, fd, file_path} if no callback is passed, or {:ok, file_path} if callback is passed, where fd is the file descriptor of a temporary file and file_path is the path of the temporary file. When no callback is passed, the file descriptor should be closed. Returns {:error, reason} if a failure occurs.

The resulting file is automatically tracked if tracking is enabled.

Options

See path/1.

Link to this function

open!(options \\ nil, func \\ nil)

View Source
@spec open!(options(), nil | (File.io_device() -> any())) ::
  Path.t() | {File.io_device(), Path.t()} | no_return()

Same as open/1, but raises an exception on failure.

@spec path(options()) :: {:ok, Path.t()} | {:error, String.t()}

Returns a {:ok, path} where path is a path that can be used freely in the system temporary directory, or {:error, reason} if it fails to get the system temporary directory.

This path is not tracked, so any file created will need manually removing, or use track_file/1 to have it removed automatically.

Options

The following options can be used to customize the generated path

  • :prefix - prepends the given prefix to the path

  • :suffix - appends the given suffix to the path, this is useful to generate a file with a particular extension

  • :basedir - places the generated file in the designated base directory instead of the system temporary directory

@spec path!(options()) :: Path.t() | no_return()

Same as path/1, but raises an exception on failure. Otherwise, returns a temporary path.

@spec track() :: Agent.on_start()

Returns :ok when the tracking server used to track temporary files started properly.

@spec track!() :: pid() | no_return()

Same as track/0, but raises an exception on failure. Otherwise, returns :ok

Link to this function

track_file(path, tracker \\ get_tracker())

View Source

Add a file to the tracker, so that it will be removed automatically or on Temp.cleanup.

Link to this function

tracked(tracker \\ get_tracker!())

View Source

Return the paths currently tracked.