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
-spec cleanup() -> ok.
Delete all tempfiles not modified in the last day.
-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.
-spec is_tempfile(file:filename_all()) -> boolean().
Check if the file is a temporary filename.
-spec monitored_attach(pid()) -> ok.
Add a process to the tempfile monitor. The tempfile is deleted after all attached processes stopped or are detached.
-spec monitored_detach(pid()) -> ok.
Remove a process from the tempfile monitor. The tempfile is deleted after all attached processes stopped or are detached.
-spec monitored_new() -> {ok, {pid(), file:filename_all()}}.
Like new/0 but also return the Pid of the monitoring process.
-spec monitored_new(string() | binary()) -> {ok, {pid(), file:filename_all()}}.
Like new/1 but also return the Pid of the monitoring process.
-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.
-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.
-spec tempfile() -> file:filename_all().
return a unique temporary filename located in the TMP directory.
-spec tempfile(string() | binary()) -> file:filename_all().
return a unique temporary filename with the given extension.
-spec temppath() -> file:filename_all().
Returns the path where to store temporary files.