Debian Codegraph @

src/yvalve/config/os/config_root.h

raw
1 /*
2 * PROGRAM: Client/Server Common Code
3 * MODULE: config_root.h
4 * DESCRIPTION: Configuration manager (platform specific)
5 *
6 * The contents of this file are subject to the Interbase Public
7 * License Version 1.0 (the "License"); you may not use this file
8 * except in compliance with the License. You may obtain a copy
9 * of the License at http://www.Inprise.com/IPL.html
10 *
11 * Software distributed under the License is distributed on an
12 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
13 * or implied. See the License for the specific language governing
14 * rights and limitations under the License.
15 *
16 * The Original Code was created by Inprise Corporation
17 * and its predecessors. Portions created by Inprise Corporation are
18 * Copyright (C) Inprise Corporation.
19 *
20 * Created by: Dmitry Yemanov <[email protected]>
21 *
22 * All Rights Reserved.
23 * Contributor(s): ______________________________________.
24 */
25
26 #ifndef CONFIG_ROOT_H
27 #define CONFIG_ROOT_H
28
29 #include "fb_types.h"
30 #include "../common/classes/init.h"
31 #include "../common/classes/fb_string.h"
32 #include "../common/config/config.h"
33
34 #include "../common/os/path_utils.h"
35 #include "../common/utils_proto.h"
36
37 /**
38 Since the original (isc.cpp) code wasn't able to provide powerful and
39 easy-to-use abilities to work with complex configurations, a decision
40 has been made to create a completely new one.
41
42 This class is used to determine root directory of the installation and
43 to get a pathname for the main configuration file. You may ask what's
44 treated as root directory. The implementation of this class uses the
45 following algorithm (located in getRootDirectory() member function):
46
47 1. Retrieve full path of the running executable.
48 2. If we're either client library or embedded server, return this path.
49 3. Otherwise, return parent directory of this path.
50
51 Pathname of the configuration file is platform-specific and defined by
52 getConfigFilePath() member function.
53 **/
54
55 class ConfigRoot : public Firebird::PermanentStorage
56 {
57 // we deal with names of files here
58 typedef Firebird::PathName string;
59
60 private:
61 void GetRoot()
62 {
63 const Firebird::PathName* clRoot = Firebird::Config::getCommandLineRootDirectory();
64 if (clRoot)
65 {
66 root_dir = *clRoot;
67 fixPath();
68 return;
69 }
70 #ifdef DEV_BUILD
71 if (getRootFromEnvironment("FIREBIRD_DEV")) {
72 return;
73 }
74 #endif
75 if (getRootFromEnvironment("FIREBIRD")) {
76 return;
77 }
78 osConfigRoot();
79 }
80
81 void GetInstallDir()
82 {
83 // we have no reliable ways to detect install directory in OS-independent way
84 osConfigInstallDir();
85 }
86
87 public:
88 explicit ConfigRoot(MemoryPool& p) : PermanentStorage(p),
89 root_dir(getPool()), install_dir(getPool())
90 {
91 GetInstallDir();
92 GetRoot();
93 }
94
95 const char* getInstallDirectory() const
96 {
97 return install_dir.c_str();
98 }
99
100 const char* getRootDirectory() const
101 {
102 return root_dir.c_str();
103 }
104
105 private:
106 string root_dir, install_dir;
107
108 // If the path ends with a separator, remove it.
109 void fixPath()
110 {
111 FB_SIZE_T pos = root_dir.rfind(PathUtils::dir_sep);
112
113 if (root_dir.hasData() && pos > 0 && pos == root_dir.length() - 1)
114 root_dir.erase(pos, 1);
115 }
116
117 bool getRootFromEnvironment(const char* envName)
118 {
119 string envValue;
120 if (!fb_utils::readenv(envName, envValue))
121 return false;
122
123 root_dir = envValue;
124 fixPath();
125 return true;
126 }
127
128 void osConfigRoot();
129 void osConfigInstallDir();
130
131 // copy prohibition
132 ConfigRoot(const ConfigRoot&);
133 void operator=(const ConfigRoot&);
134 };
135
136 #endif // CONFIG_ROOT_H