1/* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15
16#include "mysys_priv.h"
17#include <m_string.h>
18
19#if defined(__WIN__)
20#define DELIM ';'
21#else
22#define DELIM ':'
23#endif
24
25my_bool init_tmpdir(MY_TMPDIR *tmpdir, const char *pathlist)
26{
27 char *end, *copy;
28 char buff[FN_REFLEN];
29 DBUG_ENTER("init_tmpdir");
30 DBUG_PRINT("enter", ("pathlist: %s", pathlist ? pathlist : "NULL"));
31
32 mysql_mutex_init(key_TMPDIR_mutex, &tmpdir->mutex, MY_MUTEX_INIT_FAST);
33 if (my_init_dynamic_array(&tmpdir->full_list, sizeof(char*), 1, 5, MYF(0)))
34 goto err;
35 if (!pathlist || !pathlist[0])
36 {
37 /* Get default temporary directory */
38 pathlist=getenv("TMPDIR"); /* Use this if possible */
39#if defined(__WIN__)
40 if (!pathlist)
41 pathlist=getenv("TEMP");
42 if (!pathlist)
43 pathlist=getenv("TMP");
44#endif
45 if (!pathlist || !pathlist[0])
46 pathlist= DEFAULT_TMPDIR;
47 }
48 do
49 {
50 size_t length;
51 end=strcend(pathlist, DELIM);
52 strmake(buff, pathlist, (uint) (end-pathlist));
53 length= cleanup_dirname(buff, buff);
54 if (!(copy= my_strndup(buff, length, MYF(MY_WME))) ||
55 insert_dynamic(&tmpdir->full_list, (uchar*) &copy))
56 DBUG_RETURN(TRUE);
57 pathlist=end+1;
58 }
59 while (*end);
60 freeze_size(&tmpdir->full_list);
61 tmpdir->list=(char **)tmpdir->full_list.buffer;
62 tmpdir->max=tmpdir->full_list.elements-1;
63 tmpdir->cur=0;
64 DBUG_RETURN(FALSE);
65
66err:
67 delete_dynamic(&tmpdir->full_list); /* Safe to free */
68 mysql_mutex_destroy(&tmpdir->mutex);
69 DBUG_RETURN(TRUE);
70}
71
72
73char *my_tmpdir(MY_TMPDIR *tmpdir)
74{
75 char *dir;
76 if (!tmpdir->max)
77 return tmpdir->list[0];
78 mysql_mutex_lock(&tmpdir->mutex);
79 dir=tmpdir->list[tmpdir->cur];
80 tmpdir->cur= (tmpdir->cur == tmpdir->max) ? 0 : tmpdir->cur+1;
81 mysql_mutex_unlock(&tmpdir->mutex);
82 return dir;
83}
84
85void free_tmpdir(MY_TMPDIR *tmpdir)
86{
87 uint i;
88 if (!tmpdir->full_list.elements)
89 return;
90 for (i=0; i<=tmpdir->max; i++)
91 my_free(tmpdir->list[i]);
92 delete_dynamic(&tmpdir->full_list);
93 mysql_mutex_destroy(&tmpdir->mutex);
94}
95
96