~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/mf_tempfile.c

  • Committer: Brian Aker
  • Date: 2008-07-15 06:45:16 UTC
  • Revision ID: brian@tangent.org-20080715064516-fnbq7kowh7w57bxj
Merge Monty's code.

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
20
#include <errno.h>
45
45
    implementation, it's main use is to generate a file with
46
46
    a name that does not already exist.
47
47
 
 
48
    When passing O_TEMPORARY flag in "mode" the file should
 
49
    be automatically deleted
 
50
 
48
51
    The implementation using mkstemp should be considered the
49
52
    reference implementation when adding a new or modifying an
50
53
    existing one
57
60
{
58
61
  File file= -1;
59
62
 
 
63
  DBUG_ENTER("create_temp_file");
 
64
  DBUG_PRINT("enter", ("dir: %s, prefix: %s", dir, prefix));
60
65
#if defined(_ZTC__)
61
66
  if (!dir)
62
67
    dir=getenv("TMPDIR");
64
69
  {
65
70
    strmake(to,res,FN_REFLEN-1);
66
71
    (*free)(res);
67
 
    file=my_create(to, 0, mode | O_EXCL, MyFlags);
 
72
    file=my_create(to, 0, mode | O_EXCL | O_NOFOLLOW, MyFlags);
68
73
  }
69
74
#elif defined(HAVE_MKSTEMP)
70
75
  {
71
76
    char prefix_buff[30];
72
 
    uint32_t pfx_len;
 
77
    uint pfx_len;
73
78
    File org_file;
74
79
 
75
 
    pfx_len= (uint) (my_stpcpy(my_stpncpy(prefix_buff,
 
80
    pfx_len= (uint) (strmov(strnmov(prefix_buff,
76
81
                                    prefix ? prefix : "tmp.",
77
82
                                    sizeof(prefix_buff)-7),"XXXXXX") -
78
83
                     prefix_buff);
81
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
 
    my_stpcpy(convert_dirname(to,dir,NULL),prefix_buff);
 
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 */
121
123
      strmake(to,res,FN_REFLEN-1);
122
124
      (*free)(res);
123
125
      file=my_create(to,0,
124
 
                     (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),
125
128
                     MYF(MY_WME));
126
129
 
127
130
    }
 
131
    else
 
132
    {
 
133
      DBUG_PRINT("error",("Got error: %d from tempnam",errno));
 
134
    }
128
135
    environ=(const char**) old_env;
129
136
  }
130
137
#else
132
139
#endif
133
140
  if (file >= 0)
134
141
    thread_safe_increment(my_tmp_file_created,&THR_LOCK_open);
135
 
  return(file);
 
142
  DBUG_RETURN(file);
136
143
}