~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
#include "mysys_priv.h"
212.5.18 by Monty Taylor
Moved m_ctype, m_string and my_bitmap. Removed t_ctype.
17
#include <mystrings/m_string.h>
1 by brian
clean slate
18
19
#define DELIM ':'
20
146 by Brian Aker
my_bool cleanup.
21
bool init_tmpdir(MY_TMPDIR *tmpdir, const char *pathlist)
1 by brian
clean slate
22
{
23
  char *end, *copy;
24
  char buff[FN_REFLEN];
25
26
  pthread_mutex_init(&tmpdir->mutex, MY_MUTEX_INIT_FAST);
27
  if (my_init_dynamic_array(&tmpdir->full_list, sizeof(char*), 1, 5))
28
    goto err;
29
  if (!pathlist || !pathlist[0])
30
  {
31
    /* Get default temporary directory */
32
    pathlist=getenv("TMPDIR");	/* Use this if possible */
33
    if (!pathlist || !pathlist[0])
34
      pathlist=(char*) P_tmpdir;
35
  }
36
  do
37
  {
38
    uint length;
39
    end=strcend(pathlist, DELIM);
40
    strmake(buff, pathlist, (uint) (end-pathlist));
41
    length= cleanup_dirname(buff, buff);
42
    if (!(copy= my_strndup(buff, length, MYF(MY_WME))) ||
43
        insert_dynamic(&tmpdir->full_list, (uchar*) &copy))
51.3.13 by Jay Pipes
Phase 1 removal of DBUG in mysys
44
      return(true);
1 by brian
clean slate
45
    pathlist=end+1;
46
  }
47
  while (*end);
48
  freeze_size(&tmpdir->full_list);
49
  tmpdir->list=(char **)tmpdir->full_list.buffer;
50
  tmpdir->max=tmpdir->full_list.elements-1;
51
  tmpdir->cur=0;
51.3.13 by Jay Pipes
Phase 1 removal of DBUG in mysys
52
  return(false);
1 by brian
clean slate
53
54
err:
55
  delete_dynamic(&tmpdir->full_list);           /* Safe to free */
56
  pthread_mutex_destroy(&tmpdir->mutex);
51.3.13 by Jay Pipes
Phase 1 removal of DBUG in mysys
57
  return(true);
1 by brian
clean slate
58
}
59
60
61
char *my_tmpdir(MY_TMPDIR *tmpdir)
62
{
63
  char *dir;
64
  if (!tmpdir->max)
65
    return tmpdir->list[0];
66
  pthread_mutex_lock(&tmpdir->mutex);
67
  dir=tmpdir->list[tmpdir->cur];
68
  tmpdir->cur= (tmpdir->cur == tmpdir->max) ? 0 : tmpdir->cur+1;
69
  pthread_mutex_unlock(&tmpdir->mutex);
70
  return dir;
71
}
72
73
void free_tmpdir(MY_TMPDIR *tmpdir)
74
{
75
  uint i;
76
  for (i=0; i<=tmpdir->max; i++)
77
    my_free(tmpdir->list[i], MYF(0));
78
  delete_dynamic(&tmpdir->full_list);
79
  pthread_mutex_destroy(&tmpdir->mutex);
80
}
81