XRootD
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"
22 #include "XrdCl/XrdClDefaultEnv.hh"
23 #include "XrdCl/XrdClLog.hh"
24 #include "XrdCl/XrdClConstants.hh"
25 
26 namespace 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  // Get int
88  //----------------------------------------------------------------------------
89  bool Env::GetInt( const std::string &k, int &value )
90  {
91  std::string key = UnifyKey( k );
92  XrdSysRWLockHelper scopedLock( pLock );
93  IntMap::iterator it;
94  it = pIntMap.find( key );
95  if( it == pIntMap.end() )
96  {
97  Log *log = DefaultEnv::GetLog();
98  log->Debug( UtilityMsg,
99  "Env: trying to get a non-existent integer entry: %s",
100  key.c_str() );
101  return false;
102  }
103  value = it->second.first;
104  return true;
105  }
106 
107  //----------------------------------------------------------------------------
108  // Put int
109  //----------------------------------------------------------------------------
110  bool Env::PutInt( const std::string &k, int value )
111  {
112  std::string key = UnifyKey( k );
113  XrdSysRWLockHelper scopedLock( pLock, false );
114 
115  //--------------------------------------------------------------------------
116  // Insert the string if it's not there yet
117  //--------------------------------------------------------------------------
118  IntMap::iterator it;
119  it = pIntMap.find( key );
120  if( it == pIntMap.end() )
121  {
122  pIntMap[key] = std::make_pair( value, false );
123  return true;
124  }
125 
126  //--------------------------------------------------------------------------
127  // The entry exists and it has been imported from the shell
128  //--------------------------------------------------------------------------
129  Log *log = DefaultEnv::GetLog();
130  if( it->second.second )
131  {
132  log->Debug( UtilityMsg,
133  "Env: trying to override a shell-imported integer entry: %s",
134  key.c_str() );
135  return false;
136  }
137  log->Debug( UtilityMsg,
138  "Env: overriding entry: %s=%d with %d",
139  key.c_str(), it->second.first, value );
140 
141  pIntMap[key] = std::make_pair( value, false );
142  return true;
143  }
144 
145  //----------------------------------------------------------------------------
146  // Get pointer
147  //----------------------------------------------------------------------------
148  bool Env::GetPtr( const std::string &k, void* &value )
149  {
150  std::string key = UnifyKey( k );
151  XrdSysRWLockHelper scopedLock( pLock );
152  PtrMap::iterator it;
153  it = pPtrMap.find( key );
154  if( it == pPtrMap.end() )
155  {
156  Log *log = DefaultEnv::GetLog();
157  log->Debug( UtilityMsg,
158  "Env: trying to get a non-existent pointer entry: %s",
159  key.c_str() );
160  return false;
161  }
162  value = it->second;
163  return true;
164  }
165 
166  //----------------------------------------------------------------------------
167  // Put pointer
168  //----------------------------------------------------------------------------
169  bool Env::PutPtr( const std::string &k, void* value )
170  {
171  std::string key = UnifyKey( k );
172  XrdSysRWLockHelper scopedLock( pLock, false );
173 
174  // Pointers cannot be imported from shell environment, we always set it.
175  bool ret = pPtrMap.find(key) == pPtrMap.end();
176 
177  pPtrMap[key] = value;
178 
179  return ret;
180  }
181 
182  //----------------------------------------------------------------------------
183  // Import int
184  //----------------------------------------------------------------------------
185  bool Env::ImportInt( const std::string &k, const std::string &shellKey )
186  {
187  std::string key = UnifyKey( k );
188  XrdSysRWLockHelper scopedLock( pLock, false );
189  std::string strValue = GetEnv( shellKey );
190  if( strValue == "" )
191  return false;
192 
193  Log *log = DefaultEnv::GetLog();
194  char *endPtr;
195  int value = (int)strtol( strValue.c_str(), &endPtr, 0 );
196  if( *endPtr )
197  {
198  log->Error( UtilityMsg,
199  "Env: Unable to import %s as %s: %s is not a proper integer",
200  shellKey.c_str(), key.c_str(), strValue.c_str() );
201  return false;
202  }
203 
204  log->Info( UtilityMsg, "Env: Importing from shell %s=%d as %s",
205  shellKey.c_str(), value, key.c_str() );
206 
207  pIntMap[key] = std::make_pair( value, true );
208  return true;
209  }
210 
211  //----------------------------------------------------------------------------
212  // Import string
213  //----------------------------------------------------------------------------
214  bool Env::ImportString( const std::string &k, const std::string &shellKey )
215  {
216  std::string key = UnifyKey( k );
217  XrdSysRWLockHelper scopedLock( pLock, false );
218  std::string value = GetEnv( shellKey );
219  if( value == "" )
220  return false;
221 
222  Log *log = DefaultEnv::GetLog();
223  log->Info( UtilityMsg, "Env: Importing from shell %s=%s as %s",
224  shellKey.c_str(), value.c_str(), key.c_str() );
225  pStringMap[key] = std::make_pair( value, true );
226  return true;
227  }
228 
229  //------------------------------------------------------------------------
230  // Get default integer value for the given key
231  //------------------------------------------------------------------------
232  bool Env::GetDefaultIntValue( const std::string &k, int &value )
233  {
234  std::string key = UnifyKey( k );
235  auto itr = theDefaultInts.find( key );
236  if( itr == theDefaultInts.end() ) return false;
237  value = itr->second;
238  return true;
239  }
240 
241  //------------------------------------------------------------------------
242  // Get default string value for the given key
243  //------------------------------------------------------------------------
244  bool Env::GetDefaultStringValue( const std::string &k, std::string &value )
245  {
246  std::string key = UnifyKey( k );
247  auto itr = theDefaultStrs.find( key );
248  if( itr == theDefaultStrs.end() ) return false;
249  value = itr->second;
250  return true;
251  }
252 
253  //----------------------------------------------------------------------------
254  // Get a string from the environment
255  //----------------------------------------------------------------------------
256  std::string Env::GetEnv( const std::string &key )
257  {
258  char *var = getenv( key.c_str() );
259  if( !var )
260  return "";
261  return var;
262  }
263 }
static Log * GetLog()
Get default log.
bool PutInt(const std::string &key, int value)
Definition: XrdClEnv.cc:110
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:232
bool ImportString(const std::string &key, const std::string &shellKey)
Definition: XrdClEnv.cc:214
bool PutPtr(const std::string &key, void *value)
Definition: XrdClEnv.cc:169
bool ImportInt(const std::string &key, const std::string &shellKey)
Definition: XrdClEnv.cc:185
bool GetPtr(const std::string &key, void *&value)
Definition: XrdClEnv.cc:148
bool GetString(const std::string &key, std::string &value)
Definition: XrdClEnv.cc:31
bool GetInt(const std::string &key, int &value)
Definition: XrdClEnv.cc:89
bool GetDefaultStringValue(const std::string &key, std::string &value)
Definition: XrdClEnv.cc:244
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