From 45910d43ec9b4ef36e07f74348801ead61382482 Mon Sep 17 00:00:00 2001 From: Enrico Scholz Date: Thu, 20 Nov 2003 18:04:16 +0000 Subject: [PATCH] initial checkin git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@426 94cd875c-1c1d-0410-91d2-eb244daf1a30 --- util-vserver/lib/fmt-internal.h | 60 ++++++++++++++++++++++++++ util-vserver/lib/fmtx-32.c | 24 +++++++++++ util-vserver/lib/fmtx-64.c | 24 +++++++++++ util-vserver/lib/fmtx.hc | 47 +++++++++++++++++++++ util-vserver/lib/getvserverbyctx.c | 64 ++++++++++++++++++++++++++++ util-vserver/lib/getvserverctx.c | 86 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 305 insertions(+) create mode 100644 util-vserver/lib/fmt-internal.h create mode 100644 util-vserver/lib/fmtx-32.c create mode 100644 util-vserver/lib/fmtx-64.c create mode 100644 util-vserver/lib/fmtx.hc create mode 100644 util-vserver/lib/getvserverbyctx.c create mode 100644 util-vserver/lib/getvserverctx.c diff --git a/util-vserver/lib/fmt-internal.h b/util-vserver/lib/fmt-internal.h new file mode 100644 index 0000000..f12f285 --- /dev/null +++ b/util-vserver/lib/fmt-internal.h @@ -0,0 +1,60 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2003 Enrico Scholz +// +// 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 diff --git a/util-vserver/lib/fmtx-32.c b/util-vserver/lib/fmtx-32.c new file mode 100644 index 0000000..06af2c1 --- /dev/null +++ b/util-vserver/lib/fmtx-32.c @@ -0,0 +1,24 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2003 Enrico Scholz +// +// 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 +#endif + +#define FMT_BITSIZE 32 +#include "fmtx.hc" diff --git a/util-vserver/lib/fmtx-64.c b/util-vserver/lib/fmtx-64.c new file mode 100644 index 0000000..097271e --- /dev/null +++ b/util-vserver/lib/fmtx-64.c @@ -0,0 +1,24 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2003 Enrico Scholz +// +// 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 +#endif + +#define FMT_BITSIZE 64 +#include "fmtx.hc" diff --git a/util-vserver/lib/fmtx.hc b/util-vserver/lib/fmtx.hc new file mode 100644 index 0000000..df29616 --- /dev/null +++ b/util-vserver/lib/fmtx.hc @@ -0,0 +1,47 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2003 Enrico Scholz +// +// 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 +#endif + +#include "fmt.h" +#include "fmt-internal.h" +#include + +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; +} diff --git a/util-vserver/lib/getvserverbyctx.c b/util-vserver/lib/getvserverbyctx.c new file mode 100644 index 0000000..5c0be0a --- /dev/null +++ b/util-vserver/lib/getvserverbyctx.c @@ -0,0 +1,64 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2003 Enrico Scholz +// +// 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 +#endif + +#include "vserver.h" +#include "internal.h" +#include "pathconfig.h" + +#include +#include + +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; + } +} diff --git a/util-vserver/lib/getvserverctx.c b/util-vserver/lib/getvserverctx.c new file mode 100644 index 0000000..74225fd --- /dev/null +++ b/util-vserver/lib/getvserverctx.c @@ -0,0 +1,86 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2003 Enrico Scholz +// +// 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 +#endif + +#include "vserver.h" +#include "pathconfig.h" + +#include +#include +#include +#include + +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; + } +} -- 1.8.1.5