]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/edit.c
Check for incorrect duration and size modifiers in conf files.
[bacula/bacula] / bacula / src / lib / edit.c
1 /*
2  *   edit.c  edit string to ascii, and ascii to internal
3  *
4  *    Kern Sibbald, December MMII
5  *
6  *   Version $Id$
7  */
8
9 /*
10    Copyright (C) 2000-2005 Kern Sibbald
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License as
14    published by the Free Software Foundation; either version 2 of
15    the License, or (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20    General Public License for more details.
21
22    You should have received a copy of the GNU General Public
23    License along with this program; if not, write to the Free
24    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"
30 #include <math.h>
31
32 /* We assume ASCII input and don't worry about overflow */
33 uint64_t str_to_uint64(char *str)
34 {
35    register char *p = str;
36    register uint64_t value = 0;
37
38    if (!p) {
39       return 0;
40    }
41    while (B_ISSPACE(*p)) {
42       p++;
43    }
44    if (*p == '+') {
45       p++;
46    }
47    while (B_ISDIGIT(*p)) {
48       value = B_TIMES10(value) + *p - '0';
49       p++;
50    }
51    return value;
52 }
53
54 int64_t str_to_int64(char *str)
55 {
56    register char *p = str;
57    register int64_t value;
58    bool negative = false;
59
60    if (!p) {
61       return 0;
62    }
63    while (B_ISSPACE(*p)) {
64       p++;
65    }
66    if (*p == '+') {
67       p++;
68    } else if (*p == '-') {
69       negative = true;
70       p++;
71    }
72    value = str_to_uint64(p);
73    if (negative) {
74       value = -value;
75    }
76    return value;
77 }
78
79
80 /*
81  * Edit an integer number with commas, the supplied buffer
82  * must be at least 27 bytes long.  The incoming number
83  * is always widened to 64 bits.
84  */
85 char *edit_uint64_with_commas(uint64_t val, char *buf)
86 {
87    /*
88     * Replacement for sprintf(buf, "%" llu, val)
89     */
90    char mbuf[50];
91    mbuf[sizeof(mbuf)-1] = 0;
92    int i = sizeof(mbuf)-2;                 /* edit backward */
93    if (val == 0) {
94       mbuf[i--] = '0';
95    } else {
96       while (val != 0) {
97          mbuf[i--] = "0123456789"[val%10];
98          val /= 10;
99       }
100    }
101    bstrncpy(buf, &mbuf[i+1], 27);
102    return add_commas(buf, buf);
103 }
104
105 /*
106  * Edit an integer number, the supplied buffer
107  * must be at least 27 bytes long.  The incoming number
108  * is always widened to 64 bits.
109  */
110 char *edit_uint64(uint64_t val, char *buf)
111 {
112    /*
113     * Replacement for sprintf(buf, "%" llu, val)
114     */
115    char mbuf[50];
116    mbuf[sizeof(mbuf)-1] = 0;
117    int i = sizeof(mbuf)-2;                 /* edit backward */
118    if (val == 0) {
119       mbuf[i--] = '0';
120    } else {
121       while (val != 0) {
122          mbuf[i--] = "0123456789"[val%10];
123          val /= 10;
124       }
125    }
126    bstrncpy(buf, &mbuf[i+1], 27);
127    return buf;
128 }
129
130 char *edit_int64(int64_t val, char *buf)
131 {
132    /*
133     * Replacement for sprintf(buf, "%" llu, val)
134     */
135    char mbuf[50];
136    bool negative = false;
137    mbuf[sizeof(mbuf)-1] = 0;
138    int i = sizeof(mbuf)-2;                 /* edit backward */
139    if (val == 0) {
140       mbuf[i--] = '0';
141    } else {
142       if (val < 0) {
143          negative = true;
144          val = -val;
145       }
146       while (val != 0) {
147          mbuf[i--] = "0123456789"[val%10];
148          val /= 10;
149       }
150    }
151    if (negative) {
152       mbuf[i--] = '-';
153    }
154    bstrncpy(buf, &mbuf[i+1], 27);
155    return buf;
156 }
157
158
159 /*
160  * Given a string "str", separate the numeric part into
161  *   str, and the modifier into mod.
162  */
163 static bool get_modifier(char *str, char *num, int num_len, char *mod, int mod_len)
164 {
165    int i, len, num_begin, num_end, mod_begin, mod_end;
166
167    strip_trailing_junk(str);
168    len = strlen(str);
169
170    for (i=0; i<len; i++) {
171       if (!B_ISSPACE(str[i])) {
172          break;
173       }
174    }
175    num_begin = i;
176
177    /* Walk through integer part */
178    for ( ; i<len; i++) {
179       if (!B_ISDIGIT(str[i]) && str[i] != '.') {
180          break;
181       }
182    }
183    num_end = i;
184    if (num_len > (num_end - num_begin + 1)) {
185       num_len = num_end - num_begin + 1;
186    }
187    if (num_len == 0) {
188       return false;
189    }
190    /* Eat any spaces in front of modifier */
191    for ( ; i<len; i++) {
192       if (!B_ISSPACE(str[i])) {
193          break;
194       }
195    }
196    mod_begin = i;
197    for ( ; i<len; i++) {
198       if (!B_ISALPHA(str[i])) {
199          break;
200       }
201    }
202    mod_end = i;
203    if (mod_len > (mod_end - mod_begin + 1)) {
204       mod_len = mod_end - mod_begin + 1;
205    }
206    Dmsg5(900, "str=%s: num_beg=%d num_end=%d mod_beg=%d mod_end=%d\n",
207       str, num_begin, num_end, mod_begin, mod_end);
208    bstrncpy(num, &str[num_begin], num_len);
209    bstrncpy(mod, &str[mod_begin], mod_len);
210    if (!is_a_number(num)) {
211       return false;
212    }
213    bstrncpy(str, &str[mod_end], len);
214    Dmsg2(900, "num=%s mod=%s\n", num, mod);
215
216    return true;
217 }
218
219 /*
220  * Convert a string duration to utime_t (64 bit seconds)
221  * Returns false: if error
222            true:  if OK, and value stored in value
223  */
224 bool duration_to_utime(char *str, utime_t *value)
225 {
226    int i, mod_len;
227    double val, total = 0.0;
228    char mod_str[20];
229    char num_str[50];
230    /*
231     * The "n" = mins and months appears before minutes so that m maps
232     *   to months. These "kludges" make it compatible with pre 1.31
233     *   Baculas.
234     */
235    static const char *mod[] = {"n", "seconds", "months", "minutes",
236                   "hours", "days", "weeks",   "quarters",   "years", NULL};
237    static const int32_t mult[] = {60,   1, 60*60*24*30, 60,
238                   60*60, 60*60*24, 60*60*24*7, 60*60*24*91, 60*60*24*365};
239
240    while (*str) {
241       if (!get_modifier(str, num_str, sizeof(num_str), mod_str, sizeof(mod_str))) {
242          return false;
243       }
244       /* Now find the multiplier corresponding to the modifier */
245       mod_len = strlen(mod_str);
246       if (mod_len == 0) {
247          i = 1;                          /* default to seconds */
248       } else {
249          for (i=0; mod[i]; i++) {
250             if (strncasecmp(mod_str, mod[i], mod_len) == 0) {
251                break;
252             }
253          }
254          if (mod[i] == NULL) {
255             return false;
256          }
257       }
258       Dmsg2(900, "str=%s: mult=%d\n", num_str, mult[i]);
259       errno = 0;
260       val = strtod(num_str, NULL);
261       if (errno != 0 || val < 0) {
262          return false;
263       }
264       total += val * mult[i];
265    }
266    *value = (utime_t)total;
267    return true;
268 }
269
270 /*
271  * Edit a utime "duration" into ASCII
272  */
273 char *edit_utime(utime_t val, char *buf, int buf_len)
274 {
275    char mybuf[200];
276    static const int32_t mult[] = {60*60*24*365, 60*60*24*30, 60*60*24, 60*60, 60};
277    static const char *mod[]  = {"year",  "month",  "day", "hour", "min"};
278    int i;
279    uint32_t times;
280
281    *buf = 0;
282    for (i=0; i<5; i++) {
283       times = (uint32_t)(val / mult[i]);
284       if (times > 0) {
285          val = val - (utime_t)times * mult[i];
286          bsnprintf(mybuf, sizeof(mybuf), "%d %s%s ", times, mod[i], times>1?"s":"");
287          bstrncat(buf, mybuf, buf_len);
288       }
289    }
290    if (val == 0 && strlen(buf) == 0) {
291       bstrncat(buf, "0 secs", buf_len);
292    } else if (val != 0) {
293       bsnprintf(mybuf, sizeof(mybuf), "%d sec%s", (uint32_t)val, val>1?"s":"");
294       bstrncat(buf, mybuf, buf_len);
295    }
296    return buf;
297 }
298
299 /*
300  * Convert a size in bytes to uint64_t
301  * Returns false: if error
302            true:  if OK, and value stored in value
303  */
304 bool size_to_uint64(char *str, int str_len, uint64_t *value)
305 {
306    int i, mod_len;
307    double val;
308    char mod_str[20];
309    char num_str[50];
310    static const char *mod[]  = {"*", "k", "kb", "m", "mb",  "g", "gb",  NULL}; /* first item * not used */
311    const int64_t mult[] = {1,             /* byte */
312                            1024,          /* kilobyte */
313                            1000,          /* kb kilobyte */
314                            1048576,       /* megabyte */
315                            1000000,       /* mb megabyte */
316                            1073741824,    /* gigabyte */
317                            1000000000};   /* gb gigabyte */
318
319    if (!get_modifier(str, num_str, sizeof(num_str), mod_str, sizeof(mod_str))) {
320       return 0;
321    }
322    /* Now find the multiplier corresponding to the modifier */
323    mod_len = strlen(mod_str);
324    if (mod_len == 0) {
325       i = 0;                          /* default with no modifier = 1 */
326    } else {
327       for (i=0; mod[i]; i++) {
328          if (strncasecmp(mod_str, mod[i], mod_len) == 0) {
329             break;
330          }
331       }
332       if (mod[i] == NULL) {
333          return false;
334       }
335    }
336    Dmsg2(900, "str=%s: mult=%d\n", str, mult[i]);
337    errno = 0;
338    val = strtod(num_str, NULL);
339    if (errno != 0 || val < 0) {
340       return false;
341    }
342    *value = (utime_t)(val * mult[i]);
343    return true;
344 }
345
346 /*
347  * Check if specified string is a number or not.
348  *  Taken from SQLite, cool, thanks.
349  */
350 bool is_a_number(const char *n)
351 {
352    bool digit_seen = false;
353
354    if( *n == '-' || *n == '+' ) {
355       n++;
356    }
357    while (B_ISDIGIT(*n)) {
358       digit_seen = true;
359       n++;
360    }
361    if (digit_seen && *n == '.') {
362       n++;
363       while (B_ISDIGIT(*n)) { n++; }
364    }
365    if (digit_seen && (*n == 'e' || *n == 'E')
366        && (B_ISDIGIT(n[1]) || ((n[1]=='-' || n[1] == '+') && B_ISDIGIT(n[2])))) {
367       n += 2;                         /* skip e- or e+ or e digit */
368       while (B_ISDIGIT(*n)) { n++; }
369    }
370    return digit_seen && *n==0;
371 }
372
373 /*
374  * Check if the specified string is an integer
375  */
376 bool is_an_integer(const char *n)
377 {
378    bool digit_seen = false;
379    while (B_ISDIGIT(*n)) {
380       digit_seen = true;
381       n++;
382    }
383    return digit_seen && *n==0;
384 }
385
386 /*
387  * Check if Bacula Resoure Name is valid
388  */
389 /*
390  * Check if the Volume name has legal characters
391  * If ua is non-NULL send the message
392  */
393 bool is_name_valid(char *name, POOLMEM **msg)
394 {
395    int len;
396    char *p;
397    /* Special characters to accept */
398    const char *accept = ":.-_ ";
399
400    /* Restrict the characters permitted in the Volume name */
401    for (p=name; *p; p++) {
402       if (B_ISALPHA(*p) || B_ISDIGIT(*p) || strchr(accept, (int)(*p))) {
403          continue;
404       }
405       if (msg) {
406          Mmsg(msg, _("Illegal character \"%c\" in name.\n"), *p);
407       }
408       return false;
409    }
410    len = strlen(name);
411    if (len >= MAX_NAME_LENGTH) {
412       if (msg) {
413          Mmsg(msg, _("Name too long.\n"));
414       }
415       return false;
416    }
417    if (len == 0) {
418       if (msg) {
419          Mmsg(msg,  _("Volume name must be at least one character long.\n"));
420       }
421       return false;
422    }
423    return true;
424 }
425
426
427
428 /*
429  * Add commas to a string, which is presumably
430  * a number.
431  */
432 char *add_commas(char *val, char *buf)
433 {
434    int len, nc;
435    char *p, *q;
436    int i;
437
438    if (val != buf) {
439       strcpy(buf, val);
440    }
441    len = strlen(buf);
442    if (len < 1) {
443       len = 1;
444    }
445    nc = (len - 1) / 3;
446    p = buf+len;
447    q = p + nc;
448    *q-- = *p--;
449    for ( ; nc; nc--) {
450       for (i=0; i < 3; i++) {
451           *q-- = *p--;
452       }
453       *q-- = ',';
454    }
455    return buf;
456 }
457
458 #ifdef TEST_PROGRAM
459 void d_msg(const char*, int, int, const char*, ...)
460 {}
461 int main(int argc, char *argv[])
462 {
463    char *str[] = {"3", "3n", "3 hours", "3.5 day", "3 week", "3 m", "3 q", "3 years"};
464    utime_t val;
465    char buf[100];
466    char outval[100];
467
468    for (int i=0; i<8; i++) {
469       strcpy(buf, str[i]);
470       if (!duration_to_utime(buf, &val)) {
471          printf("Error return from duration_to_utime for in=%s\n", str[i]);
472          continue;
473       }
474       edit_utime(val, outval);
475       printf("in=%s val=%" lld " outval=%s\n", str[i], val, outval);
476    }
477 }
478 #endif