Debian Codegraph @

src/yvalve/config/os/darwin/config_root.cpp

raw
1 /*
2 * PROGRAM: Client/Server Common Code
3 * MODULE: config_root.cpp
4 * DESCRIPTION: Configuration manager (platform specific - Darwin)
5 *
6 * The contents of this file are subject to the Initial
7 * Developer's Public License Version 1.0 (the "License");
8 * you may not use this file except in compliance with the
9 * License. You may obtain a copy of the License at
10 * http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
11 *
12 * Software distributed under the License is distributed AS IS,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied.
14 * See the License for the specific language governing rights
15 * and limitations under the License.
16 *
17 * The Original Code was created by John Bellardo
18 * for the Firebird Open Source RDBMS project.
19 *
20 * Copyright (c) 2002 John Bellardo <bellardo at cs.ucsd.edu>
21 * and all contributors signed below.
22 *
23 * All Rights Reserved.
24 * Contributor(s): ______________________________________.
25 *
26 */
27
28 #include "firebird.h"
29
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33
34 #include "fb_types.h"
35 #include "../common/classes/fb_string.h"
36 #include "../yvalve/config/os/config_root.h"
37 #include "../common/os/path_utils.h"
38 #include "../common/file_params.h"
39
40 #ifndef IOS
41 #include <CoreServices/CoreServices.h>
42 #endif
43 #include <CoreFoundation/CFBundle.h>
44 #include <CoreFoundation/CFURL.h>
45 #include <mach-o/dyld.h>
46 #include <stdlib.h>
47
48 #include <dlfcn.h>
49
50 typedef Firebird::PathName string;
51
52 static string getFrameworkFromBundle()
53 {
54 // Attempt to locate the Firebird.framework bundle
55 CFBundleRef fbFramework = CFBundleGetBundleWithIdentifier(CFSTR(DARWIN_FRAMEWORK_ID));
56 if (fbFramework)
57 {
58 CFURLRef msgFileUrl = CFBundleCopyResourceURL(fbFramework,
59 CFSTR(DARWIN_GEN_DIR), NULL, NULL);
60 if (msgFileUrl)
61 {
62 CFStringRef msgFilePath = CFURLCopyFileSystemPath(msgFileUrl,
63 kCFURLPOSIXPathStyle);
64 if (msgFilePath)
65 {
66 char file_buff[MAXPATHLEN];
67 if (CFStringGetCString(msgFilePath, file_buff, MAXPATHLEN,
68 kCFStringEncodingMacRoman))
69 {
70 string dir = file_buff;
71 dir += PathUtils::dir_sep;
72 return dir;
73 }
74 }
75 }
76 }
77
78 // No luck
79 return "";
80 }
81
82 static string getExecutablePath()
83 {
84 char file_buff[MAXPATHLEN];
85 uint32_t bufsize = sizeof(file_buff);
86 _NSGetExecutablePath(file_buff, &bufsize);
87 char canonic[PATH_MAX];
88 if (!realpath(file_buff, canonic))
89 Firebird::system_call_failed::raise("realpath");
90 string bin_dir = canonic;
91 // get rid of the filename
92 int index = bin_dir.rfind(PathUtils::dir_sep);
93 bin_dir = bin_dir.substr(0, index);
94 // go to parent directory
95 index = bin_dir.rfind(PathUtils::dir_sep, bin_dir.length());
96 string dir = (index ? bin_dir.substr(0,index) : bin_dir) + PathUtils::dir_sep;
97 return dir;
98 }
99
100 static string getDlInfoPath()
101 {
102 Dl_info dlInfo;
103 static int test = 1;
104 if (dladdr(&test, &dlInfo) != 0) // non-zero is success in dladdr
105 {
106 string next, dummy;
107 PathUtils::splitLastComponent(next, dummy, dlInfo.dli_fname);
108 string dir;
109 PathUtils::splitLastComponent(dir, dummy, next);
110 return dir;
111 }
112 return "";
113 }
114
115
116 void ConfigRoot::osConfigRoot()
117 {
118 // Attempt to locate the Firebird.framework bundle
119 root_dir = getFrameworkFromBundle();
120 if (root_dir.hasData())
121 {
122 return;
123 }
124
125 // Ask dynamic loader to provide info about current dynamic library
126 root_dir = getDlInfoPath();
127 if (root_dir.hasData())
128 {
129 return;
130 }
131
132 // Use executable file path
133 root_dir = getExecutablePath();
134 if (root_dir.hasData())
135 {
136 return;
137 }
138
139 // As a last resort get it from the default install directory
140 root_dir = FB_PREFIX;
141 }
142
143
144 void ConfigRoot::osConfigInstallDir()
145 {
146 // Attempt to locate the Firebird.framework bundle
147 install_dir = getFrameworkFromBundle();
148 if (install_dir.hasData())
149 {
150 return;
151 }
152
153 // Ask dynamic loader to provide info about current dynamic library
154 install_dir = getDlInfoPath();
155 if (install_dir.hasData())
156 {
157 return;
158 }
159
160 install_dir = getExecutablePath();
161 if (install_dir.hasData())
162 {
163 return;
164 }
165
166 // As a last resort get it from the default install directory
167 install_dir = FB_PREFIX;
168 }