]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/tools/testfind.c
- Move Python variables from Job to Bacula. They are
[bacula/bacula] / bacula / src / tools / testfind.c
index c185f7e9c9a74cffaebecb82f83bcfcea22fdcea..0b676f02f2ea8e81fe7fb1ff97c5ba7d03f6b0e3 100644 (file)
@@ -1,12 +1,35 @@
-/*  
+/*
  * Test program for find files
  */
+/*
+   Copyright (C) 2000-2005 Kern Sibbald
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License
+   version 2 as amended with additional clauses defined in the
+   file LICENSE in the main source directory.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+   the file LICENSE for additional details.
+
+ */
 
 #include "bacula.h"
 #include "findlib/find.h"
-#include "jcr.h"
 
 
+#if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
+int win32_client = 1;
+#else
+int win32_client = 0;
+#endif
+
+/* Dummy functions */
+int generate_daemon_event(JCR *jcr, const char *event) { return 1; }
+int generate_job_event(JCR *jcr, const char *event) { return 1; }
+
 /* Global variables */
 static int num_files = 0;
 static int max_file_len = 0;
@@ -15,8 +38,9 @@ static int trunc_fname = 0;
 static int trunc_path = 0;
 static int attrs = 0;
 
+static JCR *jcr;
 
-static int print_file(FF_PKT *ff, void *pkt);
+static int print_file(FF_PKT *ff, void *pkt, bool);
 static void count_files(FF_PKT *ff);
 
 static void usage()
@@ -26,15 +50,17 @@ static void usage()
 "Usage: testfind [-d debug_level] [-] [pattern1 ...]\n"
 "       -a          print extended attributes (Win32 debug)\n"
 "       -dnn        set debug level to nn\n"
+"       -e          specify file of exclude patterns\n"
+"       -i          specify file of include patterns\n"
 "       -           read pattern(s) from stdin\n"
 "       -?          print this message.\n"
 "\n"
-"Patterns are file inclusion -- normally directories.\n"
+"Patterns are used for file inclusion -- normally directories.\n"
 "Debug level >= 1 prints each file found.\n"
 "Debug level >= 10 prints path/file for catalog.\n"
-"Errors always printed.\n"
-"Files/paths truncated is number with len > 255.\n"
-"Truncation is only in catalog.\n"
+"Errors are always printed.\n"
+"Files/paths truncated is the number of files/paths with len > 255.\n"
+"Truncation is only in the catalog.\n"
 "\n"));
 
    exit(1);
@@ -46,63 +72,104 @@ main (int argc, char *const *argv)
 {
    FF_PKT *ff;
    char name[1000];
-   int i, ch;
+   int i, ch, hard_links;
+   char *inc = NULL;
+   char *exc = NULL;
+   FILE *fd;
 
-   while ((ch = getopt(argc, argv, "ad:?")) != -1) {
+   while ((ch = getopt(argc, argv, "ad:e:i:?")) != -1) {
       switch (ch) {
          case 'a':                    /* print extended attributes *debug* */
-           attrs = 1;
-           break;
+            attrs = 1;
+            break;
 
          case 'd':                    /* set debug level */
-           debug_level = atoi(optarg);
-           if (debug_level <= 0) {
-              debug_level = 1; 
-           }
-           break;
+            debug_level = atoi(optarg);
+            if (debug_level <= 0) {
+               debug_level = 1;
+            }
+            break;
+
+         case 'e':                    /* exclude patterns */
+            exc = optarg;
+            break;
+
+         case 'i':                    /* include patterns */
+            inc = optarg;
+            break;
 
          case '?':
-        default:
-           usage();
+         default:
+            usage();
 
-      }  
+      }
    }
    argc -= optind;
    argv += optind;
 
-  ff = init_find_files();
-   if (argc == 0) {
-     add_fname_to_include_list(ff, 0, "/"); /* default to / */
-  } else {   
+   jcr = new_jcr(sizeof(JCR), NULL);
+
+   ff = init_find_files();
+   if (argc == 0 && !inc) {
+      add_fname_to_include_list(ff, 0, "/"); /* default to / */
+   } else {
       for (i=0; i < argc; i++) {
-        if (strcmp(argv[i], "-") == 0) {
-          while (fgets(name, sizeof(name)-1, stdin)) {
-             strip_trailing_junk(name);
-             add_fname_to_include_list(ff, 0, name); 
-          }
-          continue;
-       }
-       add_fname_to_include_list(ff, 0, argv[i]); 
-     }
-  }
-
-  find_files(ff, print_file, NULL);
-  term_find_files(ff);
-  
-   printf(_("\
-Total files    : %d\n\
-Max file length: %d\n\
-Max path length: %d\n\
-Files truncated: %d\n\
-Paths truncated: %d\n"),
+         if (strcmp(argv[i], "-") == 0) {
+             while (fgets(name, sizeof(name)-1, stdin)) {
+                strip_trailing_junk(name);
+                add_fname_to_include_list(ff, 0, name);
+              }
+              continue;
+         }
+         add_fname_to_include_list(ff, 0, argv[i]);
+      }
+   }
+   if (inc) {
+      fd = fopen(inc, "r");
+      if (!fd) {
+         printf("Could not open include file: %s\n", inc);
+         exit(1);
+      }
+      while (fgets(name, sizeof(name)-1, fd)) {
+         strip_trailing_junk(name);
+         add_fname_to_include_list(ff, 0, name);
+      }
+      fclose(fd);
+   }
+
+   if (exc) {
+      fd = fopen(exc, "r");
+      if (!fd) {
+         printf("Could not open exclude file: %s\n", exc);
+         exit(1);
+      }
+      while (fgets(name, sizeof(name)-1, fd)) {
+         strip_trailing_junk(name);
+         add_fname_to_exclude_list(ff, name);
+      }
+      fclose(fd);
+   }
+   match_files(jcr, ff, print_file, NULL);
+   term_include_exclude_files(ff);
+   hard_links = term_find_files(ff);
+
+   printf(_(""
+"Total files    : %d\n"
+"Max file length: %d\n"
+"Max path length: %d\n"
+"Files truncated: %d\n"
+"Paths truncated: %d\n"
+"Hard links     : %d\n"),
      num_files, max_file_len, max_path_len,
-     trunc_fname, trunc_path);
-  
-  sm_dump(False);
+     trunc_fname, trunc_path, hard_links);
+
+  free_jcr(jcr);
+  close_memory_pool();
+  sm_dump(false);
   exit(0);
 }
 
