|
1 | 1 | %% @author Marc Worrell <[email protected]> |
2 | | -%% @copyright 2011-2020 Marc Worrell |
3 | | -%% @doc Simple temporary file handling, deletes the file when the calling process stops or crashes. |
| 2 | +%% @copyright 2011-2025 Marc Worrell |
| 3 | +%% @doc Simple temporary file handling, deletes the file when the calling process |
| 4 | +%% stops or crashes. If a file is created then a watchdog process is started to |
| 5 | +%% monitor the calling process. If the calling process is stopped or crashes then |
| 6 | +%% the file is deleted. |
| 7 | +%% |
| 8 | +%% Periodically call cleanup/0 to delete temporary files older than 24 hours. |
| 9 | +%% |
| 10 | +%% Temporary files are created in the directory specified by the TMP or TEMP environment |
| 11 | +%% variable, or /tmp if none is set. |
| 12 | +%% @end |
4 | 13 |
|
5 | | -%% Copyright 2011-2020 Marc Worrell |
| 14 | +%% Copyright 2011-2025 Marc Worrell |
6 | 15 | %% |
7 | 16 | %% Licensed under the Apache License, Version 2.0 (the "License"); |
8 | 17 | %% you may not use this file except in compliance with the License. |
|
22 | 31 | -export([ |
23 | 32 | new/0, |
24 | 33 | new/1, |
| 34 | + copy/1, |
25 | 35 |
|
26 | 36 | monitored_new/0, |
27 | 37 | monitored_new/1, |
@@ -61,6 +71,20 @@ new(Extension) -> |
61 | 71 | {ok, {_Pid, Filename}} = monitored_new(Extension), |
62 | 72 | Filename. |
63 | 73 |
|
| 74 | +%% @doc Copy a file to a new tempfile, start a monitoring process to clean it up after use. |
| 75 | +-spec copy(File) -> {ok, NewFile} | {error, Reason} when |
| 76 | + File :: file:filename_all(), |
| 77 | + NewFile :: file:filename_all(), |
| 78 | + Reason :: term(). |
| 79 | +copy(File) -> |
| 80 | + {ok, {_Pid, NewFile}} = monitored_new(), |
| 81 | + case file:copy(File, NewFile) of |
| 82 | + {ok, _BytesCopied} -> |
| 83 | + {ok, NewFile}; |
| 84 | + {error, _} = Error -> |
| 85 | + Error |
| 86 | + end. |
| 87 | + |
64 | 88 | %% @doc Like new/0 but also return the Pid of the monitoring process. |
65 | 89 | -spec monitored_new() -> {ok, {pid(), file:filename_all()}}. |
66 | 90 | monitored_new() -> |
|
0 commit comments