47
45
implementation, it's main use is to generate a file with
48
46
a name that does not already exist.
48
When passing O_TEMPORARY flag in "mode" the file should
49
be automatically deleted
50
51
The implementation using mkstemp should be considered the
51
52
reference implementation when adding a new or modifying an
56
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));
62
65
#if defined(_ZTC__)
64
67
dir=getenv("TMPDIR");
65
68
if ((res=tempnam((char*) dir,(char *) prefix)))
67
strncpy(to,res,FN_REFLEN-1);
70
strmake(to,res,FN_REFLEN-1);
69
file=my_create(to, 0, mode | O_EXCL, MyFlags);
72
file=my_create(to, 0, mode | O_EXCL | O_NOFOLLOW, MyFlags);
71
74
#elif defined(HAVE_MKSTEMP)
76
prefix_str= prefix ? prefix : "tmp.";
77
prefix_str.append("XXXXXX");
80
pfx_len= (uint) (strmov(strnmov(prefix_buff,
81
prefix ? prefix : "tmp.",
82
sizeof(prefix_buff)-7),"XXXXXX") -
79
84
if (!dir && ! (dir =getenv("TMPDIR")))
81
if (strlen(dir)+prefix_str.length() > FN_REFLEN-2)
86
if (strlen(dir)+ pfx_len > FN_REFLEN-2)
83
88
errno=my_errno= ENAMETOOLONG;
86
strcpy(convert_dirname(to,dir,NULL),prefix_str.c_str());
91
strmov(convert_dirname(to,dir,NullS),prefix_buff);
87
92
org_file=mkstemp(to);
88
/* TODO: This was old behavior, but really don't we want to
89
* unlink files immediately under all circumstances?
90
* if (mode & O_TEMPORARY)
93
if (mode & O_TEMPORARY)
91
94
(void) my_delete(to, MYF(MY_WME | ME_NOINPUT));
93
95
file=my_register_filename(org_file, to, FILE_BY_MKSTEMP,
94
96
EE_CANTCREATEFILE, MyFlags);
95
97
/* If we didn't manage to register the name, remove the temp file */
120
121
if ((res=tempnam((char*) dir, (char*) prefix)))
122
strncpy(to,res,FN_REFLEN-1);
123
strmake(to,res,FN_REFLEN-1);
124
125
file=my_create(to,0,
125
(int) (O_RDWR | O_TRUNC | O_EXCL),
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));
129
135
environ=(const char**) old_env;