All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TfSafeOutputFile Class Reference

#include <safeOutputFile.h>

Public Member Functions

 TfSafeOutputFile ()=default
 TfSafeOutputFile (TfSafeOutputFile &&other)
TfSafeOutputFileoperator= (TfSafeOutputFile &&other)
TF_API ~TfSafeOutputFile ()
 Destructor invokes Close(). More...
TF_API void Close ()
TF_API void Discard ()
FILE * Get () const
 Return the opened FILE *. More...
TF_API FILE * ReleaseUpdatedFile ()
TF_API bool IsOpenForUpdate () const

Static Public Member Functions

static TF_API TfSafeOutputFile Update (std::string const &fileName)
 Open fileName for update ("r+"). More...
static TF_API TfSafeOutputFile Replace (std::string const &fileName)

Detailed Description

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 49 of file safeOutputFile.h.

Constructor & Destructor Documentation

TfSafeOutputFile::TfSafeOutputFile ( )
TfSafeOutputFile::TfSafeOutputFile ( TfSafeOutputFile &&  other)

Definition at line 56 of file safeOutputFile.h.

TF_API TfSafeOutputFile::~TfSafeOutputFile ( )

Destructor invokes Close().

Member Function Documentation

TF_API void TfSafeOutputFile::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 TfSafeOutputFile::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.

FILE* TfSafeOutputFile::Get ( ) const

Return the opened FILE *.

Definition at line 91 of file safeOutputFile.h.

TF_API bool TfSafeOutputFile::IsOpenForUpdate ( ) const

Return true if this TfSafeOutputFile was created by a call to Update(), false otherwise.

TfSafeOutputFile& TfSafeOutputFile::operator= ( TfSafeOutputFile &&  other)

Definition at line 62 of file safeOutputFile.h.

TF_API FILE* TfSafeOutputFile::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 TF_API TfSafeOutputFile TfSafeOutputFile::Replace ( std::string const fileName)

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 TF_API TfSafeOutputFile TfSafeOutputFile::Update ( std::string const fileName)

Open fileName for update ("r+").

The documentation for this class was generated from the following file: