my_symlink.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* Copyright (c) 2001, 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_symlink.cc
  24. */
  25. #include "my_config.h"
  26. #include <errno.h>
  27. #include <limits.h>
  28. #include <stdlib.h>
  29. #ifdef HAVE_UNISTD_H
  30. #include <unistd.h>
  31. #endif
  32. #include "m_string.h"
  33. #include "map_helpers.h"
  34. #include "my_dbug.h"
  35. #include "my_dir.h"
  36. #include "my_inttypes.h"
  37. #include "my_io.h"
  38. #include "my_sys.h"
  39. #include "my_thread_local.h"
  40. #include "mysys_err.h"
  41. #ifndef _WIN32
  42. #include <sys/stat.h>
  43. #endif
  44. /*
  45. Reads the content of a symbolic link
  46. If the file is not a symbolic link, return the original file name in to.
  47. RETURN
  48. 0 If filename was a symlink, (to will be set to value of symlink)
  49. 1 If filename was a normal file (to will be set to filename)
  50. -1 on error.
  51. */
  52. int my_readlink(char *to, const char *filename, myf MyFlags) {
  53. #ifdef _WIN32
  54. my_stpcpy(to, filename);
  55. return 1;
  56. #else
  57. int result = 0;
  58. int length;
  59. DBUG_ENTER("my_readlink");
  60. if ((length = readlink(filename, to, FN_REFLEN - 1)) < 0) {
  61. /* Don't give an error if this wasn't a symlink */
  62. set_my_errno(errno);
  63. if (my_errno() == EINVAL) {
  64. result = 1;
  65. my_stpcpy(to, filename);
  66. } else {
  67. if (MyFlags & MY_WME) {
  68. char errbuf[MYSYS_STRERROR_SIZE];
  69. my_error(EE_CANT_READLINK, MYF(0), filename, errno,
  70. my_strerror(errbuf, sizeof(errbuf), errno));
  71. }
  72. result = -1;
  73. }
  74. } else
  75. to[length] = 0;
  76. DBUG_PRINT("exit", ("result: %d", result));
  77. DBUG_RETURN(result);
  78. #endif /* !_WIN32 */
  79. }
  80. /* Create a symbolic link */
  81. #ifndef _WIN32
  82. int my_symlink(const char *content, const char *linkname, myf MyFlags) {
  83. int result;
  84. DBUG_ENTER("my_symlink");
  85. DBUG_PRINT("enter", ("content: %s linkname: %s", content, linkname));
  86. result = 0;
  87. if (symlink(content, linkname)) {
  88. result = -1;
  89. set_my_errno(errno);
  90. if (MyFlags & MY_WME) {
  91. char errbuf[MYSYS_STRERROR_SIZE];
  92. my_error(EE_CANT_SYMLINK, MYF(0), linkname, content, errno,
  93. my_strerror(errbuf, sizeof(errbuf), errno));
  94. }
  95. } else if ((MyFlags & MY_SYNC_DIR) && my_sync_dir_by_file(linkname, MyFlags))
  96. result = -1;
  97. DBUG_RETURN(result);
  98. }
  99. #endif /* !_WIN32 */
  100. int my_is_symlink(const char *filename, ST_FILE_ID *file_id) {
  101. #ifndef _WIN32
  102. struct stat stat_buff;
  103. int result = !lstat(filename, &stat_buff) && S_ISLNK(stat_buff.st_mode);
  104. if (file_id && !result) {
  105. file_id->st_dev = stat_buff.st_dev;
  106. file_id->st_ino = stat_buff.st_ino;
  107. }
  108. return result;
  109. #else
  110. DWORD dwAttr = GetFileAttributes(filename);
  111. return (dwAttr != INVALID_FILE_ATTRIBUTES) &&
  112. (dwAttr & FILE_ATTRIBUTE_REPARSE_POINT);
  113. #endif
  114. }
  115. /*
  116. Resolve all symbolic links in path
  117. 'to' may be equal to 'filename'
  118. */
  119. int my_realpath(char *to, const char *filename, myf MyFlags) {
  120. #ifndef _WIN32
  121. int result = 0;
  122. DBUG_ENTER("my_realpath");
  123. DBUG_PRINT("info", ("executing realpath"));
  124. unique_ptr_free<char> ptr(realpath(filename, nullptr));
  125. if (ptr) {
  126. strmake(to, ptr.get(), FN_REFLEN - 1);
  127. } else {
  128. /*
  129. Realpath didn't work; Use my_load_path() which is a poor substitute
  130. original name but will at least be able to resolve paths that starts
  131. with '.'.
  132. */
  133. DBUG_PRINT("error", ("realpath failed with errno: %d", errno));
  134. set_my_errno(errno);
  135. if (MyFlags & MY_WME) {
  136. char errbuf[MYSYS_STRERROR_SIZE];
  137. my_error(EE_REALPATH, MYF(0), filename, my_errno(),
  138. my_strerror(errbuf, sizeof(errbuf), my_errno()));
  139. }
  140. my_load_path(to, filename, NullS);
  141. result = -1;
  142. }
  143. DBUG_RETURN(result);
  144. #else
  145. int ret = GetFullPathName(filename, FN_REFLEN, to, NULL);
  146. if (ret == 0 || ret > FN_REFLEN) {
  147. set_my_errno((ret > FN_REFLEN) ? ENAMETOOLONG : GetLastError());
  148. if (MyFlags & MY_WME) {
  149. char errbuf[MYSYS_STRERROR_SIZE];
  150. my_error(EE_REALPATH, MYF(0), filename, my_errno(),
  151. my_strerror(errbuf, sizeof(errbuf), my_errno()));
  152. }
  153. /*
  154. GetFullPathName didn't work : use my_load_path() which is a poor
  155. substitute original name but will at least be able to resolve
  156. paths that starts with '.'.
  157. */
  158. my_load_path(to, filename, NullS);
  159. return -1;
  160. }
  161. return 0;
  162. #endif
  163. }
  164. /**
  165. Return non-zero if the file descriptor and a previously lstat-ed file
  166. identified by file_id point to the same file.
  167. */
  168. int my_is_same_file(File file, const ST_FILE_ID *file_id) {
  169. MY_STAT stat_buf;
  170. if (my_fstat(file, &stat_buf) == -1) {
  171. set_my_errno(errno);
  172. return 0;
  173. }
  174. return (stat_buf.st_dev == file_id->st_dev) &&
  175. (stat_buf.st_ino == file_id->st_ino);
  176. }