XRootD
XrdOucFileInfo.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d O u c F i l e I n f o . c c */
4 /* */
5 /* (c) 2015 by the Board of Trustees of the Leland Stanford, Jr., University */
6 /* Produced by Andrew Hanushevsky for Stanford University under contract */
7 /* DE-AC02-76-SFO0515 with the Department of Energy */
8 /* */
9 /* This file is part of the XRootD software suite. */
10 /* */
11 /* XRootD is free software: you can redistribute it and/or modify it under */
12 /* the terms of the GNU Lesser General Public License as published by the */
13 /* Free Software Foundation, either version 3 of the License, or (at your */
14 /* option) any later version. */
15 /* */
16 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
17 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
18 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
19 /* License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public License */
22 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
23 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
24 /* */
25 /* The copyright holder's institutional names and contributor's names may not */
26 /* be used to endorse or promote products derived from this software without */
27 /* specific prior written permission of the institution or contributor. */
28 /******************************************************************************/
29 
30 #include "XrdOucFileInfo.hh"
31 
32 /******************************************************************************/
33 /* L o c a l C l a s s e s */
34 /******************************************************************************/
35 /******************************************************************************/
36 /* X r d O u c F I H a s h */
37 /******************************************************************************/
38 
40 {
41 public:
42 char *hName;
43 char *hValue;
45 
46 const char *XrdhName();
47 
48  XrdOucFIHash(const char *hn, const char *hv, XrdOucFIHash *np=0)
49  : hName(strdup(hn)), hValue(strdup(hv)), next(np) {}
50 
51  ~XrdOucFIHash() {if (hName) free(hName);
52  if (hValue) free(hValue);
53  }
54 };
55 
57 {
58  if (!strcmp(hName, "adler-32") || !strcmp(hName, "adler32")
59  || !strcmp(hName, "adler")) return "a32";
60  return hName;
61 }
62 
63 /******************************************************************************/
64 /* X r d O u c F I U r l */
65 /******************************************************************************/
66 
68 {
69 public:
70 char *fUrl;
71 int fPrty;
72 char fCC[4];
74 
75  XrdOucFIUrl(const char *url, const char *cc=0, int pri=0)
76  : fUrl(strdup(url)), fPrty(pri), next(0)
77  {if (cc) {strncpy(fCC, cc, sizeof(fCC)-1); fCC[2] = 0;}
78  else strcpy(fCC, "us");
79  }
80 
81  ~XrdOucFIUrl() {if (fUrl) free(fUrl);}
82 };
83 
84 /******************************************************************************/
85 /* D e s t r u c t o r */
86 /******************************************************************************/
87 
89 {
90  XrdOucFIHash *hdP, *hP = fHash;
91  XrdOucFIUrl *udP, *uP = fUrl;
92 
93 // Destroy the hash list
94 //
95  while((hdP = hP)) {hP = hP->next; delete hdP;}
96 
97 // Destroy the url list
98 //
99  while((udP = uP)) {uP = uP->next; delete udP;}
100 
101 // Free the memory allocated for fTargetName
102 //
103  if( fTargetName ) free(fTargetName);
104 
105 // Free memory allocated to the lfn
106 //
107  if(fLfn) free(fLfn);
108 }
109 
110 /******************************************************************************/
111 /* A d d D i g e s t */
112 /******************************************************************************/
113 
114 void XrdOucFileInfo::AddDigest(const char *hname, const char *hval)
115 {
116  int n;
117 
118 // Chain in a new digest
119 //
120  fHashNext = fHash = new XrdOucFIHash(hname, hval, fHash);
121 
122 // Now make sure the hash type is lower case
123 //
124  n = strlen(hname);
125  for (int i = 0; i < n; i++) fHash->hName[i] = tolower(fHash->hName[i]);
126 }
127 
128 /******************************************************************************/
129 /* A d d U r l */
130 /******************************************************************************/
131 
132 void XrdOucFileInfo::AddUrl(const char *url, const char *cntry,
133  int prty, bool fifo)
134 {
135  XrdOucFIUrl *urlP = new XrdOucFIUrl(url, cntry, prty);
136  XrdOucFIUrl *unP = fUrl, *upP = 0;
137 
138 // If a country code was specified, convert it to lower case
139 //
140  if (cntry)
141  {urlP->fCC[0] = tolower(cntry[0]);
142  urlP->fCC[1] = tolower(cntry[1]);
143  urlP->fCC[2] = urlP->fCC[3] = 0;
144  } else strcpy(urlP->fCC, "us");
145 
146 // Find location to insert this url
147 //
148  if (fifo)
149  {while(unP && prty >= unP->fPrty) {upP = unP; unP = unP->next;}
150  } else {
151  while(unP && prty > unP->fPrty) {upP = unP; unP = unP->next;}
152  }
153 
154 // Do the insert
155 //
156  urlP->next = unP;
157  if (upP) upP->next = urlP;
158  else fUrl = urlP;
159  if (fUrl != fUrlNext) fUrlNext = fUrl;
160 }
161 
162 /******************************************************************************/
163 /* A d d F i l e N a m e */
164 /******************************************************************************/
165 
166 void XrdOucFileInfo::AddFileName(const char * filename)
167 {
168  if(fTargetName) {free(fTargetName); fTargetName = 0;}
169 
170  if(filename)
171  fTargetName = strdup(filename);
172 }
173 
174 /******************************************************************************/
175 /* A d d L f n */
176 /******************************************************************************/
177 
178 void XrdOucFileInfo::AddLfn(const char * lfn)
179 {
180  if(fLfn) {free(fLfn); fLfn = 0;}
181 
182  if(lfn)
183  fLfn = strdup(lfn);
184 }
185 
186 /******************************************************************************/
187 /* A d d P r o t o c o l */
188 /******************************************************************************/
189 
190 void XrdOucFileInfo::AddProtocol(const char * protname)
191 {
192  if (protList.find(protname) == std::string::npos) protList.append(protname);
193 }
194 
195 /******************************************************************************/
196 /* G e t D i g e s t */
197 /******************************************************************************/
198 
199 const char *XrdOucFileInfo::GetDigest(const char *&hval, bool xrdname)
200 {
201  XrdOucFIHash *hP;
202 
203 // Check if we are at the end
204 //
205  if (!fHashNext) {fHashNext = fHash; return 0;}
206 
207 // Skip to next hash for subsequent call
208 //
209  hP = fHashNext; fHashNext = fHashNext->next;
210 
211 // Return the appropriate values
212 //
213  hval = hP->hValue;
214  return (xrdname ? hP-> XrdhName() : hP->hName);
215 }
216 
217 /******************************************************************************/
218 /* g e t U r l */
219 /******************************************************************************/
220 
221 const char *XrdOucFileInfo::GetUrl(char *cntry, int *prty)
222 {
223  XrdOucFIUrl *uP;
224 
225 // Check if we are at the end
226 //
227  if (!fUrlNext) {fUrlNext = fUrl; return 0;}
228 
229 // Skip to next url for subsequent call
230 //
231  uP = fUrlNext; fUrlNext = fUrlNext->next;
232 
233 // Return country code if wanted
234 //
235  if (cntry) strcpy(cntry, uP->fCC);
236 
237 // Return priority if wanted
238 //
239  if (prty) *prty = uP->fPrty;
240 
241 // Return the url
242 //
243  return uP->fUrl;
244 }
245 
246 /******************************************************************************/
247 /* H a s P r o t o c o l */
248 /******************************************************************************/
249 
250 bool XrdOucFileInfo::HasProtocol(const char * protname)
251 {
252  return (protList.find(protname) != std::string::npos);
253 }
XrdOucFIHash * next
const char * XrdhName()
XrdOucFIHash(const char *hn, const char *hv, XrdOucFIHash *np=0)
XrdOucFIUrl * next
XrdOucFIUrl(const char *url, const char *cc=0, int pri=0)
~XrdOucFileInfo()
Destructor.
bool HasProtocol(const char *protname)
void AddFileName(const char *filename)
void AddProtocol(const char *protname)
void AddLfn(const char *lfn)
void AddUrl(const char *url, const char *cntry=0, int prty=0, bool fifo=true)
void AddDigest(const char *hname, const char *hval)
const char * GetUrl(char *cntry=0, int *prty=0)
const char * GetDigest(const char *&hval, bool xrdname=true)