~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000 MySQL AB
2
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.
6
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.
11
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 */
15
16
#include "mysys_priv.h"
212.5.18 by Monty Taylor
Moved m_ctype, m_string and my_bitmap. Removed t_ctype.
17
#include <mystrings/m_string.h>
1 by brian
clean slate
18
#include "my_static.h"
19
#include "mysys_err.h"
20
#include <errno.h>
21
#ifdef HAVE_PATHS_H
22
#include <paths.h>
23
#endif
24
25
26
27
/*
28
  @brief
29
  Create a temporary file with unique name in a given directory
30
31
  @details
32
  create_temp_file
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
37
    MyFlags        Magic flags
38
39
  @return
40
    File descriptor of opened file if success
41
    -1 and sets errno if fails.
42
43
  @note
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.
47
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
52
    reference implementation when adding a new or modifying an
53
    existing one
54
55
*/
56
57
File create_temp_file(char *to, const char *dir, const char *prefix,
58
		      int mode __attribute__((unused)),
59
		      myf MyFlags __attribute__((unused)))
60
{
61
  File file= -1;
62
63
#if defined(_ZTC__)
64
  if (!dir)
65
    dir=getenv("TMPDIR");
66
  if ((res=tempnam((char*) dir,(char *) prefix)))
67
  {
68
    strmake(to,res,FN_REFLEN-1);
69
    (*free)(res);
70
    file=my_create(to, 0, mode | O_EXCL | O_NOFOLLOW, MyFlags);
71
  }
72
#elif defined(HAVE_MKSTEMP)
73
  {
74
    char prefix_buff[30];
75
    uint pfx_len;
76
    File org_file;
77
78
    pfx_len= (uint) (strmov(strnmov(prefix_buff,
79
				    prefix ? prefix : "tmp.",
80
				    sizeof(prefix_buff)-7),"XXXXXX") -
81
		     prefix_buff);
82
    if (!dir && ! (dir =getenv("TMPDIR")))
83
      dir=P_tmpdir;
84
    if (strlen(dir)+ pfx_len > FN_REFLEN-2)
85
    {
86
      errno=my_errno= ENAMETOOLONG;
51.3.21 by Jay Pipes
Phase 8 - Remove DBUG from mysys
87
      return(file);
1 by brian
clean slate
88
    }
89
    strmov(convert_dirname(to,dir,NullS),prefix_buff);
90
    org_file=mkstemp(to);
91
    if (mode & O_TEMPORARY)
92
      (void) my_delete(to, MYF(MY_WME | ME_NOINPUT));
93
    file=my_register_filename(org_file, to, FILE_BY_MKSTEMP,
94
			      EE_CANTCREATEFILE, MyFlags);
95
    /* If we didn't manage to register the name, remove the temp file */
96
    if (org_file >= 0 && file < 0)
97
    {
98
      int tmp=my_errno;
99
      close(org_file);
100
      (void) my_delete(to, MYF(MY_WME | ME_NOINPUT));
101
      my_errno=tmp;
102
    }
103
  }
104
#elif defined(HAVE_TEMPNAM)
105
  {
106
    char *res,**old_env,*temp_env[1];
107
    if (dir && !dir[0])
108
    {				/* Change empty string to current dir */
109
      to[0]= FN_CURLIB;
110
      to[1]= 0;
111
      dir=to;
112
    }
113
    old_env= (char**) environ;
114
    if (dir)
115
    {				/* Don't use TMPDIR if dir is given */
116
      environ=(const char**) temp_env;
117
      temp_env[0]=0;
118
    }
119
    if ((res=tempnam((char*) dir, (char*) prefix)))
120
    {
121
      strmake(to,res,FN_REFLEN-1);
122
      (*free)(res);
123
      file=my_create(to,0,
124
		     (int) (O_RDWR | O_BINARY | O_TRUNC | O_EXCL | O_NOFOLLOW |
125
			    O_TEMPORARY | O_SHORT_LIVED),
126
		     MYF(MY_WME));
127
128
    }
129
    environ=(const char**) old_env;
130
  }
131
#else
132
#error No implementation found for create_temp_file
133
#endif
134
  if (file >= 0)
135
    thread_safe_increment(my_tmp_file_created,&THR_LOCK_open);
51.3.21 by Jay Pipes
Phase 8 - Remove DBUG from mysys
136
  return(file);
1 by brian
clean slate
137
}