~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/mf_cache.c

  • Committer: Mark Atwood
  • Date: 2008-10-03 01:39:40 UTC
  • mto: This revision was merged to the branch mainline in revision 437.
  • Revision ID: mark@fallenpegasus.com-20081003013940-mvefjo725dltz41h
rename logging_noop to logging_query

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
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 */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
/* Open a temporary file and cache it with io_cache. Delete it on close */
17
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
 
{
28
 
namespace internal
29
 
{
30
 
 
31
 
/*
32
 
** Open tempfile cached by st_io_cache
33
 
** Should be used when no seeks are done (only reinit_io_buff)
34
 
** Return false if cache is inited ok
35
 
** The actual file is created when the st_io_cache buffer gets filled
36
 
** If dir is not given, use TMPDIR.
37
 
*/
38
 
 
39
 
bool st_io_cache::open_cached_file(const char *dir_arg, const char *prefix_arg,
40
 
                      size_t cache_size_arg, myf cache_myflags)
41
 
{
42
 
  dir=   dir_arg ? strdup(dir_arg) : (char*) 0;
43
 
  prefix= (prefix_arg ? strdup(prefix_arg) : (char*) 0);
44
 
 
45
 
  if ((dir == NULL) || (prefix == NULL))
46
 
    return true;
47
 
 
48
 
  file_name= 0;
49
 
  buffer= 0;                            /* Mark that not open */
50
 
  if (not init_io_cache(-1, cache_size_arg,WRITE_CACHE,0L,0, MYF(cache_myflags | MY_NABP)))
51
 
  {
52
 
    return false;
53
 
  }
54
 
  free(dir);
55
 
  free(prefix);
56
 
 
57
 
  return true;
58
 
}
59
 
 
60
 
/* Create the temporary file */
61
 
 
62
 
bool st_io_cache::real_open_cached_file()
 
18
#include "mysys_priv.h"
 
19
#include <mystrings/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 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
bool open_cached_file(IO_CACHE *cache, const char* dir, const char *prefix,
 
61
                         size_t cache_size, myf cache_myflags)
 
62
{
 
63
  cache->dir=    dir ? my_strdup(dir,MYF(cache_myflags & MY_WME)) : (char*) 0;
 
64
  cache->prefix= (prefix ? my_strdup(prefix,MYF(cache_myflags & MY_WME)) :
 
65
                 (char*) 0);
 
66
  cache->file_name=0;
 
67
  cache->buffer=0;                              /* Mark that not open */
 
68
  if (!init_io_cache(cache,-1,cache_size,WRITE_CACHE,0L,0,
 
69
                     MYF(cache_myflags | MY_NABP)))
 
70
  {
 
71
    return(0);
 
72
  }
 
73
  my_free(cache->dir,   MYF(MY_ALLOW_ZERO_PTR));
 
74
  my_free(cache->prefix,MYF(MY_ALLOW_ZERO_PTR));
 
75
  return(1);
 
76
}
 
77
 
 
78
        /* Create the temporary file */
 
79
 
 
80
bool real_open_cached_file(IO_CACHE *cache)
63
81
{
64
82
  char name_buff[FN_REFLEN];
65
 
 
66
 
  if ((file= create_temp_file(name_buff, dir, prefix, MYF(MY_WME))) >= 0)
 
83
  int error=1;
 
84
  if ((cache->file=create_temp_file(name_buff, cache->dir, cache->prefix,
 
85
                                    (O_RDWR | O_BINARY | O_TRUNC |
 
86
                                     O_TEMPORARY | O_SHORT_LIVED),
 
87
                                    MYF(MY_WME))) >= 0)
67
88
  {
68
 
    my_delete(name_buff,MYF(MY_WME | ME_NOINPUT));
69
 
    return false;
 
89
    error=0;
 
90
    cache_remove_open_tmp(cache, name_buff);
70
91
  }
71
 
 
72
 
  return true;
 
92
  return(error);
73
93
}
74
94
 
75
95
 
76
 
void st_io_cache::close_cached_file()
 
96
void close_cached_file(IO_CACHE *cache)
77
97
{
78
 
  if (my_b_inited(this))
 
98
  if (my_b_inited(cache))
79
99
  {
80
 
    int _file= file;
81
 
    file= -1;                           /* Don't flush data */
82
 
    (void) end_io_cache();
83
 
    if (_file >= 0)
 
100
    File file=cache->file;
 
101
    cache->file= -1;                            /* Don't flush data */
 
102
    (void) end_io_cache(cache);
 
103
    if (file >= 0)
84
104
    {
85
 
      (void) my_close(_file, MYF(0));
 
105
      (void) my_close(file,MYF(0));
86
106
#ifdef CANT_DELETE_OPEN_FILES
87
 
      if (file_name)
 
107
      if (cache->file_name)
88
108
      {
89
 
        (void) my_delete(file_name, MYF(MY_WME | ME_NOINPUT));
90
 
        free(file_name);
 
109
        (void) my_delete(cache->file_name,MYF(MY_WME | ME_NOINPUT));
 
110
        my_free(cache->file_name,MYF(0));
91
111
      }
92
112
#endif
93
113
    }
94
 
    free(dir);
95
 
    free(prefix);
 
114
    my_free(cache->dir,MYF(MY_ALLOW_ZERO_PTR));
 
115
    my_free(cache->prefix,MYF(MY_ALLOW_ZERO_PTR));
96
116
  }
 
117
  return;
97
118
}
98
 
 
99
 
} /* namespace internal */
100
 
} /* namespace drizzled */