~linuxjedi/drizzle/trunk-bug-667053

« back to all changes in this revision

Viewing changes to mysys/mf_tempdir.c

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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"
 
17
#include <m_string.h>
 
18
 
 
19
#define DELIM ':'
 
20
 
 
21
my_bool init_tmpdir(MY_TMPDIR *tmpdir, const char *pathlist)
 
22
{
 
23
  char *end, *copy;
 
24
  char buff[FN_REFLEN];
 
25
  DBUG_ENTER("init_tmpdir");
 
26
  DBUG_PRINT("enter", ("pathlist: %s", pathlist ? pathlist : "NULL"));
 
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
  }
 
38
  do
 
39
  {
 
40
    uint length;
 
41
    end=strcend(pathlist, DELIM);
 
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))
 
46
      DBUG_RETURN(TRUE);
 
47
    pathlist=end+1;
 
48
  }
 
49
  while (*end);
 
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;
 
54
  DBUG_RETURN(FALSE);
 
55
 
 
56
err:
 
57
  delete_dynamic(&tmpdir->full_list);           /* Safe to free */
 
58
  pthread_mutex_destroy(&tmpdir->mutex);
 
59
  DBUG_RETURN(TRUE);
 
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
{
 
77
  uint i;
 
78
  for (i=0; i<=tmpdir->max; i++)
 
79
    my_free(tmpdir->list[i], MYF(0));
 
80
  delete_dynamic(&tmpdir->full_list);
 
81
  pthread_mutex_destroy(&tmpdir->mutex);
 
82
}
 
83