Debian Codegraph @

src/common/boost_serialization_helper.h

raw symbols SCIP
1 // Copyright (c) 2014-2022, 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 #include "common/util.h"
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
81 boost::archive::portable_binary_oarchive a(data_file);
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
108 boost::archive::portable_binary_iarchive a(data_file);
109 a >> obj;
110 }
111 catch(...)
112 {
113 // if failed, try reading in unportable mode
114 tools::copy_file(file_path, file_path + ".unportable");
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 }