Import TBBT (NFS trace replay).
[bluesky.git] / TBBT / trace_init / ns_split.pl
diff --git a/TBBT/trace_init/ns_split.pl b/TBBT/trace_init/ns_split.pl
new file mode 100755 (executable)
index 0000000..b600448
--- /dev/null
@@ -0,0 +1,153 @@
+#!/usr/bin/perl -w\r
+#\r
+# Copyright (c) 2002-2003\r
+#      The President and Fellows of Harvard College.\r
+#\r
+# Redistribution and use in source and binary forms, with or without\r
+# modification, are permitted provided that the following conditions\r
+# are met:\r
+# 1. Redistributions of source code must retain the above copyright\r
+#    notice, this list of conditions and the following disclaimer.\r
+# 2. Redistributions in binary form must reproduce the above copyright\r
+#    notice, this list of conditions and the following disclaimer in the\r
+#    documentation and/or other materials provided with the distribution.\r
+# 3. Neither the name of the University nor the names of its contributors\r
+#    may be used to endorse or promote products derived from this software\r
+#    without specific prior written permission.\r
+#\r
+# THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND\r
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
+# ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE\r
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
+# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
+# SUCH DAMAGE.\r
+#\r
+# $Id: ns_split,v 1.4 2003/07/28 14:27:17 ellard Exp $\r
+\r
+use Getopt::Std;\r
+\r
+$ProgDir = $0;\r
+$ProgDir =~ /(^.*)\//;\r
+$ProgDir = $1;\r
+if (!$ProgDir) {\r
+       $ProgDir = ".";\r
+}\r
+\r
+require "$ProgDir/userUtils.pl";\r
+\r
+@ADD_USERS     = ();\r
+@DEL_USERS     = ();\r
+@ADD_GROUPS    = ();\r
+@DEL_GROUPS    = ();\r
+@ADD_CLIENTS   = ();\r
+@DEL_CLIENTS   = ();\r
+\r
+$Usage =<< ".";\r
+\r
+Usage: $0 [options] [table1.ns [table2.ns ... ]]\r
+\r
+If no table files are specified, then the input is read from stdin.\r
+\r
+Command line options:\r
+\r
+-h             Print usage message and exit.\r
+\r
+-c c1[,c2]*    Include only activity performed by the specified clients.\r
+\r
+-C c1[,c2]*    Exclude activity performed by the specified clients.\r
+\r
+-g g1[,g2]*    Include only activity performed by the specified groups.\r
+\r
+-G g1[,g2]*    Exclude activity performed by the specified groups.\r
+\r
+-u u1[,u2]*    Include only activity performed by the specified users.\r
+\r
+-U u1[,u2]*    Exclude activity performed by the specified users.\r
+\r
+.\r
+\r
+$cmdline = "$0 " . join (' ', @ARGV);\r
+$Options = "c:C:g:G:u:U::";\r
+if (! getopts ($Options)) {\r
+       print STDERR "$0: Incorrect usage.\n";\r
+       print STDERR $Usage;\r
+exit (1);\r
+}\r
+if (defined $opt_h) {\r
+       print $Usage;\r
+       exit (0);\r
+}\r
+\r
+if (defined $opt_u) {\r
+       @ADD_USERS = &logins2uids ($opt_u);\r
+}\r
+if (defined $opt_U) {\r
+       @DEL_USERS = &logins2uids ($opt_U);\r
+}\r
+if (defined $opt_g) {\r
+       @ADD_GROUPS = &groups2gids ($opt_g);\r
+}\r
+if (defined $opt_G) {\r
+       @DEL_GROUPS = &groups2gids ($opt_G);\r
+}\r
+if (defined $opt_c) {\r
+       @ADD_CLIENTS = split (/,/, $opt_c);\r
+}\r
+if (defined $opt_C) {\r
+       @DEL_CLIENTS = split (/,/, $opt_C);\r
+}\r
+\r
+print "#cmdline $cmdline\n";\r
+\r
+while ($l = <>) {\r
+       if ($l =~ /^#/) {\r
+               print $l;\r
+               next;\r
+       }\r
+\r
+       my ($type, $time, $client, $fh, $euid, $egid, @vals) = split (' ', $l);\r
+\r
+       next unless ($type eq 'C');\r
+\r
+       # Something wrong with the input?\r
+\r
+       next if (@vals == 0);\r
+\r
+       if (saveRecord ($client, $fh, $euid, $egid)) {\r
+               print $l;\r
+       }\r
+}\r
+\r
+sub saveRecord {\r
+       my ($client, $fh, $euid, $egid) = @_;\r
+\r
+       if (@ADD_CLIENTS && !grep (/^$client$/, @ADD_CLIENTS)) {\r
+               return 0;\r
+       }\r
+       if (@DEL_CLIENTS && grep (/^$client$/, @DEL_CLIENTS)) {\r
+               return 0;\r
+       }\r
+\r
+       if (@ADD_USERS && !grep (/^$euid$/, @ADD_USERS)) {\r
+               return 0;\r
+       }\r
+       if (@DEL_USERS && grep (/^$euid$/, @DEL_USERS)) {\r
+               return 0;\r
+       }\r
+\r
+       if (@ADD_GROUPS && !grep (/^$egid$/, @ADD_GROUPS)) {\r
+               return 0;\r
+       }\r
+       if (@DEL_GROUPS && grep (/^$egid$/, @DEL_GROUPS)) {\r
+               return 0;\r
+       }\r
+\r
+       return (1);\r
+}\r
+\r
+exit 0;\r