my_symlink2.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License, version 2.0,
  4. as published by the Free Software Foundation.
  5. This program is also distributed with certain software (including
  6. but not limited to OpenSSL) that is licensed under separate terms,
  7. as designated in a particular file or component or in included license
  8. documentation. The authors of MySQL hereby grant you an additional
  9. permission to link the program and your derivative works with the
  10. separately licensed software that they have included with MySQL.
  11. Without limiting anything contained in the foregoing, this file,
  12. which is part of C Driver for MySQL (Connector/C), is also subject to the
  13. Universal FOSS Exception, version 1.0, a copy of which can be found at
  14. http://oss.oracle.com/licenses/universal-foss-exception.
  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. 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. /**
  23. @file mysys/my_symlink2.cc
  24. Advanced symlink handling.
  25. This is used in MyISAM to let users symlinks tables to different disk.
  26. The main idea with these functions is to automaticly create, delete and
  27. rename files and symlinks like they would be one unit.
  28. */
  29. #include "my_config.h"
  30. #include <errno.h>
  31. #include <string.h>
  32. #include "m_string.h"
  33. #ifdef HAVE_UNISTD_H
  34. #include <unistd.h>
  35. #endif
  36. #include "my_dbug.h"
  37. #include "my_inttypes.h"
  38. #include "my_io.h"
  39. #include "my_sys.h"
  40. #include "my_thread_local.h"
  41. #include "mysys_err.h"
  42. File my_create_with_symlink(const char *linkname, const char *filename,
  43. int createflags, int access_flags, myf MyFlags) {
  44. #ifdef _WIN32
  45. if (linkname) filename = linkname;
  46. if (!(MyFlags & MY_DELETE_OLD)) {
  47. if (!access(filename, F_OK)) {
  48. char errbuf[MYSYS_STRERROR_SIZE];
  49. errno = EEXIST;
  50. set_my_errno(EEXIST);
  51. my_error(EE_CANTCREATEFILE, MYF(0), filename, EEXIST,
  52. my_strerror(errbuf, sizeof(errbuf), EEXIST));
  53. return -1;
  54. }
  55. }
  56. return my_create(filename, createflags, access_flags, MyFlags);
  57. #else
  58. File file;
  59. int tmp_errno;
  60. /* Test if we should create a link */
  61. int create_link;
  62. char abs_linkname[FN_REFLEN];
  63. DBUG_ENTER("my_create_with_symlink");
  64. DBUG_PRINT("enter",
  65. ("linkname: %s filename: %s", linkname ? linkname : "(null)",
  66. filename ? filename : "(null)"));
  67. if (!my_enable_symlinks) {
  68. DBUG_PRINT("info", ("Symlinks disabled"));
  69. /* Create only the file, not the link and file */
  70. create_link = 0;
  71. if (linkname) filename = linkname;
  72. } else {
  73. if (linkname) my_realpath(abs_linkname, linkname, MYF(0));
  74. create_link = (linkname && strcmp(abs_linkname, filename));
  75. }
  76. if (!(MyFlags & MY_DELETE_OLD)) {
  77. if (!access(filename, F_OK)) {
  78. char errbuf[MYSYS_STRERROR_SIZE];
  79. errno = EEXIST;
  80. set_my_errno(EEXIST);
  81. my_error(EE_CANTCREATEFILE, MYF(0), filename, EEXIST,
  82. my_strerror(errbuf, sizeof(errbuf), EEXIST));
  83. DBUG_RETURN(-1);
  84. }
  85. if (create_link && !access(linkname, F_OK)) {
  86. char errbuf[MYSYS_STRERROR_SIZE];
  87. errno = EEXIST;
  88. set_my_errno(EEXIST);
  89. my_error(EE_CANTCREATEFILE, MYF(0), linkname, EEXIST,
  90. my_strerror(errbuf, sizeof(errbuf), EEXIST));
  91. DBUG_RETURN(-1);
  92. }
  93. }
  94. if ((file = my_create(filename, createflags, access_flags, MyFlags)) >= 0) {
  95. if (create_link) {
  96. /* Delete old link/file */
  97. if (MyFlags & MY_DELETE_OLD) my_delete(linkname, MYF(0));
  98. /* Create link */
  99. if (my_symlink(filename, linkname, MyFlags)) {
  100. /* Fail, remove everything we have done */
  101. tmp_errno = my_errno();
  102. my_close(file, MYF(0));
  103. my_delete(filename, MYF(0));
  104. file = -1;
  105. set_my_errno(tmp_errno);
  106. }
  107. }
  108. }
  109. DBUG_RETURN(file);
  110. #endif // !_WIN32
  111. }
  112. /*
  113. If the file was a symlink, delete both symlink and the file which the
  114. symlink pointed to.
  115. */
  116. int my_delete_with_symlink(const char *name, myf MyFlags) {
  117. #ifdef _WIN32
  118. return my_delete(name, MyFlags);
  119. #else
  120. char link_name[FN_REFLEN];
  121. int was_symlink =
  122. (my_enable_symlinks && !my_readlink(link_name, name, MYF(0)));
  123. int result;
  124. DBUG_ENTER("my_delete_with_symlink");
  125. if (!(result = my_delete(name, MyFlags))) {
  126. if (was_symlink) result = my_delete(link_name, MyFlags);
  127. }
  128. DBUG_RETURN(result);
  129. #endif // _WIN32
  130. }
  131. /*
  132. If the file is a normal file, just rename it.
  133. If the file is a symlink:
  134. - Create a new file with the name 'to' that points at
  135. symlink_dir/basename(to)
  136. - Rename the symlinked file to symlink_dir/basename(to)
  137. - Delete 'from'
  138. If something goes wrong, restore everything.
  139. */
  140. int my_rename_with_symlink(const char *from, const char *to, myf MyFlags) {
  141. #ifdef _WIN32
  142. return my_rename(from, to, MyFlags);
  143. #else
  144. char link_name[FN_REFLEN], tmp_name[FN_REFLEN];
  145. int was_symlink =
  146. (my_enable_symlinks && !my_readlink(link_name, from, MYF(0)));
  147. int result = 0;
  148. int name_is_different;
  149. DBUG_ENTER("my_rename_with_symlink");
  150. if (!was_symlink) DBUG_RETURN(my_rename(from, to, MyFlags));
  151. /* Change filename that symlink pointed to */
  152. my_stpcpy(tmp_name, to);
  153. fn_same(tmp_name, link_name, 1); /* Copy dir */
  154. name_is_different = strcmp(link_name, tmp_name);
  155. if (name_is_different && !access(tmp_name, F_OK)) {
  156. set_my_errno(EEXIST);
  157. if (MyFlags & MY_WME) {
  158. char errbuf[MYSYS_STRERROR_SIZE];
  159. my_error(EE_CANTCREATEFILE, MYF(0), tmp_name, EEXIST,
  160. my_strerror(errbuf, sizeof(errbuf), EEXIST));
  161. }
  162. DBUG_RETURN(1);
  163. }
  164. /* Create new symlink */
  165. if (my_symlink(tmp_name, to, MyFlags)) DBUG_RETURN(1);
  166. /*
  167. Rename symlinked file if the base name didn't change.
  168. This can happen if you use this function where 'from' and 'to' has
  169. the same basename and different directories.
  170. */
  171. if (name_is_different && my_rename(link_name, tmp_name, MyFlags)) {
  172. int save_errno = my_errno();
  173. my_delete(to, MyFlags); /* Remove created symlink */
  174. set_my_errno(save_errno);
  175. DBUG_RETURN(1);
  176. }
  177. /* Remove original symlink */
  178. if (my_delete(from, MyFlags)) {
  179. int save_errno = my_errno();
  180. /* Remove created link */
  181. my_delete(to, MyFlags);
  182. /* Rename file back */
  183. if (strcmp(link_name, tmp_name))
  184. (void)my_rename(tmp_name, link_name, MyFlags);
  185. set_my_errno(save_errno);
  186. result = 1;
  187. }
  188. DBUG_RETURN(result);
  189. #endif /* !_WIN32 */
  190. }