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 <vector>
23 #include <fcntl.h>
24 #include <cstdio>
25 #include <fnmatch.h>
26 
28 {
29 //----------------------------------------------------------------------------
31 //----------------------------------------------------------------------------
32 
33 public:
34 virtual bool Decide(const std::string & lfn, XrdOss &) const
35 {
36  //m_log.Emsg("BlacklistDecide", "Deciding whether to cache file", url.c_str());
37  for (std::vector<std::string>::const_iterator it = m_blacklist.begin(); it != m_blacklist.end(); it++)
38  {
39  if (! fnmatch(it->c_str(), lfn.c_str(), FNM_PATHNAME))
40  {
41  //m_log.Emsg("BlacklistDecide", "Not caching file as it matches blacklist entry", it->c_str());
42  return false;
43  }
44  }
45  //m_log.Emsg("BlacklistDecide", "Caching file", fname);
46  return true;
47 }
48 
50  : m_log(log)
51 {}
52 
53 virtual bool ConfigDecision(const char * parms)
54 {
55  if (! parms || ! parms[0] || (strlen(parms) == 0))
56  {
57  m_log.Emsg("ConfigDecision", "Blacklist file not specified.");
58  return false;
59  }
60  m_log.Emsg("ConfigDecision", "Using blacklist", parms);
61  FILE * fp = fopen(parms, "r");
62  if (fp == 0)
63  {
64  m_log.Emsg("ConfigDecision", errno, "Failed to open blacklist:", parms);
65  return false;
66  }
67 
68  char line[4096];
69  while(fgets(line, sizeof(line), fp))
70  {
71  char *trimmed = line;
72  while (trimmed[0] && isspace(trimmed[0])) {trimmed++; }
73  if (trimmed[0] == 0) {continue; }
74  size_t filelen = strlen(trimmed);
75  if (trimmed[filelen-1] == '\n') {trimmed[filelen-1] = '\0'; }
76  m_blacklist.push_back(trimmed);
77  }
78  if (! feof(fp))
79  {
80  m_log.Emsg("ConfigDecision", errno, "Failed to parse blacklist");
81  }
82  fclose(fp);
83  for (std::vector<std::string>::const_iterator it = m_blacklist.begin(); it!=m_blacklist.end(); it++)
84  {
85  m_log.Emsg("ConfigDecision", "Cache is blacklisting paths matching", it->c_str());
86  }
87  return true;
88 }
89 
90 private:
91 std::vector<std::string> m_blacklist;
92 XrdSysError &m_log;
93 };
94 
95 /******************************************************************************/
96 /* XrdPfcGetDecision */
97 /******************************************************************************/
98 
99 // Return a decision object to use.
100 extern "C"
101 {
103 {
104  return new BlacklistDecision(err);
105 }
106 }
107 
XrdPfc::Decision * XrdPfcGetDecision(XrdSysError &err)
int fclose(FILE *stream)
#define fopen(a, b)
Definition: XrdPosix.hh:49
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