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