]> git.gag.com Git - debian/amanda/blobdiff - common-src/util.c
Imported Upstream version 2.5.1p3
[debian/amanda] / common-src / util.c
index a5f88f8d57218a958685313e3313714f27923eba..d168b6f1c3143a7956f24c347baf01bcb23a1232 100644 (file)
@@ -24,7 +24,7 @@
  * file named AUTHORS, in the root directory of this distribution.
  */
 /*
- * $Id: util.c,v 1.42 2006/08/24 01:57:15 paddy_s Exp $
+ * $Id: util.c,v 1.42.2.13 2007/01/24 18:33:29 martinea Exp $
  */
 
 #include "amanda.h"
@@ -37,6 +37,7 @@ int token_pushed;
 
 tok_t tok, pushed_tok;
 val_t tokenval;
+keytab_t *keytable;
 
 int conf_line_num, got_parserror;
 FILE *conf_conf = (FILE *)NULL;
@@ -56,7 +57,7 @@ static int make_socket(void);
 static int connect_port(struct sockaddr_in *addrp, in_port_t port, char *proto,
                        struct sockaddr_in *svaddr, int nonblock);
 
-char conftoken_getc(void);
+int conftoken_getc(void);
 int conftoken_ungetc(int c);
 
 /*
@@ -250,10 +251,10 @@ connect_port(
     }
 
     if(servPort == NULL)
-       dbprintf(("%s: connect_port: Try  port %d: Available   - ",
+       dbprintf(("%s: connect_port: Try  port %d: Available   - \n",
                  debug_prefix_time(NULL), port));
     else {
-       dbprintf(("%s: connect_port: Try  port %d: Owned by %s - ",
+       dbprintf(("%s: connect_port: Try  port %d: Owned by %s - \n",
                  debug_prefix_time(NULL), port, servPort->s_name));
     }
 
@@ -292,7 +293,7 @@ connect_port(
     if (connect(s, (struct sockaddr *)svaddr,
                (socklen_t)sizeof(*svaddr)) == -1 && !nonblock) {
        save_errno = errno;
-       dbprintf(("%s: connect_portrange: connect from %s.%d failed\n",
+       dbprintf(("%s: connect_portrange: connect from %s.%d failed: %s\n",
                  debug_prefix_time(NULL),
                  inet_ntoa(addrp->sin_addr),
                  ntohs(addrp->sin_port),
@@ -305,6 +306,8 @@ connect_port(
        aclose(s);
        errno = save_errno;
        if (save_errno == ECONNREFUSED ||
+           save_errno == EHOSTUNREACH ||
+           save_errno == ENETUNREACH ||
            save_errno == ETIMEDOUT)  {
            return -2   ;
        }
@@ -495,6 +498,11 @@ quote_string(
                 *(s++) = 'f';
                str++;
                continue;
+           } else if (*str == '\\') {
+                *(s++) = '\\';
+                *(s++) = '\\';
+               str++;
+               continue;
            }
             if (*str == '"')
                 *(s++) = '\\';
@@ -1268,7 +1276,7 @@ lookup_keyword(
     /* switch to binary search if performance warrants */
 
     for(kwp = keytable; kwp->keyword != NULL; kwp++) {
-       if (strcmp(kwp->keyword, str) == 0) break;
+       if (strcasecmp(kwp->keyword, str) == 0) break;
     }
     return kwp->token;
 }
@@ -1285,7 +1293,7 @@ unget_conftoken(void)
     return;
 }
 
