Debian Codegraph @

mysys/my_symlink.c

raw symbols SCIP
1 /*
2 Copyright (c) 2001, 2011, Oracle and/or its affiliates
3 Copyright (c) 2010, 2022, MariaDB
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; version 2 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
17
18 #include "mysys_priv.h"cxx . . $ `<file>/mysys/mysys_priv.h`/
19 #include "mysys_err.h"cxx . . $ `<file>/include/mysys_err.h`/
20 #include <m_string.h>cxx . . $ `<file>/include/m_string.h`/
21 #include <errno.h>
22 #ifdef HAVE_REALPATHcxx . . $ `/build/tmp.NxIY9EeyJN/package/build/include/my_config.h:211:9`!
23 #include <sys/param.h>
24 #include <sys/stat.h>
25 #endif
26
27 static int always_validcxx . . $ always_valid(5c4306d8553b3696).(const char *filename __attribute__((unused)))
28 {
29 return 0;
30 }
31
32 int (*mysys_test_invalid_symlinkcxx . . $ mysys_test_invalid_symlink.)(const char *filename)= always_validcxx . . $ always_valid(5c4306d8553b3696).;
33
34
35 /*
36 Reads the content of a symbolic link
37 If the file is not a symbolic link, return the original file name in to.
38
39 RETURN
40 0 If filename was a symlink, (to will be set to value of symlink)
41 1 If filename was a normal file (to will be set to filename)
42 -1 on error.
43 */
44
45 int my_readlinkcxx . . $ my_readlink(e8695995859f9586).(char *to, const char *filename, myfcxx . . $ myf# MyFlags)
46 {
47 #ifndef HAVE_READLINKcxx . . $ `/build/tmp.NxIY9EeyJN/package/build/include/my_config.h:210:9`!
48 strnmov(to, filename, FN_REFLEN);
49 return 1;
50 #else
51 int result=0;
52 int length;
53 DBUG_ENTERcxx . . $ `include/my_dbug.h:165:9`!("my_readlink");
54
55 if ((length=readlinkcxx . . $ readlink(373c9bd46766ee5).(filename, to, FN_REFLENcxx . . $ `include/my_global.h:603:9`!-1)) < 0)
56 {
57 /* Don't give an error if this wasn't a symlink */
58 if ((my_errnocxx . . $ `include/my_pthread.h:720:9`!=errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!) == EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!)
59 {
60 result= 1;
61 strnmovcxx . . $ strnmov(255158a415b673b0).(to, filename, FN_REFLENcxx . . $ `include/my_global.h:603:9`!);
62 }
63 else
64 {
65 if (MyFlags & MY_WMEcxx . . $ `include/my_sys.h:57:9`!)
66 my_errorcxx . . $ my_error(21e90ace5e448e7a).(EE_CANT_READLINKcxx . . $ `include/mysys_err.h:60:9`!, MYFcxx . . $ `include/my_global.h:986:9`!(0), filename, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!);
67 result= -1;
68 }
69 }
70 else
71 to[length]=0;
72 DBUG_PRINTcxx . . $ `include/my_dbug.h:174:9`!("exit" ,("result: %d", result));
73 DBUG_RETURNcxx . . $ `include/my_dbug.h:168:9`!(result);
74 #endif /* HAVE_READLINK */
75 }
76
77
78 /* Create a symbolic link */
79
80 int my_symlinkcxx . . $ my_symlink(fd278af2f295fa2e).(const char *content, const char *linkname, myfcxx . . $ myf# MyFlags)
81 {
82 #ifndef HAVE_READLINKcxx . . $ `/build/tmp.NxIY9EeyJN/package/build/include/my_config.h:210:9`!
83 return 0;
84 #else
85 int result;
86 DBUG_ENTERcxx . . $ `include/my_dbug.h:165:9`!("my_symlink");
87 DBUG_PRINTcxx . . $ `include/my_dbug.h:174:9`!("enter",("content: %s linkname: %s", content, linkname));
88
89 result= 0;
90 if (symlinkcxx . . $ symlink(5ae29d726ae57f88).(content, linkname))
91 {
92 result= -1;
93 my_errnocxx . . $ `include/my_pthread.h:720:9`!=errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
94 if (MyFlags & MY_WMEcxx . . $ `include/my_sys.h:57:9`!)
95 my_errorcxx . . $ my_error(21e90ace5e448e7a).(EE_CANT_SYMLINKcxx . . $ `include/mysys_err.h:61:9`!, MYFcxx . . $ `include/my_global.h:986:9`!(0), linkname, content, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!);
96 }
97 else if ((MyFlags & MY_SYNC_DIRcxx . . $ `include/my_sys.h:93:9`!) && my_sync_dir_by_filecxx . . $ my_sync_dir_by_file(54dd70b47e85a2fc).(linkname, MyFlags))
98 result= -1;
99 DBUG_RETURNcxx . . $ `include/my_dbug.h:168:9`!(result);
100 #endif /* HAVE_READLINK */
101 }
102
103 #if defined(SCO)
104 #define BUFF_LEN 4097
105 #elif defined(MAXPATHLENcxx . . $ `<external>/1231830743887005719/param.h:48:10`!)
106 #define BUFF_LENcxx . . $ `mysys/my_symlink.c:106:9`! MAXPATHLENcxx . . $ `<external>/1231830743887005719/param.h:48:10`!
107 #else
108 #define BUFF_LEN FN_LEN
109 #endif
110
111
112 int my_is_symlinkcxx . . $ my_is_symlink(5c4306d8553b3696).(const char *filename __attribute__((unused)))
113 {
114 #if defined (HAVE_LSTATcxx . . $ `/build/tmp.NxIY9EeyJN/package/build/include/my_config.h:170:9`!) && defined (S_ISLNKcxx . . $ `<external>/531885356569766532/stat.h:133:10`!)
115 struct statcxx . . $ stat# stat_buff;
116 if (lstatcxx . . $ lstat(a9cd1e23e96f423c).(filename, &stat_buff))
117 return 0;
118 return !!S_ISLNKcxx . . $ `<external>/531885356569766532/stat.h:133:10`!(stat_buff.st_mode);
119 #elif defined (_WIN32)
120 DWORD dwAttr = GetFileAttributes(filename);
121 return (dwAttr != INVALID_FILE_ATTRIBUTES) &&
122 (dwAttr & FILE_ATTRIBUTE_REPARSE_POINT);
123 #else /* No symlinks */
124 return 0;
125 #endif
126 }
127
128 /*
129 Resolve all symbolic links in path.
130
131 If path is relative, assume current directory and replace with absolute.
132 If filename is empty string, resolve path to current directory.
133
134 Resolved path is placed in the 'to' buffer, and is guaranteed to never
135 be longer than FN_REFLEN (including the end \0); 'to' may be equal to
136 'filename'.
137
138 On error returns -1, unless error is file not found, in which case it
139 is 1; 'to' will then be populated with a fallback value as specified by
140 my_load_path() with null own_path_prefix; symlinks will not be
141 resolved.
142
143 Sets my_errno to specific error number.
144 */
145
146 int my_realpathcxx . . $ my_realpath(e8695995859f9586).(char *to, const char *filename, myfcxx . . $ myf# MyFlags)
147 {
148 #if defined(HAVE_REALPATHcxx . . $ `/build/tmp.NxIY9EeyJN/package/build/include/my_config.h:211:9`!)
149 int result=0;
150 char buff[BUFF_LENcxx . . $ `mysys/my_symlink.c:106:9`!];
151 char *ptr;
152 static const char cur_dir[] = {FN_CURLIBcxx . . $ `include/my_global.h:606:9`!, '\0'};
153 DBUG_ENTERcxx . . $ `include/my_dbug.h:165:9`!("my_realpath");
154
155 DBUG_PRINTcxx . . $ `include/my_dbug.h:174:9`!("info",("executing realpath"));
156 /* realpath won't accept empty string - use "." instead */
157 if ((ptr=realpathcxx . . $ realpath(ed8193f262e4eda).(filename[0] ? filename : cur_dir, buff)))
158 strmakecxx . . $ strmake(255158a415b673b0).(to, ptr, FN_REFLENcxx . . $ `include/my_global.h:603:9`!-1);
159 else
160 {
161 /*
162 Realpath didn't work; Use my_load_path() which is a poor substitute
163 original name but will at least be able to resolve paths that starts
164 with '.'.
165 */
166 if (MyFlags)
167 DBUG_PRINTcxx . . $ `include/my_dbug.h:174:9`!("error",("realpath failed with errno: %d", errno));
168 my_errnocxx . . $ `include/my_pthread.h:720:9`!=errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
169 if (MyFlags & MY_WMEcxx . . $ `include/my_sys.h:57:9`!)
170 my_errorcxx . . $ my_error(21e90ace5e448e7a).(EE_REALPATHcxx . . $ `include/mysys_err.h:62:9`!, MYFcxx . . $ `include/my_global.h:986:9`!(0), filename, my_errnocxx . . $ `include/my_pthread.h:720:9`!);
171 my_load_pathcxx . . $ my_load_path(34de2e5c4f0b635).(to, filename, NullScxx . . $ `include/my_global.h:844:9`!);
172 if (my_errnocxx . . $ `include/my_pthread.h:720:9`! == ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!)
173 result= 1;
174 else
175 result= -1;
176 }
177 DBUG_RETURNcxx . . $ `include/my_dbug.h:168:9`!(result);
178 #elif defined(_WIN32)
179 int ret= GetFullPathName(filename,FN_REFLEN, to, NULL);
180 if (ret == 0 || ret > FN_REFLEN)
181 {
182 my_errno= (ret > FN_REFLEN) ? ENAMETOOLONG : GetLastError();
183 if (MyFlags & MY_WME)
184 my_error(EE_REALPATH, MYF(0), filename, my_errno);
185 /*
186 GetFullPathName didn't work : use my_load_path() which is a poor
187 substitute original name but will at least be able to resolve
188 paths that starts with '.'.
189 */
190 my_load_path(to, filename, NullS);
191 return -1;
192 }
193 #else
194 my_load_path(to, filename, NullS);
195 #endif
196 return 0;
197 }
198
199 #ifdef HAVE_OPEN_PARENT_DIR_NOSYMLINKScxx . . $ `mysys/mysys_priv.h:146:9`!
200 /** opens the parent dir. walks the path, and does not resolve symlinks
201
202 returns the pointer to the file name (basename) within the pathname
203 or NULL in case of an error
204
205 stores the parent dir (dirname) file descriptor in pdfd.
206 It can be -1 even if there was no error!
207
208 This is used for symlinked tables for DATA/INDEX DIRECTORY.
209 The paths there have been realpath()-ed. So, we can assume here that
210
211 * `pathname` is an absolute path
212 * no '.', '..', and '//' in the path
213 * file exists
214 */
215
216 const char *my_open_parent_dir_nosymlinkscxx . . $ my_open_parent_dir_nosymlinks(6a735779416056db).(const char *pathname, int *pdfd)
217 {
218 char buf[FN_REFLENcxx . . $ `include/my_global.h:603:9`! + 1];
219 char *s= buf, *e= buf+1, *end= strnmovcxx . . $ strnmov(255158a415b673b0).(buf, pathname, sizeof(buf));
220 int fd, dfd= -1;
221
222 if (*end)
223 {
224 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!= ENAMETOOLONGcxx . . $ `<external>/17922322188510340118/errno.h:8:9`!;
225 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
226 }
227
228 if (*s != '/') /* not an absolute path */
229 {
230 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!= ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!;
231 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
232 }
233
234 for (;;)
235 {
236 if (*e == '/') /* '//' in the path */
237 {
238 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!= ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!;
239 goto err;
240 }
241 while (*e && *e != '/')
242 e++;
243 *e= 0;
244
245 if (!memcmpcxx . . $ memcmp(36f949851efa3055).(s, ".", 2) || !memcmpcxx . . $ memcmp(36f949851efa3055).(s, "..", 3))
246 {
247 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!= ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!;
248 goto err;
249 }
250
251 if (++e >= end)
252 {
253 *pdfd= dfd;
254 return pathname + (s - buf);
255 }
256
257 fd = openatcxx . . $ openat(50b278ac4ac9ad96).(dfd, s, O_NOFOLLOWcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:141:10`! | O_PATHcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:148:10`! | O_CLOEXECcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:142:10`!);
258 if (fd < 0)
259 goto err;
260
261 if (dfd >= 0)
262 closecxx . . $ close(7864480464b09eea).(dfd);
263
264 dfd= fd;
265 s= e;
266 }
267 err:
268 if (dfd >= 0)
269 closecxx . . $ close(7864480464b09eea).(dfd);
270 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
271 }
272 #endif