aboutsummaryrefslogtreecommitdiff
path: root/src/common/boost_serialization_helper.h
diff options
context:
space:
mode:
authormydesktop <dev.mc2@gmail.com>2014-05-03 12:19:43 -0400
committermydesktop <dev.mc2@gmail.com>2014-05-03 12:19:43 -0400
commit333f975760c156727dd7408f87e937af856d8bf1 (patch)
tree1928b27eaac60f0b528f17abbc52d5ad812013d0 /src/common/boost_serialization_helper.h
parentMerge branch 'master' of github.com:monero-project/bitmonero (diff)
downloadmonero-333f975760c156727dd7408f87e937af856d8bf1.tar.xz
initial [broken] update
Diffstat (limited to 'src/common/boost_serialization_helper.h')
-rw-r--r--src/common/boost_serialization_helper.h46
1 files changed, 43 insertions, 3 deletions
diff --git a/src/common/boost_serialization_helper.h b/src/common/boost_serialization_helper.h
index 74016ae75..0bf924802 100644
--- a/src/common/boost_serialization_helper.h
+++ b/src/common/boost_serialization_helper.h
@@ -14,15 +14,55 @@ namespace tools
bool serialize_obj_to_file(t_object& obj, const std::string& file_path)
{
TRY_ENTRY();
+#if defined(_MSC_VER)
+ // Need to know HANDLE of file to call FlushFileBuffers
+ HANDLE data_file_handle = ::CreateFile(file_path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
+ if (INVALID_HANDLE_VALUE == data_file_handle)
+ return false;
+
+ int data_file_descriptor = _open_osfhandle((intptr_t)data_file_handle, 0);
+ if (-1 == data_file_descriptor)
+ {
+ ::CloseHandle(data_file_handle);
+ return false;
+ }
+
+ FILE* data_file_file = _fdopen(data_file_descriptor, "wb");
+ if (0 == data_file_file)
+ {
+ // Call CloseHandle is not necessary
+ _close(data_file_descriptor);
+ return false;
+ }
+
+ // HACK: undocumented constructor, this code may not compile
+ std::ofstream data_file(data_file_file);
+ if (data_file.fail())
+ {
+ // Call CloseHandle and _close are not necessary
+ fclose(data_file_file);
+ return false;
+ }
+#else
std::ofstream data_file;
- data_file.open( file_path , std::ios_base::binary | std::ios_base::out| std::ios::trunc);
- if(data_file.fail())
+ data_file.open(file_path , std::ios_base::binary | std::ios_base::out| std::ios::trunc);
+ if (data_file.fail())
return false;
+#endif
boost::archive::binary_oarchive a(data_file);
a << obj;
+ if (data_file.fail())
+ return false;
- return !data_file.fail();
+ data_file.flush();
+#if defined(_MSC_VER)
+ // To make sure the file is fully stored on disk
+ ::FlushFileBuffers(data_file_handle);
+ fclose(data_file_file);
+#endif
+
+ return true;
CATCH_ENTRY_L0("serialize_obj_to_file", false);
}