|
Revision 1543, 2.4 kB
(checked in by ensc, 5 years ago)
|
getRecentName(): use realpath(3) instead of errorprone 'chdir()' calls
|
- Property svn:eol-style set to
native
- Property svn:keywords set to
Author Date Id Revision
|
| Line | |
|---|
| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
#ifdef HAVE_CONFIG_H |
|---|
| 20 |
# include <config.h> |
|---|
| 21 |
#endif |
|---|
| 22 |
|
|---|
| 23 |
#include "vserver.h" |
|---|
| 24 |
#include "pathconfig.h" |
|---|
| 25 |
|
|---|
| 26 |
#include <string.h> |
|---|
| 27 |
#include <fcntl.h> |
|---|
| 28 |
#include <errno.h> |
|---|
| 29 |
#include <libgen.h> |
|---|
| 30 |
#include <unistd.h> |
|---|
| 31 |
#include <limits.h> |
|---|
| 32 |
|
|---|
| 33 |
static char * |
|---|
| 34 |
getRecentName(char *start, char *end) |
|---|
| 35 |
{ |
|---|
| 36 |
char *res = 0; |
|---|
| 37 |
int fd; |
|---|
| 38 |
char buf[PATH_MAX]; |
|---|
| 39 |
|
|---|
| 40 |
strcpy(end, "/name"); |
|---|
| 41 |
fd = open(start, O_RDONLY); |
|---|
| 42 |
if (fd!=-1) { |
|---|
| 43 |
off_t len; |
|---|
| 44 |
|
|---|
| 45 |
if ((len=lseek(fd, 0, SEEK_END))!=-1 && |
|---|
| 46 |
(len<VC_LIMIT_VSERVER_NAME_LEN) && |
|---|
| 47 |
(lseek(fd, 0, SEEK_SET)!=-1)) { |
|---|
| 48 |
char buf[len+1]; |
|---|
| 49 |
|
|---|
| 50 |
if (TEMP_FAILURE_RETRY(read(fd, buf, len+1))==len) { |
|---|
| 51 |
while (len>0 && buf[len-1]=='\n') --len; |
|---|
| 52 |
buf[len] = '\0'; |
|---|
| 53 |
if (len>0) res = buf; |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
close(fd); |
|---|
| 57 |
return strdup(res); |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
close(fd); |
|---|
| 61 |
} |
|---|
| 62 |
|
|---|
| 63 |
if (res==0) { |
|---|
| 64 |
*end = '\0'; |
|---|
| 65 |
res = realpath(start, buf); |
|---|
| 66 |
|
|---|
| 67 |
if (res==0) res = start; |
|---|
| 68 |
|
|---|
| 69 |
res = basename(res); |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
return strdup(res); |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
char * |
|---|
| 76 |
vc_getVserverName(char const *id, vcCfgStyle style) |
|---|
| 77 |
{ |
|---|
| 78 |
size_t l1 = strlen(id); |
|---|
| 79 |
|
|---|
| 80 |
if (style==vcCFG_NONE || style==vcCFG_AUTO) |
|---|
| 81 |
style = vc_getVserverCfgStyle(id); |
|---|
| 82 |
|
|---|
| 83 |
switch (style) { |
|---|
| 84 |
case vcCFG_NONE : return 0; |
|---|
| 85 |
case vcCFG_LEGACY : return strdup(id); |
|---|
| 86 |
case vcCFG_RECENT_SHORT : |
|---|
| 87 |
{ |
|---|
| 88 |
char buf[sizeof(CONFDIR "/") + l1 + sizeof("/name") - 1]; |
|---|
| 89 |
|
|---|
| 90 |
strcpy(buf, CONFDIR "/"); |
|---|
| 91 |
strcpy(buf+sizeof(CONFDIR "/") - 1, id); |
|---|
| 92 |
|
|---|
| 93 |
return getRecentName(buf, buf+sizeof(CONFDIR "/")+l1 - 1); |
|---|
| 94 |
} |
|---|
| 95 |
case vcCFG_RECENT_FULL : |
|---|
| 96 |
{ |
|---|
| 97 |
char buf[l1 + sizeof("/name")]; |
|---|
| 98 |
strcpy(buf, id); |
|---|
| 99 |
|
|---|
| 100 |
return getRecentName(buf, buf+l1); |
|---|
| 101 |
} |
|---|
| 102 |
default : return 0; |
|---|
| 103 |
} |
|---|
| 104 |
} |
|---|