From c3db4dacf7ffe5b85cad8e98f7c8c399f6380583 Mon Sep 17 00:00:00 2001 From: csoler Date: Thu, 16 Aug 2012 16:59:51 +0000 Subject: [PATCH] added code to avoid corruption of bdboot.txt: first write to tmp file then move the file. Refers on Windows utf16ToUtf8 from libretroshare. This forward reference can be improved. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@5434 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libbitdht/src/bitdht/bdstore.cc | 9 +++++- libbitdht/src/libbitdht.pro | 2 ++ libbitdht/src/util/bdfile.cc | 49 +++++++++++++++++++++++++++++++++ libbitdht/src/util/bdfile.h | 9 ++++++ 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 libbitdht/src/util/bdfile.cc create mode 100644 libbitdht/src/util/bdfile.h diff --git a/libbitdht/src/bitdht/bdstore.cc b/libbitdht/src/bitdht/bdstore.cc index d201234e9..b78cc8f9b 100644 --- a/libbitdht/src/bitdht/bdstore.cc +++ b/libbitdht/src/bitdht/bdstore.cc @@ -26,6 +26,7 @@ #include "bitdht/bdstore.h" #include "util/bdnet.h" +#include "util/bdfile.h" #include #include @@ -213,8 +214,9 @@ void bdStore::writeStore(std::string file) return; } + std::string filetmp = file + ".tmp" ; - FILE *fd = fopen(file.c_str(), "w"); + FILE *fd = fopen(filetmp.c_str(), "w"); if (!fd) { @@ -234,6 +236,11 @@ void bdStore::writeStore(std::string file) } fclose(fd); + + if(!bdFile::renameFile(filetmp,file)) + std::cerr << "Could not rename file !!" << std::endl; + else + std::cerr << "Successfully renamed file " << filetmp << " to " << file << std::endl; } void bdStore::writeStore() diff --git a/libbitdht/src/libbitdht.pro b/libbitdht/src/libbitdht.pro index cb96208e5..dfeb1d187 100644 --- a/libbitdht/src/libbitdht.pro +++ b/libbitdht/src/libbitdht.pro @@ -115,6 +115,7 @@ HEADERS += \ util/bdnet.h \ util/bdthreads.h \ util/bdrandom.h \ + util/bdfile.h \ util/bdstring.h \ udp/udplayer.h \ udp/udpstack.h \ @@ -141,6 +142,7 @@ SOURCES += \ util/bdnet.cc \ util/bdthreads.cc \ util/bdrandom.cc \ + util/bdfile.cc \ util/bdstring.cc \ udp/udplayer.cc \ udp/udpstack.cc \ diff --git a/libbitdht/src/util/bdfile.cc b/libbitdht/src/util/bdfile.cc new file mode 100644 index 000000000..682448f7d --- /dev/null +++ b/libbitdht/src/util/bdfile.cc @@ -0,0 +1,49 @@ +#include +#include +#include "bdfile.h" + +namespace librs { + namespace util { + bool ConvertUtf8ToUtf16(const std::string& source, std::wstring& dest) ; + } +} + +bool bdFile::renameFile(const std::string& from, const std::string& to) +{ + int loops = 0; + +#ifdef WINDOWS_SYS + std::wstring f; + librs::util::ConvertUtf8ToUtf16(from, f); + std::wstring t; + librs::util::ConvertUtf8ToUtf16(to, t); + + while (!MoveFileEx(f.c_str(), t.c_str(), MOVEFILE_REPLACE_EXISTING)) +#else + std::string f(from),t(to) ; + + while (rename(from.c_str(), to.c_str()) < 0) +#endif + { +#ifdef WIN32 + if (GetLastError() != ERROR_ACCESS_DENIED) +#else + if (errno != EACCES) +#endif + /* set errno? */ + return false ; +#ifdef WIN32 + Sleep(100000); /* us */ +#else + usleep(100000); /* us */ +#endif + + if (loops >= 30) + return false ; + + loops++; + } + + return true ; +} + diff --git a/libbitdht/src/util/bdfile.h b/libbitdht/src/util/bdfile.h new file mode 100644 index 000000000..c3d5444d7 --- /dev/null +++ b/libbitdht/src/util/bdfile.h @@ -0,0 +1,9 @@ +#pragma once + +#include + +class bdFile +{ + public: + static bool renameFile(const std::string& from, const std::string& to) ; +} ;