Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set( CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake )

find_package( Xrootd REQUIRED )
find_package( XercesC REQUIRED )
find_package( Pcre REQUIRED )
find_package( Pcre2 REQUIRED )

if( CMAKE_COMPILER_IS_GNUCXX )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror" )
Expand All @@ -17,10 +17,10 @@ if( CMAKE_COMPILER_IS_GNUCC )
endif()

include_directories( "${PROJECT_SOURCE_DIR}" "${XROOTD_INCLUDES}"
"${XERCES_INCLUDES}" "${PCRE_INCLUDES}" )
"${XERCES_INCLUDES}" "${PCRE2_INCLUDES}" )

add_library( XrdCmsTfc MODULE src/XrdCmsTfc.cc src/XrdCmsTfc.hh )
target_link_libraries(XrdCmsTfc ${XROOTD_UTILS} ${XERCES_LIB} ${PCRE_LIB})
target_link_libraries(XrdCmsTfc ${XROOTD_UTILS} ${XERCES_LIB} ${PCRE2_LIB})

if (NOT DEFINED CMAKE_INSTALL_LIBDIR)
SET(CMAKE_INSTALL_LIBDIR "lib")
Expand Down
20 changes: 0 additions & 20 deletions cmake/FindPcre.cmake

This file was deleted.

18 changes: 18 additions & 0 deletions cmake/FindPcre2.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FIND_PATH(PCRE2_INCLUDES pcre2.h
HINTS
${PCRE2_INCLUDE_DIR}
$ENV{PCRE2_INCLUDE_DIR}
/usr
PATH_SUFFIXES include
)

FIND_LIBRARY(PCRE2_LIB pcre2-8
HINTS
${PCRE2_LIB_DIR}
$ENV{PCRE2_LIB_DIR}
/usr
PATH_SUFFIXES lib
)

INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Pcre2 DEFAULT_MSG PCRE2_LIB PCRE2_INCLUDES)
61 changes: 36 additions & 25 deletions src/XrdCmsTfc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ void XrdCmsTfc::TrivialFileCatalog::freeProtocolRules(ProtocolRules protRules) {
Rules rules = (*it).second;
for (it2 = rules.begin(); it2 != rules.end(); it2++) {
if (it2->pathMatch != NULL)
pcre_free(it2->pathMatch);
pcre2_code_free(it2->pathMatch);
if (it2->destinationMatch != NULL)
pcre2_code_free(it2->destinationMatch);
}
}
}
Expand Down Expand Up @@ -138,25 +140,25 @@ XrdCmsTfc::TrivialFileCatalog::parseRule (DOMNode *ruleNode,
= _toChar (ruleElement->getAttribute (_toDOMS ("chain")));

Rule rule;
const char *error;
int erroffset;
int errornumber;
PCRE2_SIZE erroroffset;
rule.pathMatch = NULL;
rule.pathMatchStr = pathMatchRegexp;
rule.destinationMatch = NULL;
rule.destinationMatchStr = destinationMatchRegexp;
rule.pathMatch = pcre_compile(pathMatchRegexp, 0, &error, &erroffset, NULL);
rule.pathMatch = pcre2_compile((PCRE2_SPTR)pathMatchRegexp, PCRE2_ZERO_TERMINATED, 0, &errornumber, &erroroffset, NULL);
if (rule.pathMatch == NULL) {
char *err = (char *)malloc(BUFFSIZE*sizeof(char));
snprintf(err, BUFFSIZE, "PCRE compilation failed at offset %d: %s", erroffset, error);
snprintf(err, BUFFSIZE, "PCRE2 compilation failed at offset %lu: %d", (unsigned long)erroroffset, errornumber);
eDest->Say(err);
free(err);
return XRDCMSTFC_ERR_PARSERULE;
}

rule.destinationMatch = pcre_compile(destinationMatchRegexp, 0, &error, &erroffset, NULL);
rule.destinationMatch = pcre2_compile((PCRE2_SPTR)destinationMatchRegexp, PCRE2_ZERO_TERMINATED, 0, &errornumber, &erroroffset, NULL);
if (rule.destinationMatch == NULL) {
char *err = (char *)malloc(BUFFSIZE*sizeof(char));
snprintf(err, BUFFSIZE, "PCRE compilation failed at offset %d: %s", erroffset, error);
snprintf(err, BUFFSIZE, "PCRE2 compilation failed at offset %lu: %d", (unsigned long)erroroffset, errornumber);
eDest->Say(err);
free(err);
return XRDCMSTFC_ERR_PARSERULE;
Expand Down Expand Up @@ -328,37 +330,39 @@ int XrdCmsTfc::TrivialFileCatalog::lfn2rfn(const char *lfn, char *buff, int blen

}

std::string replace(const std::string inputString, pcre * re, std::string replacementString) {
std::string replace(const std::string inputString, pcre2_code * re, std::string replacementString) {

int ovector[OVECCOUNT], rc;
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(re, NULL);
std::string result;
rc = pcre_exec(re, NULL, inputString.c_str(), inputString.length(), 0, 0,
ovector, OVECCOUNT);
int rc = pcre2_match(re, (PCRE2_SPTR)inputString.c_str(), inputString.length(), 0, 0, match_data, NULL);

if (rc <= 0) {
pcre2_match_data_free(match_data);
return "";
}

PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
int substring_end = 0, substring_begin;
for (int i = 0; i < rc; i++) {
substring_begin = ovector[2*i];
result += inputString.substr(substring_end, substring_begin-substring_end) + replacementString;
substring_end = ovector[2*i+1] + substring_begin;
}

pcre2_match_data_free(match_data);
return result;
}

std::string replaceWithRegexp (const int ovector[OVECCOUNT], const int rc,
std::string replaceWithRegexp (const PCRE2_SIZE ovector[], const int rc,
const std::string inputString,
const std::string outputFormat)
{
//std::cerr << "InputString:" << inputString << std::endl;

char buffer[11];
std::string result = outputFormat;
int substring_length;
int substring_begin;
PCRE2_SIZE substring_length;
PCRE2_SIZE substring_begin;

for (int i = 1;
i < rc;
Expand All @@ -373,19 +377,19 @@ std::string replaceWithRegexp (const int ovector[OVECCOUNT], const int rc,
substring_length = ovector[2*i+1] - substring_begin;
std::string matchResult = inputString.substr(substring_begin, substring_length);

const char *error;
int erroffset;
pcre * substitutionToken = pcre_compile(variableRegexp.c_str(),
0, &error, &erroffset, NULL);
int errornumber;
PCRE2_SIZE erroroffset;
pcre2_code * substitutionToken = pcre2_compile((PCRE2_SPTR)variableRegexp.c_str(),
PCRE2_ZERO_TERMINATED, 0, &errornumber, &erroroffset, NULL);
if (substitutionToken == NULL) {
pcre_free(substitutionToken);
pcre2_code_free(substitutionToken);
return "";
}
//std::cerr << "Current match: " << matchResult << std::endl;

result = replace(result, substitutionToken, matchResult);

pcre_free(substitutionToken);
pcre2_code_free(substitutionToken);
}
return result;
}
Expand All @@ -411,17 +415,21 @@ XrdCmsTfc::TrivialFileCatalog::applyRules (const ProtocolRules& protocolRules,
i != rules.end ();
i++)
{
int ovector[OVECCOUNT];
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(i->destinationMatch, NULL);
int rc=0;
rc = pcre_exec(i->destinationMatch, NULL, destination.c_str(), destination.length(), 0, PCRE_ANCHORED, ovector, OVECCOUNT);
rc = pcre2_match(i->destinationMatch, (PCRE2_SPTR)destination.c_str(), destination.length(), 0, PCRE2_ANCHORED, match_data, NULL);
if (rc < 0) {
pcre2_match_data_free(match_data);
continue;
} else {
//std::cerr << "Destination did match; my destination " << destination << ", regexp: " << i->destinationMatchStr << std::endl;
}

rc = pcre_exec(i->pathMatch, NULL, name.c_str(), name.length(), 0, PCRE_ANCHORED, ovector, OVECCOUNT);
pcre2_match_data_free(match_data);
match_data = pcre2_match_data_create_from_pattern(i->pathMatch, NULL);
rc = pcre2_match(i->pathMatch, (PCRE2_SPTR)name.c_str(), name.length(), 0, PCRE2_ANCHORED, match_data, NULL);
if (rc < 0) {
pcre2_match_data_free(match_data);
continue;
} else {
//std::cerr << "Path did match; my path " << name << ", regexp: " << i->pathMatchStr << std::endl;
Expand All @@ -438,10 +446,12 @@ XrdCmsTfc::TrivialFileCatalog::applyRules (const ProtocolRules& protocolRules,
//std::cerr << "Not chaining another rule!" << std::endl;
}

rc = pcre_exec(i->pathMatch, NULL, name.c_str(), name.length(), 0, PCRE_ANCHORED, ovector,
OVECCOUNT);
pcre2_match_data_free(match_data);
match_data = pcre2_match_data_create_from_pattern(i->pathMatch, NULL);
rc = pcre2_match(i->pathMatch, (PCRE2_SPTR)name.c_str(), name.length(), 0, PCRE2_ANCHORED, match_data, NULL);

if (rc >= 0) {
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
name = replaceWithRegexp(ovector, rc, name, i->result);
} else {
//std::cerr << "No replacements necessary." << std::endl;
Expand All @@ -453,6 +463,7 @@ XrdCmsTfc::TrivialFileCatalog::applyRules (const ProtocolRules& protocolRules,
applyRules (protocolRules, chain, destination, direct, name);
}
//std::cerr << "Result " << name << std::endl;
pcre2_match_data_free(match_data);
return name;
}
return "";
Expand Down
7 changes: 4 additions & 3 deletions src/XrdCmsTfc.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#include "XrdOuc/XrdOucName2Name.hh"
#include "XrdSys/XrdSysError.hh"

#include <pcre.h>
#define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.h>

#include <list>
#include <map>
Expand Down Expand Up @@ -48,9 +49,9 @@ public:
private:

typedef struct {
pcre *pathMatch;
pcre2_code *pathMatch;
std::string pathMatchStr;
pcre *destinationMatch;
pcre2_code *destinationMatch;
std::string destinationMatchStr;
std::string result;
std::string chain;
Expand Down