file: defer checking the tmpfile until it is actually needed.
[project/uci.git] / util.c
diff --git a/util.c b/util.c
index d44114d..12aec9b 100644 (file)
--- a/util.c
+++ b/util.c
@@ -9,13 +9,14 @@
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * GNU Lesser General Public License for more details.
  */
 
 /*
  * This file contains misc utility functions and wrappers to standard
  * functions, which throw exceptions upon failure.
  */
+#define _GNU_SOURCE
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/file.h>
 #include <ctype.h>
 #include <fcntl.h>
 #include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <libgen.h>
 
-__plugin void *uci_malloc(struct uci_context *ctx, size_t size)
+#include "uci.h"
+#include "uci_internal.h"
+
+__private void *uci_malloc(struct uci_context *ctx, size_t size)
 {
        void *ptr;
 
@@ -37,7 +44,7 @@ __plugin void *uci_malloc(struct uci_context *ctx, size_t size)
        return ptr;
 }
 
-__plugin void *uci_realloc(struct uci_context *ctx, void *ptr, size_t size)
+__private void *uci_realloc(struct uci_context *ctx, void *ptr, size_t size)
 {
        ptr = realloc(ptr, size);
        if (!ptr)
@@ -46,7 +53,7 @@ __plugin void *uci_realloc(struct uci_context *ctx, void *ptr, size_t size)
        return ptr;
 }
 
-__plugin char *uci_strdup(struct uci_context *ctx, const char *str)
+__private char *uci_strdup(struct uci_context *ctx, const char *str)
 {
        char *ptr;
 
@@ -57,39 +64,27 @@ __plugin char *uci_strdup(struct uci_context *ctx, const char *str)
        return ptr;
 }
 
-/* Based on an efficient hash function published by D. J. Bernstein */
-static unsigned int djbhash(unsigned int hash, char *str)
-{
-       int len = strlen(str);
-       int i;
-
-       /* initial value */
-       if (hash == ~0)
-               hash = 5381;
-
-       for(i = 0; i < len; i++) {
-               hash = ((hash << 5) + hash) + str[i];
-       }
-       return (hash & 0x7FFFFFFF);
-}
-
 /*
  * validate strings for names and types, reject special characters
  * for names, only alphanum and _ is allowed (shell compatibility)
  * for types, we allow more characters
  */
-__plugin bool uci_validate_str(const char *str, bool name)
+__private bool uci_validate_str(const char *str, bool name, bool package)
 {
        if (!*str)
                return false;
 
-       while (*str) {
+       for (; *str; str++) {
                unsigned char c = *str;
-               if (!isalnum(c) && c != '_') {
-                       if (name || (c < 33) || (c > 126))
-                               return false;
-               }
-               str++;
+
+               if (isalnum(c) || c == '_')
+                       continue;
+
+               if (c == '-' && package)
+                       continue;
+
+               if (name || (c < 33) || (c > 126))
+                       return false;
        }
        return true;
 }
@@ -98,9 +93,10 @@ bool uci_validate_text(const char *str)
 {
        while (*str) {
                unsigned char c = *str;
-               if ((c == '\r') || (c == '\n') ||
-                       ((c < 32) && (c != '\t')))
+
+               if (c < 32 && c != '\t' && c != '\n' && c != '\r')
                        return false;
+
                str++;
        }
        return true;
@@ -170,12 +166,12 @@ error:
 }
 
 
-__private void uci_parse_error(struct uci_context *ctx, char *pos, char *reason)
+__private void uci_parse_error(struct uci_context *ctx, char *reason)
 {
        struct uci_parse_context *pctx = ctx->pctx;
 
        pctx->reason = reason;
-       pctx->byte = pos - pctx->buf;
+       pctx->byte = pctx_pos(pctx);
        UCI_THROW(ctx, UCI_ERR_PARSE);
 }
 
@@ -187,22 +183,39 @@ __private void uci_parse_error(struct uci_context *ctx, char *pos, char *reason)
  * note: when opening for write and seeking to the beginning of
  * the stream, truncate the file
  */
-__private FILE *uci_open_stream(struct uci_context *ctx, const char *filename, int pos, bool write, bool create)
+__private FILE *uci_open_stream(struct uci_context *ctx, const char *filename, const char *origfilename, int pos, bool write, bool create)
 {
        struct stat statbuf;
        FILE *file = NULL;
        int fd, ret;
-       int mode = (write ? O_RDWR : O_RDONLY);
+       int flags = (write ? O_RDWR : O_RDONLY);
+       mode_t mode = UCI_FILEMODE;
+       char *name = NULL;
+       char *filename2 = NULL;
+
+       if (create) {
+               flags |= O_CREAT;
+               if (origfilename) {
+                       name = basename((char *) origfilename);
+               } else {
+                       name = basename((char *) filename);
+               }
+               if ((asprintf(&filename2, "%s/%s", ctx->confdir, name) < 0) || !filename2) {
+                       UCI_THROW(ctx, UCI_ERR_MEM);
+               } else {
+                       if (stat(filename2, &statbuf) == 0)
+                               mode = statbuf.st_mode;
 
-       if (create)
-               mode |= O_CREAT;
+                       free(filename2);
+               }
+       }
 
        if (!write && ((stat(filename, &statbuf) < 0) ||
-               ((statbuf.st_mode &  S_IFMT) != S_IFREG))) {
+               ((statbuf.st_mode & S_IFMT) != S_IFREG))) {
                UCI_THROW(ctx, UCI_ERR_NOTFOUND);
        }
 
-       fd = open(filename, mode, UCI_FILEMODE);
+       fd = open(filename, flags, mode);
        if (fd < 0)
                goto error;