Skip to content

Commit 6160db8

Browse files
committed
use openat() when the parent dir is avaialable
1 parent 0cdd1ab commit 6160db8

File tree

8 files changed

+39
-31
lines changed

8 files changed

+39
-31
lines changed

include/bf.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -325,20 +325,6 @@ typedef enum {
325325
CLOSE_DB = 0x04,
326326
} CleanUpTasks;
327327

328-
/*
329-
* A reference-counted directory handle.
330-
*/
331-
struct dir_rc {
332-
DIR *dir;
333-
uint64_t rc;
334-
};
335-
336-
struct dir_rc *open_dir_rc(int optional_fd, char *path);
337-
int get_dir_fd(struct dir_rc *dir);
338-
void dir_inc(struct dir_rc *dir);
339-
struct dir_rc *dir_clone(struct dir_rc *dir);
340-
void dir_dec(struct dir_rc *dir);
341-
342328
/*
343329
* Minimum data needs to be passed around between threads.
344330
*
@@ -376,6 +362,20 @@ struct work *new_work_with_name(const char *prefix, const size_t prefix_len,
376362
const char *basename, const size_t basename_len);
377363
void free_work(struct work *w);
378364

365+
/*
366+
* A reference-counted directory handle.
367+
*/
368+
struct dir_rc {
369+
DIR *dir;
370+
uint64_t rc;
371+
};
372+
373+
struct dir_rc *open_dir_rc(struct work *w);
374+
int get_dir_fd(struct dir_rc *dir);
375+
void dir_inc(struct dir_rc *dir);
376+
struct dir_rc *dir_clone(struct dir_rc *dir);
377+
void dir_dec(struct dir_rc *dir);
378+
379379
/* extra data used by entries that does not depend on data from other directories */
380380
struct entry_data {
381381
int parent_fd; /* holds an FD that can be used for fstatat(2), etc. */

src/bf.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -581,38 +581,43 @@ INSTALL_NUMBER(SIZE, size_t, "%zu")
581581
INSTALL_NUMBER(UINT64, uint64_t, "%" PRIu64)
582582

583583
/*
584-
* Create a new reference-counted DIR object. If optional_fd is a valid FD, then
585-
* openat() the new DIR relative to that FD. Otherwise, just opendir() the path.
584+
* Create a new reference-counted DIR object for the given `struct work`.
585+
*
586+
* If work->parent_dir is a non-NULL dir_rc, then openat() the new DIR relative to it.
587+
* This assumes that w->basename_len is correctly initialized!
588+
*
589+
* Otherwise, just opendir() the path.
586590
*
587591
* Increments the refcount on the new object.
588592
*/
589-
struct dir_rc *open_dir_rc(int optional_fd, char *path) {
593+
struct dir_rc *open_dir_rc(struct work *w) {
590594
DIR *dir;
591595

592-
if (optional_fd < 0) {
593-
dir = opendir(path);
594-
} else {
595-
// XXX: assuming path is relative to optional_fd here... is this the right way to go????
596-
// or do I need to do something like basename(path) first?
597-
int fd = openat(optional_fd, path, O_RDONLY|O_DIRECTORY);
596+
if (w->parent_dir) {
597+
int d_fd = get_dir_fd(w->parent_dir);
598+
char *basename = w->name + w->name_len - w->basename_len;
599+
int fd = openat(d_fd, basename, O_RDONLY|O_DIRECTORY);
598600
if (fd < 0) {
599601
return NULL;
600602
}
601603
dir = fdopendir(fd);
604+
} else {
605+
dir = opendir(w->name);
602606
}
603607

604608
if (!dir) {
605609
return NULL;
606610
}
607611

608612
struct dir_rc *new = calloc(1, sizeof(*new));
613+
// printf("creating dir at %p\n", new);
609614
new->dir = dir;
610615
dir_inc(new);
611616
return new;
612617
}
613618

614619
/*
615-
* Get a DIR * out of a dir_rc.
620+
* Get a directory FD out of a dir_rc.
616621
*/
617622
int get_dir_fd(struct dir_rc *dir) {
618623
return gufi_dirfd(dir->dir);
@@ -622,6 +627,7 @@ int get_dir_fd(struct dir_rc *dir) {
622627
* Increment the reference count for a dir_rc.
623628
*/
624629
void dir_inc(struct dir_rc *dir) {
630+
// printf("incrementing dir at %p\n", dir);
625631
// XXX: is relaxed OK here?
626632
__atomic_fetch_add(&dir->rc, 1, __ATOMIC_ACQ_REL);
627633
}
@@ -640,7 +646,9 @@ struct dir_rc *dir_clone(struct dir_rc *dir) {
640646
*/
641647
void dir_dec(struct dir_rc *dir) {
642648
if (dir) {
649+
// printf("decrementing dir at %p\n", dir);
643650
if (__atomic_sub_fetch(&dir->rc, 1, __ATOMIC_ACQ_REL) == 0) {
651+
// printf("freeing dir at %p\n", dir);
644652
closedir(dir->dir);
645653
free(dir);
646654
}

src/gufi_dir2index.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ static int processdir(QPTPool_t *ctx, const size_t id, void *data, void *args) {
203203
goto cleanup;
204204
}
205205

206-
struct dir_rc *dir_rc = open_dir_rc(-1, nda.work->name);
206+
struct dir_rc *dir_rc = open_dir_rc(nda.work);
207207
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);

src/gufi_dir2trace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ static int processdir(QPTPool_t *ctx, const size_t id, void *data, void *args) {
136136

137137
decompress_work(&work, data);
138138

139-
struct dir_rc *dir_rc = open_dir_rc(-1, work->name);
139+
struct dir_rc *dir_rc = open_dir_rc(work);
140140
if (!dir_rc) {
141141
rc = 1;
142142
goto cleanup;

src/gufi_index2dir.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ static int processdir(struct QPTPool * ctx, const size_t id, void * data, void *
265265
sqlite3 *db = NULL;
266266
int rc = 0;
267267

268-
struct dir_rc *dir_rc = open_dir_rc(-1, work->name);
268+
struct dir_rc *dir_rc = open_dir_rc(work);
269269
if (!dir_rc) {
270270
fprintf(stderr, "Could not open directory \"%s\"\n", work->name);
271271
rc = 1;

src/gufi_treesummary.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ static int processdir(QPTPool_t *ctx, const size_t id, void *data, void *args) {
134134
goto out_free;
135135
}
136136

137-
struct dir_rc *dir_rc = open_dir_rc(-1, passmywork->name);
137+
struct dir_rc *dir_rc = open_dir_rc(passmywork);
138138
if (!dir_rc) {
139139
goto out_free;
140140
}

src/parallel_cpr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ static int cpr_dir(QPTPool_t *ctx, const size_t id, void *data, void *args) {
231231

232232
int rc = 0;
233233

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

test/unit/googletest/descend.cpp.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ 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-
struct dir_rc *dir_rc = open_dir_rc(-1, work->name);
96+
struct dir_rc *dir_rc = open_dir_rc(work);
9797
ASSERT_NE(dir_rc, nullptr);
9898
DIR *dir = dir_rc->dir;
9999

@@ -249,7 +249,7 @@ TEST(descend, swap) {
249249
// descend to swap
250250
struct work *work = new_work_with_name(nullptr, 0, root, strlen(root));
251251

252-
struct dir_rc *dir_rc = open_dir_rc(-1, work->name);
252+
struct dir_rc *dir_rc = open_dir_rc(work);
253253
ASSERT_NE(dir_rc, nullptr);
254254
DIR *dir = dir_rc->dir;
255255

0 commit comments

Comments
 (0)