1-- Copyright (c) 2014, 2021, 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 also distributed 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 included with MySQL.
13--
14-- This program is distributed in the hope that it will be useful,
15-- but WITHOUT ANY WARRANTY; without even the implied warranty of
16-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17-- GNU General Public License, version 2.0, for more details.
18--
19-- You should have received a copy of the GNU General Public License
20-- along with this program; if not, write to the Free Software
21-- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
23DROP FUNCTION IF EXISTS format_path;
24
25DELIMITER $$
26
27CREATE DEFINER='mysql.sys'@'localhost' FUNCTION format_path (
28        in_path VARCHAR(512)
29    )
30    RETURNS VARCHAR(512) CHARSET UTF8
31    COMMENT '
32Description
33-----------
34
35Takes a raw path value, and strips out the datadir or tmpdir
36replacing with @@datadir and @@tmpdir respectively.
37
38Also normalizes the paths across operating systems, so backslashes
39on Windows are converted to forward slashes
40
41Parameters
42-----------
43
44path (VARCHAR(512)):
45  The raw file path value to format.
46
47Returns
48-----------
49
50VARCHAR(512) CHARSET UTF8
51
52Example
53-----------
54
55mysql> select @@datadir;
56+-----------------------------------------------+
57| @@datadir                                     |
58+-----------------------------------------------+
59| /Users/mark/sandboxes/SmallTree/AMaster/data/ |
60+-----------------------------------------------+
611 row in set (0.06 sec)
62
63mysql> select format_path(\'/Users/mark/sandboxes/SmallTree/AMaster/data/mysql/proc.MYD\') AS path;
64+--------------------------+
65| path                     |
66+--------------------------+
67| @@datadir/mysql/proc.MYD |
68+--------------------------+
691 row in set (0.03 sec)
70'
71    SQL SECURITY INVOKER
72    DETERMINISTIC
73    NO SQL
74BEGIN
75  DECLARE v_path VARCHAR(512);
76  DECLARE v_undo_dir VARCHAR(1024);
77
78  DECLARE path_separator CHAR(1) DEFAULT '/';
79
80  IF @@global.version_compile_os LIKE 'win%' THEN
81    SET path_separator = '\\';
82  END IF;
83
84  -- OSX hides /private/ in variables, but Performance Schema does not
85  IF in_path LIKE '/private/%' THEN
86    SET v_path = REPLACE(in_path, '/private', '');
87  ELSE
88    SET v_path = in_path;
89  END IF;
90
91  -- @@global.innodb_undo_directory is only set when separate undo logs are used
92  SET v_undo_dir = IFNULL((SELECT VARIABLE_VALUE FROM performance_schema.global_variables WHERE VARIABLE_NAME = 'innodb_undo_directory'), '');
93
94  IF v_path IS NULL THEN
95    RETURN NULL;
96  ELSEIF v_path LIKE CONCAT(@@global.datadir, IF(SUBSTRING(@@global.datadir, -1) = path_separator, '%', CONCAT(path_separator, '%'))) ESCAPE '|' THEN
97    SET v_path = REPLACE(v_path, @@global.datadir, CONCAT('@@datadir', IF(SUBSTRING(@@global.datadir, -1) = path_separator, path_separator, '')));
98  ELSEIF v_path LIKE CONCAT(@@global.tmpdir, IF(SUBSTRING(@@global.tmpdir, -1) = path_separator, '%', CONCAT(path_separator, '%'))) ESCAPE '|' THEN
99    SET v_path = REPLACE(v_path, @@global.tmpdir, CONCAT('@@tmpdir', IF(SUBSTRING(@@global.tmpdir, -1) = path_separator, path_separator, '')));
100  ELSEIF v_path LIKE CONCAT(@@global.slave_load_tmpdir, IF(SUBSTRING(@@global.slave_load_tmpdir, -1) = path_separator, '%', CONCAT(path_separator, '%'))) ESCAPE '|' THEN
101    SET v_path = REPLACE(v_path, @@global.slave_load_tmpdir, CONCAT('@@slave_load_tmpdir', IF(SUBSTRING(@@global.slave_load_tmpdir, -1) = path_separator, path_separator, '')));
102  ELSEIF v_path LIKE CONCAT(@@global.innodb_data_home_dir, IF(SUBSTRING(@@global.innodb_data_home_dir, -1) = path_separator, '%', CONCAT(path_separator, '%'))) ESCAPE '|' THEN
103    SET v_path = REPLACE(v_path, @@global.innodb_data_home_dir, CONCAT('@@innodb_data_home_dir', IF(SUBSTRING(@@global.innodb_data_home_dir, -1) = path_separator, path_separator, '')));
104  ELSEIF v_path LIKE CONCAT(@@global.innodb_log_group_home_dir, IF(SUBSTRING(@@global.innodb_log_group_home_dir, -1) = path_separator, '%', CONCAT(path_separator, '%'))) ESCAPE '|' THEN
105    SET v_path = REPLACE(v_path, @@global.innodb_log_group_home_dir, CONCAT('@@innodb_log_group_home_dir', IF(SUBSTRING(@@global.innodb_log_group_home_dir, -1) = path_separator, path_separator, '')));
106  ELSEIF v_path LIKE CONCAT(v_undo_dir, IF(SUBSTRING(v_undo_dir, -1) = path_separator, '%', CONCAT(path_separator, '%'))) ESCAPE '|' THEN
107    SET v_path = REPLACE(v_path, v_undo_dir, CONCAT('@@innodb_undo_directory', IF(SUBSTRING(v_undo_dir, -1) = path_separator, path_separator, '')));
108  ELSEIF v_path LIKE CONCAT(@@global.basedir, IF(SUBSTRING(@@global.basedir, -1) = path_separator, '%', CONCAT(path_separator, '%'))) ESCAPE '|' THEN
109    SET v_path = REPLACE(v_path, @@global.basedir, CONCAT('@@basedir', IF(SUBSTRING(@@global.basedir, -1) = path_separator, path_separator, '')));
110  END IF;
111
112  RETURN v_path;
113END$$
114
115DELIMITER ;
116