~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/mf_tempfile.c

  • Committer: Monty Taylor
  • Date: 2008-10-16 06:32:30 UTC
  • mto: (511.1.5 codestyle)
  • mto: This revision was merged to the branch mainline in revision 521.
  • Revision ID: monty@inaugust.com-20081016063230-4brxsra0qsmsg84q
Added -Wunused-macros.

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 <m_string.h>
 
17
#include <mystrings/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
 
 
51
48
    The implementation using mkstemp should be considered the
52
49
    reference implementation when adding a new or modifying an
53
50
    existing one
60
57
{
61
58
  File file= -1;
62
59
 
63
 
  DBUG_ENTER("create_temp_file");
64
 
  DBUG_PRINT("enter", ("dir: %s, prefix: %s", dir, prefix));
65
60
#if defined(_ZTC__)
66
61
  if (!dir)
67
62
    dir=getenv("TMPDIR");
69
64
  {
70
65
    strmake(to,res,FN_REFLEN-1);
71
66
    (*free)(res);
72
 
    file=my_create(to, 0, mode | O_EXCL | O_NOFOLLOW, MyFlags);
 
67
    file=my_create(to, 0, mode | O_EXCL, MyFlags);
73
68
  }
74
69
#elif defined(HAVE_MKSTEMP)
75
70
  {
76
71
    char prefix_buff[30];
77
 
    uint pfx_len;
 
72
    uint32_t pfx_len;
78
73
    File org_file;
79
74
 
80
 
    pfx_len= (uint) (strmov(strnmov(prefix_buff,
 
75
    pfx_len= (uint) (my_stpcpy(my_stpncpy(prefix_buff,
81
76
                                    prefix ? prefix : "tmp.",
82
77
                                    sizeof(prefix_buff)-7),"XXXXXX") -
83
78
                     prefix_buff);
86
81
    if (strlen(dir)+ pfx_len > FN_REFLEN-2)
87
82
    {
88
83
      errno=my_errno= ENAMETOOLONG;
89
 
      DBUG_RETURN(file);
 
84
      return(file);
90
85
    }
91
 
    strmov(convert_dirname(to,dir,NullS),prefix_buff);
 
86
    my_stpcpy(convert_dirname(to,dir,NULL),prefix_buff);
92
87
    org_file=mkstemp(to);
93
 
    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)
94
91
      (void) my_delete(to, MYF(MY_WME | ME_NOINPUT));
 
92
     */
95
93
    file=my_register_filename(org_file, to, FILE_BY_MKSTEMP,
96
94
                              EE_CANTCREATEFILE, MyFlags);
97
95
    /* If we didn't manage to register the name, remove the temp file */
123
121
      strmake(to,res,FN_REFLEN-1);
124
122
      (*free)(res);
125
123
      file=my_create(to,0,
126
 
                     (int) (O_RDWR | O_BINARY | O_TRUNC | O_EXCL | O_NOFOLLOW |
127
 
                            O_TEMPORARY | O_SHORT_LIVED),
 
124
                     (int) (O_RDWR | O_TRUNC | O_EXCL),
128
125
                     MYF(MY_WME));
129
126
 
130
127
    }
131
 
    else
132
 
    {
133
 
      DBUG_PRINT("error",("Got error: %d from tempnam",errno));
134
 
    }
135
128
    environ=(const char**) old_env;
136
129
  }
137
130
#else
139
132
#endif
140
133
  if (file >= 0)
141
134
    thread_safe_increment(my_tmp_file_created,&THR_LOCK_open);
142
 
  DBUG_RETURN(file);
 
135
  return(file);
143
136
}