XRootD
XrdCksAssist.cc File Reference
#include <cctype>
#include <cerrno>
#include <string>
#include <cstring>
#include <strings.h>
#include <ctime>
#include <vector>
#include <sys/types.h>
#include "XrdCks/XrdCksData.hh"
+ Include dependency graph for XrdCksAssist.cc:

Go to the source code of this file.

Functions

std::vector< char > XrdCksAttrData (const char *cstype, const char *csval, time_t mtime)
 
std::string XrdCksAttrName (const char *cstype, const char *nspfx)
 
std::string XrdCksAttrValue (const char *cstype, const char *csbuff, int csblen)
 

Function Documentation

◆ XrdCksAttrData()

std::vector<char> XrdCksAttrData ( const char *  cstype,
const char *  csval,
time_t  mtime 
)

This header file defines linkages to various XRootD checksum assistants. The functions described here are located in libXrdUtils.so. Generate the extended attribute data for a particular checksum that can be used to set the corresponding checksum attribute variable.

Parameters
cstypeA null terminated string holding the checksum type (e.g. "adler32", "md5", "sha2", etc).
csvalA null terminated string holding the corresonding checksum value. This must be an ASCII hex representation of the value and must be of appropriate length.
mtimeThe subject file's modification time.
Returns
A vector of bytes that should be usedto set the attribute variable. If the size of the vector is zero, then the supplied parameters were incorrect and valid data cannot be generated; errno is: EINVAL - csval length is incorrect for checksum type or contains a non-hex digit. ENAMETOOLONG - checksum type is too long. EOVERFLOW - csval could not be represented in the data.

Definition at line 84 of file XrdCksAssist.cc.

86 {
87  std::vector<char> cksError; // Null vector
88  XrdCksData cksData;
89  char csName[XrdCksData::NameSize];
90  int n = strlen(cstype);
91 
92 // Convert the checksum type to lower case
93 //
94  if (!LowerCase(cstype, csName, sizeof(csName)))
95  {errno = ENAMETOOLONG; return cksError;}
96 
97 // For cheksums we know, verify that the legnth of the input string
98 // corresponds to the checksum type.
99 //
100  n = strlen(csval);
101  for (int i = 0; i < csNum; i++)
102  {if (!strcmp(csTab[i].csName, csName) && csTab[i].csLenC != n)
103  {errno = EINVAL; return cksError;}
104  }
105 
106 // we simply fill out the cksdata structure with the provided information
107 //
108  if (!cksData.Set(csName)) {errno = ENAMETOOLONG; return cksError;}
109  if (!cksData.Set(csval, n)) {errno = EOVERFLOW; return cksError;}
110  cksData.fmTime = mtime;
111  cksData.csTime = time(0) - mtime;
112 
113 // Convert the checksum data to a string of bytes and return the vector
114 //
115  return std::vector<char>( (char *)&cksData,
116  ((char *)&cksData) + sizeof(cksData));
117 }
int Set(const char *csName)
Definition: XrdCksData.hh:81
static const int NameSize
Definition: XrdCksData.hh:41

References XrdCksData::csTime, XrdCksData::NameSize, and XrdCksData::Set().

+ Here is the call graph for this function:

◆ XrdCksAttrName()

std::string XrdCksAttrName ( const char *  cstype,
const char *  nspfx = "" 
)

Generate the extended attribute variable name for a particular checksum.

Parameters
cstypeA null terminated string holding the checksum type (e.g. "adler32", "md5", "sha2", etc).
nspfxIs the namespace prefix to add to the variable name. By default no prefix os used. Certain platforms and/or filesystems require that user attributes start with a particular prefix (e.g. Linux requires 'user.') others do not. If your are going to use the variable name to get or set an attribute you should specify any required prefix. If specified and it does not end with a dot, a dot is automatically added to the nspfx.
Returns
A string holding the variable name that should be used to get or set the extended attribute holding the correspnding checksum. If a null string is returned, the variable could not be generated; errno is set to: ENAMETOOLONG - checksum type is too long.

