66acfa3886f30cdc03d2e2b6de823cdc323cee14
[util-vserver.git] / lib / getvserverbyctx-compat.hc
1 // $Id: getvserverbyctx-compat.hc,v 1.3 2004/06/27 13:01:28 ensc Exp $    --*- c -*--
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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);
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 {
72   if (revdir==0) revdir = DEFAULT_PKGSTATEREVDIR;
73
74   {
75   vcCfgStyle    cur_style = vcCFG_NONE;
76   size_t        l = strlen(revdir);
77   size_t        l1;
78   char          path[l + sizeof(unsigned int)*3 + 3];
79
80   strcpy(path, revdir);
81   path[l]      = '/';
82   l1 = utilvserver_fmt_uint(path+l+1, ctx);
83   path[l+1+l1] = '\0';
84
85   if (style==0 || *style==vcCFG_AUTO) {
86     if (access(path, F_OK)==0) cur_style = vcCFG_RECENT_FULL;
87     else                       cur_style = vcCFG_LEGACY;
88   }
89   else
90     cur_style = *style;
91
92   switch (cur_style) {
93     case vcCFG_RECENT_SHORT     :
94     case vcCFG_RECENT_FULL      :
95         // check if expected ctx == actual ctx
96       if (vc_getVserverCtx(path, vcCFG_RECENT_FULL, false, 0)!=ctx) return 0;
97
98       if (style) *style = vcCFG_RECENT_FULL;
99       return strdup(path);
100         // TODO: handle legacy
101     case vcCFG_LEGACY           :
102     {
103       char *    tmp = handleLegacy(ctx);
104       if (tmp && style)
105         *style = vcCFG_LEGACY;
106
107       return tmp;
108     }
109       
110     default             :
111       return 0;
112   }
113   }
114 }