Use fnmatch in Dir::getNextFilename for pattern matching instead of checking for wildcard character

This commit is contained in:
Ai Lin Chia 2017-10-13 14:32:37 +02:00
parent 2bec1728ea
commit 57723624a5
3 changed files with 9 additions and 53 deletions

32
Dir.cpp

@ -7,6 +7,7 @@
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fnmatch.h>
Dir::Dir ( ) {
m_dirname = NULL;
@ -69,10 +70,9 @@ bool Dir::open ( ) {
return true;
}
const char *Dir::getNextFilename ( const char *pattern ) {
if ( ! m_dir ) {
log("dir: m_dir is NULL so can't find pattern %s",pattern);
const char *Dir::getNextFilename(const char *pattern) {
if (!m_dir) {
log("dir: m_dir is NULL so can't find pattern %s", pattern);
return NULL;
}
@ -81,27 +81,11 @@ const char *Dir::getNextFilename ( const char *pattern ) {
//be. I just take a wild guess that no paths are longer than 1024
//characters.
struct dirent *ent;
int32_t plen = pattern ? strlen(pattern) : 0;
while( readdir_r(m_dir,(dirent*)m_dentryBuffer,&ent)==0 && ent ) {
while (readdir_r(m_dir, (dirent *)m_dentryBuffer, &ent) == 0 && ent) {
const char *filename = ent->d_name;
if ( ! pattern ) return filename;
if ( plen>2 && pattern[0] == '*' && pattern[plen-1] == '*' ) {
char tmp[128];
memcpy(tmp,pattern+1,plen-2);
tmp[plen-2] = '\0';
if ( strstr ( filename, tmp ) )
return filename;
}
if ( pattern[0] == '*' ) {
if ( strlen(filename) < strlen(pattern + 1) ) continue;
const char *tail = filename +
strlen ( filename ) -
strlen ( pattern ) + 1;
if ( strcmp ( tail , pattern+1) == 0 ) return filename;
}
if ( pattern[plen-1]=='*' ) {
if ( strncmp ( filename , pattern , plen - 1 ) == 0 )
return filename;
if (!pattern || (fnmatch(pattern, filename, FNM_PATHNAME) == 0)) {
return filename;
}
}

@ -47,7 +47,6 @@ Hostdb::Hostdb ( ) {
m_created = false;
m_myHost = NULL;
m_myIp = 0;
m_myIpShotgun = 0;
m_myPort = 0;
m_myHost = NULL;
m_myShard = NULL;
@ -106,7 +105,6 @@ void Hostdb::reset ( ) {
bool Hostdb::init(int32_t hostIdArg, bool proxyHost, bool useTmpCluster, const char *cwd) {
// reset my ip and port
m_myIp = 0;
m_myIpShotgun = 0;
m_myPort = 0;
m_myHost = NULL;
//m_myPort2 = 0;
@ -669,9 +667,6 @@ createFile:
m_hosts[i].m_mergeDir[mdirlen] = '\0';
memcpy(m_hosts[i].m_mergeLockDir, ldir, ldirlen);
m_hosts[i].m_mergeLockDir[ldirlen] = '\0';
// and don't send emails on him until we got a good ping
m_hosts[i].m_emailCode = -2;
m_hosts[i].m_lastResponseReceiveTimestamp = 0;
m_hosts[i].m_lastRequestSendTimestamp = 0;
@ -834,7 +829,6 @@ createFile:
return false;
}
m_myIp = host->m_ip; // internal IP
m_myIpShotgun = host->m_ipShotgun;
m_myPort = host->m_port; // low priority udp port
m_myHost = host;
@ -931,8 +925,6 @@ bool Hostdb::hashHosts ( ) {
// this also holds g_hosts2 as well as g_hosts so we cannot preallocate
for ( int32_t i = 0 ; i < m_numHosts ; i++ ) {
Host *h = &m_hosts[i];
// init shotgun bit here, 0 or 1 depending on our hostId
h->m_shotgunBit = m_myHostId & 0x01;
int32_t ip;
ip = h->m_ip;
if ( ! hashHost ( 1,h,ip, h->m_port )) return false;
@ -970,8 +962,6 @@ bool Hostdb::hashHosts ( ) {
// and the proxies as well
for ( int32_t i = 0 ; i < m_numProxyHosts ; i++ ) {
Host *h = getProxy(i);
// init shotgun bit here, 0 or 1 depending on our hostId
h->m_shotgunBit = m_myHostId & 0x01;
int32_t ip;
ip = h->m_ip;
if ( ! hashHost ( 1,h,ip, h->m_port )) return false;
@ -1360,13 +1350,11 @@ bool Hostdb::replaceHost ( int32_t origHostId, int32_t spareHostId ) {
// reset these stats
oldHost->m_runtimeInformation.m_totalDocsIndexed = 0;
oldHost->m_emailCode = 0;
oldHost->m_errorReplies = 0;
oldHost->m_dgramsTo = 0;
oldHost->m_dgramsFrom = 0;
oldHost->m_totalResends = 0;
oldHost->m_etryagains = 0;
oldHost->m_repairMode = 0;
oldHost->m_splitsDone = 0;
oldHost->m_splitTimes = 0;

@ -83,17 +83,6 @@ public:
// send to eth 0 or 1 when sending to this host?
char m_preferEth;
// . this is used for sending email alerts to admin about dead hosts
// . 0 means we can send an email for this host if he goes dead on us
// . +1 means we already sent an email for him since he was down and
// he hasn't come back up since
// . -1 means he went down gracefully so no alert is needed
// . -2 means he went down while g_conf.m_sendEmailAlert was false
// . -3 means another host was responsible for sending to him, not us
// . -4 means we are currently in progress sending an email for him
// . -5 means he went down before host we alerted admin about revived
int32_t m_emailCode;
// we now include the working dir in the hosts.conf file
// so main.cpp can do gb --install and gb --allstart
char m_dir[256];
@ -111,9 +100,7 @@ public:
// was host in gk0 cluster and retired because its twin got
// ssds, so it was no longer really needed.
bool m_retired;
// this toggles between 0 and 1 for alternating packet sends to
// eth0 and eth1 of this host
char m_shotgunBit;
// how many total error replies we got from this host
int32_t m_errorReplies;
@ -144,8 +131,6 @@ public:
std::atomic<int32_t> m_totalResends; //how many UDP packets has been resent
std::atomic<int32_t> m_etryagains; //how many times a request got an ETRYAGAIN
char m_repairMode;
// for timing how long the msg39 takes from this host
int32_t m_splitsDone;
int64_t m_splitTimes;
@ -332,7 +317,6 @@ class Hostdb {
// our host's info used by Udp* classes for internal communication
uint32_t m_myIp;
uint32_t m_myIpShotgun;
uint16_t m_myPort;
Host *m_myHost;
Host *m_myShard;