forked from gigablast/open-source-search-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dir.cpp
211 lines (179 loc) · 4.7 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "gb-include.h"
#include "Dir.h"
Dir::Dir ( ) {
m_dirname = NULL;
m_dir = NULL;
m_needsClose = false;
}
Dir::~Dir ( ) {
reset();
}
void Dir::reset ( ) {
close();
if ( m_dirname ) free ( m_dirname );
m_dirname = NULL;
}
bool Dir::set ( char *d1 , char *d2 ) {
reset ();
char tmp[1024];
if ( gbstrlen(d1) + gbstrlen(d2) + 1 > 1024 ) {
log("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 ( char *dirname ) {
reset ();
m_dirname = strdup ( dirname );
if ( m_dirname ) return true;
log("disk: Could not set directory, directory name to \"%s\": %s.",
dirname,mstrerror(g_errno));
return false;
}
bool Dir::close ( ) {
if ( m_dir && m_needsClose ) closedir ( m_dir );
m_needsClose = false;
return true;
}
bool Dir::open ( ) {
close ( );
if ( ! m_dirname ) return false;
retry8:
// opendir() calls malloc
g_inMemFunction = true;
m_dir = opendir ( m_dirname );
g_inMemFunction = false;
// interrupted system call
if ( ! m_dir && errno == EINTR ) goto retry8;
if ( ! m_dir )
g_errno = errno;
if ( ! m_dir )
return log("disk: opendir(%s) : %s",
m_dirname,strerror( g_errno ) );
m_needsClose = true;
return true;
}
// remove all files in m_dirname
bool Dir::cleanOut ( ) {
char buf[1024];
sprintf ( buf , "rm -r %s/*", m_dirname );
gbsystem ( buf );
return true;
}
// create m_dirname
bool Dir::create ( ) {
char buf[1024];
sprintf ( buf , "mkdir %s", m_dirname );
gbsystem ( buf );
return true;
}
int Dir::getNumFiles ( char *pattern ) {
int count = 0;
while ( getNextFilename ( pattern ) ) count++;
return count;
}
// rewind to get the first filename
void Dir::rewind ( ) {
rewinddir ( m_dir );
}
char *Dir::getNextFilename ( char *pattern ) {
if ( ! m_dir ) {
log("dir: m_dir is NULL so can't find pattern %s",pattern);
return NULL;
}
struct dirent *ent;
int32_t plen = gbstrlen ( pattern );
while ( (ent = readdir ( m_dir )) ) {
char *filename = ent->d_name;
if ( ! pattern ) return filename;
if ( plen>2 && pattern[0] == '*' && pattern[plen-1] == '*' ) {
pattern[plen-1]='\0';
char *s = strstr ( filename , pattern+1 ) ;
pattern[plen-1]='*';
if ( ! s ) continue;
else return filename;
}
if ( pattern[0] == '*' ) {
if ( gbstrlen(filename) < gbstrlen(pattern + 1) ) continue;
char *tail = filename +
gbstrlen ( filename ) -
gbstrlen ( pattern ) + 1;
if ( strcmp ( tail , pattern+1) == 0 ) return filename;
}
if ( pattern[plen-1]=='*' ) {
if ( strncmp ( filename , pattern , plen - 1 ) == 0 )
return filename;
}
}
return NULL;
}
// must call set first
// not recursive!
// return -1 on error
int64_t Dir::getUsedSpace ( ) {
rewinddir ( m_dir );
int64_t total = 0;
struct dirent *ent ;
while ( ( ent = readdir ( m_dir ) ) ) {
File tmpfile;
tmpfile.set ( ent->d_name );
int64_t space = tmpfile.getFileSize ( );
if ( space > 0 ) total += space;
}
return total;
}
// . replace the * in the pattern with a unique id from getNewId()
char *Dir::getFullName ( char *filename ) {
static char buf[1024];
sprintf ( buf , "%s/%s", m_dirname , filename );
return buf;
}
// . replace the * in the pattern with a unique id from getNewId()
char *Dir::getNewFilename ( char *pattern ) {
int64_t id = getNewId ( pattern );
static char buf[1024];
strcpy ( buf , m_dirname );
int j = gbstrlen ( buf );
for ( int i = 0 ; pattern[i] ; i++ ) {
if ( pattern[i] != '*' ) {buf[j++] = pattern[i]; continue;}
sprintf ( &buf[j] , "%"INT64"" , id );
j = gbstrlen ( buf );
}
buf[j++] = '\0';
return buf;
}
// . a highly specialized function
// . gets a new id represented by files of pattern "pattern"
// . if pattern is "*.data" will return the LUB for *, a int64_t
int64_t Dir::getNewId ( char *pattern ) {
rewinddir ( m_dir );
char *filename ;
int64_t lub = 0;
while ( (filename = getNextFilename ( pattern )) ) {
int64_t id = getFileId ( filename );
if ( id >= lub ) lub = id + 1;
}
return lub;
}
// . another highly specialized function
// . expects filename to begin with a number
// . return -1 if none exists
int64_t Dir::getFileId ( char *filename ) {
int end = gbstrlen ( filename ) -1;
while ( end >= 0 && filename [ end ] != '.' ) end--;
if ( end < 0 ) return -1;
end--;
while ( end >= 0 && isdigit ( filename [ end ] ) ) end--;
// now 3 cases:
// 1. end = -1 and filename[0] is NOT a digit
// 2. end = -1 and filename[0] is a digit
// 3. end >= 0 and filename[end+1] is a digit
if ( end < 0 && ! isdigit ( filename[0] ) ) return -1;
if ( end < 0 ) end = 0;
else end++;
int64_t id = -1;
sscanf ( filename + end , "%"INT64"." , & id );
return id;
}