vshost-util-vserver

Build script and sources for util-vserver.
git clone https://ccx.te2000.cz/git/vshost-util-vserver
Log | Files | Refs

getvservervdir.c (2736B)


      1 // $Id$    --*- c -*--
      2 
      3 // Copyright (C) 2003 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 "internal.h"
     25 #include "pathconfig.h"
     26 
     27 #include <string.h>
     28 #include <unistd.h>
     29 #include <sys/param.h>
     30 #include <fcntl.h>
     31 
     32 static char *
     33 getDir(char *dir, bool physical)
     34 {
     35   int		fd;
     36   char		tmp[PATH_MAX];
     37 
     38   if (!physical) return strdup(dir);
     39 
     40   fd = open(".", O_RDONLY);
     41   if (fd==-1) return 0;
     42 
     43   if (chdir(dir)!=-1 &&
     44       getcwd(tmp, sizeof tmp)!=0)
     45     dir = strdup(tmp);
     46   else
     47     dir = 0;
     48 
     49   if (fchdir(fd)==-1) {
     50     if (write(2, "FATAL error: failed to restore directory\n", 41)!=41) { /*...*/ }
     51     abort();
     52   }
     53   close(fd);
     54   return dir;
     55 }
     56        
     57 char *
     58 vc_getVserverVdir(char const *id, vcCfgStyle style, bool physical)
     59 {
     60   size_t		l1   = strlen(id);
     61   char			*res = 0;
     62 
     63   if (style==vcCFG_NONE || style==vcCFG_AUTO)
     64     style = vc_getVserverCfgStyle(id);
     65 
     66   switch (style) {
     67     case vcCFG_NONE		:  return 0;
     68     case vcCFG_LEGACY		:
     69     {
     70       char		buf[sizeof(DEFAULT_VSERVERDIR "/") + l1];
     71 
     72       strcpy(buf,                                    DEFAULT_VSERVERDIR "/");
     73       strcpy(buf+sizeof(DEFAULT_VSERVERDIR "/") - 1, id);
     74 
     75       res = getDir(buf, physical);
     76       break;
     77     }
     78     
     79     case vcCFG_RECENT_SHORT	:
     80     {
     81       char		buf[sizeof(CONFDIR) + l1 + sizeof("//vdir") - 1];
     82 
     83       strcpy(buf,                            CONFDIR "/");
     84       strcpy(buf+sizeof(CONFDIR "/")    - 1, id);
     85       strcpy(buf+sizeof(CONFDIR "/")+l1 - 1, "/vdir");
     86       
     87       res = getDir(buf, physical);
     88       break;
     89     }
     90 
     91     case vcCFG_RECENT_FULL	:
     92     {
     93       char		buf[l1 + sizeof("/vdir")];
     94 
     95       strcpy(buf,    id);
     96       strcpy(buf+l1, "/vdir");
     97 
     98       res = getDir(buf, physical);
     99       break;
    100     }
    101 
    102     default			:  return 0;
    103   }
    104 
    105   // ignore physical-case; we went into the directory while determining
    106   // the physical path so the directory exists
    107   if (!physical && !utilvserver_isDirectory(res, true)) {
    108     free(res);
    109     res = 0;
    110   }
    111 
    112   return res;
    113 }