Skip to content

Commit 9fa91a6

Browse files
committed
make descend() take a struct dir_rc * instead of DIR *
1 parent 1567bf5 commit 9fa91a6

File tree

8 files changed

+41
-32
lines changed

8 files changed

+41
-32
lines changed

include/descend.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ struct descend_counters {
9090
*/
9191
int descend(QPTPool_t *ctx, const size_t id, void *args,
9292
struct input *in, struct work *work, ino_t inode,
93-
DIR *dir, const int skip_db,
93+
struct dir_rc *d_rc, const int skip_db,
9494
QPTPool_f processdir, process_nondir_f processnondir, void *nondir_args,
9595
struct descend_counters *counters);
9696

src/descend.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,15 @@ static int work_serialize_and_free(const int fd, QPTPool_f func, void *work, siz
9090
*/
9191
int descend(QPTPool_t *ctx, const size_t id, void *args,
9292
struct input *in, struct work *work, ino_t inode,
93-
DIR *dir, const int skip_db,
93+
struct dir_rc *d_rc, const int skip_db,
9494
QPTPool_f processdir, process_nondir_f processnondir, void *nondir_args,
9595
struct descend_counters *counters) {
9696
if (!work) {
9797
return 1;
9898
}
9999

100+
DIR *dir = d_rc->dir;
101+
100102
trie_t *skip_names = in->skip;
101103

102104
struct descend_counters ctrs;

src/gufi_dir2index.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,14 @@ static int processdir(QPTPool_t *ctx, const size_t id, void *data, void *args) {
203203
goto cleanup;
204204
}
205205

206-
dir = opendir(nda.work->name);
207-
if (!dir) {
206+
struct dir_rc *dir_rc = open_dir_rc(-1, nda.work->name);
207+
if (!dir_rc) {
208208
const int err = errno;
209209
fprintf(stderr, "Error: Could not open directory \"%s\": %s (%d)\n", nda.work->name, strerror(err), err);
210210
rc = 1;
211211
goto cleanup;
212212
}
213+
dir = dir_rc->dir;
213214

214215
/* offset by work->root_len to remove prefix */
215216
nda.topath_len = nda.in->nameto.len + 1 + nda.work->name_len - nda.work->root_parent.len;
@@ -272,7 +273,7 @@ static int processdir(QPTPool_t *ctx, const size_t id, void *data, void *args) {
272273
}
273274

274275
struct descend_counters ctrs;
275-
descend(ctx, id, pa, nda.in, nda.work, nda.ed.statuso.st_ino, dir, 0,
276+
descend(ctx, id, pa, nda.in, nda.work, nda.ed.statuso.st_ino, dir_rc, 0,
276277
processdir, process_nondir, &nda,
277278
&ctrs);
278279

@@ -315,7 +316,7 @@ static int processdir(QPTPool_t *ctx, const size_t id, void *data, void *args) {
315316
chown(nda.topath, nda.ed.statuso.st_uid, nda.ed.statuso.st_gid);
316317

317318
cleanup:
318-
closedir(dir);
319+
dir_dec(dir_rc);
319320

320321
if (process_dbdb) {
321322
pa->total_dirs[id]++;

src/gufi_dir2trace.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,12 @@ static int processdir(QPTPool_t *ctx, const size_t id, void *data, void *args) {
136136

137137
decompress_work(&work, data);
138138

139-
DIR *dir = opendir(work->name);
140-
if (!dir) {
139+
struct dir_rc *dir_rc = open_dir_rc(-1, work->name);
140+
if (!dir_rc) {
141141
rc = 1;
142142
goto cleanup;
143143
}
144+
DIR *dir = dir_rc->dir;
144145

145146
memset(&ed, 0, sizeof(ed));
146147
if (lstat(work->name, &ed.statuso) != 0) {
@@ -171,12 +172,12 @@ static int processdir(QPTPool_t *ctx, const size_t id, void *data, void *args) {
171172

172173
struct descend_counters ctrs;
173174
descend(ctx, id, pa,
174-
in, work, ed.statuso.st_ino, dir, 0,
175+
in, work, ed.statuso.st_ino, dir_rc, 0,
175176
processdir, process_nondir, &nda,
176177
&ctrs);
177178

178179
cleanup:
179-
closedir(dir);
180+
dir_dec(dir_rc);
180181

181182
free_work(work);
182183

src/gufi_index2dir.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,13 @@ static int processdir(struct QPTPool * ctx, const size_t id, void * data, void *
265265
sqlite3 *db = NULL;
266266
int rc = 0;
267267

268-
DIR *dir = opendir(work->name);
269-
if (!dir) {
268+
struct dir_rc *dir_rc = open_dir_rc(-1, work->name);
269+
if (!dir_rc) {
270270
fprintf(stderr, "Could not open directory \"%s\"\n", work->name);
271271
rc = 1;
272272
goto cleanup;
273273
}
274+
DIR *dir = dir_rc->dir;
274275

275276
// get source directory info
276277
struct stat dir_st;
@@ -297,7 +298,7 @@ static int processdir(struct QPTPool * ctx, const size_t id, void * data, void *
297298
}
298299

299300
descend(ctx, id, args, &pa->in, work, dir_st.st_ino,
300-
dir, 1,
301+
dir_rc, 1,
301302
processdir, NULL, NULL,
302303
NULL);
303304

@@ -355,7 +356,7 @@ static int processdir(struct QPTPool * ctx, const size_t id, void * data, void *
355356
closedb(db);
356357
db = NULL;
357358

358-
closedir(dir);
359+
dir_dec(dir_rc);
359360

360361
free_work(work);
361362

src/gufi_treesummary.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,11 @@ static int processdir(QPTPool_t *ctx, const size_t id, void *data, void *args) {
134134
goto out_free;
135135
}
136136

137-
DIR *dir = opendir(passmywork->name);
138-
if (!dir) {
137+
struct dir_rc *dir_rc = open_dir_rc(-1, passmywork->name);
138+
if (!dir_rc) {
139139
goto out_free;
140140
}
141+
DIR *dir = dir_rc->dir;
141142

142143
if (pa->in.printdir) {
143144
ed.type = 'd';
@@ -192,7 +193,7 @@ static int processdir(QPTPool_t *ctx, const size_t id, void *data, void *args) {
192193
*/
193194
descend(ctx, id, pa,
194195
&pa->in, passmywork, ed.statuso.st_ino,
195-
dir, 0,
196+
dir_rc, 0,
196197
processdir, NULL, NULL,
197198
NULL);
198199

@@ -208,7 +209,7 @@ static int processdir(QPTPool_t *ctx, const size_t id, void *data, void *args) {
208209
}
209210

210211
closedb(db);
211-
closedir(dir);
212+
dir_dec(dir_rc);
212213

213214
out_free:
214215
free_work(passmywork);

src/parallel_cpr.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,11 @@ static int cpr_dir(QPTPool_t *ctx, const size_t id, void *data, void *args) {
231231

232232
int rc = 0;
233233

234-
DIR *dir = opendir(work->name);
235-
if (!dir) {
234+
struct dir_rc *dir_rc = open_dir_rc(-1, work->name);
235+
if (!dir_rc) {
236236
print_error_and_goto("Could not open_directory", work->name, cleanup);
237237
}
238+
DIR *dir = dir_rc->dir;
238239

239240
struct stat st;
240241
if (lstat(work->name, &st) != 0) {
@@ -262,7 +263,7 @@ static int cpr_dir(QPTPool_t *ctx, const size_t id, void *data, void *args) {
262263

263264
/* process children */
264265
descend(ctx, id, args, in,
265-
work, st.st_ino, dir, 0,
266+
work, st.st_ino, dir_rc, 0,
266267
cpr_dir,
267268
enqueue_nondir, &qptp_vals,
268269
NULL);
@@ -279,8 +280,8 @@ static int cpr_dir(QPTPool_t *ctx, const size_t id, void *data, void *args) {
279280
free(dst.data);
280281

281282
cleanup:
282-
closedir(dir);
283-
free(work);
283+
dir_dec(dir_rc);
284+
free_work(work);
284285

285286
return rc;
286287
}

test/unit/googletest/descend.cpp.in

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ TEST(descend, builddir) {
9393
const char root[] = "@CMAKE_BINARY_DIR@";
9494
struct work *work = new_work_with_name(nullptr, 0, root, strlen(root));
9595

96-
DIR *dir = opendir(work->name);
97-
ASSERT_NE(dir, nullptr);
96+
struct dir_rc *dir_rc = open_dir_rc(-1, work->name);
97+
ASSERT_NE(dir_rc, nullptr);
98+
DIR *dir = dir_rc->dir;
9899

99100
QPTPool_t *pool = QPTPool_init(1, nullptr);
100101
ASSERT_NE(pool, nullptr);
@@ -111,7 +112,7 @@ TEST(descend, builddir) {
111112
{
112113
in.max_level = 0;
113114
work->level = 1;
114-
EXPECT_EQ(descend(nullptr, 0, nullptr, &in, work, 0, dir,
115+
EXPECT_EQ(descend(nullptr, 0, nullptr, &in, work, 0, dir_rc,
115116
0, nullptr,
116117
nullptr, nullptr,
117118
&ctrs), 0);
@@ -127,7 +128,7 @@ TEST(descend, builddir) {
127128
rewinddir(dir);
128129
in.max_level = 1;
129130
work->level = 0;
130-
EXPECT_EQ(descend(pool, 0, nullptr, &in, work, 0, dir, 0,
131+
EXPECT_EQ(descend(pool, 0, nullptr, &in, work, 0, dir_rc, 0,
131132
[](QPTPool_t *, const size_t, void *data, void *) -> int {
132133
free(data);
133134
return 0;
@@ -145,7 +146,7 @@ TEST(descend, builddir) {
145146
// good descend with skip_db
146147
{
147148
rewinddir(dir);
148-
EXPECT_EQ(descend(pool, 0, nullptr, &in, work, 0, dir, 1,
149+
EXPECT_EQ(descend(pool, 0, nullptr, &in, work, 0, dir_rc, 1,
149150
[](QPTPool_t *, const size_t, void *data, void *) -> int {
150151
free(data);
151152
return 0;
@@ -163,7 +164,7 @@ TEST(descend, builddir) {
163164
{
164165
rewinddir(dir);
165166
in.subdir_limit = 1;
166-
EXPECT_EQ(descend(pool, 0, nullptr, &in, work, 0, dir, 0,
167+
EXPECT_EQ(descend(pool, 0, nullptr, &in, work, 0, dir_rc, 0,
167168
[](QPTPool_t *, const size_t, void *data, void *) -> int {
168169
free(data);
169170
return 0;
@@ -248,12 +249,13 @@ TEST(descend, swap) {
248249
// descend to swap
249250
struct work *work = new_work_with_name(nullptr, 0, root, strlen(root));
250251

251-
DIR *dir = opendir(work->name);
252-
ASSERT_NE(dir, nullptr);
252+
struct dir_rc *dir_rc = open_dir_rc(-1, work->name);
253+
ASSERT_NE(dir_rc, nullptr);
254+
DIR *dir = dir_rc->dir;
253255

254256
struct descend_counters ctrs;
255257

256-
EXPECT_EQ(descend(pool, 0, nullptr, &in, work, 0, dir, 0,
258+
EXPECT_EQ(descend(pool, 0, nullptr, &in, work, 0, dir_rc, 0,
257259
[](QPTPool_t *, const size_t, void *data, void *) -> int {
258260
free(data);
259261
return 0;

0 commit comments

Comments
 (0)