~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/mf_tempfile.c

  • Committer: Stewart Smith
  • Date: 2008-07-13 08:00:16 UTC
  • mto: (210.1.1 drizzle)
  • mto: This revision was merged to the branch mainline in revision 211.
  • Revision ID: stewart@flamingspork.com-20080713080016-8qtjv9ypt42azzr6
CRC32() as UDF

Show diffs side-by-side

added added

removed removed

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