File: mf_tempfile.cc

package info (click to toggle)
mysql-8.0 8.0.44-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,272,892 kB
  • sloc: cpp: 4,685,345; ansic: 412,712; pascal: 108,395; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; python: 21,816; sh: 17,285; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,083; makefile: 1,793; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (345 lines) | stat: -rw-r--r-- 11,763 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* Copyright (c) 2000, 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.

   Without limiting anything contained in the foregoing, this file,
   which is part of C Driver for MySQL (Connector/C), is also subject to the
   Universal FOSS Exception, version 1.0, a copy of which can be found at
   http://oss.oracle.com/licenses/universal-foss-exception.

   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 */

/**
  @file mysys/mf_tempfile.cc
*/

#include "my_config.h"

#include <errno.h>
#ifdef HAVE_O_TMPFILE
#include <fcntl.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "m_string.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_io.h"
#include "my_sys.h"
#include "my_thread_local.h"
#include "mysql/psi/mysql_mutex.h"
#include "mysys/mysys_priv.h"
#include "mysys_err.h"

#ifdef WIN32
#include <fcntl.h>  // O_EXCL
#include <inttypes.h>
#include <rpc.h>  // UUID API
#include <sstream>
#include <string>
/* UuidCreateSequential needs Rpcrt4 library */
#pragma comment(lib, "Rpcrt4")

static void encode_crockford_base32(uint64_t val_to_encode, char *to,
                                    size_t encode_len) {
  assert(encode_len < 14);
  // Using Douglas Crockford's base 32 encoding to reduce confusion
  // between similar letters/numbers and avoid accidental obscenities.
  // See https://www.crockford.com/base32.html
  constexpr char crockford_base32[] = "0123456789abcdefghjkmnpqrstvwxyz";
  // Encode just the least significant encode_len * 5 bits of val_to_encode,
  // starting from the most significant 5 bits
  const auto BITS_PER_CHAR = 5;
  assert(encode_len * BITS_PER_CHAR < 64);
  for (size_t i = encode_len; i--;) {
    to[i] = crockford_base32[(val_to_encode >> BITS_PER_CHAR * i) % 32];
  }
}

/*
get_encoded_uuid_excluding_mac returns true on success, false on failure.
On success, the to buffer contains 16 chars of an encoded UUID and a null
terminator, so the to_len must be at least 17.
*/
static bool get_encoded_uuid_excluding_mac(char *to, size_t to_len) {
  assert(to_len >= 17);
  if (to_len < 17) {
    return false;
  }
  UUID uuid;
  const auto uuid_created = UuidCreateSequential(&uuid);
  if (!((uuid_created == RPC_S_OK) ||
        (uuid_created == RPC_S_UUID_LOCAL_ONLY))) {
    return false;
  }
  /*
    The uuid returned by UuidCreateSequential is a version 1 UUID, as described
    in rfc4122. The rfc4122 structure maps to the UUID structure thus:
      unsigned long  Data1 is time-low
      unsigned short Data2 is time-mid
      unsigned short Data3 is time-high-and-version
      unsigned char  Data4[0] is clock-seq-and-reserved
      unsigned char  Data4[1] is clock-seq-low
      unsigned char  Data4[2..7] is node (MAC address)

      As the MAC address is (usually!) unchanging for a machine, it doesn't
      help to include it in a "random" temporary filename, so it is not
      included in the encoded uuid. Including a machine's MAC address in a
      temporary filename would also leak information unnecessarily.
  */
  encode_crockford_base32(
      uuid.Data1 + (static_cast<uint64_t>(uuid.Data4[0]) << 32), to, 8);
  encode_crockford_base32(uuid.Data2 +
                              (static_cast<uint64_t>(uuid.Data3) << 16) +
                              (static_cast<uint64_t>(uuid.Data4[1]) << 32),
                          to + 8, 8);
  to[16] = '\0';
  return true;
}

