1 | /* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. |
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 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 | Open tempfile cached by IO_CACHE |
25 | |
26 | Should be used when no seeks are done (only reinit_io_buff) |
27 | Return 0 if cache is inited ok |
28 | The actual file is created when the IO_CACHE buffer gets filled |
29 | If dir is not given, use TMPDIR. |
30 | */ |
31 | my_bool open_cached_file(IO_CACHE *cache, const char* dir, const char *prefix, |
32 | size_t cache_size, myf cache_myflags) |
33 | { |
34 | DBUG_ENTER("open_cached_file" ); |
35 | cache->dir= dir; |
36 | if (prefix) |
37 | { |
38 | DBUG_ASSERT(strlen(prefix) == 2); |
39 | memcpy(cache->prefix, prefix, 3); |
40 | } |
41 | else |
42 | cache->prefix[0]= 0; |
43 | cache->file_name=0; |
44 | cache->buffer=0; /* Mark that not open */ |
45 | if (!init_io_cache(cache, -1, cache_size, WRITE_CACHE, 0L, 0, |
46 | MYF(cache_myflags | MY_NABP))) |
47 | { |
48 | DBUG_RETURN(0); |
49 | } |
50 | DBUG_RETURN(1); |
51 | } |
52 | |
53 | /** |
54 | Create the temporary file |
55 | */ |
56 | my_bool real_open_cached_file(IO_CACHE *cache) |
57 | { |
58 | char name_buff[FN_REFLEN]; |
59 | int error=1; |
60 | DBUG_ENTER("real_open_cached_file" ); |
61 | if ((cache->file= create_temp_file(name_buff, cache->dir, |
62 | cache->prefix[0] ? cache->prefix : 0, |
63 | O_BINARY, MYF(MY_WME | MY_TEMPORARY))) >= 0) |
64 | { |
65 | error=0; |
66 | } |
67 | DBUG_RETURN(error); |
68 | } |
69 | |
70 | |
71 | void close_cached_file(IO_CACHE *cache) |
72 | { |
73 | DBUG_ENTER("close_cached_file" ); |
74 | if (my_b_inited(cache)) |
75 | { |
76 | File file=cache->file; |
77 | cache->file= -1; /* Don't flush data */ |
78 | (void) end_io_cache(cache); |
79 | if (file >= 0) |
80 | { |
81 | (void) my_close(file,MYF(0)); |
82 | #ifdef CANT_DELETE_OPEN_FILES |
83 | if (cache->file_name) |
84 | { |
85 | (void) my_delete(cache->file_name,MYF(MY_WME | ME_NOINPUT)); |
86 | my_free(cache->file_name); |
87 | } |
88 | #endif |
89 | } |
90 | } |
91 | DBUG_VOID_RETURN; |
92 | } |
93 | |