XRootD
XrdPfcBlacklistDecision.cc
Go to the documentation of this file.
1 //----------------------------------------------------------------------------------
2 // Copyright (c) 2015 by Board of Trustees of the Leland Stanford, Jr., University
3 // Author: Alja Mrak-Tadel, Matevz Tadel, Brian Bockelman
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 "XrdPfcDecision.hh"
20 #include "XrdSys/XrdSysError.hh"
21 
22 #include <cerrno>
23 #include <cstdio>
24 #include <vector>
25 
26 #include <fcntl.h>
27 #include <fnmatch.h>
28 
30 {
31 //----------------------------------------------------------------------------
33 //----------------------------------------------------------------------------
34 
35 public:
36 virtual bool Decide(const std::string & lfn, XrdOss &) const
37 {
38  //m_log.Emsg("BlacklistDecide", "Deciding whether to cache file", url.c_str());
39  for (std::vector<std::string>::const_iterator it = m_blacklist.begin(); it != m_blacklist.end(); it++)
40  {
41  if (! fnmatch(it->c_str(), lfn.c_str(), FNM_PATHNAME))
42  {
43  //m_log.Emsg("BlacklistDecide", "Not caching file as it matches blacklist entry", it->c_str());
44  return false;
45  }
46  }
47  //m_log.Emsg("BlacklistDecide", "Caching file", fname);
48  return true;
49 }
50 
52  : m_log(log)
53 {}
54 
55 virtual bool ConfigDecision(const char * parms)
56 {
57  if (! parms || ! parms[0] || (strlen(parms) == 0))
58  {
59  m_log.Emsg("ConfigDecision", "Blacklist file not specified.");
60  return false;
61  }
62  m_log.Emsg("ConfigDecision", "Using blacklist", parms);
63  FILE * fp = fopen(parms, "r");
64  if (fp == 0)
65  {
66  m_log.Emsg("ConfigDecision", errno, "Failed to open blacklist:", parms);
67  return false;
68  }
69 
70  char line[4096];
71  while(fgets(line, sizeof(line), fp))
72  {
73  char *trimmed = line;
74  while (trimmed[0] && isspace(trimmed[0])) {trimmed++; }
75  if (trimmed[0] == 0) {continue; }
76  size_t filelen = strlen(trimmed);
77  if (trimmed[filelen-1] == '\n') {trimmed[filelen-1] = '\0'; }
78  m_blacklist.push_back(trimmed);
79  }
80  if (! feof(fp))
81  {
82  m_log.Emsg("ConfigDecision", errno, "Failed to parse blacklist");
83  }
84  fclose(fp);
85  for (std::vector<std::string>::const_iterator it = m_blacklist.begin(); it!=m_blacklist.end(); it++)
86  {
87  m_log.Emsg("ConfigDecision", "Cache is blacklisting paths matching", it->c_str());
88  }
89  return true;
90 }
91 
92 private:
93 std::vector<std::string> m_blacklist;
94 XrdSysError &m_log;
95 };
96 
97 /******************************************************************************/
98 /* XrdPfcGetDecision */
99 /******************************************************************************/
100 
101 // Return a decision object to use.
102 extern "C"
103 {
105 {
106  return new BlacklistDecision(err);
107 }
108 }
109 
XrdPfc::Decision * XrdPfcGetDecision(XrdSysError &err)
int fclose(FILE *stream)
#define fopen(a, b)
Definition: XrdPosix.hh:54
virtual bool ConfigDecision(const char *parms)
BlacklistDecision(XrdSysError &log)
virtual bool Decide(const std::string &lfn, XrdOss &) const
A decision library that allows all files to be cached except for a blacklist.
Base class for selecting which files should be cached.
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
Definition: XrdSysError.cc:95