~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/mf_tempfile.cc

Merge of Jay

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
   along with this program; if not, write to the Free Software
14
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
 
#include "mysys_priv.h"
 
16
#include "mysys/mysys_priv.h"
17
17
#include <mystrings/m_string.h>
18
18
#include "my_static.h"
19
 
#include "mysys_err.h"
 
19
#include "mysys/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
33
35
    to             pointer to buffer where temporary filename will be stored
34
36
    dir            directory where to create the file
35
37
    prefix         prefix the filename with this
36
 
    mode           Flags to use for my_create/my_open
37
38
    MyFlags        Magic flags
38
39
 
39
40
  @return
45
46
    implementation, it's main use is to generate a file with
46
47
    a name that does not already exist.
47
48
 
48
 
    When passing O_TEMPORARY flag in "mode" the file should
49
 
    be automatically deleted
50
 
 
51
49
    The implementation using mkstemp should be considered the
52
50
    reference implementation when adding a new or modifying an
53
51
    existing one
54
52
 
55
53
*/
56
54
 
57
 
File create_temp_file(char *to, const char *dir, const char *prefix,
58
 
                      int mode __attribute__((unused)),
59
 
                      myf MyFlags __attribute__((unused)))
 
55
File create_temp_file(char *to, const char *dir, const char *prefix, myf MyFlags)
60
56
{
61
57
  File file= -1;
62
58
 
65
61
    dir=getenv("TMPDIR");
66
62
  if ((res=tempnam((char*) dir,(char *) prefix)))
67
63
  {
68
 
    strmake(to,res,FN_REFLEN-1);
 
64
    strncpy(to,res,FN_REFLEN-1);
69
65
    (*free)(res);
70
 
    file=my_create(to, 0, mode | O_EXCL | O_NOFOLLOW, MyFlags);
 
66
    file=my_create(to, 0, mode | O_EXCL, MyFlags);
71
67
  }
72
68
#elif defined(HAVE_MKSTEMP)
73
69
  {
74
 
    char prefix_buff[30];
75
 
    uint pfx_len;
76
70
    File org_file;
77
 
 
78
 
    pfx_len= (uint) (stpcpy(stpncpy(prefix_buff,
79
 
                                    prefix ? prefix : "tmp.",
80
 
                                    sizeof(prefix_buff)-7),"XXXXXX") -
81
 
                     prefix_buff);
 
71
    string prefix_str;
 
72
 
 
73
    prefix_str= prefix ? prefix : "tmp.";
 
74
    prefix_str.append("XXXXXX");
 
75
 
82
76
    if (!dir && ! (dir =getenv("TMPDIR")))
83
 
      dir=P_tmpdir;
84
 
    if (strlen(dir)+ pfx_len > FN_REFLEN-2)
 
77
      dir= P_tmpdir;
 
78
    if (strlen(dir)+prefix_str.length() > FN_REFLEN-2)
85
79
    {
86
80
      errno=my_errno= ENAMETOOLONG;
87
81
      return(file);
88
82
    }
89
 
    stpcpy(convert_dirname(to,dir,NullS),prefix_buff);
 
83
    strcpy(convert_dirname(to,dir,NULL),prefix_str.c_str());
90
84
    org_file=mkstemp(to);
91
 
    if (mode & O_TEMPORARY)
 
85
    /* TODO: This was old behavior, but really don't we want to
 
86
     * unlink files immediately under all circumstances?
 
87
     * if (mode & O_TEMPORARY)
92
88
      (void) my_delete(to, MYF(MY_WME | ME_NOINPUT));
93
 
    file=my_register_filename(org_file, to, FILE_BY_MKSTEMP,
94
 
                              EE_CANTCREATEFILE, MyFlags);
 
89
     */
 
90
    file=my_register_filename(org_file, to, EE_CANTCREATEFILE, MyFlags);
 
91
 
95
92
    /* If we didn't manage to register the name, remove the temp file */
96
93
    if (org_file >= 0 && file < 0)
97
94
    {
103
100
  }
104
101
#elif defined(HAVE_TEMPNAM)
105
102
  {
 
103
    (void)MyFlags;
106
104
    char *res,**old_env,*temp_env[1];
107
105
    if (dir && !dir[0])
108
106
    {                           /* Change empty string to current dir */
118
116
    }
119
117
    if ((res=tempnam((char*) dir, (char*) prefix)))
120
118
    {
121
 
      strmake(to,res,FN_REFLEN-1);
 
119
      strncpy(to,res,FN_REFLEN-1);
122
120
      (*free)(res);
123
121
      file=my_create(to,0,
124
 
                     (int) (O_RDWR | O_BINARY | O_TRUNC | O_EXCL | O_NOFOLLOW |
125
 
                            O_TEMPORARY | O_SHORT_LIVED),
 
122
                     (int) (O_RDWR | O_TRUNC | O_EXCL),
126
123
                     MYF(MY_WME));
127
124
 
128
125
    }
132
129
#error No implementation found for create_temp_file
133
130
#endif
134
131
  if (file >= 0)
135
 
    thread_safe_increment(my_tmp_file_created,&THR_LOCK_open);
 
132
    my_tmp_file_created++;
 
133
 
136
134
  return(file);
137
135
}