45
47
implementation, it's main use is to generate a file with
46
48
a name that does not already exist.
48
When passing O_TEMPORARY flag in "mode" the file should
49
be automatically deleted
51
50
The implementation using mkstemp should be considered the
52
51
reference implementation when adding a new or modifying an
57
56
File create_temp_file(char *to, const char *dir, const char *prefix,
58
int mode __attribute__((unused)),
59
myf MyFlags __attribute__((unused)))
65
64
dir=getenv("TMPDIR");
66
65
if ((res=tempnam((char*) dir,(char *) prefix)))
68
strmake(to,res,FN_REFLEN-1);
67
strncpy(to,res,FN_REFLEN-1);
70
file=my_create(to, 0, mode | O_EXCL | O_NOFOLLOW, MyFlags);
69
file=my_create(to, 0, mode | O_EXCL, MyFlags);
72
71
#elif defined(HAVE_MKSTEMP)
78
pfx_len= (uint) (my_stpcpy(my_stpncpy(prefix_buff,
79
prefix ? prefix : "tmp.",
80
sizeof(prefix_buff)-7),"XXXXXX") -
76
prefix_str= prefix ? prefix : "tmp.";
77
prefix_str.append("XXXXXX");
82
79
if (!dir && ! (dir =getenv("TMPDIR")))
84
if (strlen(dir)+ pfx_len > FN_REFLEN-2)
81
if (strlen(dir)+prefix_str.length() > FN_REFLEN-2)
86
83
errno=my_errno= ENAMETOOLONG;
89
my_stpcpy(convert_dirname(to,dir,NULL),prefix_buff);
86
strcpy(convert_dirname(to,dir,NULL),prefix_str.c_str());
90
87
org_file=mkstemp(to);
91
if (mode & O_TEMPORARY)
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)
92
91
(void) my_delete(to, MYF(MY_WME | ME_NOINPUT));
93
93
file=my_register_filename(org_file, to, FILE_BY_MKSTEMP,
94
94
EE_CANTCREATEFILE, MyFlags);
95
95
/* If we didn't manage to register the name, remove the temp file */
119
120
if ((res=tempnam((char*) dir, (char*) prefix)))
121
strmake(to,res,FN_REFLEN-1);
122
strncpy(to,res,FN_REFLEN-1);
123
124
file=my_create(to,0,
124
(int) (O_RDWR | O_BINARY | O_TRUNC | O_EXCL | O_NOFOLLOW |
125
O_TEMPORARY | O_SHORT_LIVED),
125
(int) (O_RDWR | O_TRUNC | O_EXCL),