1
/* Copyright (C) 2000 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
#include "mysys_priv.h"
18
#include "my_static.h"
19
#include "mysys_err.h"
29
Create a temporary file with unique name in a given directory
33
to pointer to buffer where temporary filename will be stored
34
dir directory where to create the file
35
prefix prefix the filename with this
36
mode Flags to use for my_create/my_open
40
File descriptor of opened file if success
41
-1 and sets errno if fails.
44
The behaviour of this function differs a lot between
45
implementation, it's main use is to generate a file with
46
a name that does not already exist.
48
When passing O_TEMPORARY flag in "mode" the file should
49
be automatically deleted
51
The implementation using mkstemp should be considered the
52
reference implementation when adding a new or modifying an
57
File create_temp_file(char *to, const char *dir, const char *prefix,
58
int mode __attribute__((unused)),
59
myf MyFlags __attribute__((unused)))
63
DBUG_ENTER("create_temp_file");
64
DBUG_PRINT("enter", ("dir: %s, prefix: %s", dir, prefix));
68
if ((res=tempnam((char*) dir,(char *) prefix)))
70
strmake(to,res,FN_REFLEN-1);
72
file=my_create(to, 0, mode | O_EXCL | O_NOFOLLOW, MyFlags);
74
#elif defined(HAVE_MKSTEMP)
80
pfx_len= (uint) (strmov(strnmov(prefix_buff,
81
prefix ? prefix : "tmp.",
82
sizeof(prefix_buff)-7),"XXXXXX") -
84
if (!dir && ! (dir =getenv("TMPDIR")))
86
if (strlen(dir)+ pfx_len > FN_REFLEN-2)
88
errno=my_errno= ENAMETOOLONG;
91
strmov(convert_dirname(to,dir,NullS),prefix_buff);
93
if (mode & O_TEMPORARY)
94
(void) my_delete(to, MYF(MY_WME | ME_NOINPUT));
95
file=my_register_filename(org_file, to, FILE_BY_MKSTEMP,
96
EE_CANTCREATEFILE, MyFlags);
97
/* If we didn't manage to register the name, remove the temp file */
98
if (org_file >= 0 && file < 0)
102
(void) my_delete(to, MYF(MY_WME | ME_NOINPUT));
106
#elif defined(HAVE_TEMPNAM)
108
char *res,**old_env,*temp_env[1];
110
{ /* Change empty string to current dir */
115
old_env= (char**) environ;
117
{ /* Don't use TMPDIR if dir is given */
118
environ=(const char**) temp_env;
121
if ((res=tempnam((char*) dir, (char*) prefix)))
123
strmake(to,res,FN_REFLEN-1);
126
(int) (O_RDWR | O_BINARY | O_TRUNC | O_EXCL | O_NOFOLLOW |
127
O_TEMPORARY | O_SHORT_LIVED),
133
DBUG_PRINT("error",("Got error: %d from tempnam",errno));
135
environ=(const char**) old_env;
138
#error No implementation found for create_temp_file
141
thread_safe_increment(my_tmp_file_created,&THR_LOCK_open);