XRootD
Loading...
Searching...
No Matches
XrdClEnv.cc
Go to the documentation of this file.
1//------------------------------------------------------------------------------
2// Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
3// Author: Lukasz Janyst <ljanyst@cern.ch>
4//------------------------------------------------------------------------------
5// XRootD is free software: you can redistribute it and/or modify
6// it under the terms of the GNU Lesser General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// XRootD is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public License
16// along with XRootD. If not, see <http://www.gnu.org/licenses/>.
17//------------------------------------------------------------------------------
18
19#include <cstdlib>
20
21#include "XrdCl/XrdClEnv.hh"
23#include "XrdCl/XrdClLog.hh"
25
26namespace XrdCl
27{
28 //----------------------------------------------------------------------------
29 // Get string
30 //----------------------------------------------------------------------------
31 bool Env::GetString( const std::string &k, std::string &value )
32 {
33 std::string key = UnifyKey( k );
34 XrdSysRWLockHelper scopedLock( pLock );
35 StringMap::iterator it;
36 it = pStringMap.find( key );
37 if( it == pStringMap.end() )
38 {
39 Log *log = DefaultEnv::GetLog();
40 log->Debug( UtilityMsg,
41 "Env: trying to get a non-existent string entry: %s",
42 key.c_str() );
43 return false;
44 }
45 value = it->second.first;
46 return true;
47 }
48
49 //----------------------------------------------------------------------------
50 // Put string
51 //----------------------------------------------------------------------------
52 bool Env::PutString( const std::string &k, const std::string &value )
53 {
54 std::string key = UnifyKey( k );
55 XrdSysRWLockHelper scopedLock( pLock, false );
56
57 //--------------------------------------------------------------------------
58 // Insert the string if it's not there yet
59 //--------------------------------------------------------------------------
60 StringMap::iterator it;
61 it = pStringMap.find( key );
62 if( it == pStringMap.end() )
63 {
64 pStringMap[key] = std::make_pair( value, false );
65 return true;
66 }
67
68 //--------------------------------------------------------------------------
69 // The entry exists and it has been imported from the shell
70 //--------------------------------------------------------------------------
71 Log *log = DefaultEnv::GetLog();
72 if( it->second.second )
73 {
74 log->Debug( UtilityMsg,
75 "Env: trying to override a shell-imported string entry: %s",
76 key.c_str() );
77 return false;
78 }
79 log->Debug( UtilityMsg,
80 "Env: overriding entry: %s=\"%s\" with \"%s\"",
81 key.c_str(), it->second.first.c_str(), value.c_str() );
82 pStringMap[key] = std::make_pair( value, false );
83 return true;
84 }
85
86 //----------------------------------------------------------------------------
87 // Delete string
88 //----------------------------------------------------------------------------
89 bool Env::DelString( const std::string &k )
90 {
91 std::string key = UnifyKey( k );
92 XrdSysRWLockHelper scopedLock( pLock, false ); // obtain write lock
93
94 StringMap::iterator it;
95 it = pStringMap.find( key );
96 if( it == pStringMap.end() )
97 return true;
98
99 Log *log = DefaultEnv::GetLog();
100 if( it->second.second )
101 {
102 log->Debug( UtilityMsg,
103 "Env: trying to delete a shell-imported string entry: %s",
104 key.c_str() );
105 return false;
106 }
107 log->Debug( UtilityMsg, "Env: deleting string entry: %s", key.c_str() );
108 pStringMap.erase( it );
109 return true;
110 }
111
112 //----------------------------------------------------------------------------
113 // Get int
114 //----------------------------------------------------------------------------
115 bool Env::GetInt( const std::string &k, int &value )
116 {
117 std::string key = UnifyKey( k );
118 XrdSysRWLockHelper scopedLock( pLock );
119 IntMap::iterator it;
120 it = pIntMap.find( key );
121 if( it == pIntMap.end() )
122 {
123 Log *log = DefaultEnv::GetLog();
124 log->Debug( UtilityMsg,
125 "Env: trying to get a non-existent integer entry: %s",
126 key.c_str() );
127 return false;
128 }
129 value = it->second.first;
130 return true;
131 }
132
133 //----------------------------------------------------------------------------
134 // Put int
135 //----------------------------------------------------------------------------
136 bool Env::PutInt( const std::string &k, int value )
137 {
138 std::string key = UnifyKey( k );
139 XrdSysRWLockHelper scopedLock( pLock, false );
140
141 //--------------------------------------------------------------------------
142 // Insert the string if it's not there yet
143 //--------------------------------------------------------------------------
144 IntMap::iterator it;
145 it = pIntMap.find( key );
146 if( it == pIntMap.end() )
147 {
148 pIntMap[key] = std::make_pair( value, false );
149 return true;
150 }
151
152 //--------------------------------------------------------------------------
153 // The entry exists and it has been imported from the shell
154 //--------------------------------------------------------------------------
155 Log *log = DefaultEnv::GetLog();
156 if( it->second.second )
157 {
158 log->Debug( UtilityMsg,
159 "Env: trying to override a shell-imported integer entry: %s",
160 key.c_str() );
161 return false;
162 }
163 log->Debug( UtilityMsg,
164 "Env: overriding entry: %s=%d with %d",
165 key.c_str(), it->second.first, value );
166
167 pIntMap[key] = std::make_pair( value, false );
168 return true;
169 }
170
171 //----------------------------------------------------------------------------
172 // Delete int
173 //----------------------------------------------------------------------------
174 bool Env::DelInt( const std::string &k )
175 {
176 std::string key = UnifyKey( k );
177 XrdSysRWLockHelper scopedLock( pLock, false ); // obtain write lock
178
179 IntMap::iterator it;
180 it = pIntMap.find( key );
181 if( it == pIntMap.end() )
182 return true;
183
184 Log *log = DefaultEnv::GetLog();
185 if( it->second.second )
186 {
187 log->Debug( UtilityMsg,
188 "Env: trying to delete a shell-imported integer entry: %s",
189 key.c_str() );
190 return false;
191 }
192 log->Debug( UtilityMsg, "Env: deleting integer entry: %s", key.c_str() );
193 pIntMap.erase( it );
194 return true;
195 }
196
197 //----------------------------------------------------------------------------
198 // Get pointer
199 //----------------------------------------------------------------------------
200 bool Env::GetPtr( const std::string &k, void* &value )
201 {
202 std::string key = UnifyKey( k );
203 XrdSysRWLockHelper scopedLock( pLock );
204 PtrMap::iterator it;
205 it = pPtrMap.find( key );
206 if( it == pPtrMap.end() )
207 {
208 Log *log = DefaultEnv::GetLog();
209 log->Debug( UtilityMsg,
210 "Env: trying to get a non-existent pointer entry: %s",
211 key.c_str() );
212 return false;
213 }
214 value = it->second;
215 return true;
216 }
217
218 //----------------------------------------------------------------------------
219 // Put pointer
220 //----------------------------------------------------------------------------
221 bool Env::PutPtr( const std::string &k, void* value )
222 {
223 std::string key = UnifyKey( k );
224 XrdSysRWLockHelper scopedLock( pLock, false );
225
226 // Pointers cannot be imported from shell environment, we always set it.
227 bool ret = pPtrMap.find(key) == pPtrMap.end();
228
229 pPtrMap[key] = value;
230
231 return ret;
232 }
233
234 //----------------------------------------------------------------------------
235 // Import int
236 //----------------------------------------------------------------------------
237 bool Env::ImportInt( const std::string &k, const std::string &shellKey )
238 {
239 std::string key = UnifyKey( k );
240 XrdSysRWLockHelper scopedLock( pLock, false );
241 std::string strValue = GetEnv( shellKey );
242 if( strValue == "" )
243 return false;
244
245 Log *log = DefaultEnv::GetLog();
246 char *endPtr;
247 int value = (int)strtol( strValue.c_str(), &endPtr, 0 );
248 if( *endPtr )
249 {
250 log->Error( UtilityMsg,
251 "Env: Unable to import %s as %s: %s is not a proper integer",
252 shellKey.c_str(), key.c_str(), strValue.c_str() );
253 return false;
254 }
255
256 log->Info( UtilityMsg, "Env: Importing from shell %s=%d as %s",
257 shellKey.c_str(), value, key.c_str() );
258
259 pIntMap[key] = std::make_pair( value, true );
260 return true;
261 }
262
263 //----------------------------------------------------------------------------
264 // Import string
265 //----------------------------------------------------------------------------
266 bool Env::ImportString( const std::string &k, const std::string &shellKey )
267 {
268 std::string key = UnifyKey( k );
269 XrdSysRWLockHelper scopedLock( pLock, false );
270 std::string value = GetEnv( shellKey );
271 if( value == "" )
272 return false;
273
274 Log *log = DefaultEnv::GetLog();
275 log->Info( UtilityMsg, "Env: Importing from shell %s=%s as %s",
276 shellKey.c_str(), value.c_str(), key.c_str() );
277 pStringMap[key] = std::make_pair( value, true );
278 return true;
279 }
280
281 //------------------------------------------------------------------------
282 // Get default integer value for the given key
283 //------------------------------------------------------------------------
284 bool Env::GetDefaultIntValue( const std::string &k, int &value )
285 {
286 std::string key = UnifyKey( k );
287 auto itr = theDefaultInts.find( key );
288 if( itr == theDefaultInts.end() ) return false;
289 value = itr->second;
290 return true;
291 }
292
293 //------------------------------------------------------------------------
294 // Get default string value for the given key
295 //------------------------------------------------------------------------
296 bool Env::GetDefaultStringValue( const std::string &k, std::string &value )
297 {
298 std::string key = UnifyKey( k );
299 auto itr = theDefaultStrs.find( key );
300 if( itr == theDefaultStrs.end() ) return false;
301 value = itr->second;
302 return true;
303 }
304
305 //----------------------------------------------------------------------------
306 // Get a string from the environment
307 //----------------------------------------------------------------------------
308 std::string Env::GetEnv( const std::string &key )
309 {
310 char *var = getenv( key.c_str() );
311 if( !var )
312 return "";
313 return var;
314 }
315}
static Log * GetLog()
Get default log.
bool DelInt(const std::string &key)
Definition XrdClEnv.cc:174
bool PutInt(const std::string &key, int value)
Definition XrdClEnv.cc:136
bool PutString(const std::string &key, const std::string &value)
Definition XrdClEnv.cc:52
bool GetDefaultIntValue(const std::string &key, int &value)
Definition XrdClEnv.cc:284
bool ImportString(const std::string &key, const std::string &shellKey)
Definition XrdClEnv.cc:266
bool PutPtr(const std::string &key, void *value)
Definition XrdClEnv.cc:221
bool ImportInt(const std::string &key, const std::string &shellKey)
Definition XrdClEnv.cc:237
bool GetPtr(const std::string &key, void *&value)
Definition XrdClEnv.cc:200
bool DelString(const std::string &key)
Definition XrdClEnv.cc:89
bool GetString(const std::string &key, std::string &value)
Definition XrdClEnv.cc:31
bool GetInt(const std::string &key, int &value)
Definition XrdClEnv.cc:115
bool GetDefaultStringValue(const std::string &key, std::string &value)
Definition XrdClEnv.cc:296
Handle diagnostics.
Definition XrdClLog.hh:101
void Error(uint64_t topic, const char *format,...)
Report an error.
Definition XrdClLog.cc:231
void Info(uint64_t topic, const char *format,...)
Print an info.
Definition XrdClLog.cc:265
void Debug(uint64_t topic, const char *format,...)
Print a debug message.
Definition XrdClLog.cc:282
const uint64_t UtilityMsg
static std::unordered_map< std::string, std::string > theDefaultStrs
static std::unordered_map< std::string, int > theDefaultInts