-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclaymore.py
More file actions
executable file
·150 lines (113 loc) · 4.64 KB
/
Copy pathclaymore.py
File metadata and controls
executable file
·150 lines (113 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python3
from directory_enumerator import DirectoryEnumerator
from claymore_conf import dirs, whitelist, frequency
from typing import List
from os import listdir, path, system
from hashlib import sha512
from readwrite import Readwrite
from time import sleep, perf_counter
from inotify import adapters
class Claymore:
def __init__(self):
self.dir_enum = DirectoryEnumerator()
self.all_dirs = []
self.all_files = []
self.hash_dict = {}
self.cached_hash_dict = {}
self.reader = Readwrite().read_file_as_bytes
self.writer = Readwrite().write_to_file
self.frequency = frequency
self.cache_is_warm = False
self.logfile = "/var/log/claymore.log"
def lint(self, message: str):
try:
message = str(message)
print(message + "\n")
self.writer(self.logfile, message + "\n")
except Exception as e:
print("Error! in Lint!! " + str(e))
def notify(self):
pass
def get_subs(self, dir_list: List):
try:
addit = self.all_dirs.append
all_dirs = self.all_dirs
is_file = path.isfile
[[addit(d + "/" +l) for l in listdir(d) if not is_file(d + "/" + l)
and d + "/" + l not in all_dirs] for d in dir_list if d not in whitelist]
except FileNotFoundError:
pass
except Exception as e:
message = "Error! in get_subs: " + str(e)
self.lint(message)
def get_files(self, dir):
try:
addit = self.all_files.append
files = DirectoryEnumerator().get_files(dir)
all_files = self.all_files
is_a_file = path.isfile
if files:
[addit(f) for f in files if f not in all_files and is_a_file(f)]
except Exception as e:
self.lint("Error! in get_files: " + str(e))
def get_file_data(self, file_name) -> str:
if file_name:
data = system("stat " + file_name)
self.lint(str(data))
return str(data)
def hashpipe(self, files: List):
t1 = perf_counter()
hashy = self.hash_dict.__setitem__
read = self.reader
my_sha512 = sha512
[hashy(file, my_sha512(read(file))) for file in files]
t2 = perf_counter()
self.lint("Time to Hash all files: " + str(round(t2 - t1, 5)))
def compare_dicts(self):
# Compare Dicts
try:
self.lint("Checking Hashes..")
if self.cache_is_warm:
[self.lint(self.get_file_data(k) + "\n") for k in self.all_files
if self.cached_hash_dict[k].hexdigest() != self.hash_dict[k].hexdigest()]
except Exception as e:
self.lint("Error! - dict comparison: " + str(e))
def controller(self):
try:
while True:
self.lint("Getting files from root directories..")
[self.get_files(d) for d in dirs if d not in whitelist]
self.lint("Getting subdirectories from root directories..")
self.get_subs(dirs)
self.lint("Getting subdirectories recursively..")
#Recursively get dirs
i: int = 0
while i < 21:
self.get_subs(self.all_dirs)
i += 1
#Using complete unique dir list get files from dirs
self.lint("Getting files from all directories..")
[self.get_files(d) for d in self.all_dirs]
self.lint("Moving hash dictionary to cache")
#move last dict to cache
self.cached_hash_dict = self.hash_dict.copy()
self.hash_dict.clear()
# make new dict
self.lint("Hashing files..")
self.hashpipe(self.all_files)
self.lint("hash_dic_len: " + str(self.hash_dict.__len__()))
self.lint("cache_len: " + str(self.cached_hash_dict.__len__()))
#Is this the first run, cold cache
self.cache_is_warm: bool = self.cached_hash_dict.__len__() > 0
self.compare_dicts()
self.lint("Total Directories: " + str(self.all_dirs.__len__()))
self.lint("Total Files: " + str(self.all_files.__len__()))
if self.cache_is_warm:
self.lint("\nSleeping " + str(self.frequency) + " sec\n")
sleep(self.frequency)
except Exception as e:
self.lint("Error! in controller: " + str(e))
def main():
Claymore().controller()
if __name__ == '__main__':
main()