~drizzle-trunk/drizzle/development

1 by brian
clean slate
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
/* Open a temporary file and cache it with io_cache. Delete it on close */
17
994.2.4 by Monty Taylor
Blast. Fixed some make distcheck issues.
18
#include "mysys/mysys_priv.h"
212.5.18 by Monty Taylor
Moved m_ctype, m_string and my_bitmap. Removed t_ctype.
19
#include <mystrings/m_string.h>
481.1.16 by Monty Taylor
Merged iocache.h addition.
20
#include <mysys/my_static.h>
21
#include <mysys/mysys_err.h>
22
#include <mysys/iocache.h>
1 by brian
clean slate
23
24
25
	/*
26
	** Open tempfile cached by IO_CACHE
27
	** Should be used when no seeks are done (only reinit_io_buff)
656.1.52 by Monty Taylor
Added more return value checks.
28
	** Return false if cache is inited ok
1 by brian
clean slate
29
	** The actual file is created when the IO_CACHE buffer gets filled
30
	** If dir is not given, use TMPDIR.
31
	*/
32
146 by Brian Aker
my_bool cleanup.
33
bool open_cached_file(IO_CACHE *cache, const char* dir, const char *prefix,
1 by brian
clean slate
34
                         size_t cache_size, myf cache_myflags)
35
{
656.1.26 by Monty Taylor
Finally removed all of the my_malloc stuff.
36
  cache->dir=	 dir ? strdup(dir) : (char*) 0;
37
  cache->prefix= (prefix ? strdup(prefix) :
1 by brian
clean slate
38
		 (char*) 0);
656.1.52 by Monty Taylor
Added more return value checks.
39
  if ((cache->dir == NULL) || (cache->prefix == NULL))
40
    return true;
1 by brian
clean slate
41
  cache->file_name=0;
42
  cache->buffer=0;				/* Mark that not open */
43
  if (!init_io_cache(cache,-1,cache_size,WRITE_CACHE,0L,0,
44
		     MYF(cache_myflags | MY_NABP)))
45
  {
656.1.52 by Monty Taylor
Added more return value checks.
46
    return false;
1 by brian
clean slate
47
  }
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
48
  free(cache->dir);
49
  free(cache->prefix);
656.1.52 by Monty Taylor
Added more return value checks.
50
  return true;
1 by brian
clean slate
51
}
52
53
	/* Create the temporary file */
54
146 by Brian Aker
my_bool cleanup.
55
bool real_open_cached_file(IO_CACHE *cache)
1 by brian
clean slate
56
{
57
  char name_buff[FN_REFLEN];
58
  int error=1;
59
  if ((cache->file=create_temp_file(name_buff, cache->dir, cache->prefix,
499.1.4 by Monty Taylor
Fixed two things I missed.
60
				    (O_RDWR | O_TRUNC),
1 by brian
clean slate
61
				    MYF(MY_WME))) >= 0)
62
  {
63
    error=0;
499.1.4 by Monty Taylor
Fixed two things I missed.
64
    my_delete(name_buff,MYF(MY_WME | ME_NOINPUT));
1 by brian
clean slate
65
  }
51.3.22 by Jay Pipes
Final round of removal of DBUG in mysys/, including Makefile
66
  return(error);
1 by brian
clean slate
67
}
68
69
70
void close_cached_file(IO_CACHE *cache)
71
{
72
  if (my_b_inited(cache))
73
  {
74
    File file=cache->file;
75
    cache->file= -1;				/* Don't flush data */
76
    (void) end_io_cache(cache);
77
    if (file >= 0)
78
    {
79
      (void) my_close(file,MYF(0));
80
#ifdef CANT_DELETE_OPEN_FILES
81
      if (cache->file_name)
82
      {
83
	(void) my_delete(cache->file_name,MYF(MY_WME | ME_NOINPUT));
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
84
	free(cache->file_name);
1 by brian
clean slate
85
      }
86
#endif
87
    }
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
88
    free(cache->dir);
89
    free(cache->prefix);
1 by brian
clean slate
90
  }
51.3.22 by Jay Pipes
Final round of removal of DBUG in mysys/, including Makefile
91
  return;
1 by brian
clean slate
92
}