From 767ebd36c9c2f1d460a153da6aeccc5cc38b74f9 Mon Sep 17 00:00:00 2001 From: Enrico Scholz Date: Tue, 25 Oct 2005 16:17:12 +0000 Subject: [PATCH] added comparevserverbyid.c git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@2180 94cd875c-1c1d-0410-91d2-eb244daf1a30 --- util-vserver/lib/Makefile-files | 3 +- util-vserver/lib/comparevserverbyid.c | 83 +++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 util-vserver/lib/comparevserverbyid.c diff --git a/util-vserver/lib/Makefile-files b/util-vserver/lib/Makefile-files index 4820158..0e839b5 100644 --- a/util-vserver/lib/Makefile-files +++ b/util-vserver/lib/Makefile-files @@ -22,7 +22,8 @@ lib_VERSION = 0.0.0 lib_compat_SRCS = lib/cflags-compat.c \ lib/cflags_list-compat.c lib_legacy_SRCS = lib/getprocentry-legacy.c -lib_management_SRCS = lib/createskeleton.c \ +lib_management_SRCS = lib/comparevserverbyid.c \ + lib/createskeleton.c \ lib/getvserverbyctx.c \ lib/getvserverbyctx-compat.hc \ lib/getvserverbyctx-v13.hc \ diff --git a/util-vserver/lib/comparevserverbyid.c b/util-vserver/lib/comparevserverbyid.c new file mode 100644 index 0000000..a679bbe --- /dev/null +++ b/util-vserver/lib/comparevserverbyid.c @@ -0,0 +1,83 @@ +// $Id$ --*- c -*-- + +// Copyright (C) 2005 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 + +static char const * +completePath(char const *id, size_t len, vcCfgStyle style, char *buf) +{ + switch (style) { + case vcCFG_RECENT_FULL : return id; + case vcCFG_RECENT_SHORT : + memcpy(buf, CONFDIR "/", sizeof(CONFDIR "/")-1); + memcpy(buf+sizeof(CONFDIR "/")-1, id, len+1); // appends '\0' implicitly + return buf; + default : return 0; + } +} + +int +vc_compareVserverById(char const *lhs, vcCfgStyle lhs_style, + char const *rhs, vcCfgStyle rhs_style) +{ + if (lhs_style==vcCFG_NONE || lhs_style==vcCFG_AUTO) + lhs_style = vc_getVserverCfgStyle(lhs); + + if (rhs_style==vcCFG_NONE || rhs_style==vcCFG_AUTO) + rhs_style = vc_getVserverCfgStyle(rhs); + + // compare legacy vservers by their names only resp. return false on mixed + // styles + if (lhs_style==vcCFG_LEGACY || rhs_style==vcCFG_LEGACY) { + if (lhs_style!=rhs_style) return lhs_style - rhs_style; + else return strcmp(lhs, rhs); + } + + { + size_t len_lhs = strlen(lhs); + size_t len_rhs = strlen(rhs); + char buf_lhs[sizeof(CONFDIR "//") + len_lhs]; + char buf_rhs[sizeof(CONFDIR "//") + len_rhs]; + + char const * path_lhs = completePath(lhs, len_lhs, lhs_style, buf_lhs); + char const * path_rhs = (path_lhs==0 + ? 0 // skip following calculation + : completePath(rhs, len_rhs, rhs_style, buf_rhs)); + + struct stat st_lhs; + struct stat st_rhs; + + // this is true only iff both path_* are 0; compare ids in this case + if (path_lhs==path_rhs) return strcmp(lhs, rhs); + if (path_lhs==0) return -1; // path_rhs!=0 is implied by check above + if (path_rhs==0) return +1; + + if (stat(path_lhs, &st_lhs)==-1 || + stat(path_rhs, &st_rhs)==-1) return strcmp(lhs,rhs); + + return (st_lhs.st_dev - st_rhs.st_dev) + (st_lhs.st_ino - st_rhs.st_ino); + } +} -- 1.8.1.5