2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
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
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.
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
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.
29 * scan.c -- scanning routines for Bacula
31 * Kern Sibbald, MM separated from util.c MMIII
38 #include "findlib/find.h"
40 /* Strip leading space from command line arguments */
41 void strip_leading_space(char *str)
44 while (B_ISSPACE(*p)) {
53 /* Strip any trailing junk from the command */
54 void strip_trailing_junk(char *cmd)
57 p = cmd + strlen(cmd) - 1;
59 /* strip trailing junk from command */
60 while ((p >= cmd) && (*p == '\n' || *p == '\r' || *p == ' '))
64 /* Strip any trailing newline characters from the string */
65 void strip_trailing_newline(char *cmd)
68 p = cmd + strlen(cmd) - 1;
70 while ((p >= cmd) && (*p == '\n' || *p == '\r'))
74 /* Strip any trailing slashes from a directory path */
75 void strip_trailing_slashes(char *dir)
78 p = dir + strlen(dir) - 1;
80 /* strip trailing slashes */
81 while (p >= dir && IsPathSeparator(*p))
87 * Returns: 0 on failure (EOF)
89 * new address in passed parameter
91 bool skip_spaces(char **msg)
97 while (*p && B_ISSPACE(*p)) {
101 return *p ? true : false;
106 * Returns: 0 on failure (EOF)
108 * new address in passed parameter
110 bool skip_nonspaces(char **msg)
117 while (*p && !B_ISSPACE(*p)) {
121 return *p ? true : false;
124 /* folded search for string - case insensitive */
126 fstrsch(const char *a, const char *b) /* folded case search */
133 while (*s1) { /* do it the fast way */
134 if ((*s1++ | 0x20) != (*s2++ | 0x20))
135 return 0; /* failed */
137 while (*a) { /* do it over the correct slow way */
138 if (B_ISUPPER(c1 = *a)) {
139 c1 = tolower((int)c1);
141 if (B_ISUPPER(c2 = *b)) {
142 c2 = tolower((int)c2);
155 * Return next argument from command line. Note, this
156 * routine is destructive because it stored 0 at the end
158 * Called with pointer to pointer to command line. This
159 * pointer is updated to point to the remainder of the
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
169 char *next_arg(char **s)
172 bool in_quote = false;
174 /* skip past spaces to next arg */
175 for (p=*s; *p && B_ISSPACE(*p); ) {
178 Dmsg1(900, "Next arg=%s\n", p);
179 for (n = q = p; *p ; ) {
180 if (*p == '\\') { /* slash? */
181 p++; /* yes, skip it */
189 if (*p == '"') { /* start or end of quote */
191 in_quote = !in_quote; /* change state */
194 if (!in_quote && B_ISSPACE(*p)) { /* end of field */
202 Dmsg2(900, "End arg=%s next=%s\n", n, p);
207 * This routine parses the input command line.
208 * It makes a copy in args, then builds an
209 * argc, argk, argv list where:
211 * argc = count of arguments
212 * argk[i] = argument keyword (part preceding =)
213 * argv[i] = argument value (part after =)
215 * example: arg1 arg2=abc arg3=
225 int parse_args(POOLMEM *cmd, POOLMEM **args, int *argc,
226 char **argk, char **argv, int max_args)
230 parse_args_only(cmd, args, argc, argk, argv, max_args);
232 /* Separate keyword and value */
233 for (int i=0; i < *argc; i++) {
234 p = strchr(argk[i], '=');
236 *p++ = 0; /* terminate keyword and point to value */
238 argv[i] = p; /* save ptr to value or NULL */
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");
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.
258 * argc = count of arguments
259 * argk[i] = argument keyword (part preceding =)
262 * example: arg1 arg2=abc arg3=
272 int parse_args_only(POOLMEM *cmd, POOLMEM **args, int *argc,
273 char **argk, char **argv, int max_args)
277 pm_strcpy(args, cmd);
278 strip_trailing_junk(*args);
281 /* Pick up all arguments */
282 while (*argc < max_args) {
286 argv[(*argc)++] = NULL;
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.
300 void split_path_and_filename(const char *fname, POOLMEM **path, int *pnl,
301 POOLMEM **file, int *fnl)
305 int len = slen = strlen(fname);
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:).
315 /* "strip" any trailing slashes */
316 while (slen > 1 && IsPathSeparator(*f)) {
320 /* Walk back to last slash -- begin of filename */
321 while (slen > 0 && !IsPathSeparator(*f)) {
325 if (IsPathSeparator(*f)) { /* did we find a slash? */
326 f++; /* yes, point to filename */
327 } else { /* no, whole thing must be path name */
330 Dmsg2(200, "after strip len=%d f=%s\n", len, f);
331 *fnl = fname - f + len;
333 *file = check_pool_memory_size(*file, *fnl+1);
334 memcpy(*file, f, *fnl); /* copy filename */
340 *path = check_pool_memory_size(*path, *pnl+1);
341 memcpy(*path, fname, *pnl);
345 Dmsg2(200, "pnl=%d fnl=%d\n", *pnl, *fnl);
346 Dmsg3(200, "split fname=%s path=%s file=%s\n", fname, *path, *file);
350 * Extremely simple sscanf. Handles only %(u,d,ld,qd,qu,lu,lld,llu,c,nns)
352 const int BIG = 1000;
353 int bsscanf(const char *buf, const char *fmt, ...)
366 while (*fmt && !error) {
367 // Dmsg1(000, "fmt=%c\n", *fmt);
370 // Dmsg1(000, "Got %% nxt=%c\n", *fmt);
375 while (B_ISDIGIT(*buf)) {
376 value = B_TIMES10(value) + *buf++ - '0';
378 vp = (void *)va_arg(ap, void *);
379 // Dmsg2(000, "val=%lld at 0x%lx\n", value, (long unsigned)vp);
381 *((int *)vp) = (int)value;
383 *((uint32_t *)vp) = (uint32_t)value;
384 // Dmsg0(000, "Store 32 bit int\n");
386 *((uint64_t *)vp) = (uint64_t)value;
387 // Dmsg0(000, "Store 64 bit int\n");
400 while (B_ISDIGIT(*buf)) {
401 value = B_TIMES10(value) + *buf++ - '0';
406 vp = (void *)va_arg(ap, void *);
407 // Dmsg2(000, "val=%lld at 0x%lx\n", value, (long unsigned)vp);
409 *((int *)vp) = (int)value;
411 *((int32_t *)vp) = (int32_t)value;
412 // Dmsg0(000, "Store 32 bit int\n");
414 *((int64_t *)vp) = (int64_t)value;
415 // Dmsg0(000, "Store 64 bit int\n");
421 // Dmsg0(000, "got l\n");
427 if (*fmt == 'd' || *fmt == 'u') {
430 // Dmsg1(000, "fmt=%c !=d,u\n", *fmt);
435 if (*fmt == 'd' || *fmt == 'u') {
438 // Dmsg1(000, "fmt=%c !=d,u\n", *fmt);
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) {
452 cp = (char *)va_arg(ap, char *);
464 while (B_ISDIGIT(*fmt)) {
465 max_len = B_TIMES10(max_len) + *fmt++ - '0';
467 // Dmsg1(000, "Default max_len=%d\n", max_len);
471 // Dmsg1(000, "Default c=%c\n", *fmt);
473 break; /* error: unknown format */
477 /* White space eats zero or more whitespace */
478 } else if (B_ISSPACE(*fmt)) {
480 while (B_ISSPACE(*buf)) {
483 /* Plain text must match */
484 } else if (*buf++ != *fmt++) {
485 // Dmsg2(000, "Mismatch buf=%c fmt=%c\n", *--buf, *--fmt);
491 // Dmsg2(000, "Error=%d count=%d\n", error, count);
499 int main(int argc, char *argv[])
504 uint32_t FirstIndex, LastIndex, StartFile, EndFile, StartBlock, EndBlock;
507 char *helloreq= "Hello *UserAgent* calling\n";
508 char *hello = "Hello %127s calling\n";
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;
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 */
544 struct VOLUME_CAT_INFO vol;
547 bsscanf("Hello_world 123 1234", "%120s %ld %lld", buf, &val32, &val64);
548 printf("%s %d %lld\n", buf, val32, val64);
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);
558 cnt = bsscanf(media, OK_media,
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);