1
/* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
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.
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.
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 Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
#include "mysys_priv.h"
23
static char * expand_tilde(char **path);
25
/* Pack a dirname ; Changes HOME to ~/ and current dev to ./ */
26
/* from is a dirname (from dirname() ?) ending with FN_LIBCHAR */
27
/* to may be == from */
29
void pack_dirname(char * to, const char *from)
32
size_t d_length,length,UNINIT_VAR(buff_length);
35
DBUG_ENTER("pack_dirname");
37
(void) intern_filename(to,from); /* Change to intern name */
40
if ((start=strrchr(to,FN_DEVCHAR)) != 0) /* Skip device part */
46
if (!(cwd_err= my_getwd(buff,FN_REFLEN,MYF(0))))
48
buff_length= strlen(buff);
49
d_length= (size_t) (start-to);
51
(buff_length == d_length && !memcmp(buff,start,d_length))) &&
52
*start != FN_LIBCHAR && *start)
53
{ /* Put current dir before */
54
bchange((uchar*) to, d_length, (uchar*) buff, buff_length, strlen(to)+1);
58
if ((d_length= cleanup_dirname(to,to)) != 0)
63
length= strlen(home_dir);
64
if (home_dir[length-1] == FN_LIBCHAR)
65
length--; /* Don't test last '/' */
67
if (length > 1 && length < d_length)
68
{ /* test if /xx/yy -> ~/yy */
69
if (memcmp(to,home_dir,length) == 0 && to[length] == FN_LIBCHAR)
71
to[0]=FN_HOMELIB; /* Filename begins with ~ */
72
(void) strmov_overlapp(to+1,to+length);
76
{ /* Test if cwd is ~/... */
77
if (length > 1 && length < buff_length)
79
if (memcmp(buff,home_dir,length) == 0 && buff[length] == FN_LIBCHAR)
82
(void) strmov_overlapp(buff+1,buff+length);
85
if (is_prefix(to,buff))
89
(void) strmov_overlapp(to,to+length); /* Remove everything before */
92
to[0]= FN_CURLIB; /* Put ./ instead of cwd */
99
DBUG_PRINT("exit",("to: '%s'",to));
105
remove unwanted chars from dirname
110
from Dirname to fix. May be same as to
113
"/../" removes prev dir
114
"/~/" removes all before ~
115
//" is same as "/", except on Win32 at start of a file
117
Unpacks home_dir if "~/.." used
118
Unpacks current dir if if "./.." used
124
size_t cleanup_dirname(register char *to, const char *from)
128
reg3 char * from_ptr;
130
char parent[5], /* for "FN_PARENTDIR" */
131
buff[FN_REFLEN+1],*end_parentdir;
132
#ifdef BACKSLASH_MBTAIL
133
CHARSET_INFO *fs= fs_character_set();
135
DBUG_ENTER("cleanup_dirname");
136
DBUG_PRINT("enter",("from: '%s'",from));
139
from_ptr=(char *) from;
141
if ((pos=strrchr(from_ptr,FN_DEVCHAR)) != 0)
142
{ /* Skip device part */
143
length=(size_t) (pos-from_ptr)+1;
144
start=strnmov(buff,from_ptr,length); from_ptr+=length;
148
parent[0]=FN_LIBCHAR;
149
length=(size_t) (strmov(parent+1,FN_PARENTDIR)-parent);
150
for (pos=start ; (*pos= *from_ptr++) != 0 ; pos++)
152
#ifdef BACKSLASH_MBTAIL
154
if (use_mb(fs) && (l= my_ismbchar(fs, from_ptr - 1, from_ptr + 2)))
156
for (l-- ; l ; *++pos= *from_ptr++, l--);
157
start= pos + 1; /* Don't look inside multi-byte char */
163
if (*pos == FN_LIBCHAR)
165
if ((size_t) (pos-start) > length && memcmp(pos-length,parent,length) == 0)
166
{ /* If .../../; skip prev */
171
if (*pos == FN_HOMELIB && (pos == start || pos[-1] == FN_LIBCHAR))
175
pos+=length+1; /* Don't unpack ~/.. */
178
pos=strmov(buff,home_dir)-1; /* Unpacks ~/.. */
179
if (*pos == FN_LIBCHAR)
180
pos--; /* home ended with '/' */
182
if (*pos == FN_CURLIB && (pos == start || pos[-1] == FN_LIBCHAR))
184
if (my_getwd(curr_dir,FN_REFLEN,MYF(0)))
186
pos+=length+1; /* Don't unpack ./.. */
189
pos=strmov(buff,curr_dir)-1; /* Unpacks ./.. */
190
if (*pos == FN_LIBCHAR)
191
pos--; /* home ended with '/' */
194
while (pos >= start && *pos != FN_LIBCHAR) /* remove prev dir */
196
if (pos[1] == FN_HOMELIB ||
197
(pos >= start && memcmp(pos, parent, length) == 0))
198
{ /* Don't remove ~user/ */
199
pos=strmov(end_parentdir+1,parent);
205
else if ((size_t) (pos-start) == length-1 &&
206
!memcmp(start,parent+1,length-1))
207
start=pos; /* Starts with "../" */
208
else if (pos-start > 0 && pos[-1] == FN_LIBCHAR)
210
#ifdef FN_NETWORK_DRIVES
213
pos--; /* Remove dupplicate '/' */
215
else if (pos-start > 1 && pos[-1] == FN_CURLIB && pos[-2] == FN_LIBCHAR)
216
pos-=2; /* Skip /./ */
217
else if (pos > buff+1 && pos[-1] == FN_HOMELIB && pos[-2] == FN_LIBCHAR)
218
{ /* Found ..../~/ */
221
start=buff; pos=buff+1;
225
(void) strmov(to,buff);
226
DBUG_PRINT("exit",("to: '%s'",to));
227
DBUG_RETURN((size_t) (pos-buff));
228
} /* cleanup_dirname */
232
On system where you don't have symbolic links, the following
233
code will allow you to create a file:
234
directory-name.sym that should contain the real path
235
to the directory. This will be used if the directory name
240
my_bool my_use_symdir=0; /* Set this if you want to use symdirs */
243
void symdirget(char *dir, my_bool *is_symdir)
245
char buff[FN_REFLEN+1];
246
char *pos=strend(dir);
247
if (dir[0] && pos[-1] != FN_DEVCHAR && my_access(dir, F_OK))
251
char temp= *(--pos); /* May be "/" or "\" */
253
file= my_open(dir, O_RDONLY, MYF(0));
254
*pos++=temp; *pos=0; /* Restore old filename */
257
if ((length= my_read(file, buff, sizeof(buff) - 1, MYF(0))) > 0)
259
for (pos= buff + length ;
260
pos > buff && (iscntrl(pos[-1]) || isspace(pos[-1])) ;
263
/* Ensure that the symlink ends with the directory symbol */
264
if (pos == buff || pos[-1] != FN_LIBCHAR)
267
strmake(dir,buff, (size_t) (pos-buff));
271
my_close(file, MYF(0));
275
#endif /* USE_SYMDIR */
279
Convert a directory name to a format which can be compared as strings
281
@param to result buffer, FN_REFLEN chars in length; may be == from
282
@param from 'packed' directory name, in whatever format
283
@returns size of the normalized name
286
- Ensures that last char is FN_LIBCHAR, unless it is FN_DEVCHAR
287
- Uses cleanup_dirname
289
It does *not* expand ~/ (although, see cleanup_dirname). Nor does it do
290
any case folding. All case-insensitive normalization should be done by
294
size_t normalize_dirname(char *to, const char *from)
297
char buff[FN_REFLEN];
298
DBUG_ENTER("normalize_dirname");
301
Despite the name, this actually converts the name to the system's
302
format (TODO: name this properly).
304
(void) intern_filename(buff, from);
305
length= strlen(buff); /* Fix that '/' is last */
308
buff[length - 1] != FN_DEVCHAR &&
310
buff[length - 1] != FN_LIBCHAR && buff[length - 1] != '/')
312
/* we need reserve 2 bytes for the trailing slash and the zero */
313
if (length >= sizeof (buff) - 1)
314
length= sizeof (buff) - 2;
315
buff[length]= FN_LIBCHAR;
316
buff[length + 1]= '\0';
319
length=cleanup_dirname(to, buff);
326
Fixes a directory name so that can be used by open()
328
@param to Result buffer, FN_REFLEN characters. May be == from
329
@param from 'Packed' directory name (may contain ~)
330
@param[out] is_symdir Indicates that directory in question turned
331
out to be fake .sym symbolic link, which was
332
resolved to real directory it points to.
335
- Uses normalize_dirname()
336
- Expands ~/... to home_dir/...
337
- Resolves MySQL's fake "foo.sym" symbolic directory names (if USE_SYMDIR)
338
- Changes a UNIX filename to system filename (replaces / with \ on windows)
341
Length of new directory name (= length of to)
344
size_t unpack_dirname(char * to, const char *from, my_bool *is_symdir)
346
size_t length, h_length;
347
char buff[FN_REFLEN+1+4],*suffix,*tilde_expansion;
348
DBUG_ENTER("unpack_dirname");
352
length= normalize_dirname(buff, from);
354
if (buff[0] == FN_HOMELIB)
356
suffix=buff+1; tilde_expansion=expand_tilde(&suffix);
359
length-= (size_t) (suffix-buff)-1;
360
if (length+(h_length= strlen(tilde_expansion)) <= FN_REFLEN)
362
if ((h_length > 0) && (tilde_expansion[h_length-1] == FN_LIBCHAR))
364
if (buff+h_length < suffix)
365
bmove(buff+h_length,suffix,length);
367
bmove_upp((uchar*) buff+h_length+length, (uchar*) suffix+length, length);
368
bmove(buff,tilde_expansion,h_length);
374
symdirget(buff, is_symdir);
376
DBUG_RETURN(system_filename(to,buff)); /* Fix for open */
377
} /* unpack_dirname */
380
/* Expand tilde to home or user-directory */
381
/* Path is reset to point at FN_LIBCHAR after ~xxx */
383
static char * expand_tilde(char **path)
385
if (path[0][0] == FN_LIBCHAR)
386
return home_dir; /* ~/ expanded to home */
390
struct passwd *user_entry;
392
if (!(str=strchr(*path,FN_LIBCHAR)))
394
save= *str; *str= '\0';
395
user_entry=getpwnam(*path);
401
return user_entry->pw_dir;
410
Fix filename so it can be used by open, create
414
to Store result here. Must be at least of size FN_REFLEN.
415
from Filename in unix format (with ~)
422
~ will only be expanded if total length < FN_REFLEN
426
size_t unpack_filename(char * to, const char *from)
428
size_t length, n_length, buff_length;
429
char buff[FN_REFLEN];
431
DBUG_ENTER("unpack_filename");
433
length=dirname_part(buff, from, &buff_length);/* copy & convert dirname */
434
n_length=unpack_dirname(buff, buff, ¬_used);
435
if (n_length+strlen(from+length) < FN_REFLEN)
437
(void) strmov(buff+n_length,from+length);
438
length= system_filename(to,buff); /* Fix to usably filename */
441
length= system_filename(to,from); /* Fix to usably filename */
443
} /* unpack_filename */
446
/* Convert filename (unix standard) to system standard */
447
/* Used before system command's like open(), create() .. */
448
/* Returns used length of to; total length should be FN_REFLEN */
450
size_t system_filename(char *to, const char *from)
452
return (size_t) (strmake(to,from,FN_REFLEN-1)-to);
455
/* Fix a filename to intern (UNIX format) */
457
char *intern_filename(char *to, const char *from)
459
size_t length, to_length;
460
char buff[FN_REFLEN];
462
{ /* Dirname may destroy from */
463
(void) strnmov(buff, from, FN_REFLEN);
466
length= dirname_part(to, from, &to_length); /* Copy dirname & fix chars */
467
(void) strnmov(to + to_length, from + length, FN_REFLEN - to_length);
469
} /* intern_filename */