-char
+int
 conftoken_getc(void)
 {
     if(conf_line == NULL)
@@ -1368,7 +1376,6 @@ get_conftoken(
            buf = tkbuf;
            token_overflow = 0;
            do {
-               if (islower(ch)) ch = toupper(ch);
                if (buf < tkbuf+sizeof(tkbuf)-1) {
                    *buf++ = (char)ch;
                } else {
@@ -1747,9 +1754,9 @@ copy_val_t(
            break;
 
        case CONFTYPE_EXINCLUDE:
-           valdst->v.exinclude.type = valsrc->v.exinclude.type;
            valdst->v.exinclude.optional = valsrc->v.exinclude.optional;
-           valdst->v.exinclude.sl = duplicate_sl(valsrc->v.exinclude.sl);
+           valdst->v.exinclude.sl_list = duplicate_sl(valsrc->v.exinclude.sl_list);
+           valdst->v.exinclude.sl_file = duplicate_sl(valsrc->v.exinclude.sl_file);
            break;
        }
     }
@@ -1789,7 +1796,8 @@ free_val_t(
            break;
 
        case CONFTYPE_EXINCLUDE:
-           free_sl(val->v.exinclude.sl);
+           free_sl(val->v.exinclude.sl_list);
+           free_sl(val->v.exinclude.sl_file);
            break;
     }
     val->seen = 0;
@@ -1812,11 +1820,9 @@ static char buffer_conf_print[1025];
 
 char *
 conf_print(
-    val_t *val)
+    val_t *val,
+    int    str_need_quote)
 {
-    struct tm *stm;
-    int pos;
-
     buffer_conf_print[0] = '\0';
     switch(val->type) {
     case CONFTYPE_INT:
@@ -1854,26 +1860,31 @@ conf_print(
        break;
 
     case CONFTYPE_STRING:
-       buffer_conf_print[0] = '"';
-       if(val->v.s) {
-           strncpy(&buffer_conf_print[1], val->v.s,
-                       SIZEOF(buffer_conf_print) - 1);
-           buffer_conf_print[SIZEOF(buffer_conf_print) - 2] = '\0';
-           buffer_conf_print[strlen(buffer_conf_print)] = '"';
+       if(str_need_quote) {
+           buffer_conf_print[0] = '"';
+           if(val->v.s) {
+               strncpy(&buffer_conf_print[1], val->v.s,
+                       SIZEOF(buffer_conf_print) - 1);
+               buffer_conf_print[SIZEOF(buffer_conf_print) - 2] = '\0';
+               buffer_conf_print[strlen(buffer_conf_print)] = '"';
+           } else {
+               buffer_conf_print[1] = '"';
+               buffer_conf_print[2] = '\0';
+           }
        } else {
-           buffer_conf_print[1] = '"';
-           buffer_conf_print[2] = '\0';
+           if(val->v.s) {
+               strncpy(&buffer_conf_print[0], val->v.s,
+                       SIZEOF(buffer_conf_print));
+               buffer_conf_print[SIZEOF(buffer_conf_print) - 1] = '\0';
+           } else {
+               buffer_conf_print[0] = '\0';
+           }
        }
        break;
 
     case CONFTYPE_TIME:
-       stm = localtime(&val->v.t);
-       if (stm) {
-           snprintf(buffer_conf_print, SIZEOF(buffer_conf_print),
-                    "%d%02d%02d", stm->tm_hour, stm->tm_min, stm->tm_sec);
-       } else {
-           strcpy(buffer_conf_print, "00000");
-       }
+       snprintf(buffer_conf_print, SIZEOF(buffer_conf_print),
+                "%2d%02d", (int)val->v.t/100, (int)val->v.t % 100);
        break;
 
     case CONFTYPE_SL:
@@ -1881,15 +1892,7 @@ conf_print(
        break;
 
     case CONFTYPE_EXINCLUDE:
-       buffer_conf_print[0] = '\0';
-       if(val->v.exinclude.type == 0)
-           strncpy(buffer_conf_print, "LIST ", SIZEOF(buffer_conf_print));
-       else
-           strncpy(buffer_conf_print, "FILE ", SIZEOF(buffer_conf_print));
-       pos = 5;
-       if(val->v.exinclude.optional == 1)
-           strncpy(&buffer_conf_print[pos], "OPTIONAL ", SIZEOF(buffer_conf_print));
-       pos += 9;
+       strcpy(buffer_conf_print, "ERROR: use print_conf_exinclude");
        break;
 
     case CONFTYPE_BOOL:
@@ -2031,6 +2034,54 @@ conf_print(
     return buffer_conf_print;
 }
 
+char *
+conf_print_exinclude(
+    val_t *val,
+    int    str_need_quote,
+    int    file)
+{
+    int    pos;
+    sl_t  *sl;
+    sle_t *excl;
+
+    (void)str_need_quote;
+    buffer_conf_print[0] = '\0';
+    if (val->type != CONFTYPE_EXINCLUDE) {
+       strcpy(buffer_conf_print, "ERROR: conf_print_exinclude called for type != CONFTYPE_EXINCLUDE");
+       return buffer_conf_print;
+    }
+
+    if (file == 0) {
+       sl = val->v.exinclude.sl_list;
+       strncpy(buffer_conf_print, "LIST ", SIZEOF(buffer_conf_print));
+       pos = 5;
+    } else {
+       sl = val->v.exinclude.sl_file;
+       strncpy(buffer_conf_print, "FILE ", SIZEOF(buffer_conf_print));
+       pos = 5;
+    }
+
+    if(val->v.exinclude.optional == 1) {
+       strncpy(&buffer_conf_print[pos], "OPTIONAL ", SIZEOF(buffer_conf_print)-pos);
+       pos += 9;
+    }
+
+    if( sl != NULL) {
+       for(excl = sl->first; excl != NULL; excl = excl->next) {
+           if (pos + 3 + strlen(excl->name) < SIZEOF(buffer_conf_print)) {
+               buffer_conf_print[pos++] = ' ';
+               buffer_conf_print[pos++] = '"';
+               strcpy(&buffer_conf_print[pos], excl->name);
+               pos += strlen(excl->name);
+               buffer_conf_print[pos++] = '"';
+           }
+       }
+    }
+
+    buffer_conf_print[SIZEOF(buffer_conf_print) - 1] = '\0';
+    return buffer_conf_print;
+}
+
 void
 conf_init_string(
     val_t *val,
@@ -2225,9 +2276,9 @@ conf_init_exinclude(
 {
     val->seen = 0;
     val->type = CONFTYPE_EXINCLUDE;
-    val->v.exinclude.type = 0;
     val->v.exinclude.optional = 0;
-    val->v.exinclude.sl = NULL;
+    val->v.exinclude.sl_list = NULL;
+    val->v.exinclude.sl_file = NULL;
 }
 
 void
@@ -2505,8 +2556,9 @@ void
 dump_sockaddr(
        struct sockaddr_in *    sa)
 {
-       dbprintf(("%s: (sockaddr_in *)%p = { %d, %hd, %s }\n",
-               debug_prefix(NULL), sa, sa->sin_family, sa->sin_port,
+       dbprintf(("%s: (sockaddr_in *)%p = { %d, %d, %s }\n",
+               debug_prefix_time(NULL), sa, sa->sin_family,
+               (int)ntohs(sa->sin_port),
                inet_ntoa(sa->sin_addr)));
 }
 
@@ -2586,6 +2638,7 @@ command_overwrite(
     keytab_t        *kt;
     char            *myprefix;
     command_option_t *command_option;
+    int                      duplicate;
 
     if(!command_options) return;
 
@@ -2598,10 +2651,15 @@ command_overwrite(
            /* NOTREACHED */
        }
 
-        for(command_option = command_options; command_option->name != NULL;
+       for(command_option = command_options; command_option->name != NULL;
                                                            command_option++) {
            myprefix = stralloc2(prefix, kt->keyword);
            if(strcasecmp(myprefix, command_option->name) == 0) {
+               duplicate = 0;
+               if (command_option->used == 0 &&
+                   valarray[np->parm].seen == -2) {
+                   duplicate = 1;
+               }
                command_option->used = 1;
                valarray[np->parm].seen = -2;
                if(np->type == CONFTYPE_STRING &&
@@ -2619,8 +2677,12 @@ command_overwrite(
                amfree(conf_line);
                conf_line = conf_char = NULL;
 
-               if(np->validate)
+               if (np->validate)
                    np->validate(np, &valarray[np->parm]);
+               if (duplicate == 1) {
+                   fprintf(stderr,"Duplicate %s option, using %s\n",
+                           command_option->name, command_option->value);
+               }
            }
            amfree(myprefix);
        }
@@ -2639,6 +2701,64 @@ free_new_argv(
 }
 
 
+int copy_file(
+    char  *dst,
+    char  *src,
+    char **errmsg)
+{
+    int     infd, outfd;
+    int     save_errno;
+    ssize_t nb;
+    char    buf[32768];
+    char   *quoted;
+
+    if ((infd = open(src, O_RDONLY)) == -1) {
+       save_errno = errno;
+       quoted = quote_string(src);
+       *errmsg = vstralloc("Can't open file ", quoted, " for reading: %s",
+                           strerror(save_errno));
+       amfree(quoted);
+       return -1;
+    }
+
+    if ((outfd = open(dst, O_WRONLY|O_CREAT, 0600)) == -1) {
+       save_errno = errno;
+       quoted = quote_string(dst);
+       *errmsg = vstralloc("Can't open file ", quoted, " for writting: %s",
+                           strerror(save_errno));
+       amfree(quoted);
+       close(infd);
+       return -1;
+    }
+
+    while((nb=read(infd, &buf, SIZEOF(buf))) > 0) {
+       if(fullwrite(outfd,&buf,(size_t)nb) < nb) {
+           save_errno = errno;
+           quoted = quote_string(dst);
+           *errmsg = vstralloc("Error writing to \"", quoted, "\":",
+                               strerror(save_errno));
+           amfree(quoted);
+           close(infd);
+           close(outfd);
+           return -1;
+       }
+    }
+
+    if (nb < 0) {
+       save_errno = errno;
+       quoted = quote_string(src);
+       *errmsg = vstralloc("Error reading from \"", quoted, "\":",
+                           strerror(save_errno));
+       amfree(quoted);
+       close(infd);
+       close(outfd);
+       return -1;
+    }
+
+    close(infd);
+    close(outfd);
+    return 0;
+}
 #ifndef HAVE_LIBREADLINE
 /*
  * simple readline() replacements