import os, fcntl
import json
from threading import Event
def makedirs():
for dir in ["cache", "meta", "temp"]:
if not os.path.isdir(dir):
os.mkdir(dir)
def hash2fname(hash,temp=False):
return os.path.join("temp" if temp else "cache",hash[:2],hash[:10])
def hash2meta_fname(hash):
return os.path.join("meta",hash[:10]+".json")
class MetaFile:
def __init__(self, hash):
self.hash=hash
def __enter__(self):
fname=hash2meta_fname(self.hash)
self.fd=open( fname, "r+" if os.path.isfile(fname) else "x+" )
fcntl.lockf(self.fd, fcntl.LOCK_EX)
return self
def read(self):
self.fd.seek(0)
if self.fd.read(1):
self.fd.seek(0)
return json.load(self.fd)
else: # file is empty
return {}
def write(self, data):
self.fd.seek(0)
self.fd.truncate()
json.dump(data, self.fd)
def __exit__(self, type, value, traceback):
fcntl.lockf(self.fd, fcntl.LOCK_UN)
hashlocks:dict[str,Event]={}
class DataFile:
def __init__(self, fd):
self.fd=fd
def __enter__(self):
fcntl.lockf(self.fd, fcntl.LOCK_EX)
return self.fd
def __exit__(self, type, value, exception):
fcntl.lockf(self.fd, fcntl.LOCK_UN)