drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
mf_tempfile.cc
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15
16#include <config.h>
17
18#include <drizzled/internal/my_sys.h>
19#include <drizzled/internal/m_string.h>
20#include "my_static.h"
21#include <drizzled/error.h>
22#include <stdio.h>
23#include <errno.h>
24#include <string>
25#include <paths.h>
26
27using namespace std;
28namespace drizzled
29{
30namespace internal
31{
32
33/*
34 @brief
35 Create a temporary file with unique name in a given directory
36
37 @details
38 create_temp_file
39 to pointer to buffer where temporary filename will be stored
40 dir directory where to create the file
41 prefix prefix the filename with this
42 MyFlags Magic flags
43
44 @return
45 File descriptor of opened file if success
46 -1 and sets errno if fails.
47
48 @note
49 The behaviour of this function differs a lot between
50 implementation, it's main use is to generate a file with
51 a name that does not already exist.
52
53 The implementation using mkstemp should be considered the
54 reference implementation when adding a new or modifying an
55 existing one
56
57*/
58
59int create_temp_file(char *to, const char *dir, const char *prefix,
60 myf MyFlags)
61{
62 string prefix_str= prefix ? prefix : "tmp.";
63 prefix_str.append("XXXXXX");
64
65 if (!dir && ! (dir =getenv("TMPDIR")))
66 dir= P_tmpdir;
67 if (strlen(dir)+prefix_str.length() > FN_REFLEN-2)
68 {
69 errno= ENAMETOOLONG;
70 return -1;
71 }
72 strcpy(convert_dirname(to,dir,NULL),prefix_str.c_str());
73 int org_file= mkstemp(to);
74 /* TODO: This was old behavior, but really don't we want to
75 * unlink files immediately under all circumstances?
76 * if (mode & O_TEMPORARY)
77 (void) my_delete(to, MYF(MY_WME | ME_NOINPUT));
78 */
79 int file= my_register_filename(org_file, to, EE_CANTCREATEFILE, MyFlags);
80
81 /* If we didn't manage to register the name, remove the temp file */
82 if (org_file >= 0 && file < 0)
83 {
84 int tmp= errno;
85 close(org_file);
86 (void) my_delete(to, MYF(MY_WME | ME_NOINPUT));
87 errno= tmp;
88 }
89
90 return file;
91}
92
93} /* namespace internal */
94} /* namespace drizzled */
TODO: Rename this file - func.h is stupid.
STL namespace.