-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdSysE2T.cc
110 lines (96 loc) · 4.12 KB
/
XrdSysE2T.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/******************************************************************************/
/* */
/* X r d S y s E 2 T . c c */
/* */
/*(c) 2019 by the Board of Trustees of the Leland Stanford, Jr., University */
/*Produced by Andrew Hanushevsky for Stanford University under contract */
/* DE-AC02-76-SFO0515 with the Deprtment of Energy */
/* */
/* This file is part of the XRootD software suite. */
/* */
/* XRootD is free software: you can redistribute it and/or modify it under */
/* the terms of the GNU Lesser General Public License as published by the */
/* Free Software Foundation, either version 3 of the License, or (at your */
/* option) any later version. */
/* */
/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
/* License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public License */
/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
/* */
/* The copyright holder's institutional names and contributor's names may not */
/* be used to endorse or promote products derived from this software without */
/* specific prior written permission of the institution or contributor. */
/******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <map>
#include <string>
#include "XrdSys/XrdSysPthread.hh"
namespace
{
static const int errSlots = 144;
XrdSysMutex e2sMutex;
std::map<int, std::string> e2sMap;
const char* Errno2String[errSlots] = {0};
int initErrTable()
{
char *eTxt, eBuff[80];
int lastGood = 0;
// Premap all known error codes.
//
for(int i = 1; i <errSlots; i++)
{eTxt = strerror(i);
if (eTxt)
{ eTxt = strdup(eTxt);
*eTxt = tolower(*eTxt);
Errno2String[i] = eTxt;
lastGood = i;
}
}
// Supply generic message for missing ones
//
for (int i = 1; i < lastGood; i++)
{if (!Errno2String[i])
{snprintf(eBuff, sizeof(eBuff), "unknown error %d", i);
Errno2String[i] = strdup(eBuff);
}
}
// Return the highest valid one
//
Errno2String[0] = "no error";
return lastGood;
}
int maxErrno = initErrTable();
}
/******************************************************************************/
/* X r d S y s E 2 T */
/******************************************************************************/
const char *XrdSysE2T(int errcode)
{
char eBuff[80];
// Check if we can return this immediately
//
if (errcode >= 0 && errcode <= maxErrno) return Errno2String[errcode];
// If this is a negative value, then return a generic message
//
if (errcode < 0) return "negative error";
// Out errrno registration wasn't sufficient, so check if it's already
// registered and if not, register it.
//
e2sMutex.Lock();
std::string &eTxt = e2sMap[errcode];
if (!eTxt.size())
{snprintf(eBuff, sizeof(eBuff), "unknown error %d", errcode);
eTxt = std::string(eBuff);
e2sMap[errcode] = eTxt;
}
// Return the result
//
e2sMutex.UnLock();
return eTxt.c_str();
}