backup: Refuse to create a new FileBakend if the backing file exists

This commit is contained in:
Christian Decker
2020-04-05 15:42:25 +02:00
parent ce49404eb4
commit ef31a4ac85
2 changed files with 7 additions and 4 deletions

View File

@@ -12,7 +12,7 @@ import sys
@click.argument("backend-url")
def init(lightning_dir, backend_url):
destination = backend_url
backend = get_backend(destination)
backend = get_backend(destination, create=True)
backend.version, backend.prev_version = 0, 0
backend.offsets = [512, 0]
backend.version_count = 0

View File

@@ -121,7 +121,7 @@ class Backend(object):
class FileBackend(Backend):
def __init__(self, destination: str):
def __init__(self, destination: str, create: bool):
self.version = None
self.prev_version = None
self.destination = destination
@@ -129,6 +129,9 @@ class FileBackend(Backend):
self.version_count = 0
self.url = urlparse(self.destination)
if os.path.exists(self.url.path) and create:
raise ValueError("Attempted to create a FileBackend, but file already exists.")
def initialize(self) -> bool:
if not os.path.exists(self.url.path):
self.version = 0
@@ -232,13 +235,13 @@ def resolve_backend_class(backend_url):
return backend_cl
def get_backend(destination):
def get_backend(destination, create=False):
backend_cl = resolve_backend_class(destination)
if backend_cl is None:
raise ValueError("No backend implementation found for {destination}".format(
destination=destination,
))
backend = backend_cl(destination)
backend = backend_cl(destination, create=create)
backend.initialize()
assert(backend.version is not None)
assert(backend.prev_version is not None)