-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcephfs-find-files.py
65 lines (54 loc) · 1.74 KB
/
cephfs-find-files.py
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
#!/usr/bin/env python3
import os
import sys
import threading
from queue import Queue
import xattr
# Function to process each file and directory
def process_file_or_directory(path, output_file, match_string):
if os.path.isfile(path):
try:
ext_attr = xattr.getxattr(path, 'ceph.file.layout.pool')
ext_attr_value = ext_attr.decode('utf-8')
if ext_attr_value == match_string:
with open(output_file, 'a') as f:
f.write(f"{path}\n")
except (OSError, IOError):
pass
elif os.path.isdir(path):
queue.put(path)
# Worker thread function
def worker():
while True:
item = queue.get()
if item is None:
break
for entry in os.scandir(item):
process_file_or_directory(entry.path, output_file, match_string)
queue.task_done()
if __name__ == "__main__":
if len(sys.argv) != 5:
print("Usage: python script.py <directory_to_scan> <output_file> <num_worker_threads> <match_string>")
sys.exit(1)
root_directory = sys.argv[1]
output_file = sys.argv[2]
num_worker_threads = int(sys.argv[3])
match_string = sys.argv[4]
# Create a Queue to hold the directories to be processed
queue = Queue()
# Create and start the worker threads
threads = []
for i in range(num_worker_threads):
t = threading.Thread(target=worker)
t.start()
threads.append(t)
# Add the root directory to the queue
queue.put(root_directory)
# Wait for the queue to be empty
queue.join()
# Stop the worker threads
for i in range(num_worker_threads):
queue.put(None)
for t in threads:
t.join()
print("Finished processing.")