From: Felix Fietkau Date: Wed, 30 Jan 2008 01:25:47 +0000 (+0100) Subject: move the stat check to the stream open function X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fuci.git;a=commitdiff_plain;h=396dd7a78674c5afc08240b91b045f366b54face;hp=8e57ddf6b61e9e95e1f319b1def18789f19f9ee1 move the stat check to the stream open function --- 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);