Drizzled Public API Documentation

iocache.cc
00001 /* Copyright (C) 2000 MySQL AB
00002 
00003    This program is free software; you can redistribute it and/or modify
00004    it under the terms of the GNU General Public License as published by
00005    the Free Software Foundation; version 2 of the License.
00006 
00007    This program is distributed in the hope that it will be useful,
00008    but WITHOUT ANY WARRANTY; without even the implied warranty of
00009    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010    GNU General Public License for more details.
00011 
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software
00014    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00015 
00016 /* Open a temporary file and cache it with io_cache. Delete it on close */
00017 
00018 #include <config.h>
00019 
00020 #include <drizzled/internal/my_sys.h>
00021 #include <drizzled/internal/m_string.h>
00022 #include <drizzled/internal/my_static.h>
00023 #include <drizzled/internal/iocache.h>
00024 #include <drizzled/error.h>
00025 
00026 namespace drizzled {
00027 namespace internal {
00028 
00029 /*
00030 ** Open tempfile cached by io_cache_st
00031 ** Should be used when no seeks are done (only reinit_io_buff)
00032 ** Return false if cache is inited ok
00033 ** The actual file is created when the io_cache_st buffer gets filled
00034 ** If dir is not given, use TMPDIR.
00035 */
00036 
00037 bool io_cache_st::open_cached_file(const char *dir_arg, const char *prefix_arg,
00038           size_t cache_size_arg, myf cache_myflags)
00039 {
00040   dir= dir_arg ? strdup(dir_arg) : NULL;
00041   prefix= prefix_arg ? strdup(prefix_arg) : NULL;
00042 
00043   if ((dir == NULL) || (prefix == NULL))
00044     return true;
00045 
00046   file_name= 0;
00047   buffer= 0;        /* Mark that not open */
00048   if (not init_io_cache(-1, cache_size_arg,WRITE_CACHE,0L,0, MYF(cache_myflags | MY_NABP)))
00049   {
00050     return false;
00051   }
00052   free(dir);
00053   free(prefix);
00054 
00055   return true;
00056 }
00057 
00058 /* Create the temporary file */
00059 
00060 bool io_cache_st::real_open_cached_file()
00061 {
00062   char name_buff[FN_REFLEN];
00063 
00064   if ((file= create_temp_file(name_buff, dir, prefix, MYF(MY_WME))) >= 0)
00065   {
00066     my_delete(name_buff,MYF(MY_WME | ME_NOINPUT));
00067     return false;
00068   }
00069 
00070   return true;
00071 }
00072 
00073 
00074 void io_cache_st::close_cached_file()
00075 {
00076   if (inited())
00077   {
00078     int _file= file;
00079     file= -1;       /* Don't flush data */
00080     (void) end_io_cache();
00081     if (_file >= 0)
00082     {
00083       (void) my_close(_file, MYF(0));
00084 #ifdef CANT_DELETE_OPEN_FILES
00085       if (file_name)
00086       {
00087   (void) my_delete(file_name, MYF(MY_WME | ME_NOINPUT));
00088   free(file_name);
00089       }
00090 #endif
00091     }
00092     free(dir);
00093     free(prefix);
00094   }
00095 }
00096 
00097 } /* namespace internal */
00098 } /* namespace drizzled */