~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"
612.2.6 by Monty Taylor
OpenSolaris builds.
20
#include <stdio.h>
1 by brian
clean slate
21
#include <errno.h>
670.3.1 by Toru Maesaka
Replaced MySQL's my_stpncpy() with libc and c++ calls
22
#include <string>
1 by brian
clean slate
23
#ifdef HAVE_PATHS_H
24
#include <paths.h>
25
#endif
26
670.3.3 by Toru Maesaka
Added namespacing for std to .cc files that needed it
27
using namespace std;
1 by brian
clean slate
28
29
/*
30
  @brief
31
  Create a temporary file with unique name in a given directory
32
33
  @details
34
  create_temp_file
35
    to             pointer to buffer where temporary filename will be stored
36
    dir            directory where to create the file
37
    prefix         prefix the filename with this
38
    mode           Flags to use for my_create/my_open
39
    MyFlags        Magic flags
40
41
  @return
42
    File descriptor of opened file if success
43
    -1 and sets errno if fails.
44
45
  @note
46
    The behaviour of this function differs a lot between
47
    implementation, it's main use is to generate a file with
48
    a name that does not already exist.
49
50
    The implementation using mkstemp should be considered the
51
    reference implementation when adding a new or modifying an
52
    existing one
53
54
*/
55
56
File create_temp_file(char *to, const char *dir, const char *prefix,
632.1.11 by Monty Taylor
Fixed Sun Studio warnings in mysys.
57
		      int,
58
		      myf MyFlags)
1 by brian
clean slate
59
{
60
  File file= -1;
61
62
#if defined(_ZTC__)
63
  if (!dir)
64
    dir=getenv("TMPDIR");
65
  if ((res=tempnam((char*) dir,(char *) prefix)))
66
  {
629.5.1 by Toru Maesaka
First pass of replacing MySQL's strmake() with libc calls
67
    strncpy(to,res,FN_REFLEN-1);
1 by brian
clean slate
68
    (*free)(res);
499.1.3 by Monty Taylor
Removed O_NOFOLLOW
69
    file=my_create(to, 0, mode | O_EXCL, MyFlags);
1 by brian
clean slate
70
  }
71
#elif defined(HAVE_MKSTEMP)
72
  {
73
    File org_file;
670.3.3 by Toru Maesaka
Added namespacing for std to .cc files that needed it
74
    string prefix_str;
670.3.1 by Toru Maesaka
Replaced MySQL's my_stpncpy() with libc and c++ calls
75
76
    prefix_str= prefix ? prefix : "tmp.";
77
    prefix_str.append("XXXXXX");
78
1 by brian
clean slate
79
    if (!dir && ! (dir =getenv("TMPDIR")))
595 by Brian Aker
Fix, partial, for Sun Studio.
80
      dir= P_tmpdir;
670.3.1 by Toru Maesaka
Replaced MySQL's my_stpncpy() with libc and c++ calls
81
    if (strlen(dir)+prefix_str.length() > FN_REFLEN-2)
1 by brian
clean slate
82
    {
83
      errno=my_errno= ENAMETOOLONG;
51.3.21 by Jay Pipes
Phase 8 - Remove DBUG from mysys
84
      return(file);
1 by brian
clean slate
85
    }
670.3.1 by Toru Maesaka
Replaced MySQL's my_stpncpy() with libc and c++ calls
86
    strcpy(convert_dirname(to,dir,NULL),prefix_str.c_str());
1 by brian
clean slate
87
    org_file=mkstemp(to);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
88
    /* TODO: This was old behavior, but really don't we want to
499.1.2 by Monty Taylor
Removed O_TEMPORARY and O_SHORT_LIVED, they were Windows things.
89
     * unlink files immediately under all circumstances?
90
     * if (mode & O_TEMPORARY)
1 by brian
clean slate
91
      (void) my_delete(to, MYF(MY_WME | ME_NOINPUT));
499.1.2 by Monty Taylor
Removed O_TEMPORARY and O_SHORT_LIVED, they were Windows things.
92
     */
1 by brian
clean slate
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
  {
632.1.11 by Monty Taylor
Fixed Sun Studio warnings in mysys.
106
    (void)MyFlags;
1 by brian
clean slate
107
    char *res,**old_env,*temp_env[1];
108
    if (dir && !dir[0])
109
    {				/* Change empty string to current dir */
110
      to[0]= FN_CURLIB;
111
      to[1]= 0;
112
      dir=to;
113
    }
114
    old_env= (char**) environ;
115
    if (dir)
116
    {				/* Don't use TMPDIR if dir is given */
117
      environ=(const char**) temp_env;
118
      temp_env[0]=0;
119
    }
120
    if ((res=tempnam((char*) dir, (char*) prefix)))
121
    {
629.5.1 by Toru Maesaka
First pass of replacing MySQL's strmake() with libc calls
122
      strncpy(to,res,FN_REFLEN-1);
1 by brian
clean slate
123
      (*free)(res);
124
      file=my_create(to,0,
499.1.3 by Monty Taylor
Removed O_NOFOLLOW
125
		     (int) (O_RDWR | O_TRUNC | O_EXCL),
1 by brian
clean slate
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
}