~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/mf_cache.cc

  • Committer: Brian Aker
  • Date: 2009-07-11 19:23:04 UTC
  • mfrom: (1089.1.14 merge)
  • Revision ID: brian@gaz-20090711192304-ootijyl5yf9jq9kd
Merge Brian

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
/* Open a temporary file and cache it with io_cache. Delete it on close */
17
17
 
18
 
#include "mysys_priv.h"
 
18
#include "mysys/mysys_priv.h"
19
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
 
}
 
20
#include <mysys/my_static.h>
 
21
#include <mysys/mysys_err.h>
 
22
#include <mysys/iocache.h>
 
23
 
51
24
 
52
25
        /*
53
26
        ** Open tempfile cached by IO_CACHE
54
27
        ** Should be used when no seeks are done (only reinit_io_buff)
55
 
        ** Return 0 if cache is inited ok
 
28
        ** Return false if cache is inited ok
56
29
        ** The actual file is created when the IO_CACHE buffer gets filled
57
30
        ** If dir is not given, use TMPDIR.
58
31
        */
60
33
bool open_cached_file(IO_CACHE *cache, const char* dir, const char *prefix,
61
34
                         size_t cache_size, myf cache_myflags)
62
35
{
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)) :
 
36
  cache->dir=    dir ? strdup(dir) : (char*) 0;
 
37
  cache->prefix= (prefix ? strdup(prefix) :
65
38
                 (char*) 0);
 
39
  if ((cache->dir == NULL) || (cache->prefix == NULL))
 
40
    return true;
66
41
  cache->file_name=0;
67
42
  cache->buffer=0;                              /* Mark that not open */
68
43
  if (!init_io_cache(cache,-1,cache_size,WRITE_CACHE,0L,0,
69
44
                     MYF(cache_myflags | MY_NABP)))
70
45
  {
71
 
    return(0);
 
46
    return false;
72
47
  }
73
 
  my_free(cache->dir,   MYF(MY_ALLOW_ZERO_PTR));
74
 
  my_free(cache->prefix,MYF(MY_ALLOW_ZERO_PTR));
75
 
  return(1);
 
48
  free(cache->dir);
 
49
  free(cache->prefix);
 
50
  return true;
76
51
}
77
52
 
78
53
        /* Create the temporary file */
81
56
{
82
57
  char name_buff[FN_REFLEN];
83
58
  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)
 
59
  if ((cache->file=create_temp_file(name_buff, cache->dir, cache->prefix, MYF(MY_WME))) >= 0)
88
60
  {
89
61
    error=0;
90
 
    cache_remove_open_tmp(cache, name_buff);
 
62
    my_delete(name_buff,MYF(MY_WME | ME_NOINPUT));
91
63
  }
92
64
  return(error);
93
65
}
107
79
      if (cache->file_name)
108
80
      {
109
81
        (void) my_delete(cache->file_name,MYF(MY_WME | ME_NOINPUT));
110
 
        my_free(cache->file_name,MYF(0));
 
82
        free(cache->file_name);
111
83
      }
112
84
#endif
113
85
    }
114
 
    my_free(cache->dir,MYF(MY_ALLOW_ZERO_PTR));
115
 
    my_free(cache->prefix,MYF(MY_ALLOW_ZERO_PTR));
 
86
    free(cache->dir);
 
87
    free(cache->prefix);
116
88
  }
117
89
  return;
118
90
}