This document is for a version of USD that is under development. See this page for the current release.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TfSafeOutputFile Class Reference

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

#include <safeOutputFile.h>

Public Member Functions

 TfSafeOutputFile (TfSafeOutputFile &&other)
 
TfSafeOutputFileoperator= (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.
 

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

Constructor & Destructor Documentation

◆ TfSafeOutputFile()

TfSafeOutputFile ( TfSafeOutputFile &&  other)
inline

Definition at line 39 of file safeOutputFile.h.

◆ ~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 74 of file safeOutputFile.h.

◆ IsOpenForUpdate()

TF_API bool IsOpenForUpdate ( ) const

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

◆ operator=()

TfSafeOutputFile & operator= ( TfSafeOutputFile &&  other)
inline

Definition at line 45 of file safeOutputFile.h.

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