File: unit_test_common.cc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (155 lines) | stat: -rw-r--r-- 4,100 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* Copyright (c) 2020, 2025, Oracle and/or its affiliates.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.

This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation.  The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License, version 2.0, for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#ifndef UNIT_TEST_COMMON
#define UNIT_TEST_COMMON

#include "unit_test_common.h"
#include <string.h>
#ifdef _WIN32
#include <Windows.h>
#include <direct.h>  // getcwd
#else
#include <unistd.h>  // getcwd
#endif

#define FN_REFLEN 2048
bool make_absolute_urn(const char *input_urn, std::string *out_path) {
  char component_dir[FN_REFLEN];
  if (getcwd(component_dir, FN_REFLEN) == nullptr) {
    return true;
  }
  /* Omit scheme prefix to get filename. */
  const char *file = strstr(input_urn, "://");
  if (file == nullptr) {
    return true;
  }
  /* Offset by "://" */
  file += 3;

  /* Compile library path */
  std::string path = component_dir;
  path += "/";
  path += file;

  char path_buff[FN_REFLEN + 1];
  strcpy(path_buff, path.c_str());

  *out_path = "file://";
  *out_path += path_buff;

  return false;
}

char *make_str(char *dst, const char *src, size_t length) {
  while (length--)
    if (!(*dst++ = *src++)) return dst - 1;
  *dst = 0;
  return dst;
}

/*
  Resolve all symbolic links in path
  'to' may be equal to 'filename'
*/
int make_realpath(char *to, const char *filename) {
#ifndef _WIN32
  int result = 0;

  unique_ptr_free<char> ptr(realpath(filename, nullptr));
  if (ptr) {
    make_str(to, ptr.get(), FN_REFLEN - 1);
  } else {
    result = -1;
  }
  return result;
#else
  int ret = GetFullPathName(filename, FN_REFLEN, to, NULL);
  if (ret == 0) {
    return -1;
  }
  return 0;
#endif
}

size_t make_dirname_length(const char *name) {
  const char *pos = name - 1;
  const char *gpos = pos++;
  for (; *pos; pos++) /* Find last FN_LIBCHAR */
  {
    if (directory_separator(*pos)) gpos = pos;
  }
  return gpos + 1 - name;
}

char *make_convert_dirname(char *to, const char *from, const char *from_end) {
  char *to_org = to;

  /* We use -2 here, becasue we need place for the last FN_LIBCHAR */
  if (!from_end || (from_end - from) > FN_REFLEN - 2)
    from_end = from + FN_REFLEN - 2;

#if FN_LIBCHAR != '/'
  {
    for (; from < from_end && *from; from++) {
      if (*from == '/')
        *to++ = FN_LIBCHAR;
      else {
        *to++ = *from;
      }
    }
    *to = 0;
  }
#else
  /* This is ok even if to == from, becasue we need to cut the string */
  to = make_str(to, from, (size_t)(from_end - from));
#endif

  /* Add FN_LIBCHAR to the end of directory path */
  if (to != to_org && (to[-1] != FN_LIBCHAR && to[-1] != FN_DEV_CHAR)) {
    *to++ = FN_LIBCHAR;
    *to = 0;
  }
  return to; /* Pointer to end of dir */
}

size_t make_dirname_part(char *to, const char *name, size_t *to_res_length) {
  size_t length;
  length = make_dirname_length(name);
  *to_res_length = (size_t)(make_convert_dirname(to, name, name + length) - to);
  return length;
}

/* Set new working directory */

int make_setwd(const char *dir) {
  int res;

  if (!dir[0] || (dir[0] == FN_LIBCHAR && dir[1] == 0)) dir = FN_ROOTDIR;
  if ((res = chdir(dir)) != 0) {
    return res;
  }
  return res;
} /* my_setwd */

#endif