Electroneum
boost_serialization_helper.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2021, The Electroneum Project
2 // Copyrights(c) 2014-2019, The Monero Project
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
31 
32 #pragma once
33 
34 #include <boost/archive/binary_iarchive.hpp>
37 #include <boost/filesystem/operations.hpp>
38 
39 
40 namespace tools
41 {
42  template<class t_object>
43  bool serialize_obj_to_file(t_object& obj, const std::string& file_path)
44  {
45  TRY_ENTRY();
46 #if defined(_MSC_VER)
47  // Need to know HANDLE of file to call FlushFileBuffers
48  HANDLE data_file_handle = ::CreateFile(file_path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
49  if (INVALID_HANDLE_VALUE == data_file_handle)
50  return false;
51 
52  int data_file_descriptor = _open_osfhandle((intptr_t)data_file_handle, 0);
53  if (-1 == data_file_descriptor)
54  {
55  ::CloseHandle(data_file_handle);
56  return false;
57  }
58 
59  const std::unique_ptr<FILE, tools::close_file> data_file_file{_fdopen(data_file_descriptor, "wb")};
60  if (nullptr == data_file_file)
61  {
62  // Call CloseHandle is not necessary
63  _close(data_file_descriptor);
64  return false;
65  }
66 
67  // HACK: undocumented constructor, this code may not compile
68  std::ofstream data_file(data_file_file.get());
69  if (data_file.fail())
70  {
71  // Call CloseHandle and _close are not necessary
72  return false;
73  }
74 #else
75  std::ofstream data_file;
76  data_file.open(file_path , std::ios_base::binary | std::ios_base::out| std::ios::trunc);
77  if (data_file.fail())
78  return false;
79 #endif
80 
82  a << obj;
83  if (data_file.fail())
84  return false;
85 
86  data_file.flush();
87 #if defined(_MSC_VER)
88  // To make sure the file is fully stored on disk
89  ::FlushFileBuffers(data_file_handle);
90 #endif
91 
92  return true;
93  CATCH_ENTRY_L0("serialize_obj_to_file", false);
94  }
95 
96  template<class t_object>
97  bool unserialize_obj_from_file(t_object& obj, const std::string& file_path)
98  {
99  TRY_ENTRY();
100 
101  std::ifstream data_file;
102  data_file.open( file_path, std::ios_base::binary | std::ios_base::in);
103  if(data_file.fail())
104  return false;
105  try
106  {
107  // first try reading in portable mode
109  a >> obj;
110  }
111  catch(...)
112  {
113  // if failed, try reading in unportable mode
114  boost::filesystem::copy_file(file_path, file_path + ".unportable", boost::filesystem::copy_option::overwrite_if_exists);
115  data_file.close();
116  data_file.open( file_path, std::ios_base::binary | std::ios_base::in);
117  if(data_file.fail())
118  return false;
119  boost::archive::binary_iarchive a(data_file);
120  a >> obj;
121  }
122  return !data_file.fail();
123  CATCH_ENTRY_L0("unserialize_obj_from_file", false);
124  }
125 }
::std::string string
Definition: gtest-port.h:1097
bool unserialize_obj_from_file(t_object &obj, const std::string &file_path)
#define TRY_ENTRY()
Definition: misc_log_ex.h:151
bool serialize_obj_to_file(t_object &obj, const std::string &file_path)
Various Tools.
Definition: tools.cpp:31
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1124
#define CATCH_ENTRY_L0(lacation, return_val)
Definition: misc_log_ex.h:165
_W64 signed int intptr_t
Definition: stdint.h:164