~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/mf_tempfile.c

  • Committer: Jay Pipes
  • Date: 2008-07-17 17:54:00 UTC
  • mto: This revision was merged to the branch mainline in revision 182.
  • Revision ID: jay@mysql.com-20080717175400-xm2aazihjra8mdzq
Removal of DBUG from libdrizzle/ - Round 2

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
 
 
16
 
#include <config.h>
17
 
 
18
 
#include <drizzled/internal/my_sys.h>
19
 
#include <drizzled/internal/m_string.h>
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#include "mysys_priv.h"
 
17
#include <m_string.h>
20
18
#include "my_static.h"
21
 
#include <drizzled/error.h>
22
 
#include <stdio.h>
 
19
#include "mysys_err.h"
23
20
#include <errno.h>
24
 
#include <string>
25
21
#ifdef HAVE_PATHS_H
26
22
#include <paths.h>
27
23
#endif
28
24
 
29
 
using namespace std;
30
 
namespace drizzled
31
 
{
32
 
namespace internal
33
 
{
 
25
 
34
26
 
35
27
/*
36
28
  @brief
41
33
    to             pointer to buffer where temporary filename will be stored
42
34
    dir            directory where to create the file
43
35
    prefix         prefix the filename with this
 
36
    mode           Flags to use for my_create/my_open
44
37
    MyFlags        Magic flags
45
38
 
46
39
  @return
52
45
    implementation, it's main use is to generate a file with
53
46
    a name that does not already exist.
54
47
 
 
48
    When passing O_TEMPORARY flag in "mode" the file should
 
49
    be automatically deleted
 
50
 
55
51
    The implementation using mkstemp should be considered the
56
52
    reference implementation when adding a new or modifying an
57
53
    existing one
58
54
 
59
55
*/
60
56
 
61
 
int create_temp_file(char *to, const char *dir, const char *prefix,
62
 
                     myf MyFlags)
 
57
File create_temp_file(char *to, const char *dir, const char *prefix,
 
58
                      int mode __attribute__((unused)),
 
59
                      myf MyFlags __attribute__((unused)))
63
60
{
64
 
  int file= -1;
65
 
 
66
 
  int org_file;
67
 
  string prefix_str;
68
 
 
69
 
  prefix_str= prefix ? prefix : "tmp.";
70
 
  prefix_str.append("XXXXXX");
71
 
 
72
 
  if (!dir && ! (dir =getenv("TMPDIR")))
73
 
    dir= P_tmpdir;
74
 
  if (strlen(dir)+prefix_str.length() > FN_REFLEN-2)
75
 
  {
76
 
    errno= ENAMETOOLONG;
77
 
    return(file);
78
 
  }
79
 
  strcpy(convert_dirname(to,dir,NULL),prefix_str.c_str());
80
 
  org_file=mkstemp(to);
81
 
  /* TODO: This was old behavior, but really don't we want to
82
 
   * unlink files immediately under all circumstances?
83
 
   * if (mode & O_TEMPORARY)
84
 
    (void) my_delete(to, MYF(MY_WME | ME_NOINPUT));
85
 
  */
86
 
  file=my_register_filename(org_file, to, EE_CANTCREATEFILE, MyFlags);
87
 
 
88
 
  /* If we didn't manage to register the name, remove the temp file */
89
 
  if (org_file >= 0 && file < 0)
90
 
  {
91
 
    int tmp=errno;
92
 
    close(org_file);
93
 
    (void) my_delete(to, MYF(MY_WME | ME_NOINPUT));
94
 
    errno=tmp;
95
 
  }
96
 
 
97
 
  return(file);
 
61
  File file= -1;
 
62
 
 
63
  DBUG_ENTER("create_temp_file");
 
64
  DBUG_PRINT("enter", ("dir: %s, prefix: %s", dir, prefix));
 
65
#if defined(_ZTC__)
 
66
  if (!dir)
 
67
    dir=getenv("TMPDIR");
 
68
  if ((res=tempnam((char*) dir,(char *) prefix)))
 
69
  {
 
70
    strmake(to,res,FN_REFLEN-1);
 
71
    (*free)(res);
 
72
    file=my_create(to, 0, mode | O_EXCL | O_NOFOLLOW, MyFlags);
 
73
  }
 
74
#elif defined(HAVE_MKSTEMP)
 
75
  {
 
76
    char prefix_buff[30];
 
77
    uint pfx_len;
 
78
    File org_file;
 
79
 
 
80
    pfx_len= (uint) (strmov(strnmov(prefix_buff,
 
81
                                    prefix ? prefix : "tmp.",
 
82
                                    sizeof(prefix_buff)-7),"XXXXXX") -
 
83
                     prefix_buff);
 
84
    if (!dir && ! (dir =getenv("TMPDIR")))
 
85
      dir=P_tmpdir;
 
86
    if (strlen(dir)+ pfx_len > FN_REFLEN-2)
 
87
    {
 
88
      errno=my_errno= ENAMETOOLONG;
 
89
      DBUG_RETURN(file);
 
90
    }
 
91
    strmov(convert_dirname(to,dir,NullS),prefix_buff);
 
92
    org_file=mkstemp(to);
 
93
    if (mode & O_TEMPORARY)
 
94
      (void) my_delete(to, MYF(MY_WME | ME_NOINPUT));
 
95
    file=my_register_filename(org_file, to, FILE_BY_MKSTEMP,
 
96
                              EE_CANTCREATEFILE, MyFlags);
 
97
    /* If we didn't manage to register the name, remove the temp file */
 
98
    if (org_file >= 0 && file < 0)
 
99
    {
 
100
      int tmp=my_errno;
 
101
      close(org_file);
 
102
      (void) my_delete(to, MYF(MY_WME | ME_NOINPUT));
 
103
      my_errno=tmp;
 
104
    }
 
105
  }
 
106
#elif defined(HAVE_TEMPNAM)
 
107
  {
 
108
    char *res,**old_env,*temp_env[1];
 
109
    if (dir && !dir[0])
 
110
    {                           /* Change empty string to current dir */
 
111
      to[0]= FN_CURLIB;
 
112
      to[1]= 0;
 
113
      dir=to;
 
114
    }
 
115
    old_env= (char**) environ;
 
116
    if (dir)
 
117
    {                           /* Don't use TMPDIR if dir is given */
 
118
      environ=(const char**) temp_env;
 
119
      temp_env[0]=0;
 
120
    }
 
121
    if ((res=tempnam((char*) dir, (char*) prefix)))
 
122
    {
 
123
      strmake(to,res,FN_REFLEN-1);
 
124
      (*free)(res);
 
125
      file=my_create(to,0,
 
126
                     (int) (O_RDWR | O_BINARY | O_TRUNC | O_EXCL | O_NOFOLLOW |
 
127
                            O_TEMPORARY | O_SHORT_LIVED),
 
128
                     MYF(MY_WME));
 
129
 
 
130
    }
 
131
    else
 
132
    {
 
133
      DBUG_PRINT("error",("Got error: %d from tempnam",errno));
 
134
    }
 
135
    environ=(const char**) old_env;
 
136
  }
 
137
#else
 
138
#error No implementation found for create_temp_file
 
139
#endif
 
140
  if (file >= 0)
 
141
    thread_safe_increment(my_tmp_file_created,&THR_LOCK_open);
 
142
  DBUG_RETURN(file);
98
143
}
99
 
 
100
 
} /* namespace internal */
101
 
} /* namespace drizzled */