lua: emit a proper deletion when setting an existing list to a new list with only...
[project/uci.git] / util.c
1 /*
2  * libuci - Library for the Unified Configuration Interface
3  * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License version 2.1
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  */
14
15 /*
16  * This file contains misc utility functions and wrappers to standard
17  * functions, which throw exceptions upon failure.
18  */
19 #define _GNU_SOURCE
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/file.h>
23 #include <stdbool.h>
24 #include <unistd.h>
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "uci.h"
32 #include "uci_internal.h"
33
34 __private void *uci_malloc(struct uci_context *ctx, size_t size)
35 {
36         void *ptr;
37
38         ptr = malloc(size);
39         if (!ptr)
40                 UCI_THROW(ctx, UCI_ERR_MEM);
41         memset(ptr, 0, size);
42
43         return ptr;
44 }
45
46 __private void *uci_realloc(struct uci_context *ctx, void *ptr, size_t size)
47 {
48         ptr = realloc(ptr, size);
49         if (!ptr)
50                 UCI_THROW(ctx, UCI_ERR_MEM);
51
52         return ptr;
53 }
54
55 __private char *uci_strdup(struct uci_context *ctx, const char *str)
56 {
57         char *ptr;
58
59         ptr = strdup(str);
60         if (!ptr)
61                 UCI_THROW(ctx, UCI_ERR_MEM);
62
63         return ptr;
64 }
65
66 /*
67  * validate strings for names and types, reject special characters
68  * for names, only alphanum and _ is allowed (shell compatibility)
69  * for types, we allow more characters
70  */
71 __private bool uci_validate_str(const char *str, bool name)
72 {
73         if (!*str)
74                 return false;
75
76         while (*str) {
77                 unsigned char c = *str;
78                 if (!isalnum(c) && c != '_') {
79                         if (name || (c < 33) || (c > 126))
80                                 return false;
81                 }
82                 str++;
83         }
84         return true;
85 }
86
87 bool uci_validate_text(const char *str)
88 {
89         while (*str) {
90                 unsigned char c = *str;
91                 if ((c == '\r') || (c == '\n') ||
92                         ((c < 32) && (c != '\t')))
93                         return false;
94                 str++;
95         }
96         return true;
97 }
98
99 __private void uci_alloc_parse_context(struct uci_context *ctx)
100 {
101         ctx->pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
102 }
103
104 int uci_parse_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str)
105 {
106         char *last = NULL;
107         char *tmp;
108
109         UCI_HANDLE_ERR(ctx);
110         UCI_ASSERT(ctx, str);
111         UCI_ASSERT(ctx, ptr);
112
113         memset(ptr, 0, sizeof(struct uci_ptr));
114
115         /* value */
116         last = strchr(str, '=');
117         if (last) {
118                 *last = 0;
119                 last++;
120                 ptr->value = last;
121         }
122
123         ptr->package = strsep(&str, ".");
124         if (!ptr->package)
125                 goto error;
126
127         ptr->section = strsep(&str, ".");
128         if (!ptr->section) {
129                 ptr->target = UCI_TYPE_PACKAGE;
130                 goto lastval;
131         }
132
133         ptr->option = strsep(&str, ".");
134         if (!ptr->option) {
135                 ptr->target = UCI_TYPE_SECTION;
136                 goto lastval;
137         } else {
138                 ptr->target = UCI_TYPE_OPTION;
139         }
140
141         tmp = strsep(&str, ".");
142         if (tmp)
143                 goto error;
144
145 lastval:
146         if (ptr->package && !uci_validate_package(ptr->package))
147                 goto error;
148         if (ptr->section && !uci_validate_name(ptr->section))
149                 ptr->flags |= UCI_LOOKUP_EXTENDED;
150         if (ptr->option && !uci_validate_name(ptr->option))
151                 goto error;
152         if (ptr->value && !uci_validate_text(ptr->value))
153                 goto error;
154
155         return 0;
156
157 error:
158         memset(ptr, 0, sizeof(struct uci_ptr));
159         UCI_THROW(ctx, UCI_ERR_PARSE);
160 }
161
162
163 __private void uci_parse_error(struct uci_context *ctx, char *pos, char *reason)
164 {
165         struct uci_parse_context *pctx = ctx->pctx;
166
167         pctx->reason = reason;
168         pctx->byte = pos - pctx->buf;
169         UCI_THROW(ctx, UCI_ERR_PARSE);
170 }
171
172
173
174 /*
175  * open a stream and go to the right position
176  *
177  * note: when opening for write and seeking to the beginning of
178  * the stream, truncate the file
179  */
180 __private FILE *uci_open_stream(struct uci_context *ctx, const char *filename, int pos, bool write, bool create)
181 {
182         struct stat statbuf;
183         FILE *file = NULL;
184         int fd, ret;
185         int flags = (write ? O_RDWR : O_RDONLY);
186         mode_t mode = UCI_FILEMODE;
187         char *name = NULL;
188         char *filename2 = NULL;
189
190         if (create) {
191                 flags |= O_CREAT;
192                 name = basename(filename);
193                 if ((asprintf(&filename2, "%s/%s", ctx->confdir, name) < 0) || !filename2) {
194                         UCI_THROW(ctx, UCI_ERR_MEM);
195                 } else {
196                         if (stat(filename2,&statbuf) == 0)
197                                 mode = statbuf.st_mode;
198
199                         free(filename2);
200                 }
201         }
202
203         if (!write && ((stat(filename, &statbuf) < 0) ||
204                 ((statbuf.st_mode &  S_IFMT) != S_IFREG))) {
205                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
206         }
207
208         fd = open(filename, flags, mode);
209         if (fd < 0)
210                 goto error;
211
212         ret = flock(fd, (write ? LOCK_EX : LOCK_SH));
213         if ((ret < 0) && (errno != ENOSYS))
214                 goto error;
215
216         ret = lseek(fd, 0, pos);
217
218         if (ret < 0)
219                 goto error;
220
221         file = fdopen(fd, (write ? "w+" : "r"));
222         if (file)
223                 goto done;
224
225 error:
226         UCI_THROW(ctx, UCI_ERR_IO);
227 done:
228         return file;
229 }
230
231 __private void uci_close_stream(FILE *stream)
232 {
233         int fd;
234
235         if (!stream)
236                 return;
237
238         fflush(stream);
239         fd = fileno(stream);
240         flock(fd, LOCK_UN);
241         fclose(stream);
242 }
243
244