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 |
||
994.2.4
by Monty Taylor
Blast. Fixed some make distcheck issues. |
16 |
#include "mysys/mysys_priv.h" |
17 |
#include "mysys/mysys_err.h" |
|
212.5.18
by Monty Taylor
Moved m_ctype, m_string and my_bitmap. Removed t_ctype. |
18 |
#include <mystrings/m_string.h> |
1
by brian
clean slate |
19 |
#include <errno.h> |
20 |
#ifdef HAVE_REALPATH
|
|
21 |
#include <sys/param.h> |
|
22 |
#include <sys/stat.h> |
|
23 |
#endif
|
|
24 |
||
25 |
/*
|
|
26 |
Reads the content of a symbolic link
|
|
27 |
If the file is not a symbolic link, return the original file name in to.
|
|
28 |
||
29 |
RETURN
|
|
30 |
0 If filename was a symlink, (to will be set to value of symlink)
|
|
31 |
1 If filename was a normal file (to will be set to filename)
|
|
32 |
-1 on error.
|
|
33 |
*/
|
|
34 |
||
35 |
int my_readlink(char *to, const char *filename, myf MyFlags) |
|
36 |
{
|
|
37 |
#ifndef HAVE_READLINK
|
|
641.4.1
by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls |
38 |
strcpy(to,filename); |
1
by brian
clean slate |
39 |
return 1; |
40 |
#else
|
|
41 |
int result=0; |
|
42 |
int length; |
|
43 |
||
44 |
if ((length=readlink(filename, to, FN_REFLEN-1)) < 0) |
|
45 |
{
|
|
46 |
/* Don't give an error if this wasn't a symlink */
|
|
47 |
if ((my_errno=errno) == EINVAL) |
|
48 |
{
|
|
49 |
result= 1; |
|
641.4.1
by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls |
50 |
strcpy(to,filename); |
1
by brian
clean slate |
51 |
}
|
52 |
else
|
|
53 |
{
|
|
54 |
if (MyFlags & MY_WME) |
|
55 |
my_error(EE_CANT_READLINK, MYF(0), filename, errno); |
|
56 |
result= -1; |
|
57 |
}
|
|
58 |
}
|
|
59 |
else
|
|
60 |
to[length]=0; |
|
51.3.15
by Jay Pipes
Phase 3 removal of DBUG in mysys |
61 |
return(result); |
1
by brian
clean slate |
62 |
#endif /* HAVE_READLINK */ |
63 |
}
|
|
64 |
||
65 |
||
66 |
/* Create a symbolic link */
|
|
67 |
||
68 |
int my_symlink(const char *content, const char *linkname, myf MyFlags) |
|
69 |
{
|
|
70 |
#ifndef HAVE_READLINK
|
|
71 |
return 0; |
|
72 |
#else
|
|
73 |
int result; |
|
74 |
||
75 |
result= 0; |
|
76 |
if (symlink(content, linkname)) |
|
77 |
{
|
|
78 |
result= -1; |
|
79 |
my_errno=errno; |
|
80 |
if (MyFlags & MY_WME) |
|
81 |
my_error(EE_CANT_SYMLINK, MYF(0), linkname, content, errno); |
|
82 |
}
|
|
83 |
else if ((MyFlags & MY_SYNC_DIR) && my_sync_dir_by_file(linkname, MyFlags)) |
|
84 |
result= -1; |
|
51.3.15
by Jay Pipes
Phase 3 removal of DBUG in mysys |
85 |
return(result); |
1
by brian
clean slate |
86 |
#endif /* HAVE_READLINK */ |
87 |
}
|
|
88 |
||
89 |
/*
|
|
90 |
Resolve all symbolic links in path
|
|
91 |
'to' may be equal to 'filename'
|
|
92 |
||
93 |
Because purify gives a lot of UMR errors when using realpath(),
|
|
94 |
this code is disabled when using purify.
|
|
95 |
||
96 |
If MY_RESOLVE_LINK is given, only do realpath if the file is a link.
|
|
97 |
*/
|
|
98 |
||
99 |
#if defined(SCO)
|
|
100 |
#define BUFF_LEN 4097
|
|
101 |
#elif defined(MAXPATHLEN)
|
|
102 |
#define BUFF_LEN MAXPATHLEN
|
|
103 |
#else
|
|
104 |
#define BUFF_LEN FN_LEN
|
|
105 |
#endif
|
|
106 |
||
779.1.27
by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files. |
107 |
int my_realpath(char *to, const char *filename, myf MyFlags) |
1
by brian
clean slate |
108 |
{
|
779.1.27
by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files. |
109 |
#if defined(HAVE_REALPATH) && !defined(HAVE_BROKEN_REALPATH)
|
1
by brian
clean slate |
110 |
int result=0; |
111 |
char buff[BUFF_LEN]; |
|
112 |
struct stat stat_buff; |
|
113 |
||
114 |
if (!(MyFlags & MY_RESOLVE_LINK) || |
|
115 |
(!lstat(filename,&stat_buff) && S_ISLNK(stat_buff.st_mode))) |
|
116 |
{
|
|
117 |
char *ptr; |
|
118 |
if ((ptr=realpath(filename,buff))) |
|
119 |
{
|
|
629.5.1
by Toru Maesaka
First pass of replacing MySQL's strmake() with libc calls |
120 |
strncpy(to,ptr,FN_REFLEN-1); |
1
by brian
clean slate |
121 |
}
|
122 |
else
|
|
123 |
{
|
|
124 |
/*
|
|
125 |
Realpath didn't work; Use my_load_path() which is a poor substitute
|
|
126 |
original name but will at least be able to resolve paths that starts
|
|
127 |
with '.'.
|
|
128 |
*/
|
|
129 |
my_errno=errno; |
|
130 |
if (MyFlags & MY_WME) |
|
131 |
my_error(EE_REALPATH, MYF(0), filename, my_errno); |
|
461
by Monty Taylor
Removed NullS. bu-bye. |
132 |
my_load_path(to, filename, NULL); |
1
by brian
clean slate |
133 |
result= -1; |
134 |
}
|
|
135 |
}
|
|
51.3.15
by Jay Pipes
Phase 3 removal of DBUG in mysys |
136 |
return(result); |
1
by brian
clean slate |
137 |
#else
|
461
by Monty Taylor
Removed NullS. bu-bye. |
138 |
my_load_path(to, filename, NULL); |
1
by brian
clean slate |
139 |
return 0; |
140 |
#endif
|
|
141 |
}
|
|
1022.2.29
by Monty Taylor
Fixed some no-inline warnings. |
142 |
|
143 |
bool test_if_hard_path(const char *dir_name) |
|
144 |
{
|
|
145 |
if (dir_name[0] == FN_HOMELIB && dir_name[1] == FN_LIBCHAR) |
|
146 |
return (home_dir != NULL && test_if_hard_path(home_dir)); |
|
147 |
if (dir_name[0] == FN_LIBCHAR) |
|
148 |
return (true); |
|
149 |
return false; |
|
150 |
} /* test_if_hard_path */ |