mf_dirname.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Copyright (c) 2000, 2018, 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. #include <stddef.h>
  23. #ifdef _WIN32
  24. #include "m_ctype.h"
  25. #endif
  26. #include "m_string.h"
  27. #include "my_dbug.h"
  28. #include "my_io.h"
  29. #include "my_sys.h" // IWYU pragma: keep
  30. /**
  31. @file mysys/mf_dirname.cc
  32. */
  33. /**
  34. Get the string length of the directory part of name, including the
  35. last FN_LIBCHAR. If name is not a path, return 0.
  36. Pre-condition: 'name' is a '\0'-terminated byte buffer.
  37. @param name path to calculate directory length for.
  38. @return length of directory part
  39. */
  40. size_t dirname_length(const char *name) {
  41. #ifdef _WIN32
  42. CHARSET_INFO *fs = fs_character_set();
  43. #endif
  44. const char *pos = name - 1;
  45. #ifdef FN_DEVCHAR
  46. const char *devchar_pos = strrchr(name, FN_DEVCHAR);
  47. if (devchar_pos != nullptr) pos = devchar_pos;
  48. #endif
  49. const char *gpos = pos++;
  50. for (; *pos; pos++) /* Find last FN_LIBCHAR */
  51. {
  52. #ifdef _WIN32
  53. uint l;
  54. if (use_mb(fs) && (l = my_ismbchar(fs, pos, pos + 3))) {
  55. pos += l - 1;
  56. continue;
  57. }
  58. #endif
  59. if (is_directory_separator(*pos)) gpos = pos;
  60. }
  61. return gpos + 1 - name;
  62. }
  63. /**
  64. Gives directory part of filename. Directory ends with '/'.
  65. Pre-condition: At least FN_REFLEN bytes can be stored in buffer
  66. pointed to by 'to'. 'from' is a '\0'-terminated byte buffer.
  67. Post-condition: At most FN_REFLEN bytes will have been written to
  68. 'to'. If the combined length of 'from' and any expanded elements
  69. exceeds FN_REFLEN-1, the result is truncated and likely not what the
  70. caller expects. If the result is truncated, the return value will be
  71. larger than the length stored in 'to_length'.
  72. @param to destination buffer.
  73. @param name path to get the directory part of.
  74. @param to_res_length store the the number of bytes written into 'to'.
  75. @return Actual length of directory part in 'name' (the number of
  76. bytes which would have been written if 'to' had been large enough).
  77. */
  78. size_t dirname_part(char *to, const char *name, size_t *to_res_length) {
  79. size_t length;
  80. DBUG_ENTER("dirname_part");
  81. DBUG_PRINT("enter", ("'%s'", name));
  82. length = dirname_length(name);
  83. *to_res_length = (size_t)(convert_dirname(to, name, name + length) - to);
  84. DBUG_RETURN(length);
  85. } /* dirname */
  86. #ifndef FN_DEVCHAR
  87. #define FN_DEVCHAR '\0' /* For easier code */
  88. #endif
  89. /**
  90. Convert directory name to use under this system.
  91. Pre-condition: At least FN_REFLEN bytes can be stored in buffer
  92. pointed to by 'to'. 'from' is a '\0'-terminated byte buffer.
  93. Post-condition: At most FN_REFLEN bytes will have been written to
  94. 'to'. If the combined length of 'from' and any expanded elements
  95. exceeds FN_REFLEN-1, the result is truncated and likely not what the
  96. caller expects.
  97. IMPLEMENTATION:
  98. If Windows converts '/' to '\'
  99. Adds a FN_LIBCHAR to end if the result string if there isn't one
  100. and the last isn't dev_char.
  101. Copies data from 'from' until ASCII(0) for until from == from_end
  102. If you want to use the whole 'from' string, just send NullS as the
  103. last argument.
  104. If the result string is larger than FN_REFLEN -1, then it's cut.
  105. @param to destination buffer. Store result here. Must be at least of
  106. size min(FN_REFLEN, strlen(from) + 1) to make room for adding
  107. FN_LIBCHAR at the end.
  108. @param from Original filename. May be == to
  109. @param from_end Pointer at end of filename (normally end \0)
  110. @return Returns pointer to end \0 in to
  111. */
  112. char *convert_dirname(char *to, const char *from, const char *from_end) {
  113. char *to_org = to;
  114. #ifdef _WIN32
  115. CHARSET_INFO *fs = fs_character_set();
  116. #endif
  117. DBUG_ENTER("convert_dirname");
  118. /* We use -2 here, becasue we need place for the last FN_LIBCHAR */
  119. if (!from_end || (from_end - from) > FN_REFLEN - 2)
  120. from_end = from + FN_REFLEN - 2;
  121. #if FN_LIBCHAR != '/'
  122. {
  123. for (; from < from_end && *from; from++) {
  124. if (*from == '/')
  125. *to++ = FN_LIBCHAR;
  126. else {
  127. #ifdef _WIN32
  128. uint l;
  129. if (use_mb(fs) && (l = my_ismbchar(fs, from, from + 3))) {
  130. memmove(to, from, l);
  131. to += l;
  132. from += l - 1;
  133. to_org = to; /* Don't look inside mbchar */
  134. } else
  135. #endif
  136. {
  137. *to++ = *from;
  138. }
  139. }
  140. }
  141. *to = 0;
  142. }
  143. #else
  144. /* This is ok even if to == from, becasue we need to cut the string */
  145. to = strmake(to, from, (size_t)(from_end - from));
  146. #endif
  147. /* Add FN_LIBCHAR to the end of directory path */
  148. if (to != to_org && (to[-1] != FN_LIBCHAR && to[-1] != FN_DEVCHAR)) {
  149. *to++ = FN_LIBCHAR;
  150. *to = 0;
  151. }
  152. DBUG_RETURN(to); /* Pointer to end of dir */
  153. } /* convert_dirname */