The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Public Member Functions | Private Attributes | List of all members
filesystem::atomic_commit Class Reference

Wrapper class that guarantees that file commit atomicity. More...

#include <fs_commit.hpp>

Public Member Functions

 atomic_commit (const std::string &filename)
 Constructor. More...
 
 atomic_commit (const atomic_commit &)=delete
 
atomic_commitoperator= (const atomic_commit &)=delete
 
 ~atomic_commit ()
 
scoped_ostreamostream ()
 Returns the write stream associated with the file. More...
 
void commit ()
 Commits the new file contents to disk atomically. More...
 

Private Attributes

std::string temp_name_
 
std::string dest_name_
 
scoped_ostream out_
 
int outfd_
 

Detailed Description

Wrapper class that guarantees that file commit atomicity.

It is possible for a signal or exception to cause a file write to be aborted in the middle of the process, leaving potentially inconsistent contents behind which may be read by the same or another application later and result in errors or further data loss.

This wrapper prevents this by providing callers with an interface to request a write stream that will be actually associated to a temporary file. Once the caller is done with the file, it should call the commit() method to complete the process so that the temporary replaces the original in an atomic step.

The rationale for using an explicit commit() method instead of the class destructor is that the destructor will also be invoked during exception handling, which could still cause the destination file to end up in an inconsistent state.

Note that if the destructor is invoked before commit() is, the temporary file will be left behind. This is deliberate so as to provide a way for the user to look at the resulting file and optionally try to reconcile it against the original.

Definition at line 51 of file fs_commit.hpp.

Constructor & Destructor Documentation

filesystem::atomic_commit::atomic_commit ( const std::string &  filename)

Constructor.

Exceptions
filesystem::io_exceptionif the operation fails in some way.

Definition at line 72 of file fs_commit.cpp.

References dest_name_, LOG_FS, and temp_name_.

filesystem::atomic_commit::atomic_commit ( const atomic_commit )
delete
filesystem::atomic_commit::~atomic_commit ( )

Definition at line 83 of file fs_commit.cpp.

References ERR_FS, and temp_name_.

Member Function Documentation

void filesystem::atomic_commit::commit ( )

Commits the new file contents to disk atomically.

Exceptions
filesystem::io_exceptionif the operation fails in some way.

Definition at line 90 of file fs_commit.cpp.

References dest_name_, ERR_FS, filesystem::file_exists(), LOG_FS, outfd_, gui2::dialogs::tip::remove(), and temp_name_.

Referenced by campaignd::server::handle_upload(), and campaignd::server::write_config().

atomic_commit& filesystem::atomic_commit::operator= ( const atomic_commit )
delete
scoped_ostream& filesystem::atomic_commit::ostream ( )
inline

Returns the write stream associated with the file.

Before commit() is invoked, this refers to the temporary file; afterwards, to the destination.

Definition at line 72 of file fs_commit.hpp.

References out_.

Referenced by campaignd::server::handle_upload(), and campaignd::server::write_config().

Member Data Documentation

std::string filesystem::atomic_commit::dest_name_
private

Definition at line 86 of file fs_commit.hpp.

Referenced by atomic_commit(), and commit().

scoped_ostream filesystem::atomic_commit::out_
private

Definition at line 87 of file fs_commit.hpp.

Referenced by ostream().

int filesystem::atomic_commit::outfd_
private

Definition at line 89 of file fs_commit.hpp.

Referenced by commit().

std::string filesystem::atomic_commit::temp_name_
private

Definition at line 85 of file fs_commit.hpp.

Referenced by atomic_commit(), commit(), and ~atomic_commit().


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