#ifndef H_CORAL_LIB_SAFE_SAVE_FILE
#define H_CORAL_LIB_SAFE_SAVE_FILE

#include <string>
#include <iostream>

/*
 * This file aims define routines for saving a file safely
 * on a multi-user system without any possibility for
 * accidentally overwriting another file by mistake, e.g.
 * via a symlink attack.
 *
 * It is invoked with a save_file object reference
 * which is responsible for doing the actual saving of
 * the file contents.
 *
 * Throws on any error.
 *
 * The implementation is BUGgy as hell from a security point-of-view.
 */
namespace coral {

	class save_file {
	public:
		save_file() { ; }
		virtual ~save_file() { ; }
		/** Override this. May throw exceptions. */
		virtual void operator()(std::ostream& out) = 0;

	};

	void safe_save_file(const std::string& filename,
			    save_file& output_object)
		throw (std::exception);
}

#endif
