Drizzled Public API Documentation

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