~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/mf_tempfile.cc

  • Committer: Brian Aker
  • Date: 2009-03-27 22:55:28 UTC
  • mto: This revision was merged to the branch mainline in revision 968.
  • Revision ID: brian@tangent.org-20090327225528-8y76cfx8a4oemqv9
Remove ref_count

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
#include <mystrings/m_string.h>
18
18
#include "my_static.h"
19
19
#include "mysys_err.h"
 
20
#include <stdio.h>
20
21
#include <errno.h>
 
22
#include <string>
21
23
#ifdef HAVE_PATHS_H
22
24
#include <paths.h>
23
25
#endif
24
26
 
25
 
 
 
27
using namespace std;
26
28
 
27
29
/*
28
30
  @brief
45
47
    implementation, it's main use is to generate a file with
46
48
    a name that does not already exist.
47
49
 
48
 
    When passing O_TEMPORARY flag in "mode" the file should
49
 
    be automatically deleted
50
 
 
51
50
    The implementation using mkstemp should be considered the
52
51
    reference implementation when adding a new or modifying an
53
52
    existing one
55
54
*/
56
55
 
57
56
File create_temp_file(char *to, const char *dir, const char *prefix,
58
 
                      int mode __attribute__((unused)),
59
 
                      myf MyFlags __attribute__((unused)))
 
57
                      int,
 
58
                      myf MyFlags)
60
59
{
61
60
  File file= -1;
62
61
 
65
64
    dir=getenv("TMPDIR");
66
65
  if ((res=tempnam((char*) dir,(char *) prefix)))
67
66
  {
68
 
    strmake(to,res,FN_REFLEN-1);
 
67
    strncpy(to,res,FN_REFLEN-1);
69
68
    (*free)(res);
70
 
    file=my_create(to, 0, mode | O_EXCL | O_NOFOLLOW, MyFlags);
 
69
    file=my_create(to, 0, mode | O_EXCL, MyFlags);
71
70
  }
72
71
#elif defined(HAVE_MKSTEMP)
73
72
  {
74
 
    char prefix_buff[30];
75
 
    uint32_t pfx_len;
76
73
    File org_file;
77
 
 
78
 
    pfx_len= (uint) (my_stpcpy(my_stpncpy(prefix_buff,
79
 
                                    prefix ? prefix : "tmp.",
80
 
                                    sizeof(prefix_buff)-7),"XXXXXX") -
81
 
                     prefix_buff);
 
74
    string prefix_str;
 
75
 
 
76
    prefix_str= prefix ? prefix : "tmp.";
 
77
    prefix_str.append("XXXXXX");
 
78
 
82
79
    if (!dir && ! (dir =getenv("TMPDIR")))
83
 
      dir=P_tmpdir;
84
 
    if (strlen(dir)+ pfx_len > FN_REFLEN-2)
 
80
      dir= P_tmpdir;
 
81
    if (strlen(dir)+prefix_str.length() > FN_REFLEN-2)
85
82
    {
86
83
      errno=my_errno= ENAMETOOLONG;
87
84
      return(file);
88
85
    }
89
 
    my_stpcpy(convert_dirname(to,dir,NULL),prefix_buff);
 
86
    strcpy(convert_dirname(to,dir,NULL),prefix_str.c_str());
90
87
    org_file=mkstemp(to);
91
 
    if (mode & O_TEMPORARY)
 
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)
92
91
      (void) my_delete(to, MYF(MY_WME | ME_NOINPUT));
 
92
     */
93
93
    file=my_register_filename(org_file, to, FILE_BY_MKSTEMP,
94
94
                              EE_CANTCREATEFILE, MyFlags);
95
95
    /* If we didn't manage to register the name, remove the temp file */
103
103
  }
104
104
#elif defined(HAVE_TEMPNAM)
105
105
  {
 
106
    (void)MyFlags;
106
107
    char *res,**old_env,*temp_env[1];
107
108
    if (dir && !dir[0])
108
109
    {                           /* Change empty string to current dir */
118
119
    }
119
120
    if ((res=tempnam((char*) dir, (char*) prefix)))
120
121
    {
121
 
      strmake(to,res,FN_REFLEN-1);
 
122
      strncpy(to,res,FN_REFLEN-1);
122
123
      (*free)(res);
123
124
      file=my_create(to,0,
124
 
                     (int) (O_RDWR | O_BINARY | O_TRUNC | O_EXCL | O_NOFOLLOW |
125
 
                            O_TEMPORARY | O_SHORT_LIVED),
 
125
                     (int) (O_RDWR | O_TRUNC | O_EXCL),
126
126
                     MYF(MY_WME));
127
127
 
128
128
    }