move the stat check to the stream open function
authorFelix Fietkau <nbd@openwrt.org>
Wed, 30 Jan 2008 01:25:47 +0000 (02:25 +0100)
committerFelix Fietkau <nbd@openwrt.org>
Wed, 30 Jan 2008 01:25:47 +0000 (02:25 +0100)
file.c

diff --git a/file.c b/file.c
index 7a57612..b8aa865 100644 (file)
--- 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);