1
by brian
clean slate |
1 |
/* Copyright (C) 2000 MySQL AB
|
2 |
||
3 |
This program is free software; you can redistribute it and/or modify
|
|
4 |
it under the terms of the GNU General Public License as published by
|
|
5 |
the Free Software Foundation; version 2 of the License.
|
|
6 |
||
7 |
This program is distributed in the hope that it will be useful,
|
|
8 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 |
GNU General Public License for more details.
|
|
11 |
||
12 |
You should have received a copy of the GNU General Public License
|
|
13 |
along with this program; if not, write to the Free Software
|
|
14 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
15 |
||
16 |
/* Open a temporary file and cache it with io_cache. Delete it on close */
|
|
17 |
||
18 |
#include "mysys_priv.h" |
|
19 |
#include <m_string.h> |
|
20 |
#include "my_static.h" |
|
21 |
#include "mysys_err.h" |
|
22 |
||
23 |
/*
|
|
24 |
Remove an open tempfile so that it doesn't survive
|
|
25 |
if we crash; If the operating system doesn't support
|
|
26 |
this, just remember the file name for later removal
|
|
27 |
*/
|
|
28 |
||
29 |
static my_bool cache_remove_open_tmp(IO_CACHE *cache __attribute__((unused)), |
|
30 |
const char *name) |
|
31 |
{
|
|
32 |
#if O_TEMPORARY == 0
|
|
33 |
#if !defined(CANT_DELETE_OPEN_FILES)
|
|
34 |
/* The following should always succeed */
|
|
35 |
(void) my_delete(name,MYF(MY_WME | ME_NOINPUT)); |
|
36 |
#else
|
|
37 |
int length; |
|
38 |
if (!(cache->file_name= |
|
39 |
(char*) my_malloc((length=strlen(name)+1),MYF(MY_WME)))) |
|
40 |
{
|
|
41 |
my_close(cache->file,MYF(0)); |
|
42 |
cache->file = -1; |
|
43 |
errno=my_errno=ENOMEM; |
|
44 |
return 1; |
|
45 |
}
|
|
46 |
memcpy(cache->file_name,name,length); |
|
47 |
#endif
|
|
48 |
#endif /* O_TEMPORARY == 0 */ |
|
49 |
return 0; |
|
50 |
}
|
|
51 |
||
52 |
/*
|
|
53 |
** Open tempfile cached by IO_CACHE
|
|
54 |
** Should be used when no seeks are done (only reinit_io_buff)
|
|
55 |
** Return 0 if cache is inited ok
|
|
56 |
** The actual file is created when the IO_CACHE buffer gets filled
|
|
57 |
** If dir is not given, use TMPDIR.
|
|
58 |
*/
|
|
59 |
||
60 |
my_bool open_cached_file(IO_CACHE *cache, const char* dir, const char *prefix, |
|
61 |
size_t cache_size, myf cache_myflags) |
|
62 |
{
|
|
63 |
DBUG_ENTER("open_cached_file"); |
|
64 |
cache->dir= dir ? my_strdup(dir,MYF(cache_myflags & MY_WME)) : (char*) 0; |
|
65 |
cache->prefix= (prefix ? my_strdup(prefix,MYF(cache_myflags & MY_WME)) : |
|
66 |
(char*) 0); |
|
67 |
cache->file_name=0; |
|
68 |
cache->buffer=0; /* Mark that not open */ |
|
69 |
if (!init_io_cache(cache,-1,cache_size,WRITE_CACHE,0L,0, |
|
70 |
MYF(cache_myflags | MY_NABP))) |
|
71 |
{
|
|
72 |
DBUG_RETURN(0); |
|
73 |
}
|
|
74 |
my_free(cache->dir, MYF(MY_ALLOW_ZERO_PTR)); |
|
75 |
my_free(cache->prefix,MYF(MY_ALLOW_ZERO_PTR)); |
|
76 |
DBUG_RETURN(1); |
|
77 |
}
|
|
78 |
||
79 |
/* Create the temporary file */
|
|
80 |
||
81 |
my_bool real_open_cached_file(IO_CACHE *cache) |
|
82 |
{
|
|
83 |
char name_buff[FN_REFLEN]; |
|
84 |
int error=1; |
|
85 |
DBUG_ENTER("real_open_cached_file"); |
|
86 |
if ((cache->file=create_temp_file(name_buff, cache->dir, cache->prefix, |
|
87 |
(O_RDWR | O_BINARY | O_TRUNC | |
|
88 |
O_TEMPORARY | O_SHORT_LIVED), |
|
89 |
MYF(MY_WME))) >= 0) |
|
90 |
{
|
|
91 |
error=0; |
|
92 |
cache_remove_open_tmp(cache, name_buff); |
|
93 |
}
|
|
94 |
DBUG_RETURN(error); |
|
95 |
}
|
|
96 |
||
97 |
||
98 |
void close_cached_file(IO_CACHE *cache) |
|
99 |
{
|
|
100 |
DBUG_ENTER("close_cached_file"); |
|
101 |
if (my_b_inited(cache)) |
|
102 |
{
|
|
103 |
File file=cache->file; |
|
104 |
cache->file= -1; /* Don't flush data */ |
|
105 |
(void) end_io_cache(cache); |
|
106 |
if (file >= 0) |
|
107 |
{
|
|
108 |
(void) my_close(file,MYF(0)); |
|
109 |
#ifdef CANT_DELETE_OPEN_FILES
|
|
110 |
if (cache->file_name) |
|
111 |
{
|
|
112 |
(void) my_delete(cache->file_name,MYF(MY_WME | ME_NOINPUT)); |
|
113 |
my_free(cache->file_name,MYF(0)); |
|
114 |
}
|
|
115 |
#endif
|
|
116 |
}
|
|
117 |
my_free(cache->dir,MYF(MY_ALLOW_ZERO_PTR)); |
|
118 |
my_free(cache->prefix,MYF(MY_ALLOW_ZERO_PTR)); |
|
119 |
}
|
|
120 |
DBUG_VOID_RETURN; |
|
121 |
}
|