--- /dev/null
+// $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.
+
+
+#ifndef H_UTIL_VSERVER_LIB_FMT_COMMON_H
+#define H_UTIL_VSERVER_LIB_FMT_COMMON_H
+
+static char const DIGITS[] = "0123456789abcdefghijklmnopqrstuvwxyz";
+
+#define FMT_P__(X,Y) X ## Y
+#define FMT_P_(X,Y) FMT_P__(X,Y)
+#define FMT_P(X) FMT_P_(FMT_PREFIX, X)
+
+#define CONCAT__(x,y,z) x ## y ## z
+#define CONCAT_(x,y,z) CONCAT__(x,y,z)
+#define CONCAT(x,z) CONCAT_(x, FMT_BITSIZE, z)
+
+#define FMT_FN(BASE,SZ) \
+ do { \
+ register __typeof__(val) v = val; \
+ register size_t l = 0; \
+ \
+ if (ptr==0) { \
+ do { \
+ ++l; \
+ v /= BASE; \
+ } while (v!=0); \
+ } \
+ else { \
+ char buf[sizeof(val)*SZ]; \
+ \
+ do { \
+ register unsigned int d = v%BASE; \
+ v /= BASE; \
+ ++l; \
+ buf[sizeof(buf)-l] = DIGITS[d]; \
+ } while (v!=0); \
+ \
+ memcpy(ptr, buf+sizeof(buf)-l, l); \
+ } \
+ \
+ return l; \
+ } while (0)
+
+
+#endif // H_UTIL_VSERVER_LIB_FMT_COMMON_H
--- /dev/null
+// $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
+
+#define FMT_BITSIZE 32
+#include "fmtx.hc"
--- /dev/null
+// $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
+
+#define FMT_BITSIZE 64
+#include "fmtx.hc"
--- /dev/null
+// $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 "fmt.h"
+#include "fmt-internal.h"
+#include <string.h>
+
+size_t
+CONCAT(FMT_P(xuint),)(char *ptr, CONCAT(uint_least,_t) val)
+{
+ FMT_FN(16,8);
+}
+
+size_t
+CONCAT(FMT_P(xint),)(char *ptr,
+ CONCAT(int_least,_t) val)
+{
+ size_t offset=0;
+ if (val<0) {
+ val = -val;
+ offset = 1;
+
+ if (ptr!=0)
+ *ptr++ = '-';
+ }
+
+ return CONCAT(FMT_P(xuint),)(ptr, val) + offset;
+}
--- /dev/null
+// $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 "internal.h"
+#include "pathconfig.h"
+
+#include <string.h>
+#include <unistd.h>
+
+char *
+vc_getVserverByCtx(ctx_t ctx, vcCfgStyle *style, char const *revdir)
+{
+ if (revdir==0) revdir = DEFAULT_PKGSTATEREVDIR;
+
+ vcCfgStyle cur_style = vcCFG_NONE;
+ size_t l = strlen(revdir);
+ size_t l1;
+ char path[l + sizeof(unsigned int)*3 + 2 + sizeof("/name")];
+
+ strcpy(path, revdir);
+ path[l] = '/';
+ l1 = utilvserver_fmt_uint(path+l+1, ctx);
+ path[l+1+l1] = '\0';
+
+ if (style==0 || *style==vcCFG_AUTO) {
+ if (access(path, F_OK)==0) cur_style = vcCFG_RECENT_FULL;
+ // TODO: handle legacy
+ }
+ else
+ cur_style = *style;
+
+ switch (cur_style) {
+ case vcCFG_RECENT_SHORT :
+ case vcCFG_RECENT_FULL :
+ // check if expected ctx == actual ctx
+ if (vc_getVserverCtx(path, vcCFG_RECENT_FULL)!=ctx) return 0;
+
+ if (style) *style = vcCFG_RECENT_FULL;
+ return strdup(path);
+ // TODO: handle legacy
+ default :
+ return 0;
+ }
+}
--- /dev/null
+// $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 <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+
+static ctx_t
+getCtxFromFile(char const *pathname)
+{
+ int fd;
+ off_t len;
+
+ fd = open(pathname, O_RDONLY);
+
+ if (fd==-1 ||
+ (len=lseek(fd, 0, SEEK_END))==-1 ||
+ (len>50) ||
+ (lseek(fd, 0, SEEK_SET)==-1))
+ return VC_NOCTX;
+
+ char buf[len+1];
+ if (TEMP_FAILURE_RETRY(read(fd, buf, len+1))!=len)
+ return VC_NOCTX;
+
+ char *errptr;
+ ctx_t res;
+
+ res = strtol(buf, &errptr, 10);
+ if (*errptr!='\0' && *errptr!='\n') return VC_NOCTX;
+
+ return res;
+}
+
+ctx_t
+vc_getVserverCtx(char const *id, vcCfgStyle style)
+{
+ size_t l1 = strlen(id);
+ char buf[sizeof(CONFDIR "//") + l1 + sizeof("/run")];
+
+ if (style==vcCFG_NONE || style==vcCFG_AUTO)
+ style = vc_getVserverCfgStyle(id);
+
+ switch (style) {
+ case vcCFG_NONE : return VC_NOCTX;
+ case vcCFG_LEGACY : return VC_NOCTX; // todo
+ case vcCFG_RECENT_SHORT :
+ case vcCFG_RECENT_FULL : {
+ size_t idx = 0;
+ if (style==vcCFG_RECENT_SHORT) {
+ memcpy(buf, CONFDIR "/", sizeof(CONFDIR "/")-1);
+ idx = sizeof(CONFDIR "/") - 1;
+ }
+ memcpy(buf+idx, id, l1); idx += l1;
+ memcpy(buf+idx, "/run", 4); idx += 4;
+ buf[idx] = '\0';
+
+
+ return getCtxFromFile(buf);
+ }
+ default : return VC_NOCTX;
+ }
+}