9 from collections
import namedtuple
18 return path[:len(self.
path)+1] == self.
path+
"/"\
19 or path == self.
path or self.
path==
""
22 return "^" + self.
path +
"/*"
24 return "^" + (self.
path if self.
path !=
"" else "/")
26 return self.
path == other.path
28 return self.
path != other.path
30 return self.
path < other.path
32 return self.
path > other.path
35 return stack[-pos]
if len(stack)>0
else None
39 return path[:pos]
if pos>0
else ""
43 for curPoint
in pathsToInclude:
45 if curPoint.action
in exact_parser.dirFlat:
49 specsToInclude.append(specPoint)
51 exact_parser.dirFlat = [
"opendir()"]
56 for curPoint
in pathsToInclude:
58 if curPoint.action
in parent_dir_parser.parentDirFlat:
60 elif curPoint.action
in parent_dir_parser.dirFlat:
64 specsToInclude.append(specPoint)
68 parent_dir_parser.parentDirFlat = [
"open()"]
69 parent_dir_parser.dirFlat = [
"opendir()"]
72 "pdir": parent_dir_parser,
76 class TracePoint(namedtuple(
"TracePoint", [
"path",
"action"])):
78 return self.
path == other.path
80 return self.
path != other.path
82 return self.
path < other.path
84 return self.
path > other.path
88 print(
"Parsing file: " + args.infile)
89 print(
"Output file: " + args.outfile)
90 print(
"Policy: " + args.policy)
91 print(
"Filters: " + (
",".join(args.filters)
if len(args.filters) > 0
else "None"))
94 self.
policy = ParsingPolicies[args.policy]
95 self.
filters = dict([(f+
"()"),
True]
for f
in args.filters)
98 with open(self.
inputName,
"r") as logFile:
99 csvLogReader = csv.reader(logFile, delimiter=',', quotechar=
'"')
101 for r
in csvLogReader
102 if r[3]
not in self.
filters and int(r[1])>=0
103 and r[2]
not in TraceParser.blacklist]:
104 if row[2] ==
"@UNKNOWN":
105 print(
"ERROR: An error occurred during tracing (event code 8)")
107 pathsToInclude.append(
TracePoint(row[2], row[3]))
108 specsToInclude = self.
policy(pathsToInclude)
109 specsToInclude.sort()
113 for specPoint
in specsToInclude:
114 while not peek(workStack).isParentOf(specPoint.path):
117 self.pathSpec.append(el)
118 topEl =
peek(workStack)
119 if topEl.path == specPoint.path
and topEl.mode < specPoint.mode:
121 topEl.mode = specPoint.mode
122 elif topEl.path != specPoint.path:
126 or specPoint.mode!=0:
127 workStack.append(specPoint)
132 print(
"No path specification created. Please call read_log first")
136 specFile.write(str(p)+
"\n")
137 specFile.write(str(self.
pathSpec[-1]))
139 TraceParser.blacklist = [
"/.Trash",
"/.Trash-1000"]
143 argparser = argparse.ArgumentParser()
144 argparser.add_argument(
"--policy",
148 help=
"What files should be included given a traced"
149 +
" action on a certain file")
152 argparser.add_argument(
"infile",
154 help=
"The trace log file")
156 argparser.add_argument(
"outfile",
158 help=
"The output file")
160 argparser.add_argument(
"--filters",
164 choices=[
"open",
"opendir",
"lookup",
"statfs",
"getattr",
"listxattr",
"getxattr",
"readlink"],
165 help=
"Calls which should be filtered")
167 return argparser.parse_args()
172 traceParser.read_log()
173 traceParser.write_spec()
175 if __name__ ==
"__main__":