MySQL 9.1.0
Source Code Documentation
auto_cleaner.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2020, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTER_AUTO_CLEANER_INCLUDED
27#define ROUTER_AUTO_CLEANER_INCLUDED
28
29#include <functional>
30#include <map>
31#include <string>
32#include <vector>
33
35
36namespace mysqlrouter {
37
38/**
39 * Automatic cleanup on scope exit utility class.
40 *
41 * Automatic cleanup takes place on AutoCleaner object destruction. It allows
42 * to:
43 * - cleanup files
44 * - remove directories (non-recursive and recursive)
45 * - revert files from auto-managed backup file
46 * - revert files from a user provided backup file
47 * - call user provided callbacks
48 * Callbacks are called in order they were added into AutoCleaner. Files and
49 * directories are being deleted in a reverse order in which they were added to
50 * the AutoCleaner. If automatic cleanup fails (file or directory could not be
51 * deleted, failed to revert a file) a proper error is being logged and
52 * AutoCleaner continues with the next cleanup steps.
53 *
54 * AutoCleaner allows to clear its state so that no action will be taken on
55 * scope exit (auto generated backup files will be cleaned up in such case).
56 *
57 * Adding an action (cleanup, revert) is done once per file. It is not possible
58 * to add a second action for the same file (such add call will fail, initial
59 * action will not be affected). Adding a revert file action may fail if initial
60 * or backup files could not be opened.
61 */
63 public:
64 void add_file_delete(const std::string &file);
65 void add_directory_delete(const std::string &d, bool recursive = false);
66 void add_file_revert(const std::string &file);
67 void add_file_revert(const std::string &file, const std::string &backup_file);
68 void add_cleanup_callback(std::function<void()> callback) noexcept;
69 void clear_cleanup_callbacks() noexcept;
70 void remove(const std::string &file) noexcept;
71 void clear();
72
73 AutoCleaner() = default;
75
76 AutoCleaner(AutoCleaner &&other) noexcept = default;
77 AutoCleaner &operator=(AutoCleaner &&other) = default;
78
79 AutoCleaner(const AutoCleaner &) = delete;
80 AutoCleaner &operator=(const AutoCleaner &) = delete;
81
82 private:
83 enum Type { Directory, DirectoryRecursive, File, FileBackup };
84
85 /*
86 * The vector stores all the files that are scheduled to be auto-removed or
87 * restored from backup if clean() wasn't called.
88 * The first value of pair is a name of file to backup, and second is a pair
89 * of backup's type and name of backup file (used only for FileBackup type).
90 */
91 std::vector<std::pair<std::string, std::pair<Type, std::string>>> files_;
92
93 /*
94 * The vector stores callbacks that are scheduled to be called if clean()
95 * wasn't called. Callbacks are not allowed to throw exceptions.
96 */
97 std::vector<std::function<void()>> callbacks_;
98};
99
100} // namespace mysqlrouter
101#endif // ROUTER_AUTO_CLEANER_INCLUDED
Automatic cleanup on scope exit utility class.
Definition: auto_cleaner.h:62
AutoCleaner(AutoCleaner &&other) noexcept=default
Type
Definition: auto_cleaner.h:83
@ Directory
Definition: auto_cleaner.h:83
AutoCleaner & operator=(const AutoCleaner &)=delete
AutoCleaner(const AutoCleaner &)=delete
AutoCleaner & operator=(AutoCleaner &&other)=default
std::vector< std::pair< std::string, std::pair< Type, std::string > > > files_
Definition: auto_cleaner.h:91
std::vector< std::function< void()> > callbacks_
Definition: auto_cleaner.h:97
int File
Definition: my_io_bits.h:51
Definition: os0file.h:89
Definition: base64.h:43
static mysql_service_status_t remove(reference_caching_channel channel, const char *implementation_name) noexcept
Definition: component.cc:137
static mysql_service_status_t clear(reference_caching_channel channel) noexcept
Definition: component.cc:146
std::vector< T, ut::allocator< T > > vector
Specialization of vector which uses allocator.
Definition: ut0new.h:2876
#define ROUTER_LIB_EXPORT
Definition: router_export.h:15