Debian Codegraph @

components/keyrings/common/data_file/reader.cc

raw
1 /* Copyright (c) 2021, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include <fstream> /* std::ifstream */
25
26 #include "reader.h"
27 #include "writer.h" /* File_writer */
28
29 namespace keyring_common {
30
31 namespace data_file {
32 /* Constructor */
33 File_reader::File_reader(const std::string &file, bool read_only,
34 std::string &data)
35 : valid_(false), size_(0) {
36 std::string backup_file(file);
37 backup_file.append(".backup");
38 if (read_data_from_file(backup_file, data)) {
39 /*
40 Backup file found. Likely because server
41 exited in the middle of writing keys to file.
42 */
43
44 /*
45 We are trying to access keyring in RO mode AND
46 we found a valid backup file. So invalidate the
47 reader and return.
48 */
49 if (read_only) return;
50
51 if (data.length() > 0) {
52 /* Complete the operation if backup data is valid */
53 File_writer write_from_backup(file, data, true);
54 valid_ = write_from_backup.valid();
55 if (!valid_) data.clear();
56 } else {
57 /*
58 If backup file was empty, server likely exited
59 even before backup file was written to the disk.
60 In such a case, do not attempt to restore the
61 data because it may overwrite a perfectly good
62 keyring data file.
63 */
64 valid_ = read_data_from_file(file, data);
65 remove(backup_file.c_str());
66 }
67 } else {
68 valid_ = read_data_from_file(file, data);
69 }
70 size_ = data.length();
71 }
72
73 bool File_reader::read_data_from_file(const std::string &file,
74 std::string &data) {
75 bool retval = false;
76 std::ifstream file_stream(file, std::ios::in | std::ios::ate);
77 if (!file_stream.is_open()) return false;
78 auto file_length = file_stream.tellg();
79 if (file_length > 0) {
80 data.reserve(file_length);
81 file_stream.seekg(std::ios::beg);
82 char *read_data = new (std::nothrow) char[file_length];
83 if (read_data == nullptr) {
84 file_stream.close();
85 return false;
86 }
87 retval = !(file_stream.read(read_data, file_length).fail());
88 if (retval) data.assign(read_data, file_length);
89 delete[] read_data;
90 } else {
91 /* An empty file is ok. */
92 retval = true;
93 }
94 file_stream.close();
95 return retval;
96 }
97
98 } // namespace data_file
99
100 } // namespace keyring_common