The Battle for Wesnoth  1.19.5+dev
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 52 of file fs_commit.hpp.

Constructor & Destructor Documentation

◆ atomic_commit() [1/2]

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

Constructor.

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

Definition at line 185 of file fs_commit.cpp.

References dest_name_, LOG_FS, and temp_name_.

◆ atomic_commit() [2/2]

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

◆ ~atomic_commit()

filesystem::atomic_commit::~atomic_commit ( )

Definition at line 199 of file fs_commit.cpp.

References ERR_FS, and temp_name_.

Member Function Documentation

◆ commit()

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 206 of file fs_commit.cpp.

References dest_name_, ERR_FS, LOG_FS, outfd_, and temp_name_.

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

◆ operator=()

atomic_commit& filesystem::atomic_commit::operator= ( const atomic_commit )
delete

◆ ostream()

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 73 of file fs_commit.hpp.

References out_.

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

Member Data Documentation

◆ dest_name_

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

Definition at line 87 of file fs_commit.hpp.

Referenced by atomic_commit(), and commit().

◆ out_

scoped_ostream filesystem::atomic_commit::out_
private

Definition at line 88 of file fs_commit.hpp.

Referenced by ostream().

◆ outfd_

int filesystem::atomic_commit::outfd_
private

Definition at line 90 of file fs_commit.hpp.

Referenced by commit().

◆ temp_name_

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

Definition at line 86 of file fs_commit.hpp.

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


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