This repository has been archived by the owner on Apr 30, 2020. It is now read-only.
forked from gigablast/open-source-search-engine
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathDir.cpp
99 lines (83 loc) · 2.27 KB
/
Dir.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "Dir.h"
#include "Log.h"
#include "Errno.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fnmatch.h>
Dir::Dir ( ) {
m_dirname = NULL;
m_dir = NULL;
memset(m_dentryBuffer, 0, sizeof(m_dentryBuffer));
}
Dir::~Dir ( ) {
reset();
}
void Dir::reset ( ) {
close();
free(m_dirname);
m_dirname = NULL;
}
bool Dir::set ( const char *d1, const char *d2 ) {
reset ();
char tmp[1024];
if ( strlen(d1) + strlen(d2) + 1 > 1024 ) {
log(LOG_WARN, "disk: Could not set directory, directory name \"%s/%s\" "
"is too big.",d1,d2);
return false;
}
sprintf ( tmp , "%s/%s", d1 , d2 );
return set ( tmp );
}
bool Dir::set ( const char *dirname ) {
reset ();
m_dirname = strdup ( dirname );
if ( m_dirname ) return true;
log(LOG_WARN, "disk: Could not set directory, directory name to \"%s\": %s.",
dirname,mstrerror(g_errno));
return false;
}
void Dir::close() {
if ( m_dir )
closedir ( m_dir );
m_dir = NULL;
}
bool Dir::open ( ) {
close ( );
if ( ! m_dirname ) return false;
do {
m_dir = opendir ( m_dirname );
// interrupted system call
} while( ! m_dir && errno == EINTR );
if ( ! m_dir ) {
g_errno = errno;
log( LOG_WARN, "disk: opendir(%s) : %s", m_dirname,strerror( g_errno ) );
return false;
}
return true;
}
//Disable deprecated-declarations about use of readdir_r(). the glib maintainers deprecate readdir_r() because the linux
//version of readdir() is thread-safe. But the maintainers didn't think of other platforms, so that deprecation is
//linux-centric and not portable.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
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;
}
//Note: m_dentryBuffer has a fixed sized. The reccommended way is to
//use a dynamic size and use sysconf() to determine how large it should
//be. I just take a wild guess that no paths are longer than 1024
//characters.
struct dirent *ent;
while (readdir_r(m_dir, (dirent *)m_dentryBuffer, &ent) == 0 && ent) {
const char *filename = ent->d_name;
if (!pattern || (fnmatch(pattern, filename, FNM_PATHNAME) == 0)) {
return filename;
}
}
return NULL;
}
#pragma GCC diagnostic pop