~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>
612.2.6 by Monty Taylor
OpenSolaris builds.
18
#include <stdio.h>
1 by brian
clean slate
19
20
#define DELIM ':'
21
146 by Brian Aker
my_bool cleanup.
22
bool init_tmpdir(MY_TMPDIR *tmpdir, const char *pathlist)
1 by brian
clean slate
23
{
266.1.18 by Monty Taylor
Merged. Plus a few fixes.
24
  const char *end;
25
  char *copy;
1 by brian
clean slate
26
  char buff[FN_REFLEN];
27
28
  pthread_mutex_init(&tmpdir->mutex, MY_MUTEX_INIT_FAST);
29
  if (my_init_dynamic_array(&tmpdir->full_list, sizeof(char*), 1, 5))
30
    goto err;
31
  if (!pathlist || !pathlist[0])
32
  {
33
    /* Get default temporary directory */
34
    pathlist=getenv("TMPDIR");	/* Use this if possible */
35
    if (!pathlist || !pathlist[0])
36
      pathlist=(char*) P_tmpdir;
37
  }
266.1.18 by Monty Taylor
Merged. Plus a few fixes.
38
  do {
482 by Brian Aker
Remove uint.
39
    uint32_t length;
266.1.18 by Monty Taylor
Merged. Plus a few fixes.
40
    end= strrchr(pathlist, DELIM);
41
    if (end == NULL)
42
      end= pathlist+strlen(pathlist);
629.5.1 by Toru Maesaka
First pass of replacing MySQL's strmake() with libc calls
43
    strncpy(buff, pathlist, FN_REFLEN-1);
1 by brian
clean slate
44
    length= cleanup_dirname(buff, buff);
656.1.26 by Monty Taylor
Finally removed all of the my_malloc stuff.
45
    if (!(copy= strndup(buff, length)) ||
481 by Brian Aker
Remove all of uchar.
46
        insert_dynamic(&tmpdir->full_list, (unsigned char*) &copy))
51.3.13 by Jay Pipes
Phase 1 removal of DBUG in mysys
47
      return(true);
1 by brian
clean slate
48
    pathlist=end+1;
266.1.18 by Monty Taylor
Merged. Plus a few fixes.
49
  } while (*end != '\0');
1 by brian
clean slate
50
  freeze_size(&tmpdir->full_list);
51
  tmpdir->list=(char **)tmpdir->full_list.buffer;
52
  tmpdir->max=tmpdir->full_list.elements-1;
53
  tmpdir->cur=0;
51.3.13 by Jay Pipes
Phase 1 removal of DBUG in mysys
54
  return(false);
1 by brian
clean slate
55
56
err:
57
  delete_dynamic(&tmpdir->full_list);           /* Safe to free */
58
  pthread_mutex_destroy(&tmpdir->mutex);
51.3.13 by Jay Pipes
Phase 1 removal of DBUG in mysys
59
  return(true);
1 by brian
clean slate
60
}
61
62
63
char *my_tmpdir(MY_TMPDIR *tmpdir)
64
{
65
  char *dir;
66
  if (!tmpdir->max)
67
    return tmpdir->list[0];
68
  pthread_mutex_lock(&tmpdir->mutex);
69
  dir=tmpdir->list[tmpdir->cur];
70
  tmpdir->cur= (tmpdir->cur == tmpdir->max) ? 0 : tmpdir->cur+1;
71
  pthread_mutex_unlock(&tmpdir->mutex);
72
  return dir;
73
}
74
75
void free_tmpdir(MY_TMPDIR *tmpdir)
76
{
482 by Brian Aker
Remove uint.
77
  uint32_t i;
1 by brian
clean slate
78
  for (i=0; i<=tmpdir->max; i++)
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.
79
    free(tmpdir->list[i]);
1 by brian
clean slate
80
  delete_dynamic(&tmpdir->full_list);
81
  pthread_mutex_destroy(&tmpdir->mutex);
82
}
83