-static int print_file(FF_PKT *ff, void *pkt)
+static int print_file(FF_PKT *ff, void *pkt, bool top_level) 
 {
 
    switch (ff->type) {
@@ -120,7 +187,7 @@ static int print_file(FF_PKT *ff, void *pkt)
          printf("Empty: %s\n", ff->fname);
       }
       count_files(ff);
-      break; 
+      break;
    case FT_REG:
       if (debug_level == 1) {
          printf("%s\n", ff->fname);
@@ -137,12 +204,24 @@ static int print_file(FF_PKT *ff, void *pkt)
       }
       count_files(ff);
       break;
-   case FT_DIR:
-      if (debug_level == 1) {
-         printf("%s\n", ff->fname);
-      } else if (debug_level > 1) {
-         printf("Dir: %s\n", ff->fname);
+   case FT_DIRBEGIN:
+      return 1;
+   case FT_NORECURSE:
+   case FT_NOFSCHG:
+   case FT_INVALIDFS:
+   case FT_DIREND:
+      if (debug_level) {
+         char errmsg[100] = "";
+         if (ff->type == FT_NORECURSE) {
+            bstrncpy(errmsg, "\t[will not descend: recursion turned off]", sizeof(errmsg));
+         } else if (ff->type == FT_NOFSCHG) {
+            bstrncpy(errmsg, "\t[will not descend: file system change not allowed]", sizeof(errmsg));
+         } else if (ff->type == FT_INVALIDFS) {
+            bstrncpy(errmsg, "\t[will not descend: disallowed file system]", sizeof(errmsg));
+         }
+         printf("%s%s%s\n", (debug_level > 1 ? "Dir: " : ""), ff->fname, errmsg);
       }
+      ff->type = FT_DIREND;
       count_files(ff);
       break;
    case FT_SPEC:
@@ -168,12 +247,6 @@ static int print_file(FF_PKT *ff, void *pkt)
    case FT_ISARCH:
       printf(_("Err: Attempt to backup archive. Not saved. %s\n"), ff->fname);
       break;
-   case FT_NORECURSE:
-      printf(_("Recursion turned off. Directory not entered. %s\n"), ff->fname);
-      break;
-   case FT_NOFSCHG:
-      printf(_("Skip: File system change prohibited. Directory not entered. %s\n"), ff->fname);
-      break;
    case FT_NOOPEN:
       printf(_("Err: Could not open directory %s: %s\n"), ff->fname, strerror(errno));
       break;
@@ -192,7 +265,7 @@ static int print_file(FF_PKT *ff, void *pkt)
    return 1;
 }
 
-static void count_files(FF_PKT *ar) 
+static void count_files(FF_PKT *ar)
 {
    int fnl, pnl;
    char *l, *p;
@@ -201,7 +274,7 @@ static void count_files(FF_PKT *ar)
 
    num_files++;
 
-   /* Find path without the filename.  
+   /* Find path without the filename.
     * I.e. everything after the last / is a "filename".
     * OK, maybe it is a directory name, but we treat it like
     * a filename. If we don't find a / then the whole name
@@ -209,17 +282,17 @@ static void count_files(FF_PKT *ar)
     */
    for (p=l=ar->fname; *p; p++) {
       if (*p == '/') {
-        l = p;                       /* set pos of last slash */
+         l = p;                       /* set pos of last slash */
       }
    }
    if (*l == '/') {                   /* did we find a slash? */
-      l++;                           /* yes, point to filename */
-   } else {                          /* no, whole thing must be path name */
+      l++;                            /* yes, point to filename */
+   } else {                           /* no, whole thing must be path name */
       l = p;
    }
 
    /* If filename doesn't exist (i.e. root directory), we
-    * simply create a blank name consisting of a single 
+    * simply create a blank name consisting of a single
     * space. This makes handling zero length filenames
     * easier.
     */
@@ -233,14 +306,14 @@ static void count_files(FF_PKT *ar)
       trunc_fname++;
    }
    if (fnl > 0) {
-      strncpy(file, l, fnl);         /* copy filename */
+      strncpy(file, l, fnl);          /* copy filename */
       file[fnl] = 0;
    } else {
       file[0] = ' ';                  /* blank filename */
       file[1] = 0;
    }
 
-   pnl = l - ar->fname;    
+   pnl = l - ar->fname;
    if (pnl > max_path_len) {
       max_path_len = pnl;
    }
@@ -262,3 +335,5 @@ static void count_files(FF_PKT *ar)
    }
 
 }
+
+bool python_set_prog(JCR*, char const*) { return false; }