~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
994.2.4 by Monty Taylor
Blast. Fixed some make distcheck issues.
16
#include "mysys/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"
994.2.4 by Monty Taylor
Blast. Fixed some make distcheck issues.
19
#include "mysys/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
    MyFlags        Magic flags
39
40
  @return
41
    File descriptor of opened file if success
42
    -1 and sets errno if fails.
43
44
  @note
45
    The behaviour of this function differs a lot between
46
    implementation, it's main use is to generate a file with
47
    a name that does not already exist.
48
49
    The implementation using mkstemp should be considered the
50
    reference implementation when adding a new or modifying an
51
    existing one
52
53
*/
54
1192.3.43 by Monty Taylor
Removed many, many checks for functions that do not need to be checked.
55
File create_temp_file(char *to, const char *dir, const char *prefix,
56
                      myf MyFlags)
1 by brian
clean slate
57
{
58
  File file= -1;
59
1192.3.43 by Monty Taylor
Removed many, many checks for functions that do not need to be checked.
60
  File org_file;
61
  string prefix_str;
62
63
  prefix_str= prefix ? prefix : "tmp.";
64
  prefix_str.append("XXXXXX");
65
66
  if (!dir && ! (dir =getenv("TMPDIR")))
67
    dir= P_tmpdir;
68
  if (strlen(dir)+prefix_str.length() > FN_REFLEN-2)
69
  {
70
    errno=my_errno= ENAMETOOLONG;
71
    return(file);
72
  }
73
  strcpy(convert_dirname(to,dir,NULL),prefix_str.c_str());
74
  org_file=mkstemp(to);
75
  /* TODO: This was old behavior, but really don't we want to
76
   * unlink files immediately under all circumstances?
77
   * if (mode & O_TEMPORARY)
78
    (void) my_delete(to, MYF(MY_WME | ME_NOINPUT));
79
  */
80
  file=my_register_filename(org_file, to, EE_CANTCREATEFILE, MyFlags);
81
82
  /* If we didn't manage to register the name, remove the temp file */
83
  if (org_file >= 0 && file < 0)
84
  {
85
    int tmp=my_errno;
86
    close(org_file);
87
    (void) my_delete(to, MYF(MY_WME | ME_NOINPUT));
88
    my_errno=tmp;
89
  }
1 by brian
clean slate
90
  if (file >= 0)
1034.1.8 by Brian Aker
Remove locks around my_open(). Open file counts are now "best effort" (not
91
    my_tmp_file_created++;
92
51.3.21 by Jay Pipes
Phase 8 - Remove DBUG from mysys
93
  return(file);
1 by brian
clean slate
94
}