| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2013, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file fsp/fsp0space.cc |
| 29 |
General shared tablespace implementation. |
| 30 |
|
| 31 |
Created 2012-11-16 by Sunny Bains as srv/srv0space.cc |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include "ha_prototypes.h" |
| 35 |
|
| 36 |
#include "fsp0fsp.h" |
| 37 |
#include "fsp0space.h" |
| 38 |
#include "fsp0sysspace.h" |
| 39 |
#include "os0file.h" |
| 40 |
|
| 41 |
#include "my_sys.h" |
| 42 |
|
| 43 |
/** Check if two tablespaces have common data file names. |
| 44 |
@param[in] other_space Tablespace to check against this. |
| 45 |
@return true if they have the same data filenames and paths */ |
| 46 |
bool Tablespace::intersection(const Tablespace *other_space) { |
| 47 |
files_t::const_iterator end = other_space->m_files.end(); |
| 48 |
|
| 49 |
for (files_t::const_iterator it = other_space->m_files.begin(); it != end; |
| 50 |
++it) { |
| 51 |
if (find(it->m_filename)) { |
| 52 |
return (true); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
return (false); |
| 57 |
} |
| 58 |
|
| 59 |
/** Frees the memory allocated by the SysTablespace object. */ |
| 60 |
void Tablespace::shutdown() { |
| 61 |
files_t::iterator end = m_files.end(); |
| 62 |
|
| 63 |
for (files_t::iterator it = m_files.begin(); it != end; ++it) { |
| 64 |
it->shutdown(); |
| 65 |
} |
| 66 |
|
| 67 |
m_files.clear(); |
| 68 |
|
| 69 |
m_space_id = SPACE_UNKNOWN; |
| 70 |
} |
| 71 |
|
| 72 |
/** Note that the data file was found. |
| 73 |
@param[in,out] file Data file object to set */ |
| 74 |
void Tablespace::file_found(Datafile &file) { |
| 75 |
/* Note that the file exists and can be opened |
| 76 |
in the appropriate mode. */ |
| 77 |
file.m_exists = true; |
| 78 |
|
| 79 |
file.set_open_flags(&file == &m_files.front() ? OS_FILE_OPEN_RETRY |
| 80 |
: OS_FILE_OPEN); |
| 81 |
} |
| 82 |
|
| 83 |
/** Find a filename in the list of Datafiles for a tablespace |
| 84 |
@return true if the filename exists in the data files */ |
| 85 |
bool Tablespace::find(const char *filename) { |
| 86 |
files_t::const_iterator end = m_files.end(); |
| 87 |
|
| 88 |
for (files_t::const_iterator it = m_files.begin(); it != end; ++it) { |
| 89 |
if (innobase_strcasecmp(filename, it->m_filename) == 0) { |
| 90 |
return (true); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
return (false); |
| 95 |
} |
| 96 |
|
| 97 |
/** Delete all the data files. */ |
| 98 |
void Tablespace::delete_files() { |
| 99 |
files_t::iterator end = m_files.end(); |
| 100 |
|
| 101 |
for (files_t::iterator it = m_files.begin(); it != end; ++it) { |
| 102 |
it->close(); |
| 103 |
|
| 104 |
bool file_pre_exists; |
| 105 |
bool success = os_file_delete_if_exists(innodb_data_file_key, |
| 106 |
it->m_filepath, &file_pre_exists); |
| 107 |
|
| 108 |
if (success && file_pre_exists) { |
| 109 |
ib::info(ER_IB_MSG_430) << "Removed temporary tablespace data" |
| 110 |
" file: \"" |
| 111 |
<< it->m_name << "\""; |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/** Use the ADD DATAFILE path to create a Datafile object and add |
| 117 |
it to the front of m_files. Parse the datafile path into a path |
| 118 |
and a basename with extension 'ibd'. This datafile_path provided |
| 119 |
may be an absolute or relative path, but it must end with the |
| 120 |
extension .ibd and have a basename of at least 1 byte. |
| 121 |
|
| 122 |
Set tablespace m_path member and add a Datafile with the filename. |
| 123 |
@param[in] datafile_added full path of the tablespace file. */ |
| 124 |
dberr_t Tablespace::add_datafile(const char *datafile_added) { |
| 125 |
/* The path provided ends in ".ibd". This was assured by |
| 126 |
validate_create_tablespace_info() */ |
| 127 |
ut_d(const char *dot = strrchr(datafile_added, '.')); |
| 128 |
ut_ad(dot != nullptr && Fil_path::has_suffix(IBD, dot)); |
| 129 |
|
| 130 |
std::string filepath{datafile_added}; |
| 131 |
|
| 132 |
Fil_path::normalize(filepath); |
| 133 |
|
| 134 |
/* If the path is an absolute path, separate it onto m_path and a |
| 135 |
basename. For relative paths, make the whole thing a basename so that |
| 136 |
it can be appended to the datadir. */ |
| 137 |
bool is_abs_path = Fil_path::is_absolute_path(filepath); |
| 138 |
size_t dirlen = (is_abs_path ? dirname_length(filepath.c_str()) : 0); |
| 139 |
|
| 140 |
/* If the pathname contains a directory separator, fill the |
| 141 |
m_path member which is the default directory for files in this |
| 142 |
tablespace. Leave it null otherwise. */ |
| 143 |
if (dirlen > 0) { |
| 144 |
set_path(filepath.c_str(), dirlen); |
| 145 |
} |
| 146 |
|
| 147 |
Datafile value(m_name, m_flags, FIL_IBD_FILE_INITIAL_SIZE, 0); |
| 148 |
|
| 149 |
/* Now add a new Datafile and set the filepath using the m_path |
| 150 |
created above. */ |
| 151 |
m_files.push_back(value); |
| 152 |
|
| 153 |
Datafile *datafile = &m_files.back(); |
| 154 |
|
| 155 |
if (dirlen > 0) { |
| 156 |
filepath.erase(0, dirlen); |
| 157 |
} |
| 158 |
|
| 159 |
datafile->make_filepath(m_path, filepath.c_str(), IBD); |
| 160 |
|
| 161 |
return (DB_SUCCESS); |
| 162 |
} |