1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Manage a cache of file names' existence */
6 #include <linux/list.h>
17 static struct hlist_head fncache_hash
[FNHSIZE
];
19 unsigned shash(const unsigned char *s
)
27 static bool lookup_fncache(const char *name
, bool *res
)
29 int h
= shash((const unsigned char *)name
) % FNHSIZE
;
32 hlist_for_each_entry(n
, &fncache_hash
[h
], nd
) {
33 if (!strcmp(n
->name
, name
)) {
41 static void update_fncache(const char *name
, bool res
)
43 struct fncache
*n
= malloc(sizeof(struct fncache
) + strlen(name
) + 1);
44 int h
= shash((const unsigned char *)name
) % FNHSIZE
;
48 strcpy(n
->name
, name
);
50 hlist_add_head(&n
->nd
, &fncache_hash
[h
]);
53 /* No LRU, only use when bounded in some other way. */
54 bool file_available(const char *name
)
58 if (lookup_fncache(name
, &res
))
60 res
= access(name
, R_OK
) == 0;
61 update_fncache(name
, res
);