Definition at line 125 of file XrdCksAssist.cc.

126 {
127  std::string xaName;
128  char csName[XrdCksData::NameSize];
129  int pfxlen = strlen(nspfx);
130 
131 // Do some easy checks for this we know are common
132 //
133  if (!pfxlen)
134  {if (!strcmp(cstype, "adler32")) return std::string("XrdCks.adler32");
135  if (!strcmp(cstype, "md5" )) return std::string("XrdCks.md5");
136  if (!strcmp(cstype, "crc32" )) return std::string("XrdCks.crc32");
137  }
138 
139 // Convert the checksum type to lower case
140 //
141  if (!LowerCase(cstype, csName, sizeof(csName)))
142  {errno = ENAMETOOLONG; return xaName;}
143 
144 // Reserve storage for the string and construct the variable name
145 //
146  xaName.reserve(strlen(nspfx) + strlen(cstype) + 8);
147  if (pfxlen)
148  {xaName = nspfx;
149  if (nspfx[pfxlen-1] != '.') xaName += '.';
150  }
151  xaName += "XrdCks.";
152  xaName += csName;
153 
154 // Return the variable name
155 //
156  return xaName;
157 }

References XrdCksData::NameSize.

◆ XrdCksAttrValue()

std::string XrdCksAttrValue ( const char *  cstype,
const char *  csbuff,
int  csblen 
)

Extract th checksum value from checksum extended attribute data.

Parameters
cstypeA null terminated string holding the checksum type (e.g. "adler32", "md5", "sha2", etc).
csbuffA pointer to a buffer hlding the checksum data.
csblenThe length of the checksum data (i.e. the length of the retrieved extended attribute).
Returns
A string holding the ASCII hexstring correspoding to the checksum value. If a null string is returned then the checksum data was invalid or did not correspond to the specified checksum type, the errno is set to: EINVAL - the checksum length in csbuff is incorrect. EMSGSIZE - csblen was not the expected value. ENOENT - the specified cstype did not match the one in csbuff. EOVERFLOW - checksum value could not be generated from csbuff.

Definition at line 163 of file XrdCksAssist.cc.

165 {
166  XrdCksData cksData;
167  std::string csError;
168  char csBuff[XrdCksData::ValuSize*2+1];
169 
170 // Verify that the length matches our object length
171 //
172  if (csblen != (int)sizeof(cksData)) {errno = EMSGSIZE; return csError;}
173 
174 // Copy the buffer into the object
175 //
176  memcpy(&cksData, csbuff, sizeof(cksData));
177 
178 // Now verify that all the fields are consistent
179 //
180  if (strncasecmp(cksData.Name, cstype, XrdCksData::NameSize))
181  {errno = ENOENT; return csError;}
182  if (cksData.Length <= 0 || cksData.Length > XrdCksData::ValuSize)
183  {errno = EINVAL; return csError;}
184 
185 // For known checksum values make sure the length matches
186 //
187  for (int i = 0; i < csNum; i++)
188  {if (!strcmp(csTab[i].csName, cstype)
189  && csTab[i].csLenB != int(cksData.Length))
190  {errno = EINVAL; return csError;}
191  }
192 
193 // Convert value to a hex string
194 //
195  if (!cksData.Get(csBuff, sizeof(csBuff)))
196  {errno = EOVERFLOW; return csError;}
197 
198 // Return string version of the hex string
199 //
200  return std::string(csBuff);
201 }
static const int ValuSize
Definition: XrdCksData.hh:42
int Get(char *Buff, int Blen)
Definition: XrdCksData.hh:69
char Length
Definition: XrdCksData.hh:52
char Name[NameSize]
Definition: XrdCksData.hh:44

References XrdCksData::Get(), XrdCksData::Length, XrdCksData::Name, XrdCksData::NameSize, and XrdCksData::ValuSize.

+ Here is the call graph for this function: