|
Opens a file for output, either for update "r+" or to completely replace "w+". More...
#include <safeOutputFile.h>
Public Member Functions | |
TfSafeOutputFile (TfSafeOutputFile &&other) | |
TfSafeOutputFile & | operator= (TfSafeOutputFile &&other) |
TF_API | ~TfSafeOutputFile () |
Destructor invokes Close(). | |
TF_API void | Close () |
Close the file. | |
TF_API void | Discard () |
Close the file. | |
FILE * | Get () const |
Return the opened FILE *. | |
TF_API FILE * | ReleaseUpdatedFile () |
If the underlying file was opened by Update(), return it. | |
TF_API bool | IsOpenForUpdate () const |
Return true if this TfSafeOutputFile was created by a call to Update(), false otherwise. | |
Static Public Member Functions | |
static TF_API TfSafeOutputFile | Update (std::string const &fileName) |
Open fileName for update ("r+"). | |
static TF_API TfSafeOutputFile | Replace (std::string const &fileName) |
Arrange for fileName to be replaced. | |
Opens a file for output, either for update "r+" or to completely replace "w+".
In the case of complete replacement, create a sibling temporary file to write to instead. When writing is complete, rename the temporary file over the target file. This provides some safety to other processes reading the existing file (at least on unix-like OSs). They will continue to see the existing contents of the old file. If we overwrote the file itself, then those other processes would see undefined, possibly partially updated content.
Definition at line 32 of file safeOutputFile.h.
|
inline |
Definition at line 39 of file safeOutputFile.h.
TF_API ~TfSafeOutputFile | ( | ) |
Destructor invokes Close().
TF_API void Close | ( | ) |
Close the file.
If the file was opened with Replace(), rename the temporary file over the target file to replace it.
TF_API void Discard | ( | ) |
Close the file.
If the file was opened with Replace(), the temporary file is removed and not renamed over the target file. It is an error to call this for files opened for Update.
|
inline |
Return the opened FILE *.
Definition at line 74 of file safeOutputFile.h.
TF_API bool IsOpenForUpdate | ( | ) | const |
Return true if this TfSafeOutputFile was created by a call to Update(), false otherwise.
|
inline |
Definition at line 45 of file safeOutputFile.h.
TF_API FILE * ReleaseUpdatedFile | ( | ) |
If the underlying file was opened by Update(), return it.
The caller takes responsibility for closing the file later. It is an error to call this for files opened for Replace.
|
static |
Arrange for fileName
to be replaced.
Create a sibling temporary file and open that for writing. When Close() is called (or the destructor is run) close the temporary file and rename it over fileName
.
|
static |
Open fileName
for update ("r+").