~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/mf_tempfile.cc

  • Committer: Eric Herman
  • Date: 2008-12-07 15:29:44 UTC
  • mto: (656.1.14 devel)
  • mto: This revision was merged to the branch mainline in revision 670.
  • Revision ID: eric@mysql.com-20081207152944-cq1nx1cyi0huqj0f
Added pointer to online version of the FAQ

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
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"
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#include "mysys_priv.h"
 
17
#include <mystrings/m_string.h>
20
18
#include "my_static.h"
21
 
#include "drizzled/error.h"
 
19
#include "mysys_err.h"
22
20
#include <stdio.h>
23
21
#include <errno.h>
24
 
#include <string>
25
22
#ifdef HAVE_PATHS_H
26
23
#include <paths.h>
27
24
#endif
28
25
 
29
 
using namespace std;
30
 
namespace drizzled
31
 
{
32
 
namespace internal
33
 
{
 
26
 
34
27
 
35
28
/*
36
29
  @brief
41
34
    to             pointer to buffer where temporary filename will be stored
42
35
    dir            directory where to create the file
43
36
    prefix         prefix the filename with this
 
37
    mode           Flags to use for my_create/my_open
44
38
    MyFlags        Magic flags
45
39
 
46
40
  @return
58
52
 
59
53
*/
60
54
 
61
 
int create_temp_file(char *to, const char *dir, const char *prefix,
62
 
                     myf MyFlags)
 
55
File create_temp_file(char *to, const char *dir, const char *prefix,
 
56
                      int,
 
57
                      myf MyFlags)
63
58
{
64
 
  int file= -1;
65
 
 
66
 
  int org_file;
67
 
  string prefix_str;
68
 
 
69
 
  prefix_str= prefix ? prefix : "tmp.";
70
 
  prefix_str.append("XXXXXX");
71
 
 
72
 
  if (!dir && ! (dir =getenv("TMPDIR")))
73
 
    dir= P_tmpdir;
74
 
  if (strlen(dir)+prefix_str.length() > FN_REFLEN-2)
75
 
  {
76
 
    errno= ENAMETOOLONG;
77
 
    return(file);
78
 
  }
79
 
  strcpy(convert_dirname(to,dir,NULL),prefix_str.c_str());
80
 
  org_file=mkstemp(to);
81
 
  /* TODO: This was old behavior, but really don't we want to
82
 
   * unlink files immediately under all circumstances?
83
 
   * if (mode & O_TEMPORARY)
84
 
    (void) my_delete(to, MYF(MY_WME | ME_NOINPUT));
85
 
  */
86
 
  file=my_register_filename(org_file, to, EE_CANTCREATEFILE, MyFlags);
87
 
 
88
 
  /* If we didn't manage to register the name, remove the temp file */
89
 
  if (org_file >= 0 && file < 0)
90
 
  {
91
 
    int tmp=errno;
92
 
    close(org_file);
93
 
    (void) my_delete(to, MYF(MY_WME | ME_NOINPUT));
94
 
    errno=tmp;
95
 
  }
96
 
 
 
59
  File file= -1;
 
60
 
 
61
#if defined(_ZTC__)
 
62
  if (!dir)
 
63
    dir=getenv("TMPDIR");
 
64
  if ((res=tempnam((char*) dir,(char *) prefix)))
 
65
  {
 
66
    strncpy(to,res,FN_REFLEN-1);
 
67
    (*free)(res);
 
68
    file=my_create(to, 0, mode | O_EXCL, MyFlags);
 
69
  }
 
70
#elif defined(HAVE_MKSTEMP)
 
71
  {
 
72
    char prefix_buff[30];
 
73
    uint32_t pfx_len;
 
74
    File org_file;
 
75
 
 
76
    pfx_len= (uint32_t)(strcpy(my_stpncpy(prefix_buff,prefix ? prefix : "tmp.",
 
77
                                          sizeof(prefix_buff)-7),"XXXXXX")+6
 
78
                               -prefix_buff);
 
79
    if (!dir && ! (dir =getenv("TMPDIR")))
 
80
      dir= P_tmpdir;
 
81
    if (strlen(dir)+ pfx_len > FN_REFLEN-2)
 
82
    {
 
83
      errno=my_errno= ENAMETOOLONG;
 
84
      return(file);
 
85
    }
 
86
    strcpy(convert_dirname(to,dir,NULL),prefix_buff);
 
87
    org_file=mkstemp(to);
 
88
    /* TODO: This was old behavior, but really don't we want to
 
89
     * unlink files immediately under all circumstances?
 
90
     * if (mode & O_TEMPORARY)
 
91
      (void) my_delete(to, MYF(MY_WME | ME_NOINPUT));
 
92
     */
 
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
  {
 
106
    (void)MyFlags;
 
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
    {
 
122
      strncpy(to,res,FN_REFLEN-1);
 
123
      (*free)(res);
 
124
      file=my_create(to,0,
 
125
                     (int) (O_RDWR | O_TRUNC | O_EXCL),
 
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);
97
136
  return(file);
98
137
}
99
 
 
100
 
} /* namespace internal */
101
 
} /* namespace drizzled */