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