first pass at a script to rename a vserver
[util-vserver.git] / src / vserver.cc
1 // $Id: vserver.cc,v 1.1 2003/10/21 13:26:21 ensc Exp $    --*- 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.hh"
24 #include "pathconfig.h"
25
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <fstream>
30 #include <iostream>
31
32 static bool
33 dirExists(std::string const &path)
34 {
35   struct stat   st;
36   return (stat(path.c_str(), &st)!=-1 &&
37           S_ISDIR(st.st_mode));
38 }
39
40 Vserver::Vserver(std::string const &str) :
41   conf_dir_(str), vdir_(str), name_(str)
42 {
43   vdir_       += "/.vdir";
44   
45   if (!dirExists(vdir_)) {
46     conf_dir_  = CONFDIR;
47     conf_dir_ += str;
48   }
49
50   rpmdb_path_  = conf_dir_;
51   rpmdb_path_ += "/apps/pkgmgmt/rpmstate";
52
53   if (!dirExists(rpmdb_path_)) {
54     rpmdb_path_  = conf_dir_;
55     rpmdb_path_ += "/apps/pkgmgmt/base/rpm/state";
56   }
57
58   if (!dirExists(rpmdb_path_)) {
59     rpmdb_path_  = vdir_;
60     rpmdb_path_ += "/var/lib/rpm";
61   }
62
63   std::string   tmp = conf_dir_;
64   tmp  += ".name";
65
66   std::ifstream         name_file(tmp.c_str());
67
68   if (name_file.good()) getline(name_file, name_);
69 }
70
71 std::ostream &
72 operator << (std::ostream &lhs, Vserver const &rhs)
73 {
74   return lhs << rhs.getName();
75 }