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 {
00028 namespace internal
00029 {
00030 
00031 /*
00032 ** Open tempfile cached by st_io_cache
00033 ** Should be used when no seeks are done (only reinit_io_buff)
00034 ** Return false if cache is inited ok
00035 ** The actual file is created when the st_io_cache buffer gets filled
00036 ** If dir is not given, use TMPDIR.
00037 */
00038 
00039 bool st_io_cache::open_cached_file(const char *dir_arg, const char *prefix_arg,
00040           size_t cache_size_arg, myf cache_myflags)
00041 {
00042   dir=   dir_arg ? strdup(dir_arg) : (char*) 0;
00043   prefix= (prefix_arg ? strdup(prefix_arg) : (char*) 0);
00044 
00045   if ((dir == NULL) || (prefix == NULL))
00046     return true;
00047 
00048   file_name= 0;
00049   buffer= 0;        /* Mark that not open */
00050   if (not init_io_cache(-1, cache_size_arg,WRITE_CACHE,0L,0, MYF(cache_myflags | MY_NABP)))
00051   {
00052     return false;
00053   }
00054   free(dir);
00055   free(prefix);
00056 
00057   return true;
00058 }
00059 
00060 /* Create the temporary file */
00061 
00062 bool st_io_cache::real_open_cached_file()
00063 {
00064   char name_buff[FN_REFLEN];
00065 
00066   if ((file= create_temp_file(name_buff, dir, prefix, MYF(MY_WME))) >= 0)
00067   {
00068     my_delete(name_buff,MYF(MY_WME | ME_NOINPUT));
00069     return false;
00070   }
00071 
00072   return true;
00073 }
00074 
00075 
00076 void st_io_cache::close_cached_file()
00077 {
00078   if (my_b_inited(this))
00079   {
00080     int _file= file;
00081     file= -1;       /* Don't flush data */
00082     (void) end_io_cache();
00083     if (_file >= 0)
00084     {
00085       (void) my_close(_file, MYF(0));
00086 #ifdef CANT_DELETE_OPEN_FILES
00087       if (file_name)
00088       {
00089   (void) my_delete(file_name, MYF(MY_WME | ME_NOINPUT));
00090   free(file_name);
00091       }
00092 #endif
00093     }
00094     free(dir);
00095     free(prefix);
00096   }
00097 }
00098 
00099 } /* namespace internal */
00100 } /* namespace drizzled */