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" |
|
17 |
#include "mysys_err.h" |
|
18 |
#include <m_string.h> |
|
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
|
|
38 |
strmov(to,filename); |
|
39 |
return 1; |
|
40 |
#else
|
|
41 |
int result=0; |
|
42 |
int length; |
|
43 |
DBUG_ENTER("my_readlink"); |
|
44 |
||
45 |
if ((length=readlink(filename, to, FN_REFLEN-1)) < 0) |
|
46 |
{
|
|
47 |
/* Don't give an error if this wasn't a symlink */
|
|
48 |
if ((my_errno=errno) == EINVAL) |
|
49 |
{
|
|
50 |
result= 1; |
|
51 |
strmov(to,filename); |
|
52 |
}
|
|
53 |
else
|
|
54 |
{
|
|
55 |
if (MyFlags & MY_WME) |
|
56 |
my_error(EE_CANT_READLINK, MYF(0), filename, errno); |
|
57 |
result= -1; |
|
58 |
}
|
|
59 |
}
|
|
60 |
else
|
|
61 |
to[length]=0; |
|
62 |
DBUG_PRINT("exit" ,("result: %d", result)); |
|
63 |
DBUG_RETURN(result); |
|
64 |
#endif /* HAVE_READLINK */ |
|
65 |
}
|
|
66 |
||
67 |
||
68 |
/* Create a symbolic link */
|
|
69 |
||
70 |
int my_symlink(const char *content, const char *linkname, myf MyFlags) |
|
71 |
{
|
|
72 |
#ifndef HAVE_READLINK
|
|
73 |
return 0; |
|
74 |
#else
|
|
75 |
int result; |
|
76 |
DBUG_ENTER("my_symlink"); |
|
77 |
DBUG_PRINT("enter",("content: %s linkname: %s", content, linkname)); |
|
78 |
||
79 |
result= 0; |
|
80 |
if (symlink(content, linkname)) |
|
81 |
{
|
|
82 |
result= -1; |
|
83 |
my_errno=errno; |
|
84 |
if (MyFlags & MY_WME) |
|
85 |
my_error(EE_CANT_SYMLINK, MYF(0), linkname, content, errno); |
|
86 |
}
|
|
87 |
else if ((MyFlags & MY_SYNC_DIR) && my_sync_dir_by_file(linkname, MyFlags)) |
|
88 |
result= -1; |
|
89 |
DBUG_RETURN(result); |
|
90 |
#endif /* HAVE_READLINK */ |
|
91 |
}
|
|
92 |
||
93 |
/*
|
|
94 |
Resolve all symbolic links in path
|
|
95 |
'to' may be equal to 'filename'
|
|
96 |
||
97 |
Because purify gives a lot of UMR errors when using realpath(),
|
|
98 |
this code is disabled when using purify.
|
|
99 |
||
100 |
If MY_RESOLVE_LINK is given, only do realpath if the file is a link.
|
|
101 |
*/
|
|
102 |
||
103 |
#if defined(SCO)
|
|
104 |
#define BUFF_LEN 4097
|
|
105 |
#elif defined(MAXPATHLEN)
|
|
106 |
#define BUFF_LEN MAXPATHLEN
|
|
107 |
#else
|
|
108 |
#define BUFF_LEN FN_LEN
|
|
109 |
#endif
|
|
110 |
||
111 |
int my_realpath(char *to, const char *filename, |
|
112 |
myf MyFlags __attribute__((unused))) |
|
113 |
{
|
|
114 |
#if defined(HAVE_REALPATH) && !defined(HAVE_purify) && !defined(HAVE_BROKEN_REALPATH)
|
|
115 |
int result=0; |
|
116 |
char buff[BUFF_LEN]; |
|
117 |
struct stat stat_buff; |
|
118 |
DBUG_ENTER("my_realpath"); |
|
119 |
||
120 |
if (!(MyFlags & MY_RESOLVE_LINK) || |
|
121 |
(!lstat(filename,&stat_buff) && S_ISLNK(stat_buff.st_mode))) |
|
122 |
{
|
|
123 |
char *ptr; |
|
124 |
DBUG_PRINT("info",("executing realpath")); |
|
125 |
if ((ptr=realpath(filename,buff))) |
|
126 |
{
|
|
127 |
strmake(to,ptr,FN_REFLEN-1); |
|
128 |
}
|
|
129 |
else
|
|
130 |
{
|
|
131 |
/*
|
|
132 |
Realpath didn't work; Use my_load_path() which is a poor substitute
|
|
133 |
original name but will at least be able to resolve paths that starts
|
|
134 |
with '.'.
|
|
135 |
*/
|
|
136 |
DBUG_PRINT("error",("realpath failed with errno: %d", errno)); |
|
137 |
my_errno=errno; |
|
138 |
if (MyFlags & MY_WME) |
|
139 |
my_error(EE_REALPATH, MYF(0), filename, my_errno); |
|
140 |
my_load_path(to, filename, NullS); |
|
141 |
result= -1; |
|
142 |
}
|
|
143 |
}
|
|
144 |
DBUG_RETURN(result); |
|
145 |
#else
|
|
146 |
my_load_path(to, filename, NullS); |
|
147 |
return 0; |
|
148 |
#endif
|
|
149 |
}
|