View Source z_tempfile (zotonic_stdlib v1.26.1)

Simple temporary file handling, deletes the file when the calling process stops or crashes. If a file is created then a watchdog process is started to monitor the calling process. If the calling process is stopped or crashes then the file is deleted.

Periodically call cleanup/0 to delete temporary files older than 24 hours.

Temporary files are created in the directory specified by the TMP or TEMP environment variable, or /tmp if none is set.

Summary

Functions

Delete all tempfiles not modified in the last day.

Copy a file to a new tempfile, start a monitoring process to clean it up after use.

Check if the file is a temporary filename.

Add a process to the tempfile monitor. The tempfile is deleted after all attached processes stopped or are detached.

Remove a process from the tempfile monitor. The tempfile is deleted after all attached processes stopped or are detached.

Like new/0 but also return the Pid of the monitoring process.

Like new/1 but also return the Pid of the monitoring process.

Return a new unique filename, start a monitoring process to clean it up after use. The file must be created and written within 10 seconds, or it will be deleted.

Return a new unique filename, start a monitoring process to clean it up after use. The file must be created and written within 10 seconds, or it will be deleted.

return a unique temporary filename located in the TMP directory.

return a unique temporary filename with the given extension.

Returns the path where to store temporary files.

Functions

cleanup()

-spec cleanup() -> ok.

Delete all tempfiles not modified in the last day.

copy(File)

-spec copy(File) -> {ok, NewFile} | {error, Reason}
              when File :: file:filename_all(), NewFile :: file:filename_all(), Reason :: term().

Copy a file to a new tempfile, start a monitoring process to clean it up after use.

is_tempfile(Filename)

-spec is_tempfile(file:filename_all()) -> boolean().

Check if the file is a temporary filename.

monitored_attach(MonitorPid)

-spec monitored_attach(pid()) -> ok.

Add a process to the tempfile monitor. The tempfile is deleted after all attached processes stopped or are detached.

monitored_detach(MonitorPid)

-spec monitored_detach(pid()) -> ok.

Remove a process from the tempfile monitor. The tempfile is deleted after all attached processes stopped or are detached.

monitored_new()

-spec monitored_new() -> {ok, {pid(), file:filename_all()}}.

Like new/0 but also return the Pid of the monitoring process.

monitored_new(Extension)

-spec monitored_new(string() | binary()) -> {ok, {pid(), file:filename_all()}}.

Like new/1 but also return the Pid of the monitoring process.

new()

-spec new() -> file:filename_all().

Return a new unique filename, start a monitoring process to clean it up after use. The file must be created and written within 10 seconds, or it will be deleted.

new(Extension)

-spec new(string() | binary()) -> file:filename_all().

Return a new unique filename, start a monitoring process to clean it up after use. The file must be created and written within 10 seconds, or it will be deleted.

tempfile()

-spec tempfile() -> file:filename_all().

return a unique temporary filename located in the TMP directory.

tempfile(Extension)

-spec tempfile(string() | binary()) -> file:filename_all().

return a unique temporary filename with the given extension.

temppath()

-spec temppath() -> file:filename_all().

Returns the path where to store temporary files.