initial checkin
authorEnrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
Thu, 23 Oct 2003 20:55:47 +0000 (20:55 +0000)
committerEnrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
Thu, 23 Oct 2003 20:55:47 +0000 (20:55 +0000)
git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@300 94cd875c-1c1d-0410-91d2-eb244daf1a30

util-vserver/lib/getvservercfgstyle.c [new file with mode: 0644]
util-vserver/lib/getvservername.c [new file with mode: 0644]
util-vserver/lib/getvservervdir.c [new file with mode: 0644]
util-vserver/tests/vserver-info.c [new file with mode: 0644]

diff --git a/util-vserver/lib/getvservercfgstyle.c b/util-vserver/lib/getvservercfgstyle.c
new file mode 100644 (file)
index 0000000..f56143b
--- /dev/null
@@ -0,0 +1,68 @@
+// $Id$    --*- c -*--
+
+// Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//  
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//  
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//  
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#include "vserver.h"
+#include "pathconfig.h"
+
+#include <string.h>
+#include <sys/param.h>
+#include <unistd.h>
+#include <assert.h>
+
+vcCfgStyle
+vc_getVserverCfgStyle(char const *id)
+{
+  vcCfgStyle   res = vcCFG_NONE;
+  size_t       l1  = strlen(id);
+  char         buf[l1 +
+                   MAX(sizeof(CONFDIR "/"),sizeof(DEFAULT_VSERVERDIR "/")) +
+                   sizeof("/legacy") - 1];
+  char *       marker = 0;
+
+  strcpy(buf,    id);
+  marker = buf+l1;
+  strcpy(marker, "/vdir");
+  
+  if (access(buf, X_OK)==0) res = vcCFG_RECENT_FULL;
+  else {
+    strcpy(buf,                         CONFDIR "/");
+    strcpy(buf+sizeof(CONFDIR "/") - 1, id);
+    marker = buf+sizeof(CONFDIR "/")+l1 - 1;
+    strcpy(marker, "/vdir");
+
+    if (access(buf, X_OK)==0) res = vcCFG_RECENT_SHORT;
+    else {
+      strcpy(buf,                                  DEFAULT_VSERVERDIR "/");
+      strcpy(buf+sizeof(DEFAULT_VSERVERDIR)+1 - 1, id);
+
+      if (access(buf, X_OK)) res = vcCFG_LEGACY;
+    }
+  }
+
+  if (res==vcCFG_RECENT_FULL || res==vcCFG_RECENT_SHORT) {
+    assert(marker!=0);
+    strcpy(marker, "/legacy");
+    if (access(buf, F_OK)==0) res=vcCFG_LEGACY;
+  }
+
+  return res;
+}
diff --git a/util-vserver/lib/getvservername.c b/util-vserver/lib/getvservername.c
new file mode 100644 (file)
index 0000000..00dfd3a
--- /dev/null
@@ -0,0 +1,96 @@
+// $Id$    --*- c -*--
+
+// Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//  
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//  
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//  
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#include "vserver.h"
+#include "pathconfig.h"
+
+#include <string.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <libgen.h>
+
+static char *
+getRecentName(char *start, char *end)
+{
+  char         *res = 0;
+  int          fd;
+  
+  strcpy(end, "/name");
+  fd = open(start, O_RDONLY);
+  if (fd!=-1) {
+    off_t      len;
+
+    if ((len=lseek(fd, 0, SEEK_END))!=-1 &&
+       (len<VC_LIMIT_VSERVER_NAME_LEN) &&
+       (lseek(fd, 0, SEEK_SET)!=-1)) {
+      char     buf[len+1];
+
+      if (TEMP_FAILURE_RETRY(read(fd, buf, len+1))==len) {
+       while (len>0 && buf[len-1]=='\n') --len;
+       if (len>0) res = buf;
+      }
+
+      close(fd);
+      return strdup(res);
+    }
+
+    close(fd);
+  }
+
+  if (res==0) {
+    *end = '\0';
+    res  = basename(start);
+  }
+
+  return strdup(res);
+}
+
+char *
+vc_getVserverName(char const *id, vcCfgStyle style)
+{
+  size_t               l1  = strlen(id);
+
+  if (style==vcCFG_NONE || style==vcCFG_AUTO)
+    style = vc_getVserverCfgStyle(id);
+
+  switch (style) {
+    case vcCFG_NONE            :  return 0;
+    case vcCFG_LEGACY          :  return strdup(id);
+    case vcCFG_RECENT_SHORT    :
+    {
+      char             buf[sizeof(CONFDIR "/") + l1 + sizeof("/name") - 1];
+
+      strcpy(buf,                         CONFDIR "/");
+      strcpy(buf+sizeof(CONFDIR "/") - 1, id);
+      
+      return getRecentName(buf, buf+sizeof(CONFDIR "/")+l1 - 1);
+    }
+    case vcCFG_RECENT_FULL     :
+    {
+      char             buf[l1 + sizeof("/name")];
+      strcpy(buf, id);
+
+      return getRecentName(buf, buf+l1);
+    }
+    default                    :  return 0;
+  }
+}
diff --git a/util-vserver/lib/getvservervdir.c b/util-vserver/lib/getvservervdir.c
new file mode 100644 (file)
index 0000000..f1b3b92
--- /dev/null
@@ -0,0 +1,85 @@
+// $Id$    --*- c -*--
+
+// Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//  
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//  
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//  
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#include "vserver.h"
+#include "pathconfig.h"
+
+#include <string.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <libgen.h>
+
+char *
+vc_getVserverVdir(char const *id, vcCfgStyle style)
+{
+  size_t               l1   = strlen(id);
+  char                 *res = 0;
+
+  if (style==vcCFG_NONE || style==vcCFG_AUTO)
+    style = vc_getVserverCfgStyle(id);
+
+  switch (style) {
+    case vcCFG_NONE            :  return 0;
+    case vcCFG_LEGACY          :
+    {
+      char             buf[sizeof(DEFAULT_VSERVERDIR "/") + l1];
+
+      strcpy(buf,                                    DEFAULT_VSERVERDIR "/");
+      strcpy(buf+sizeof(DEFAULT_VSERVERDIR "/") - 1, id);
+
+      res = strdup(buf);
+      break;
+    }
+    
+    case vcCFG_RECENT_SHORT    :
+    {
+      char             buf[sizeof(CONFDIR) + l1 + sizeof("//vdir") - 1];
+
+      strcpy(buf,                            CONFDIR "/");
+      strcpy(buf+sizeof(CONFDIR "/")    - 1, id);
+      strcpy(buf+sizeof(CONFDIR "/")+l1 - 1, "/vdir");
+      
+      res = strdup(buf);
+      break;
+    }
+
+    case vcCFG_RECENT_FULL     :
+    {
+      char             buf[l1 + sizeof("/vdir")];
+
+      strcpy(buf,    id);
+      strcpy(buf+l1, "/vdir");
+
+      res = strdup(buf);
+      break;
+    }
+
+    default                    :  return 0;
+  }
+
+  if (access(res, X_OK)==-1) {
+    free(res);
+    res = 0;
+  }
+
+  return res;
+}
diff --git a/util-vserver/tests/vserver-info.c b/util-vserver/tests/vserver-info.c
new file mode 100644 (file)
index 0000000..0204958
--- /dev/null
@@ -0,0 +1,55 @@
+// $Id$    --*- c -*--
+
+// Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//  
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//  
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//  
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#include "vserver.h"
+
+#include "src/util.h"
+#include <stdlib.h>
+
+int
+main(int argc, char *argv[])
+{
+  vcCfgStyle   style = vc_getVserverCfgStyle(argv[1]);
+  char const * name  = vc_getVserverName(argv[1], style);
+  char const * vdir  = vc_getVserverVdir(argv[1], style);
+
+  WRITE_MSG(2, "Style: ");
+  switch (style) {
+    case vcCFG_NONE            :  WRITE_MSG(2, "CFG_NONE");   break;
+    case vcCFG_AUTO            :  WRITE_MSG(2, "CFG_AUTO");   break;
+    case vcCFG_LEGACY          :  WRITE_MSG(2, "CFG_LEGACY"); break;
+    case vcCFG_RECENT_FULL     :  WRITE_MSG(2, "CFG_RECENT_FULL");  break;
+    case vcCFG_RECENT_SHORT    :  WRITE_MSG(2, "CFG_RECENT_SHORT"); break;
+    default                    :  WRITE_MSG(2, "???"); break;
+  }
+
+  WRITE_MSG(2, "\nName:  ");
+  if (name==0) WRITE_MSG(2, "<null>");
+  else         WRITE_STR(2, name);
+
+  WRITE_MSG(2, "\nVdir:  ");
+  if (vdir==0) WRITE_MSG(2, "<null>");
+  else         WRITE_STR(2, vdir);
+
+  WRITE_MSG(2, "\n");
+  return EXIT_SUCCESS;
+}