added legacy support
authorEnrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
Sun, 27 Jun 2004 13:02:07 +0000 (13:02 +0000)
committerEnrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
Sun, 27 Jun 2004 13:02:07 +0000 (13:02 +0000)
git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@1592 94cd875c-1c1d-0410-91d2-eb244daf1a30

util-vserver/lib/getvserverbyctx-compat.hc
util-vserver/lib/getvserverctx.c

index c1e7198..9418e2f 100644 (file)
 #include <string.h>
 #include <unistd.h>
 
+#ifdef VC_ENABLE_API_COMPAT
+#include <dirent.h>
+#include <sys/types.h>
+
+
+static char *
+handleLegacy(xid_t xid)
+{
+  DIR                  *dir = opendir(DEFAULT_PKGSTATEDIR);
+  struct dirent                *ep;
+  char *               result = 0;
+  
+  if (dir==0) return 0;
+  while ((ep=readdir(dir))!=0) {
+    char * const               name = ep->d_name;
+    size_t                     l    = name ? strlen(name) : 0;
+    xid_t                      cur_xid;
+
+    if (l<=4 || strcmp(name+l-4, ".ctx")!=0) continue;
+    name[l-4]   = '\0';
+    cur_xid = vc_getVserverCtx(name, vcCFG_LEGACY, false, 0);
+    if (cur_xid!=xid) continue;
+
+    result      = strdup(name);
+    break;
+  }
+
+  closedir(dir);
+  return result;
+}
+#else
+static inline char *
+handleLegacy(xid_t UNUSED xid)
+{
+  return 0;
+}
+#endif
+
 static char *
 vc_getVserverByCtx_compat(xid_t ctx, vcCfgStyle *style, char const *revdir)
 {
@@ -46,7 +84,7 @@ vc_getVserverByCtx_compat(xid_t ctx, vcCfgStyle *style, char const *revdir)
 
   if (style==0 || *style==vcCFG_AUTO) {
     if (access(path, F_OK)==0) cur_style = vcCFG_RECENT_FULL;
-      // TODO: handle legacy
+    else                       cur_style = vcCFG_LEGACY;
   }
   else
     cur_style = *style;
@@ -60,6 +98,15 @@ vc_getVserverByCtx_compat(xid_t ctx, vcCfgStyle *style, char const *revdir)
       if (style) *style = vcCFG_RECENT_FULL;
       return strdup(path);
        // TODO: handle legacy
+    case vcCFG_LEGACY          :
+    {
+      char *   tmp = handleLegacy(ctx);
+      if (tmp && style)
+       *style = vcCFG_LEGACY;
+
+      return tmp;
+    }
+      
     default            :
       return 0;
   }
index 5334a5c..cf7a4f9 100644 (file)
@@ -23,6 +23,7 @@
 #include "vserver.h"
 #include "pathconfig.h"
 #include "compat-c99.h"
+#include "lib_internal/util.h"
 
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <string.h>
 #include <unistd.h>
 
+#ifdef VC_ENABLE_API_COMPAT
+#include <fcntl.h>
+
+static xid_t
+extractLegacyXID(char const *dir, char const *basename)
+{
+  size_t       l1 = strlen(dir);
+  size_t       l2 = strlen(basename);
+  char         path[l1 + l2 + sizeof("/.ctx")];
+  char *       ptr = path;
+  int          fd;
+  ssize_t      len;
+  xid_t                result = VC_NOXID;
+
+  ptr    = Xmemcpy(ptr, dir, l1);
+  *ptr++ = '/';
+  ptr    = Xmemcpy(ptr, basename, l2);
+  ptr    = Xmemcpy(ptr, ".ctx",    5);
+
+  fd = open(path, O_RDONLY);
+  if (fd==-1) return VC_NOXID;
+
+  len = lseek(fd, 0, SEEK_END);
+
+  if (len!=-1 && lseek(fd, 0, SEEK_SET)!=-1) {
+    char       buf[len+2];
+    char const *pos = 0;
+
+    buf[0] = '\n';
+    
+    if (read(fd, buf+1, len+1)==len) {
+      buf[len+1] = '\0';
+      pos        = strstr(buf, "\nS_CONTEXT=");
+    }
+
+    if (pos) pos += 11;
+    if (*pos>='1' && *pos<='9')
+      result = atoi(pos);
+  }
+
+  close(fd);
+  return result;
+}
+#else
+static xid_t
+extractLegacyXID(char const UNUSED *dir, char const UNUSED *basename)
+{
+  return VC_NOXID;
+}
+#endif
+
+
 static xid_t
 getCtxFromFile(char const *pathname)
 {
@@ -73,7 +126,8 @@ vc_getVserverCtx(char const *id, vcCfgStyle style, bool honor_static, bool *is_r
 
   switch (style) {
     case vcCFG_NONE            :  return VC_NOCTX;
-    case vcCFG_LEGACY          :  return VC_NOCTX;     // todo
+    case vcCFG_LEGACY          :
+      return extractLegacyXID(DEFAULT_PKGSTATEDIR, id);
     case vcCFG_RECENT_SHORT    :
     case vcCFG_RECENT_FULL     : {
       size_t           idx = 0;