]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/scan.c
Use jcr->setJobStatus() in favor of set_jcr_job_status(jcr...)
[bacula/bacula] / bacula / src / lib / scan.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *   scan.c -- scanning routines for Bacula
30  *
31  *    Kern Sibbald, MM  separated from util.c MMIII
32  *
33  */
34
35
36 #include "bacula.h"
37 #include "jcr.h"
38 #include "findlib/find.h"
39
40 /* Strip leading space from command line arguments */
41 void strip_leading_space(char *str)
42 {
43    char *p = str;
44    while (B_ISSPACE(*p)) {
45       p++;
46    }
47    if (p != str) {
48       strcpy(str, p);
49    }
50 }
51
52
53 /* Strip any trailing junk from the command */
54 void strip_trailing_junk(char *cmd)
55 {
56    char *p;
57    p = cmd + strlen(cmd) - 1;
58
59    /* strip trailing junk from command */
60    while ((p >= cmd) && (*p == '\n' || *p == '\r' || *p == ' '))
61       *p-- = 0;
62 }
63
64 /* Strip any trailing newline characters from the string */
65 void strip_trailing_newline(char *cmd)
66 {
67    char *p;
68    p = cmd + strlen(cmd) - 1;
69
70    while ((p >= cmd) && (*p == '\n' || *p == '\r'))
71       *p-- = 0;
72 }
73
74 /* Strip any trailing slashes from a directory path */
75 void strip_trailing_slashes(char *dir)
76 {
77    char *p;
78    p = dir + strlen(dir) - 1;
79
80    /* strip trailing slashes */
81    while (p >= dir && IsPathSeparator(*p))
82       *p-- = 0;
83 }
84
85 /*
86  * Skip spaces
87  *  Returns: 0 on failure (EOF)
88  *           1 on success
89  *           new address in passed parameter
90  */
91 bool skip_spaces(char **msg)
92 {
93    char *p = *msg;
94    if (!p) {
95       return false;
96    }
97    while (*p && B_ISSPACE(*p)) {
98       p++;
99    }
100    *msg = p;
101    return *p ? true : false;
102 }
103
104 /*
105  * Skip nonspaces
106  *  Returns: 0 on failure (EOF)
107  *           1 on success
108  *           new address in passed parameter
109  */
110 bool skip_nonspaces(char **msg)
111 {
112    char *p = *msg;
113
114    if (!p) {
115       return false;
116    }
117    while (*p && !B_ISSPACE(*p)) {
118       p++;
119    }
120    *msg = p;
121    return *p ? true : false;
122 }
123
124 /* folded search for string - case insensitive */
125 int
126 fstrsch(const char *a, const char *b)   /* folded case search */
127 {
128    const char *s1,*s2;
129    char c1, c2;
130
131    s1=a;
132    s2=b;
133    while (*s1) {                      /* do it the fast way */
134       if ((*s1++ | 0x20) != (*s2++ | 0x20))
135          return 0;                    /* failed */
136    }
137    while (*a) {                       /* do it over the correct slow way */
138       if (B_ISUPPER(c1 = *a)) {
139          c1 = tolower((int)c1);
140       }
141       if (B_ISUPPER(c2 = *b)) {
142          c2 = tolower((int)c2);
143       }
144       if (c1 != c2) {
145          return 0;
146       }
147       a++;
148       b++;
149    }
150    return 1;
151 }
152
153
154 /*
155  * Return next argument from command line.  Note, this
156  *   routine is destructive because it stored 0 at the end
157  *   of each argument.
158  * Called with pointer to pointer to command line. This
159  *   pointer is updated to point to the remainder of the
160  *   command line.
161  * 
162  * Returns pointer to next argument -- don't store the result
163  *   in the pointer you passed as an argument ...
164  *   The next argument is terminated by a space unless within
165  *   quotes. Double quote characters (unless preceded by a \) are
166  *   stripped.
167  *   
168  */
169 char *next_arg(char **s)
170 {
171    char *p, *q, *n;
172    bool in_quote = false;
173
174    /* skip past spaces to next arg */
175    for (p=*s; *p && B_ISSPACE(*p); ) {
176       p++;
177    }
178    Dmsg1(900, "Next arg=%s\n", p);
179    for (n = q = p; *p ; ) {
180       if (*p == '\\') {                 /* slash? */
181          p++;                           /* yes, skip it */
182          if (*p) {
183             *q++ = *p++;
184          } else {
185             *q++ = *p;
186          }
187          continue;
188       }
189       if (*p == '"') {                  /* start or end of quote */
190          p++;
191          in_quote = !in_quote;          /* change state */
192          continue;
193       }
194       if (!in_quote && B_ISSPACE(*p)) { /* end of field */
195          p++;
196          break;
197       }
198       *q++ = *p++;
199    }
200    *q = 0;
201    *s = p;
202    Dmsg2(900, "End arg=%s next=%s\n", n, p);
203    return n;
204 }
205
206 /*
207  * This routine parses the input command line.
208  * It makes a copy in args, then builds an
209  *  argc, argk, argv list where:
210  *
211  *  argc = count of arguments
212  *  argk[i] = argument keyword (part preceding =)
213  *  argv[i] = argument value (part after =)
214  *
215  *  example:  arg1 arg2=abc arg3=
216  *
217  *  argc = c
218  *  argk[0] = arg1
219  *  argv[0] = NULL
220  *  argk[1] = arg2
221  *  argv[1] = abc
222  *  argk[2] = arg3
223  *  argv[2] =
224  */
225 int parse_args(POOLMEM *cmd, POOLMEM **args, int *argc,
226                char **argk, char **argv, int max_args)
227 {
228    char *p;
229
230    parse_args_only(cmd, args, argc, argk, argv, max_args);
231
232    /* Separate keyword and value */
233    for (int i=0; i < *argc; i++) {
234       p = strchr(argk[i], '=');
235       if (p) {
236          *p++ = 0;                    /* terminate keyword and point to value */
237       }
238       argv[i] = p;                    /* save ptr to value or NULL */
239    }
240 #ifdef xxx_debug
241    for (int i=0; i < *argc; i++) {
242       Pmsg3(000, "Arg %d: kw=%s val=%s\n", i, argk[i], argv[i]?argv[i]:"NULL");
243    }
244 #endif
245    return 1;
246 }
247
248
249 /*
250  * This routine parses the input command line.
251  *   It makes a copy in args, then builds an
252  *   argc, argk, but no argv (values).
253  *   This routine is useful for scanning command lines where the data 
254  *   is a filename and no keywords are expected.  If we scan a filename
255  *   for keywords, any = in the filename will be interpreted as the
256  *   end of a keyword, and this is not good.
257  *
258  *  argc = count of arguments
259  *  argk[i] = argument keyword (part preceding =)
260  *  argv[i] = NULL                         
261  *
262  *  example:  arg1 arg2=abc arg3=
263  *
264  *  argc = c
265  *  argk[0] = arg1
266  *  argv[0] = NULL
267  *  argk[1] = arg2=abc
268  *  argv[1] = NULL
269  *  argk[2] = arg3
270  *  argv[2] =
271  */
272 int parse_args_only(POOLMEM *cmd, POOLMEM **args, int *argc,
273                     char **argk, char **argv, int max_args)
274 {
275    char *p, *n;
276
277    pm_strcpy(args, cmd);
278    strip_trailing_junk(*args);
279    p = *args;
280    *argc = 0;
281    /* Pick up all arguments */
282    while (*argc < max_args) {
283       n = next_arg(&p);
284       if (*n) {
285          argk[*argc] = n;
286          argv[(*argc)++] = NULL;
287       } else {
288          break;
289       }
290    }
291    return 1;
292 }
293
294
295 /*
296  * Given a full filename, split it into its path
297  *  and filename parts. They are returned in pool memory
298  *  in the arguments provided.
299  */
300 void split_path_and_filename(const char *fname, POOLMEM **path, int *pnl,
301         POOLMEM **file, int *fnl)
302 {
303    const char *f;
304    int slen;
305    int len = slen = strlen(fname);
306
307    /*
308     * Find path without the filename.
309     * I.e. everything after the last / is a "filename".
310     * OK, maybe it is a directory name, but we treat it like
311     * a filename. If we don't find a / then the whole name
312     * must be a path name (e.g. c:).
313     */
314    f = fname + len - 1;
315    /* "strip" any trailing slashes */
316    while (slen > 1 && IsPathSeparator(*f)) {
317       slen--;
318       f--;
319    }
320    /* Walk back to last slash -- begin of filename */
321    while (slen > 0 && !IsPathSeparator(*f)) {
322       slen--;
323       f--;
324    }
325    if (IsPathSeparator(*f)) {         /* did we find a slash? */
326       f++;                            /* yes, point to filename */
327    } else {                           /* no, whole thing must be path name */
328       f = fname;
329    }
330    Dmsg2(200, "after strip len=%d f=%s\n", len, f);
331    *fnl = fname - f + len;
332    if (*fnl > 0) {
333       *file = check_pool_memory_size(*file, *fnl+1);
334       memcpy(*file, f, *fnl);    /* copy filename */
335    }
336    (*file)[*fnl] = 0;
337
338    *pnl = f - fname;
339    if (*pnl > 0) {
340       *path = check_pool_memory_size(*path, *pnl+1);
341       memcpy(*path, fname, *pnl);
342    }
343    (*path)[*pnl] = 0;
344
345    Dmsg2(200, "pnl=%d fnl=%d\n", *pnl, *fnl);
346    Dmsg3(200, "split fname=%s path=%s file=%s\n", fname, *path, *file);
347 }
348
349 /*
350  * Extremely simple sscanf. Handles only %(u,d,ld,qd,qu,lu,lld,llu,c,nns)
351  */
352 const int BIG = 1000;
353 int bsscanf(const char *buf, const char *fmt, ...)
354 {
355    va_list ap;
356    int count = 0;
357    void *vp;
358    char *cp;
359    int l = 0;
360    int max_len = BIG;
361    uint64_t value;
362    bool error = false;
363    bool negative;
364
365    va_start(ap, fmt);
366    while (*fmt && !error) {
367 //    Dmsg1(000, "fmt=%c\n", *fmt);
368       if (*fmt == '%') {
369          fmt++;
370 //       Dmsg1(000, "Got %% nxt=%c\n", *fmt);
371 switch_top:
372          switch (*fmt++) {
373          case 'u':
374             value = 0;
375             while (B_ISDIGIT(*buf)) {
376                value = B_TIMES10(value) + *buf++ - '0';
377             }
378             vp = (void *)va_arg(ap, void *);
379 //          Dmsg2(000, "val=%lld at 0x%lx\n", value, (long unsigned)vp);
380             if (l == 0) {
381                *((int *)vp) = (int)value;
382             } else if (l == 1) {
383                *((uint32_t *)vp) = (uint32_t)value;
384 //             Dmsg0(000, "Store 32 bit int\n");
385             } else {
386                *((uint64_t *)vp) = (uint64_t)value;
387 //             Dmsg0(000, "Store 64 bit int\n");
388             }
389             count++;
390             l = 0;
391             break;
392          case 'd':
393             value = 0;
394             if (*buf == '-') {
395                negative = true;
396                buf++;
397             } else {
398                negative = false;
399             }
400             while (B_ISDIGIT(*buf)) {
401                value = B_TIMES10(value) + *buf++ - '0';
402             }
403             if (negative) {
404                value = -value;
405             }
406             vp = (void *)va_arg(ap, void *);
407 //          Dmsg2(000, "val=%lld at 0x%lx\n", value, (long unsigned)vp);
408             if (l == 0) {
409                *((int *)vp) = (int)value;
410             } else if (l == 1) {
411                *((int32_t *)vp) = (int32_t)value;
412 //             Dmsg0(000, "Store 32 bit int\n");
413             } else {
414                *((int64_t *)vp) = (int64_t)value;
415 //             Dmsg0(000, "Store 64 bit int\n");
416             }
417             count++;
418             l = 0;
419             break;
420          case 'l':
421 //          Dmsg0(000, "got l\n");
422             l = 1;
423             if (*fmt == 'l') {
424                l++;
425                fmt++;
426             }
427             if (*fmt == 'd' || *fmt == 'u') {
428                goto switch_top;
429             }
430 //          Dmsg1(000, "fmt=%c !=d,u\n", *fmt);
431             error = true;
432             break;
433          case 'q':
434             l = 2;
435             if (*fmt == 'd' || *fmt == 'u') {
436                goto switch_top;
437             }
438 //          Dmsg1(000, "fmt=%c !=d,u\n", *fmt);
439             error = true;
440             break;
441          case 's':
442 //          Dmsg1(000, "Store string max_len=%d\n", max_len);
443             cp = (char *)va_arg(ap, char *);
444             while (*buf && !B_ISSPACE(*buf) && max_len-- > 0) {
445                *cp++ = *buf++;
446             }
447             *cp = 0;
448             count++;
449             max_len = BIG;
450             break;
451          case 'c':
452             cp = (char *)va_arg(ap, char *);
453             *cp = *buf++;
454             count++;
455             break;
456          case '%':
457             if (*buf++ != '%') {
458                error = true;
459             }
460             break;
461          default:
462             fmt--;
463             max_len = 0;
464             while (B_ISDIGIT(*fmt)) {
465                max_len = B_TIMES10(max_len) + *fmt++ - '0';
466             }
467 //          Dmsg1(000, "Default max_len=%d\n", max_len);
468             if (*fmt == 's') {
469                goto switch_top;
470             }
471 //          Dmsg1(000, "Default c=%c\n", *fmt);
472             error = true;
473             break;                    /* error: unknown format */
474          }
475          continue;
476
477       /* White space eats zero or more whitespace */
478       } else if (B_ISSPACE(*fmt)) {
479          fmt++;
480          while (B_ISSPACE(*buf)) {
481             buf++;
482          }
483       /* Plain text must match */
484       } else if (*buf++ != *fmt++) {
485 //       Dmsg2(000, "Mismatch buf=%c fmt=%c\n", *--buf, *--fmt);
486          error = true;
487          break;
488       }
489    }
490    va_end(ap);
491 // Dmsg2(000, "Error=%d count=%d\n", error, count);
492    if (error) {
493       count = -1;
494    }
495    return count;
496 }
497
498 #ifdef TEST_PROGRAM
499 int main(int argc, char *argv[])
500 {
501    char buf[100];
502    uint32_t val32;
503    uint64_t val64;
504    uint32_t FirstIndex, LastIndex, StartFile, EndFile, StartBlock, EndBlock;
505    char Job[200];
506    int cnt;
507    char *helloreq= "Hello *UserAgent* calling\n";
508    char *hello = "Hello %127s calling\n";
509    char *catreq =
510 "CatReq Job=NightlySave.2004-06-11_19.11.32 CreateJobMedia FirstIndex=1 LastIndex=114 StartFile=0 EndFile=0 StartBlock=208 EndBlock=2903248";
511 static char Create_job_media[] = "CatReq Job=%127s CreateJobMedia "
512   "FirstIndex=%u LastIndex=%u StartFile=%u EndFile=%u "
513   "StartBlock=%u EndBlock=%u\n";
514 static char OK_media[] = "1000 OK VolName=%127s VolJobs=%u VolFiles=%u"
515    " VolBlocks=%u VolBytes=%" lld " VolMounts=%u VolErrors=%u VolWrites=%u"
516    " MaxVolBytes=%" lld " VolCapacityBytes=%" lld " VolStatus=%20s"
517    " Slot=%d MaxVolJobs=%u MaxVolFiles=%u InChanger=%d"
518    " VolReadTime=%" lld " VolWriteTime=%" lld;
519    char *media =
520 "1000 OK VolName=TestVolume001 VolJobs=0 VolFiles=0 VolBlocks=0 VolBytes=1 VolMounts=0 VolErrors=0 VolWrites=0 MaxVolBytes=0 VolCapacityBytes=0 VolStatus=Append Slot=0 MaxVolJobs=0 MaxVolFiles=0 InChanger=1 VolReadTime=0 VolWriteTime=0";
521 struct VOLUME_CAT_INFO {
522    /* Media info for the current Volume */
523    uint32_t VolCatJobs;               /* number of jobs on this Volume */
524    uint32_t VolCatFiles;              /* Number of files */
525    uint32_t VolCatBlocks;             /* Number of blocks */
526    uint64_t VolCatBytes;              /* Number of bytes written */
527    uint32_t VolCatMounts;             /* Number of mounts this volume */
528    uint32_t VolCatErrors;             /* Number of errors this volume */
529    uint32_t VolCatWrites;             /* Number of writes this volume */
530    uint32_t VolCatReads;              /* Number of reads this volume */
531    uint64_t VolCatRBytes;             /* Number of bytes read */
532    uint32_t VolCatRecycles;           /* Number of recycles this volume */
533    int32_t  Slot;                     /* Slot in changer */
534    bool     InChanger;                /* Set if vol in current magazine */
535    uint32_t VolCatMaxJobs;            /* Maximum Jobs to write to volume */
536    uint32_t VolCatMaxFiles;           /* Maximum files to write to volume */
537    uint64_t VolCatMaxBytes;           /* Max bytes to write to volume */
538    uint64_t VolCatCapacityBytes;      /* capacity estimate */
539    uint64_t VolReadTime;              /* time spent reading */
540    uint64_t VolWriteTime;             /* time spent writing this Volume */
541    char VolCatStatus[20];             /* Volume status */
542    char VolCatName[MAX_NAME_LENGTH];  /* Desired volume to mount */
543 };
544    struct VOLUME_CAT_INFO vol;
545
546 #ifdef xxx
547    bsscanf("Hello_world 123 1234", "%120s %ld %lld", buf, &val32, &val64);
548    printf("%s %d %lld\n", buf, val32, val64);
549
550    *Job=0;
551    cnt = bsscanf(catreq, Create_job_media, &Job,
552       &FirstIndex, &LastIndex, &StartFile, &EndFile,
553       &StartBlock, &EndBlock);
554    printf("cnt=%d Job=%s\n", cnt, Job);
555    cnt = bsscanf(helloreq, hello, &Job);
556    printf("cnt=%d Agent=%s\n", cnt, Job);
557 #endif
558    cnt = bsscanf(media, OK_media,
559                vol.VolCatName,
560                &vol.VolCatJobs, &vol.VolCatFiles,
561                &vol.VolCatBlocks, &vol.VolCatBytes,
562                &vol.VolCatMounts, &vol.VolCatErrors,
563                &vol.VolCatWrites, &vol.VolCatMaxBytes,
564                &vol.VolCatCapacityBytes, vol.VolCatStatus,
565                &vol.Slot, &vol.VolCatMaxJobs, &vol.VolCatMaxFiles,
566                &vol.InChanger, &vol.VolReadTime, &vol.VolWriteTime);
567    printf("cnt=%d Vol=%s\n", cnt, vol.VolCatName);
568
569 }
570
571 #endif