Skip to content

Commit d766201

Browse files
authored
Merge pull request #448 from stanhu/sh-fix-stable-sort
main: fix incorrect directory entries due to unstable iteration order
2 parents 2772fa3 + 4266f68 commit d766201

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

main.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,6 +2308,17 @@ ovl_dirp (struct fuse_file_info *fi)
23082308
return (struct ovl_dirp *) (uintptr_t) fi->fh;
23092309
}
23102310

2311+
/* Comparison function for qsort to ensure stable directory entry order.
2312+
Entries are sorted by name to guarantee consistent offsets across
2313+
multiple opendir sessions. */
2314+
static int
2315+
compare_nodes_by_name (const void *a, const void *b)
2316+
{
2317+
struct ovl_node *node_a = *(struct ovl_node **) a;
2318+
struct ovl_node *node_b = *(struct ovl_node **) b;
2319+
return strcmp (node_a->name, node_b->name);
2320+
}
2321+
23112322
static int
23122323
reload_tbl (struct ovl_data *lo, struct ovl_dirp *d, struct ovl_node *node)
23132324
{
@@ -2341,6 +2352,16 @@ reload_tbl (struct ovl_data *lo, struct ovl_dirp *d, struct ovl_node *node)
23412352
d->tbl[counter++] = it;
23422353
}
23432354

2355+
/* Sort the directory entries by name to ensure stable offsets.
2356+
2357+
The kernel FUSE layer caches directory listings using offsets as
2358+
identifiers. If entries change positions between opendir sessions
2359+
(which can happen when nodes are freed and recreated between loads),
2360+
the kernel may incorrectly skip or duplicate entries.
2361+
2362+
Sorting ensures the same filename always appears at the same offset. */
2363+
qsort (&d->tbl[2], counter - 2, sizeof (struct ovl_node *), compare_nodes_by_name);
2364+
23442365
return 0;
23452366
}
23462367

0 commit comments

Comments
 (0)