From 396dd7a78674c5afc08240b91b045f366b54face Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Wed, 30 Jan 2008 02:25:47 +0100 Subject: [PATCH] move the stat check to the stream open function --- file.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/file.c b/file.c index 7a57612..b8aa865 100644 --- a/file.c +++ b/file.c @@ -526,9 +526,15 @@ int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct u */ static FILE *uci_open_stream(struct uci_context *ctx, const char *filename, int pos, bool write) { + struct stat statbuf; FILE *file = NULL; int fd, ret; + if (!write && ((stat(filename, &statbuf) < 0) || + ((statbuf.st_mode & S_IFMT) != S_IFREG))) { + UCI_THROW(ctx, UCI_ERR_NOTFOUND); + } + fd = open(filename, (write ? O_RDWR | O_CREAT : O_RDONLY)); if (fd <= 0) goto error; @@ -645,7 +651,6 @@ done: int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package) { - struct stat statbuf; char *filename; bool confdir; FILE *file = NULL; @@ -675,11 +680,6 @@ int uci_load(struct uci_context *ctx, const char *name, struct uci_package **pac break; } - if ((stat(filename, &statbuf) < 0) || - ((statbuf.st_mode & S_IFMT) != S_IFREG)) { - UCI_THROW(ctx, UCI_ERR_NOTFOUND); - } - file = uci_open_stream(ctx, filename, SEEK_SET, false); ctx->errno = 0; UCI_TRAP_SAVE(ctx, done); -- 2.30.2