From: Sascha Hauer <s.hauer@pengutronix.de>
To: BAREBOX <barebox@lists.infradead.org>
Subject: [PATCH 2/2] treewise: use file_exists()
Date: Fri, 30 Oct 2026 08:16:22 +0100 [thread overview]
Message-ID: <20261030-file-exists-v1-2-34035d7392e5@pengutronix.de> (raw)
In-Reply-To: <20261030-file-exists-v1-0-34035d7392e5@pengutronix.de>
Use file_exists() and file_exists_type() instead of calling stat() just
to check whether a file exists or is of a given type.
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
arch/arm/boards/raspberry-pi/rpi-common.c | 7 ++-----
arch/arm/mach-k3/r5.c | 4 +---
commands/bfetch.c | 6 +++---
commands/cp.c | 7 ++-----
commands/edit.c | 3 +--
commands/readlink.c | 4 ++--
common/bbu.c | 3 +--
common/blspec.c | 4 +---
common/boot.c | 3 +--
common/complete.c | 5 ++---
common/environment.c | 5 +----
common/file-list.c | 4 ++--
| 3 +--
common/startup.c | 21 +++++++--------------
drivers/usb/gadget/function/dfu.c | 4 +---
lib/glob.c | 5 ++---
lib/libbb.c | 4 ++--
lib/libfile.c | 5 +----
net/ifup.c | 13 ++++---------
19 files changed, 37 insertions(+), 73 deletions(-)
diff --git a/arch/arm/boards/raspberry-pi/rpi-common.c b/arch/arm/boards/raspberry-pi/rpi-common.c
index 9898e03111..442520993d 100644
--- a/arch/arm/boards/raspberry-pi/rpi-common.c
+++ b/arch/arm/boards/raspberry-pi/rpi-common.c
@@ -195,7 +195,6 @@ mem_initcall(rpi_mem_init);
static int rpi_env_init(void)
{
- struct stat s;
const char *diskdev;
int ret;
@@ -205,13 +204,11 @@ static int rpi_env_init(void)
device_detect_by_name("mci1");
diskdev = "/dev/disk0.0";
- ret = stat(diskdev, &s);
- if (ret) {
+ if (!file_exists(diskdev)) {
device_detect_by_name("mmc0");
diskdev = "/dev/mmc0.0";
- ret = stat(diskdev, &s);
}
- if (ret) {
+ if (!file_exists(diskdev)) {
printf("no /dev/disk0.0 or /dev/mmc0.0. using default env\n");
return 0;
}
diff --git a/arch/arm/mach-k3/r5.c b/arch/arm/mach-k3/r5.c
index 65f7f152d0..8816cbb409 100644
--- a/arch/arm/mach-k3/r5.c
+++ b/arch/arm/mach-k3/r5.c
@@ -364,7 +364,6 @@ static int do_dfu(void)
{
struct usbgadget_funcs funcs = {};
int ret;
- struct stat s;
funcs.flags |= USBGADGET_DFU;
funcs.dfu_opts = "/fip.img(fip)cs";
@@ -376,8 +375,7 @@ static int do_dfu(void)
}
while (1) {
- ret = stat("/fip.img", &s);
- if (!ret) {
+ if (file_exists("/fip.img")) {
printf("Downloaded FIP image, DFU done\n");
ret = load_fip("/fip.img", 0);
if (!ret)
diff --git a/commands/bfetch.c b/commands/bfetch.c
index 3083ae9933..5e5a6fe94e 100644
--- a/commands/bfetch.c
+++ b/commands/bfetch.c
@@ -14,6 +14,7 @@
#include <firmware.h>
#include <getopt.h>
#include <globalvar.h>
+#include <libfile.h>
#include <machine_id.h>
#include <memory.h>
#include <net.h>
@@ -357,7 +358,6 @@ static void print_framebuffers(unsigned *line)
static void print_features(unsigned *line)
{
- struct stat st;
struct string_list sl;
bool features = false;
size_t tmp;
@@ -373,7 +373,7 @@ static void print_features(unsigned *line)
if (IS_ENABLED(CONFIG_FEATURE_CONTROLLER) &&
!list_empty(&of_feature_controllers))
string_list_add(&sl, "FEATCTL");
- if (!stat("/dev/fw_cfg", &st))
+ if (file_exists("/dev/fw_cfg"))
string_list_add(&sl, "FW_CFG");
if (firmwaremgr_find_by_node(NULL))
string_list_add(&sl, "FWMGR");
@@ -440,7 +440,7 @@ static void print_features(unsigned *line)
if (IS_ENABLED(CONFIG_ARM_MMU_PERMISSIONS))
string_list_add(&sl, "W^X");
- if (IS_ENABLED(CONFIG_FS_UBOOTVARFS) && !stat("/dev/ubootvar", &st))
+ if (IS_ENABLED(CONFIG_FS_UBOOTVARFS) && file_exists("/dev/ubootvar"))
string_list_add(&sl, "UBOOTVARFS");
/* TODO: detect semihosting */
diff --git a/commands/cp.c b/commands/cp.c
index dc38cbab88..c30efca054 100644
--- a/commands/cp.c
+++ b/commands/cp.c
@@ -21,7 +21,6 @@
static int do_cp(int argc, char *argv[])
{
int ret = 1;
- struct stat statbuf;
int last_is_dir = 0;
int i;
int opt;
@@ -47,10 +46,8 @@ static int do_cp(int argc, char *argv[])
if (argc < argc_min)
return COMMAND_ERROR_USAGE;
- if (!stat(argv[argc - 1], &statbuf)) {
- if (S_ISDIR(statbuf.st_mode))
- last_is_dir = 1;
- }
+ if (file_exists_type(argv[argc - 1], S_IFDIR))
+ last_is_dir = 1;
if (((recursive && argc - optind > 2) || (argc > argc_min)) && !last_is_dir) {
printf("cp: target `%s' is not a directory\n", argv[argc - 1]);
diff --git a/commands/edit.c b/commands/edit.c
index 4442543b9a..cf11aaa7fa 100644
--- a/commands/edit.c
+++ b/commands/edit.c
@@ -176,9 +176,8 @@ static int edit_read_file(const char *path)
struct line *lastline = NULL;
char *filebuffer;
char *linestr, *lineend;
- struct stat s;
- if (!stat(path, &s)) {
+ if (file_exists(path)) {
filebuffer = read_file(path, NULL);
if (!filebuffer) {
printf("could not read %s: %m\n", path);
diff --git a/commands/readlink.c b/commands/readlink.c
index 021fbaa3e0..35f2b7a593 100644
--- a/commands/readlink.c
+++ b/commands/readlink.c
@@ -10,6 +10,7 @@
#include <fs.h>
#include <malloc.h>
#include <getopt.h>
+#include <libfile.h>
enum can_mode { CAN_NONE, CAN_ALL_BUT_LAST, CAN_EXISTING };
@@ -25,7 +26,6 @@ static void output_result(const char *var, const char *val)
static int canonicalize_filename_mode(const char *var, char *path, enum can_mode can_mode)
{
char *buf = NULL, *file, *dir;
- struct stat s;
int ret = -1;
buf = canonicalize_path(AT_FDCWD, path);
@@ -38,7 +38,7 @@ static int canonicalize_filename_mode(const char *var, char *path, enum can_mode
dir = dirname(path);
buf = canonicalize_path(AT_FDCWD, dir);
- if (!buf || stat(dir, &s) || !S_ISDIR(s.st_mode))
+ if (!buf || !file_exists_type(dir, S_IFDIR))
goto err;
buf = xrasprintf(buf, "/%s", file);
diff --git a/common/bbu.c b/common/bbu.c
index 4b1cf2ee88..70d49e3563 100644
--- a/common/bbu.c
+++ b/common/bbu.c
@@ -44,13 +44,12 @@ void bbu_append_handlers_to_file_list(struct file_list *files)
list_for_each_entry(handler, &bbu_image_handlers, list) {
const char *devicefile;
- struct stat s;
devicefile = handler->devicefile;
device_detect_by_name(devpath_to_name(devicefile));
- if (stat(devicefile, &s) == 0) {
+ if (file_exists(devicefile)) {
append_bbu_entry(handler->name, devicefile, files);
} else {
pr_info("Skipping handler bbu-%s: %s unavailable\n",
diff --git a/common/blspec.c b/common/blspec.c
index 0552e93b79..130c806e48 100644
--- a/common/blspec.c
+++ b/common/blspec.c
@@ -561,10 +561,8 @@ static int blspec_scan_directory(struct bootscanner *bootscanner,
for (i = 0; i < globb.gl_pathc; i++) {
const char *configname = globb.gl_pathv[i];
- struct stat s;
- ret = stat(configname, &s);
- if (ret || !S_ISREG(s.st_mode))
+ if (!file_exists_type(configname, S_IFREG))
continue;
ret = __blspec_scan_file(bootentries, root, configname);
diff --git a/common/boot.c b/common/boot.c
index 149bfeca00..5c74b1dd5d 100644
--- a/common/boot.c
+++ b/common/boot.c
@@ -348,7 +348,6 @@ static int bootscript_scan_name(struct bootentries *bootentries, const char *nam
{
const char *args = NULL;
char *path, *at;
- struct stat s;
int ret;
if (*name != '/')
@@ -357,7 +356,7 @@ static int bootscript_scan_name(struct bootentries *bootentries, const char *nam
path = xstrdup(name);
at = strchr(path, '@');
- if (at && !stat(path, &s))
+ if (at && file_exists(path))
at = NULL;
if (at) {
diff --git a/common/complete.c b/common/complete.c
index a01ce9b471..b778d9cecd 100644
--- a/common/complete.c
+++ b/common/complete.c
@@ -14,6 +14,7 @@
#include <libgen.h>
#include <command.h>
#include <environment.h>
+#include <libfile.h>
#define DEVPARAM_COMPLETE_ASSIGNMENT BIT(0)
#define DEVPARAM_COMPLETE_DOLLAR BIT(1)
@@ -86,7 +87,6 @@ static int file_complete(struct string_list *sl, char *instr,
static int path_command_complete(struct string_list *sl, char *instr)
{
- struct stat s;
DIR *dir;
struct dirent *d;
char tmp[PATH_MAX];
@@ -121,8 +121,7 @@ static int path_command_complete(struct string_list *sl, char *instr)
if (strstarts_escaped(d->d_name, instr)) {
strcpy(tmp, d->d_name);
- if (!stat(tmp, &s) &&
- S_ISDIR(s.st_mode))
+ if (file_exists_type(tmp, S_IFDIR))
continue;
else
strcat(tmp, " ");
diff --git a/common/environment.c b/common/environment.c
index faf9fc274e..4180aa90be 100644
--- a/common/environment.c
+++ b/common/environment.c
@@ -252,16 +252,13 @@ static int file_remove_action(const char *filename, struct stat *statbuf,
{
struct action_data *data = userdata;
char *envname;
- struct stat s;
- int ret;
struct envfs_entry *env;
filename += sizeof(TMPDIR) - 1;
envname = basprintf("%s/%s", data->base, filename);
- ret = stat(envname, &s);
- if (ret) {
+ if (!file_exists(envname)) {
char *base;
base = basename(envname);
diff --git a/common/file-list.c b/common/file-list.c
index 6298f8f82d..2b533657c2 100644
--- a/common/file-list.c
+++ b/common/file-list.c
@@ -10,6 +10,7 @@
#include <linux/err.h>
#include <driver.h>
#include <block.h>
+#include <libfile.h>
#define PARSE_DEVICE 0
#define PARSE_NAME 1
@@ -283,11 +284,10 @@ char *file_list_to_str(const struct file_list *files)
int file_list_detect_all(const struct file_list *files)
{
struct file_list_entry *fentry;
- struct stat s;
int i = 0;
list_for_each_entry(fentry, &files->list, list) {
- if (!stat(fentry->filename, &s))
+ if (file_exists(fentry->filename))
continue;
if (device_detect_by_name(devpath_to_name(fentry->filename)) == 0)
i++;
--git a/common/menutree.c b/common/menutree.c
index 6370ad1f56..2026a6e553 100644
--- a/common/menutree.c
+++ b/common/menutree.c
@@ -139,8 +139,7 @@ int menutree(const char *path, int toplevel)
mt->action = basprintf("%s/action", g.gl_pathv[i]);
- ret = stat(mt->action, &s);
- if (ret) {
+ if (!file_exists(mt->action)) {
mt->me.action = menutree_action_subdir;
free(mt->action);
mt->action = xstrdup(g.gl_pathv[i]);
diff --git a/common/startup.c b/common/startup.c
index 98da24873e..12a538715d 100644
--- a/common/startup.c
+++ b/common/startup.c
@@ -100,7 +100,6 @@ static void env_merge_of_compat_overlays(const char *dir)
{
struct device_node *root;
const char *compat;
- struct stat s;
char *ofdir, *src;
int count;
@@ -110,7 +109,7 @@ static void env_merge_of_compat_overlays(const char *dir)
ofdir = xasprintf("%s/match.of_compatible", dir);
- if (stat(ofdir, &s) || !S_ISDIR(s.st_mode))
+ if (!file_exists_type(ofdir, S_IFDIR))
goto out;
count = of_property_count_strings(root, "compatible");
@@ -126,7 +125,7 @@ static void env_merge_of_compat_overlays(const char *dir)
src = xasprintf("%s/%s", ofdir, compat);
- if (stat(src, &s) == 0 && S_ISDIR(s.st_mode)) {
+ if (file_exists_type(src, S_IFDIR)) {
pr_debug("Applying '%s'-compatible environment overlay\n",
compat);
copy_recursive(src, dir, 0);
@@ -239,7 +238,6 @@ enum autoboot_state do_autoboot_countdown(void)
static enum autoboot_state autoboot_state = AUTOBOOT_UNKNOWN;
unsigned flags = CONSOLE_COUNTDOWN_EXTERN;
int ret;
- struct stat s;
bool menu_exists;
char abortkeys[3] = {};
unsigned char outkey;
@@ -257,7 +255,7 @@ enum autoboot_state do_autoboot_countdown(void)
if (global_autoboot_state != AUTOBOOT_COUNTDOWN)
return global_autoboot_state;
- menu_exists = stat(MENUFILE, &s) == 0;
+ menu_exists = file_exists(MENUFILE);
if (menu_exists)
strcat(abortkeys, "m");
@@ -338,7 +336,6 @@ static int run_init(void)
{
const char *bmode, *cmdline;
bool env_bin_init_exists;
- struct stat s;
glob_t g;
int i, ret;
size_t size;
@@ -348,7 +345,7 @@ static int run_init(void)
export("PATH");
/* Run legacy /env/bin/init if it exists */
- env_bin_init_exists = stat(INITFILE, &s) == 0;
+ env_bin_init_exists = file_exists(INITFILE);
if (env_bin_init_exists) {
pr_info("running legacy %s...\n", INITFILE);
run_command("source " INITFILE);
@@ -369,11 +366,7 @@ static int run_init(void)
for (i = 0; i < g.gl_pathc; i++) {
const char *path = g.gl_pathv[i];
- ret = stat(path, &s);
- if (ret)
- continue;
-
- if (!S_ISREG(s.st_mode))
+ if (!file_exists_type(path, S_IFREG))
continue;
pr_debug("Executing '%s'...\n", path);
@@ -404,7 +397,7 @@ static int run_init(void)
scr = xasprintf("source /env/bmode/%s", bmode);
path = &scr[strlen("source ")];
- if (stat(path, &s) == 0) {
+ if (file_exists(path)) {
pr_info("Invoking '%s'...\n", path);
run_command("%s", scr);
}
@@ -436,7 +429,7 @@ static int run_init(void)
* message is printed if the file doesn't exist
*/
run_command(MENUFILE);
- } while (stat(MENUFILE, &s) == 0);
+ } while (file_exists(MENUFILE));
hang();
}
diff --git a/drivers/usb/gadget/function/dfu.c b/drivers/usb/gadget/function/dfu.c
index 90363586ee..9c145a7f8f 100644
--- a/drivers/usb/gadget/function/dfu.c
+++ b/drivers/usb/gadget/function/dfu.c
@@ -298,8 +298,6 @@ static void dfu_do_open_upload(struct dfu_work *dw)
static void dfu_do_close(struct dfu_work *dw)
{
- struct stat s;
-
pr_debug("do close\n");
if (dfufd > 0) {
@@ -307,7 +305,7 @@ static void dfu_do_close(struct dfu_work *dw)
dfufd = -EINVAL;
}
- if (!stat(DFU_TEMPFILE, &s))
+ if (file_exists(DFU_TEMPFILE))
unlink(DFU_TEMPFILE);
dw->dfu->dfu_state = DFU_STATE_dfuIDLE;
diff --git a/lib/glob.c b/lib/glob.c
index a077f2702b..0ffad8079d 100644
--- a/lib/glob.c
+++ b/lib/glob.c
@@ -10,6 +10,7 @@
#include <fnmatch.h>
#include <qsort.h>
#include <glob.h>
+#include <libfile.h>
static int glob_in_dir (const char *pattern, const char *directory,
int flags,
@@ -219,10 +220,8 @@ int glob(const char *pattern, int flags,
/* Append slashes to directory names. glob_in_dir has already
allocated the extra character for us. */
int i;
- struct stat st;
for (i = oldcount; i < pglob->gl_pathc; ++i)
- if (stat(pglob->gl_pathv[i], &st) == 0 &&
- S_ISDIR(st.st_mode))
+ if (file_exists_type(pglob->gl_pathv[i], S_IFDIR))
strcat(pglob->gl_pathv[i], "/");
}
#ifdef CONFIG_GLOB_SORT
diff --git a/lib/libbb.c b/lib/libbb.c
index 642e54d78f..1aff728b6f 100644
--- a/lib/libbb.c
+++ b/lib/libbb.c
@@ -14,6 +14,7 @@
#include <xfuncs.h>
#include <malloc.h>
#include <environment.h>
+#include <libfile.h>
/* concatenate path and file name to new allocation buffer,
* not adding '/' if path name already has '/'
@@ -91,8 +92,7 @@ EXPORT_SYMBOL(find_path);
*/
bool execable_file(const char *name)
{
- struct stat s;
- return (!stat(name, &s) && S_ISREG(s.st_mode));
+ return file_exists_type(name, S_IFREG);
}
EXPORT_SYMBOL(execable_file);
diff --git a/lib/libfile.c b/lib/libfile.c
index 2330d52d8e..0af8585c40 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -787,14 +787,11 @@ EXPORT_SYMBOL(file_exists_type);
char *make_temp(const char *template)
{
char *name = NULL;
- struct stat s;
- int ret;
do {
free(name);
name = basprintf("/tmp/%s-%08x", template, random32());
- ret = stat(name, &s);
- } while (!ret);
+ } while (file_exists(name));
return name;
}
diff --git a/net/ifup.c b/net/ifup.c
index cffdfe7c9b..a47eb4d543 100644
--- a/net/ifup.c
+++ b/net/ifup.c
@@ -18,18 +18,15 @@
#include <driver.h>
#include <init.h>
#include <magicvar.h>
+#include <libfile.h>
#include <linux/stat.h>
static int eth_discover(char *file)
{
- struct stat s;
- int ret;
+ int ret = 0;
- ret = stat(file, &s);
- if (ret) {
- ret = 0;
+ if (!file_exists(file))
goto out;
- }
ret = run_command("%s", file);
if (ret) {
@@ -69,11 +66,9 @@ static int source_env_network(struct eth_device *edev)
char *file;
const char *ethaddrstr, *modestr, *linuxdevname;
int ret, mode, ethaddr_valid = 0, i;
- struct stat s;
file = basprintf("/env/network/%s", edev->devname);
- ret = stat(file, &s);
- if (ret) {
+ if (!file_exists(file)) {
free(file);
return 0;
}
--
2.47.3
prev parent reply other threads:[~2026-10-30 7:17 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-30 7:16 [PATCH 0/2] Add and use file_exists() helper Sascha Hauer
2026-10-30 7:16 ` [PATCH 1/2] libfile: add " Sascha Hauer
2026-10-30 7:16 ` Sascha Hauer [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261030-file-exists-v1-2-34035d7392e5@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox