mf_tempfile.c 4.75 KB
Newer Older
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1 2 3 4
/* Copyright (C) 2000 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6 7

   This program is distributed in the hope that it will be useful,
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
9 10 11 12 13 14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
15 16 17 18 19 20

#include "mysys_priv.h"
#include <m_string.h>
#include "my_static.h"
#include "mysys_err.h"
#include <errno.h>
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
21
#ifdef HAVE_PATHS_H
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
22 23 24
#include <paths.h>
#endif

25

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
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
  @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
    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.

    When passing O_TEMPORARY flag in "mode" the file should
    be automatically deleted

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

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
55 56 57
*/

File create_temp_file(char *to, const char *dir, const char *prefix,
58 59
		      int mode __attribute__((unused)),
		      myf MyFlags __attribute__((unused)))
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
60 61
{
  File file= -1;
62

63
  DBUG_ENTER("create_temp_file");
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  DBUG_PRINT("enter", ("dir: %s, prefix: %s", dir, prefix));
#if defined (__WIN__)

   /*
     Use GetTempFileName to generate a unique filename, create
     the file and release it's handle
      - uses up to the first three letters from prefix
   */
  if (GetTempFileName(dir, prefix, 0, to) == 0)
    DBUG_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 GetTempFileName
  */
  if ((file= my_open(to,  (mode & ~O_EXCL), MyFlags)) < 0)
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
82
  {
83 84 85 86
    /* Open failed, remove the file created by GetTempFileName */
    int tmp= my_errno;
    (void) my_delete(to, MYF(0));
    my_errno= tmp;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
87
  }
88

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
89 90 91 92 93
#elif defined(_ZTC__)
  if (!dir)
    dir=getenv("TMPDIR");
  if ((res=tempnam((char*) dir,(char *) prefix)))
  {
94
    strmake(to,res,FN_REFLEN-1);
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
95
    (*free)(res);
96
    file=my_create(to, 0, mode | O_EXCL | O_NOFOLLOW, MyFlags);
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
97
  }
98
#elif defined(HAVE_MKSTEMP) && !defined(__NETWARE__)
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
99
  {
100
    char prefix_buff[30];
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
101
    uint pfx_len;
102
    File org_file;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
103

104 105 106 107
    pfx_len= (uint) (strmov(strnmov(prefix_buff,
				    prefix ? prefix : "tmp.",
				    sizeof(prefix_buff)-7),"XXXXXX") -
		     prefix_buff);
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
108 109 110 111 112
    if (!dir && ! (dir =getenv("TMPDIR")))
      dir=P_tmpdir;
    if (strlen(dir)+ pfx_len > FN_REFLEN-2)
    {
      errno=my_errno= ENAMETOOLONG;
serg@serg.mylan's avatar
serg@serg.mylan committed
113
      DBUG_RETURN(file);
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
114
    }
115
    strmov(convert_dirname(to,dir,NullS),prefix_buff);
116
    org_file=mkstemp(to);
117 118
    if (mode & O_TEMPORARY)
      (void) my_delete(to, MYF(MY_WME | ME_NOINPUT));
119 120 121 122 123 124 125 126 127
    file=my_register_filename(org_file, to, FILE_BY_MKSTEMP,
			      EE_CANTCREATEFILE, MyFlags);
    /* If we didn't manage to register the name, remove the temp file */
    if (org_file >= 0 && file < 0)
    {
      int tmp=my_errno;
      (void) my_delete(to, MYF(MY_WME | ME_NOINPUT));
      my_errno=tmp;
    }
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
128 129 130
  }
#elif defined(HAVE_TEMPNAM)
  {
131 132 133 134
#if !defined(__NETWARE__)
    extern char **environ;
#endif

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
135 136 137 138 139 140 141
    char *res,**old_env,*temp_env[1];
    if (dir && !dir[0])
    {				/* Change empty string to current dir */
      to[0]= FN_CURLIB;
      to[1]= 0;
      dir=to;
    }
142
#if !defined(__NETWARE__)
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
143
    old_env= (char**) environ;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
144 145
    if (dir)
    {				/* Don't use TMPDIR if dir is given */
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
146
      environ=(const char**) temp_env;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
147 148
      temp_env[0]=0;
    }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
149
#endif
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
150
    if ((res=tempnam((char*) dir, (char*) prefix)))
151
    {
152
      strmake(to,res,FN_REFLEN-1);
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
153 154
      (*free)(res);
      file=my_create(to,0,
155
		     (int) (O_RDWR | O_BINARY | O_TRUNC | O_EXCL | O_NOFOLLOW |
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
156 157 158 159 160 161 162 163
			    O_TEMPORARY | O_SHORT_LIVED),
		     MYF(MY_WME));

    }
    else
    {
      DBUG_PRINT("error",("Got error: %d from tempnam",errno));
    }
164
#if !defined(__NETWARE__)
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
165 166
    environ=(const char**) old_env;
#endif
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
167 168
  }
#else
169
#error No implementation found for create_temp_file
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
170
#endif
171 172
  if (file >= 0)
    thread_safe_increment(my_tmp_file_created,&THR_LOCK_open);
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
173 174
  DBUG_RETURN(file);
}