| 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 "internal.h" |
|---|
| 25 |
#include "pathconfig.h" |
|---|
| 26 |
#include "compat-c99.h" |
|---|
| 27 |
|
|---|
| 28 |
#include <string.h> |
|---|
| 29 |
#include <unistd.h> |
|---|
| 30 |
|
|---|
| 31 |
#ifdef VC_ENABLE_API_COMPAT |
|---|
| 32 |
#include <dirent.h> |
|---|
| 33 |
#include <sys/types.h> |
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
static char * |
|---|
| 37 |
handleLegacy(xid_t xid) |
|---|
| 38 |
{ |
|---|
| 39 |
DIR *dir = opendir(DEFAULT_PKGSTATEDIR); |
|---|
| 40 |
struct dirent *ep; |
|---|
| 41 |
char * result = 0; |
|---|
| 42 |
|
|---|
| 43 |
if (dir==0) return 0; |
|---|
| 44 |
while ((ep=readdir(dir))!=0) { |
|---|
| 45 |
char * const name = ep->d_name; |
|---|
| 46 |
size_t l = name ? strlen(name) : 0; |
|---|
| 47 |
xid_t cur_xid; |
|---|
| 48 |
|
|---|
| 49 |
if (l<=4 || strcmp(name+l-4, ".ctx")!=0) continue; |
|---|
| 50 |
name[l-4] = '\0'; |
|---|
| 51 |
cur_xid = vc_getVserverCtx(name, vcCFG_LEGACY, false, 0, vcCTX_XID); |
|---|
| 52 |
if (cur_xid!=xid) continue; |
|---|
| 53 |
|
|---|
| 54 |
result = strdup(name); |
|---|
| 55 |
break; |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
closedir(dir); |
|---|
| 59 |
return result; |
|---|
| 60 |
} |
|---|
| 61 |
#else |
|---|
| 62 |
static inline char * |
|---|
| 63 |
handleLegacy(xid_t UNUSED xid) |
|---|
| 64 |
{ |
|---|
| 65 |
return 0; |
|---|
| 66 |
} |
|---|
| 67 |
#endif |
|---|
| 68 |
|
|---|
| 69 |
static char * |
|---|
| 70 |
vc_getVserverByCtx_compat(xid_t ctx, vcCfgStyle *style, char const *revdir, |
|---|
| 71 |
bool validate_result) |
|---|
| 72 |
{ |
|---|
| 73 |
if (revdir==0) revdir = DEFAULT_PKGSTATEREVDIR; |
|---|
| 74 |
|
|---|
| 75 |
{ |
|---|
| 76 |
vcCfgStyle cur_style = vcCFG_NONE; |
|---|
| 77 |
size_t l = strlen(revdir); |
|---|
| 78 |
size_t l1; |
|---|
| 79 |
char path[l + sizeof(unsigned int)*3 + 3]; |
|---|
| 80 |
|
|---|
| 81 |
strcpy(path, revdir); |
|---|
| 82 |
path[l] = '/'; |
|---|
| 83 |
l1 = utilvserver_fmt_uint(path+l+1, ctx); |
|---|
| 84 |
path[l+1+l1] = '\0'; |
|---|
| 85 |
|
|---|
| 86 |
if (style==0 || *style==vcCFG_AUTO) { |
|---|
| 87 |
if (access(path, F_OK)==0) cur_style = vcCFG_RECENT_FULL; |
|---|
| 88 |
else cur_style = vcCFG_LEGACY; |
|---|
| 89 |
} |
|---|
| 90 |
else |
|---|
| 91 |
cur_style = *style; |
|---|
| 92 |
|
|---|
| 93 |
switch (cur_style) { |
|---|
| 94 |
case vcCFG_RECENT_SHORT : |
|---|
| 95 |
case vcCFG_RECENT_FULL : |
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
if (validate_result && |
|---|
| 99 |
vc_getVserverCtx(path, vcCFG_RECENT_FULL, false, 0, vcCTX_XID)!=ctx) return 0; |
|---|
| 100 |
|
|---|
| 101 |
if (style) *style = vcCFG_RECENT_FULL; |
|---|
| 102 |
return strdup(path); |
|---|
| 103 |
|
|---|
| 104 |
case vcCFG_LEGACY : |
|---|
| 105 |
{ |
|---|
| 106 |
char * tmp = handleLegacy(ctx); |
|---|
| 107 |
if (tmp && style) |
|---|
| 108 |
*style = vcCFG_LEGACY; |
|---|
| 109 |
|
|---|
| 110 |
return tmp; |
|---|
| 111 |
} |
|---|
| 112 |
|
|---|
| 113 |
default : |
|---|
| 114 |
return 0; |
|---|
| 115 |
} |
|---|
| 116 |
} |
|---|
| 117 |
} |
|---|