Format storage.filesystem a bit

This commit is contained in:
Lee Miller 2023-09-21 03:30:36 +03:00
parent a629c15f7d
commit c34b827f0f
Signed by untrusted user: lee.miller
GPG Key ID: 4F97A5EA88F4AB63

View File

@ -2,9 +2,9 @@
Module for using filesystem (directory with files) for inventory storage
"""
import logging
import os
import time
from binascii import hexlify, unhexlify
from os import listdir, makedirs, path, remove, rmdir
from threading import RLock
from paths import lookupAppdataFolder
@ -22,15 +22,15 @@ class FilesystemInventory(InventoryStorage):
def __init__(self):
super(FilesystemInventory, self).__init__()
self.baseDir = path.join(
self.baseDir = os.path.join(
lookupAppdataFolder(), FilesystemInventory.topDir)
for createDir in [self.baseDir, path.join(self.baseDir, "objects")]:
if path.exists(createDir):
if not path.isdir(createDir):
for createDir in [self.baseDir, os.path.join(self.baseDir, "objects")]:
if os.path.exists(createDir):
if not os.path.isdir(createDir):
raise IOError(
"%s exists but it's not a directory" % createDir)
else:
makedirs(createDir)
os.makedirs(createDir)
# Guarantees that two receiveDataThreads
# don't receive and process the same message
# concurrently (probably sent by a malicious individual)
@ -67,7 +67,7 @@ class FilesystemInventory(InventoryStorage):
with self.lock:
value = InventoryItem(*value)
try:
makedirs(path.join(
os.makedirs(os.path.join(
self.baseDir,
FilesystemInventory.objectDir,
hexlify(hashval).decode()))
@ -75,7 +75,7 @@ class FilesystemInventory(InventoryStorage):
pass
try:
with open(
path.join(
os.path.join(
self.baseDir,
FilesystemInventory.objectDir,
hexlify(hashval).decode(),
@ -89,7 +89,7 @@ class FilesystemInventory(InventoryStorage):
value.expires,
hexlify(value.tag).decode()))
with open(
path.join(
os.path.join(
self.baseDir,
FilesystemInventory.objectDir,
hexlify(hashval).decode(),
@ -115,8 +115,8 @@ class FilesystemInventory(InventoryStorage):
pass
with self.lock:
try:
remove(
path.join(
os.remove(
os.path.join(
self.baseDir,
FilesystemInventory.objectDir,
hexlify(hashval).decode(),
@ -124,8 +124,8 @@ class FilesystemInventory(InventoryStorage):
except IOError:
pass
try:
remove(
path.join(
os.remove(
os.path.join(
self.baseDir,
FilesystemInventory.objectDir,
hexlify(hashval).decode(),
@ -133,7 +133,7 @@ class FilesystemInventory(InventoryStorage):
except IOError:
pass
try:
rmdir(path.join(
os.rmdir(os.path.join(
self.baseDir,
FilesystemInventory.objectDir,
hexlify(hashval).decode()))
@ -169,8 +169,6 @@ class FilesystemInventory(InventoryStorage):
logger.debug(
'error loading %s', hexlify(hashId), exc_info=True)
self._inventory = newInventory
# for i, v in self._inventory.items():
# print "loaded stream: %s, %i items" % (i, len(v))
def stream_list(self):
"""Return list of streams"""
@ -178,14 +176,14 @@ class FilesystemInventory(InventoryStorage):
def object_list(self):
"""Return inventory vectors (hashes) from a directory"""
return [unhexlify(x) for x in listdir(path.join(
return [unhexlify(x) for x in os.listdir(os.path.join(
self.baseDir, FilesystemInventory.objectDir))]
def getData(self, hashId):
"""Get object data"""
try:
with open(
path.join(
os.path.join(
self.baseDir,
FilesystemInventory.objectDir,
hexlify(hashId).decode(),
@ -201,7 +199,7 @@ class FilesystemInventory(InventoryStorage):
"""Get object metadata"""
try:
with open(
path.join(
os.path.join(
self.baseDir,
FilesystemInventory.objectDir,
hexlify(hashId).decode(),
@ -247,10 +245,10 @@ class FilesystemInventory(InventoryStorage):
def unexpired_hashes_by_stream(self, stream):
"""Return unexpired hashes in the inventory for a particular stream"""
t = int(time.time())
try:
return [x for x, value in self._inventory[stream].items()
if value.expires > t]
return [
x for x, value in self._inventory[stream].items()
if value.expires > int(time.time())]
except KeyError:
return []
@ -260,7 +258,7 @@ class FilesystemInventory(InventoryStorage):
def clean(self):
"""Clean out old items from the inventory"""
minTime = int(time.time()) - (60 * 60 * 30)
minTime = int(time.time()) - 60 * 60 * 30
deletes = []
for streamDict in self._inventory.values():
for hashId, item in streamDict.items():