[DEV] add safe store

This commit is contained in:
Edouard DUPIN 2018-10-24 22:52:02 +02:00
parent 9844ad4bd4
commit 5747de93a8
2 changed files with 40 additions and 0 deletions

View File

@ -7,6 +7,8 @@
#include <exml/Document.hpp>
#include <exml/debug.hpp>
#include <exml/internal/Document.hpp>
#include <etk/path/fileSystem.hpp>
#include <etk/uri/uri.hpp>
exml::Document::Document(ememory::SharedPtr<exml::internal::Node> _internalNode) :
exml::Element(_internalNode) {
@ -82,6 +84,35 @@ bool exml::Document::store(const etk::Uri& _uri) {
return static_cast<exml::internal::Document*>(m_data.get())->store(_uri);
}
bool exml::Document::storeSafe(const etk::Path& _path) {
if (m_data == null) {
EXML_DEBUG("Can not store (null) ...");
return false;
}
bool done = static_cast<exml::internal::Document*>(m_data.get())->store(_path+".tmp");
if (done == false) {
return false;
}
return etk::path::move(_path+".tmp", _path);
}
bool exml::Document::storeSafe(const etk::Uri& _uri) {
if (m_data == null) {
EXML_DEBUG("Can not store (null) ...");
return false;
}
if (etk::uri::canMove(_uri) == false) {
return static_cast<exml::internal::Document*>(m_data.get())->store(_uri);
}
etk::Uri uriTmp = _uri;
uriTmp.setPath(uriTmp.getPath() + ".tmp");
bool done = static_cast<exml::internal::Document*>(m_data.get())->store(uriTmp);
if (done == false) {
return false;
}
return etk::uri::move(uriTmp, _uri);
}
void exml::Document::display() {
if (m_data == null) {
EXML_DEBUG("Can not display (null) ...");

View File

@ -78,6 +78,15 @@ namespace exml {
bool store(const etk::Path& _path);
/// @previous
bool store(const etk::Uri& _uri);
/**
* @brief Store the Json in the file (safe mode mean that the file is store in a second file xxx.tmp and moved in the file xxx (only one mode to be really safe with filesystem ...)
* @param[in] _path/_uri Path/URI of the json
* @return false : An error occured
* @return true : Parsing is OK
*/
bool storeSafe(const etk::Path& _path);
/// @previous
bool storeSafe(const etk::Uri& _uri);
/**
* @brief Display the Document on console
*/