mf_tempfile.c 5.33 KB
Newer Older
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1 2 3 4 5 6 7 8
/* 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
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
10 11 12 13 14 15
   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
16 17 18 19 20 21

#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
22
#ifdef HAVE_PATHS_H
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
23 24 25 26
#include <paths.h>
#endif

#ifdef HAVE_TEMPNAM
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
27
#if !defined(MSDOS) && !defined(OS2) && !defined(__NETWARE__)
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
28 29 30 31 32 33 34 35 36 37
extern char **environ;
#endif
#endif

/*
  Create a temporary file in a given directory
  This function should be used instead of my_tempnam() !
*/

File create_temp_file(char *to, const char *dir, const char *prefix,
38 39
		      int mode __attribute__((unused)),
		      myf MyFlags __attribute__((unused)))
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
40 41
{
  File file= -1;
42
  DBUG_ENTER("create_temp_file");
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
43 44
#if defined(_MSC_VER)
  {
45
    char temp[FN_REFLEN],*end,*res,**old_env,*temp_env[1];
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    old_env=environ;
    if (dir)
    {
      end=strend(dir)-1;
      if (!dir[0])
      {				/* Change empty string to current dir */
	to[0]= FN_CURLIB;
	to[1]= 0;
	dir=to;
      }
      else if (*end == FN_DEVCHAR)
      {				/* Get current dir for drive */
	_fullpath(temp,dir,FN_REFLEN);
	dir=to;
      }
      else if (*end == FN_LIBCHAR && dir < end && end[-1] != FN_DEVCHAR)
      {
	strmake(to,dir,(uint) (end-dir));	/* Copy and remove last '\' */
	dir=to;
      }
      environ=temp_env;		/* Force use of dir (dir not checked) */
      temp_env[0]=0;
    }
    if ((res=tempnam((char*) dir,(char *) prefix)))
    {
71
      strmake(to,res,FN_REFLEN-1);
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
72 73 74 75 76 77 78 79 80 81
      (*free)(res);
      file=my_create(to,0, mode, MyFlags);
    }
    environ=old_env;
  }
#elif defined(_ZTC__)
  if (!dir)
    dir=getenv("TMPDIR");
  if ((res=tempnam((char*) dir,(char *) prefix)))
  {
82
    strmake(to,res,FN_REFLEN-1);
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
83 84 85 86 87
    (*free)(res);
    file=my_create(to, 0, mode, MyFlags);
  }
#elif defined(HAVE_MKSTEMP)
  {
88
    char prefix_buff[30];
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
89
    uint pfx_len;
90
    File org_file;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
91

92 93 94 95
    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
96 97 98 99 100 101 102
    if (!dir && ! (dir =getenv("TMPDIR")))
      dir=P_tmpdir;
    if (strlen(dir)+ pfx_len > FN_REFLEN-2)
    {
      errno=my_errno= ENAMETOOLONG;
      return 1;
    }
103
    strmov(convert_dirname(to,dir,NullS),prefix_buff);
104 105 106 107 108 109 110 111 112 113
    org_file=mkstemp(to);
    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
114 115 116 117 118 119 120 121 122 123
  }
#elif defined(HAVE_TEMPNAM)
  {
    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;
    }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
124
#ifdef OS2
salle@geopard.online.bg's avatar
salle@geopard.online.bg committed
125
    /* changing environ variable doesn't work with VACPP */
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
126
    char  buffer[256];
127
    strxnmov(buffer, sizeof(buffer), "TMP=", dir);
salle@geopard.online.bg's avatar
salle@geopard.online.bg committed
128
    /* remove ending backslash */
129 130
    if (buffer[strlen(buffer)-1] == '\\')
       buffer[strlen(buffer)-1] = '\0';
131
    putenv(buffer);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
132
#elif !defined(__NETWARE__)
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
133
    old_env= (char**) environ;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
134 135
    if (dir)
    {				/* Don't use TMPDIR if dir is given */
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
136
      environ=(const char**) temp_env;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
137 138
      temp_env[0]=0;
    }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
139
#endif
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
140
    if ((res=tempnam((char*) dir, (char*) prefix)))
141
    {
142
      strmake(to,res,FN_REFLEN-1);
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
143 144 145 146 147 148 149 150 151 152 153
      (*free)(res);
      file=my_create(to,0,
		     (int) (O_RDWR | O_BINARY | O_TRUNC |
			    O_TEMPORARY | O_SHORT_LIVED),
		     MYF(MY_WME));

    }
    else
    {
      DBUG_PRINT("error",("Got error: %d from tempnam",errno));
    }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
154
#if !defined(OS2) && !defined(__NETWARE__)
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
155 156
    environ=(const char**) old_env;
#endif
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
  }
#else
  {
    register long uniq;
    register int length;
    my_string pos,end_pos;
    /* Make an unique number */
    pthread_mutex_lock(&THR_LOCK_open);
    uniq= ((long) getpid() << 20) + (long) _my_tempnam_used++ ;
    pthread_mutex_unlock(&THR_LOCK_open);
    if (!dir && !(dir=getenv("TMPDIR")))	/* Use this if possibly */
      dir=P_tmpdir;			/* Use system default */
    length=strlen(dir)+strlen(pfx)+1;

    DBUG_PRINT("test",("mallocing %d byte",length+8+sizeof(TMP_EXT)+1));
    if (length+8+sizeof(TMP_EXT)+1 > FN_REFLENGTH)
      errno=my_errno= ENAMETOOLONG;
    else
    {
      end_pos=strmov(to,dir);
      if (end_pos != to && end_pos[-1] != FN_LIBCHAR)
	*end_pos++=FN_LIBCHAR;
      end_pos=strmov(end_pos,pfx);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
180

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
181 182 183 184 185 186 187 188 189 190 191 192 193
      for (length=0 ; length < 8 && uniq ; length++)
      {
	*end_pos++= _dig_vec[(int) (uniq & 31)];
	uniq >>= 5;
      }
      (void) strmov(end_pos,TMP_EXT);
      file=my_create(to,0,
		     (int) (O_RDWR | O_BINARY | O_TRUNC |
			    O_TEMPORARY | O_SHORT_LIVED),
		     MYF(MY_WME));
    }
  }
#endif
194 195
  if (file >= 0)
    thread_safe_increment(my_tmp_file_created,&THR_LOCK_open);
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
196 197
  DBUG_RETURN(file);
}