Drizzled Public API Documentation

iocache.cc
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., 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 <config.h>
19 
20 #include <drizzled/internal/my_sys.h>
21 #include <drizzled/internal/m_string.h>
22 #include <drizzled/internal/my_static.h>
23 #include <drizzled/internal/iocache.h>
24 #include <drizzled/error.h>
25 
26 namespace drizzled {
27 namespace internal {
28 
29 /*
30 ** Open tempfile cached by io_cache_st
31 ** Should be used when no seeks are done (only reinit_io_buff)
32 ** Return false if cache is inited ok
33 ** The actual file is created when the io_cache_st buffer gets filled
34 ** If dir is not given, use TMPDIR.
35 */
36 
37 bool io_cache_st::open_cached_file(const char *dir_arg, const char *prefix_arg,
38  size_t cache_size_arg, myf cache_myflags)
39 {
40  dir= dir_arg ? strdup(dir_arg) : NULL;
41  prefix= prefix_arg ? strdup(prefix_arg) : NULL;
42 
43  if ((dir == NULL) || (prefix == NULL))
44  return true;
45 
46  file_name= 0;
47  buffer= 0; /* Mark that not open */
48  if (not init_io_cache(-1, cache_size_arg,WRITE_CACHE,0L,0, MYF(cache_myflags | MY_NABP)))
49  {
50  return false;
51  }
52  free(dir);
53  free(prefix);
54 
55  return true;
56 }
57 
58 /* Create the temporary file */
59 
60 bool io_cache_st::real_open_cached_file()
61 {
62  char name_buff[FN_REFLEN];
63 
64  if ((file= create_temp_file(name_buff, dir, prefix, MYF(MY_WME))) >= 0)
65  {
66  my_delete(name_buff,MYF(MY_WME | ME_NOINPUT));
67  return false;
68  }
69 
70  return true;
71 }
72 
73 
74 void io_cache_st::close_cached_file()
75 {
76  if (inited())
77  {
78  int _file= file;
79  file= -1; /* Don't flush data */
80  (void) end_io_cache();
81  if (_file >= 0)
82  {
83  (void) my_close(_file, MYF(0));
84 #ifdef CANT_DELETE_OPEN_FILES
85  if (file_name)
86  {
87  (void) my_delete(file_name, MYF(MY_WME | ME_NOINPUT));
88  free(file_name);
89  }
90 #endif
91  }
92  free(dir);
93  free(prefix);
94  }
95 }
96 
97 } /* namespace internal */
98 } /* namespace drizzled */