1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
3
This library is free software; you can redistribute it and/or
4
modify it under the terms of the GNU Library General Public
5
License as published by the Free Software Foundation; either
6
version 2 of the License, or (at your option) any later version.
8
This library is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
Library General Public License for more details.
13
You should have received a copy of the GNU Library General Public
14
License along with this library; if not, write to the Free
15
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18
/* Open a temporary file and cache it with io_cache. Delete it on close */
20
#include "mysys_priv.h"
22
#include "my_static.h"
23
#include "mysys_err.h"
26
Remove an open tempfile so that it doesn't survive
27
if we crash; If the operating system doesn't support
28
this, just remember the file name for later removal
31
static my_bool cache_remove_open_tmp(IO_CACHE *cache __attribute__((unused)),
35
#if !defined(CANT_DELETE_OPEN_FILES)
36
/* The following should always succeed */
37
(void) my_delete(name,MYF(MY_WME | ME_NOINPUT));
40
if (!(cache->file_name=
41
(char*) my_malloc((length=strlen(name)+1),MYF(MY_WME))))
43
my_close(cache->file,MYF(0));
45
errno=my_errno=ENOMEM;
48
memcpy(cache->file_name,name,length);
50
#endif /* O_TEMPORARY == 0 */
55
** Open tempfile cached by IO_CACHE
56
** Should be used when no seeks are done (only reinit_io_buff)
57
** Return 0 if cache is inited ok
58
** The actual file is created when the IO_CACHE buffer gets filled
59
** If dir is not given, use TMPDIR.
62
my_bool open_cached_file(IO_CACHE *cache, const char* dir, const char *prefix,
63
uint cache_size, myf cache_myflags)
65
DBUG_ENTER("open_cached_file");
66
cache->dir= dir ? my_strdup(dir,MYF(cache_myflags & MY_WME)) : (char*) 0;
67
cache->prefix= (prefix ? my_strdup(prefix,MYF(cache_myflags & MY_WME)) :
70
cache->buffer=0; /* Mark that not open */
71
if (!init_io_cache(cache,-1,cache_size,WRITE_CACHE,0L,0,
72
MYF(cache_myflags | MY_NABP)))
76
my_free(cache->dir, MYF(MY_ALLOW_ZERO_PTR));
77
my_free(cache->prefix,MYF(MY_ALLOW_ZERO_PTR));
81
/* Create the temporary file */
83
my_bool real_open_cached_file(IO_CACHE *cache)
85
char name_buff[FN_REFLEN];
87
DBUG_ENTER("real_open_cached_file");
88
if ((cache->file=create_temp_file(name_buff, cache->dir, cache->prefix,
89
(O_RDWR | O_BINARY | O_TRUNC |
90
O_TEMPORARY | O_SHORT_LIVED),
94
cache_remove_open_tmp(cache, name_buff);
100
void close_cached_file(IO_CACHE *cache)
102
DBUG_ENTER("close_cached_file");
103
if (my_b_inited(cache))
105
File file=cache->file;
106
cache->file= -1; /* Don't flush data */
107
(void) end_io_cache(cache);
110
(void) my_close(file,MYF(0));
111
#ifdef CANT_DELETE_OPEN_FILES
112
if (cache->file_name)
114
(void) my_delete(cache->file_name,MYF(MY_WME | ME_NOINPUT));
115
my_free(cache->file_name,MYF(0));
119
my_free(cache->dir,MYF(MY_ALLOW_ZERO_PTR));
120
my_free(cache->prefix,MYF(MY_ALLOW_ZERO_PTR));