/*
  @brief
  Create a temporary file with unique name in a given directory

  @details
  create_temp_file_uuid
    to             pointer to buffer where temporary filename will be stored
    dir            directory where to create the file
    prefix         prefix the filename with up to a maximum of three chars
                   from this value.

  @return
    1 on success, 0 on failure.

  @note
    This function uses UuidCreateSequential to create a UUID value that is
    unique on this machine. The UUID will include a MAC address obtained from
    a network adapter where available. The inclusion of a (non-varying) MAC
    address in a temporary filename would be neither useful nor secure, so it is
    omitted from the base 32 encoding of the UUID value obtained from
    UuidCreateSequential.
    The use of UuidCreateSequential rather than UuidCreate (which does not
    include a MAC address in the generated UUID) is a compromise between
    uniqueness guarantees and performance.

    The use of only up to three characters of the prefix string on Windows is
    to retain some backward compatibility with earlier implementations
    which used the Windows function GetTempFilename.
*/

static int create_temp_file_uuid(char *to, const std::string &dir,
                                 const std::string &prefix) {
  // Verify that the dir path is not too long...
  const auto reduced_prefix =
      prefix.substr(0, std::min(prefix.length(), (size_t)3));

  const std::string dir_with_separator =
      is_directory_separator(dir.back()) ? dir : dir + FN_LIBCHAR;

  if (dir_with_separator.length() > (MAX_PATH - MY_MAX_TEMP_FILENAME_LEN)) {
    my_osmaperr(CO_E_PATHTOOLONG);
    set_my_errno(errno);
    return 0;
  }
  char encoded_uuid[17];
  if (!get_encoded_uuid_excluding_mac(encoded_uuid, sizeof(encoded_uuid))) {
    return 0;
  }
  // Finally, assemble the filename...
  std::ostringstream full_path_builder;
  full_path_builder << dir_with_separator << reduced_prefix << encoded_uuid;
  strcpy(to, full_path_builder.str().c_str());

  HANDLE hfile = CreateFile(to, GENERIC_READ | GENERIC_WRITE, 0, NULL,
                            CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
  if (hfile == INVALID_HANDLE_VALUE) {
    my_osmaperr(GetLastError());
    set_my_errno(errno);
    return 0;
  }
  if (!CloseHandle(hfile)) {
    my_osmaperr(GetLastError());
    set_my_errno(errno);
    return 0;
  }
  return 1;
}
#endif  // WIN32
/*
  @brief
  Create a temporary file with unique name in a given directory

  @details
  create_temp_file
    to             pointer to buffer where temporary filename will be stored
    dir            directory where to create the file
    prefix         prefix the filename with this (only up to the first three
                   chars will be used on Windows).
    mode           Flags to use for my_create/my_open
    MyFlags        Magic flags

  @return
    File descriptor of opened file if success
    -1 and sets errno if fails.

  @note
    The behaviour of this function differs a lot between
    implementation, it's main use is to generate a file with
    a name that does not already exist.

    The implementation using mkstemp should be considered the
    reference implementation when adding a new or modifying an
    existing one

*/

File create_temp_file(char *to, const char *dir, const char *prefix,
                      int mode [[maybe_unused]],
                      UnlinkOrKeepFile unlink_or_keep,
                      myf MyFlags [[maybe_unused]]) {
  File file = -1;
#ifdef _WIN32
  TCHAR path_buf[MAX_PATH + 1];
#endif

  DBUG_TRACE;
  DBUG_PRINT("enter", ("dir: %s, prefix: %s", dir, prefix));
#if defined(_WIN32)

  if (!dir) {
    auto res = GetTempPath(sizeof(path_buf), path_buf);
    if (res == 0) {
      // There's been a problem with GetTempPath
      my_osmaperr(GetLastError());
      set_my_errno(errno);
      return -1;
    }
    if (res > sizeof(path_buf)) {
      my_osmaperr(CO_E_PATHTOOLONG);
      set_my_errno(errno);
      return -1;
    }
    dir = path_buf;
  }
  // Treat null prefix as an empty string
  if (!prefix) {
    prefix = "";
  }
  /*
    Use create_temp_file_uuid to generate a unique filename, create
    the file and release its handle
     - uses up to the first three letters from prefix
  */
  if (create_temp_file_uuid(to, dir, prefix) == 0) return -1;

  DBUG_PRINT("info", ("name: %s", to));

  /*
    Open the file without the "open only if file doesn't already exist"
    since the file has already been created by create_temp_file_uuid
  */
  if ((file = my_open(to, (mode & ~O_EXCL), MyFlags)) < 0) {
    /* Open failed, remove the file created by GetTempFileName */
    int tmp = my_errno();
    (void)my_delete(to, MYF(0));
    set_my_errno(tmp);
    return file;
  }
  if (unlink_or_keep == UNLINK_FILE) {
    my_delete(to, MYF(0));
  }

#else /* mkstemp() is available on all non-Windows supported platforms. */
#ifdef HAVE_O_TMPFILE
  if (unlink_or_keep == UNLINK_FILE) {
    if (!dir && !(dir = getenv("TMPDIR"))) dir = DEFAULT_TMPDIR;

    char dirname_buf[FN_REFLEN];
    convert_dirname(dirname_buf, dir, nullptr);

    // Verify that the generated filename will fit in a FN_REFLEN size buffer.
    int max_filename_len = snprintf(to, FN_REFLEN, "%s%.20sfd=%d", dirname_buf,
                                    prefix ? prefix : "tmp.", 4 * 1024 * 1024);
    if (max_filename_len >= FN_REFLEN) {
      errno = ENAMETOOLONG;
      set_my_errno(ENAMETOOLONG);
      return file;
    }

    /* Explicitly don't use O_EXCL here as it has a different
       meaning with O_TMPFILE.
    */
    file = mysys_priv::RetryOnEintr(
        [&]() {
          return open(dirname_buf, O_RDWR | O_TMPFILE | O_CLOEXEC,
                      S_IRUSR | S_IWUSR);
        },
        -1);

    if (file >= 0) {
      sprintf(to, "%s%.20sfd=%d", dirname_buf, prefix ? prefix : "tmp.", file);
      file_info::RegisterFilename(file, to,
                                  file_info::OpenType::FILE_BY_O_TMPFILE);
    }
  }
  // Fall through, in case open() failed above (or we have KEEP_FILE).
#endif /* HAVE_O_TMPFILE */
  if (file == -1) {
    char prefix_buff[30];
    uint pfx_len;

    pfx_len = (uint)(my_stpcpy(my_stpnmov(prefix_buff, prefix ? prefix : "tmp.",
                                          sizeof(prefix_buff) - 7),
                               "XXXXXX") -
                     prefix_buff);
    if (!dir && !(dir = getenv("TMPDIR"))) dir = DEFAULT_TMPDIR;
    if (strlen(dir) + pfx_len > FN_REFLEN - 2) {
      errno = ENAMETOOLONG;
      set_my_errno(ENAMETOOLONG);
      return file;
    }
    my_stpcpy(convert_dirname(to, dir, NullS), prefix_buff);
    file = mkstemp(to);
    if (file < 0) {
      set_my_errno(errno);
      if (MyFlags & (MY_FAE | MY_WME)) {
        MyOsError(my_errno(), EE_CANTCREATEFILE, MYF(0), to);
      }
      return file;
    }
    file_info::RegisterFilename(file, to, file_info::OpenType::FILE_BY_MKSTEMP);
    if (unlink_or_keep == UNLINK_FILE) {
      unlink(to);
    }
  }
#endif /* _WIN32 */
  if (file >= 0) {
    mysql_mutex_lock(&THR_LOCK_open);
    my_tmp_file_created++;
    mysql_mutex_unlock(&THR_LOCK_open);
  }
  return file;
}