TfSafeOutputFile Class Reference

Opens a file for output, either for update "r+" or to completely replace "w+". More...

Public Member Functions

 TfSafeOutputFile (TfSafeOutputFile &&other)
 
TfSafeOutputFileoperator= (TfSafeOutputFile &&other)
 
TF_API ~TfSafeOutputFile ()
 Destructor invokes Close(). More...
 
TF_API void Close ()
 Close the file. More...
 
TF_API void Discard ()
 Close the file. More...
 
FILE * Get () const
 Return the opened FILE *. More...
 
TF_API FILE * ReleaseUpdatedFile ()
 If the underlying file was opened by Update(), return it. More...
 
TF_API bool IsOpenForUpdate () const
 Return true if this TfSafeOutputFile was created by a call to Update(), false otherwise. More...
 

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)
 Arrange for fileName to be replaced. More...
 

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()

TF_API ~TfSafeOutputFile ( )

Destructor invokes Close().

Member Function Documentation

◆ 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.

◆ Discard()

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.

◆ Get()

FILE* Get ( ) const
inline

Return the opened FILE *.

Definition at line 91 of file safeOutputFile.h.

◆ IsOpenForUpdate()

TF_API bool IsOpenForUpdate ( ) const

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

◆ ReleaseUpdatedFile()

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.

◆ Replace()

static TF_API TfSafeOutputFile Replace ( std::string const &  fileName)
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.

◆ Update()

static TF_API TfSafeOutputFile Update ( std::string const &  fileName)
static

Open fileName for update ("r+").


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