merge with 0.30.213
[util-vserver.git] / lib / comparevserverbyid.c
1 // $Id: comparevserverbyid.c 2180 2005-10-25 16:17:12Z ensc $    --*- c -*--
2
3 // Copyright (C) 2005 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 //  
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
8 //  
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //  
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "vserver.h"
24 #include "pathconfig.h"
25
26 #include <sys/stat.h>
27 #include <string.h>
28
29 static char const *
30 completePath(char const *id, size_t len, vcCfgStyle style, char *buf)
31 {
32   switch (style) {
33     case vcCFG_RECENT_FULL      :  return id;
34     case vcCFG_RECENT_SHORT     :
35       memcpy(buf,              CONFDIR "/", sizeof(CONFDIR "/")-1);
36       memcpy(buf+sizeof(CONFDIR "/")-1, id, len+1);     // appends '\0' implicitly
37       return buf;
38     default                     :  return 0;
39   }
40 }
41
42 int
43 vc_compareVserverById(char const *lhs, vcCfgStyle lhs_style,
44                       char const *rhs, vcCfgStyle rhs_style)
45 {
46   if (lhs_style==vcCFG_NONE || lhs_style==vcCFG_AUTO)
47     lhs_style = vc_getVserverCfgStyle(lhs);
48
49   if (rhs_style==vcCFG_NONE || rhs_style==vcCFG_AUTO)
50     rhs_style = vc_getVserverCfgStyle(rhs);
51
52     // compare legacy vservers by their names only resp. return false on mixed
53     // styles
54   if (lhs_style==vcCFG_LEGACY || rhs_style==vcCFG_LEGACY) {
55     if (lhs_style!=rhs_style) return lhs_style - rhs_style;
56     else                      return strcmp(lhs, rhs);
57   }
58
59   {
60     size_t              len_lhs = strlen(lhs);
61     size_t              len_rhs = strlen(rhs);
62     char                buf_lhs[sizeof(CONFDIR "//") + len_lhs];
63     char                buf_rhs[sizeof(CONFDIR "//") + len_rhs];
64
65     char const *        path_lhs = completePath(lhs, len_lhs, lhs_style, buf_lhs);
66     char const *        path_rhs = (path_lhs==0
67                                     ? 0 // skip following calculation
68                                     : completePath(rhs, len_rhs, rhs_style, buf_rhs));
69
70     struct stat         st_lhs;
71     struct stat         st_rhs;
72     
73       // this is true only iff both path_* are 0; compare ids in this case
74     if (path_lhs==path_rhs) return strcmp(lhs, rhs);
75     if (path_lhs==0) return -1;         // path_rhs!=0 is implied by check above
76     if (path_rhs==0) return +1;
77
78     if (stat(path_lhs, &st_lhs)==-1 ||
79         stat(path_rhs, &st_rhs)==-1) return strcmp(lhs,rhs);
80
81     return (st_lhs.st_dev - st_rhs.st_dev) + (st_lhs.st_ino - st_rhs.st_ino);
82   }
83 }