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") @click.argument("backend-url")
def init(lightning_dir, backend_url): def init(lightning_dir, backend_url):
destination = backend_url destination = backend_url
backend = get_backend(destination) backend = get_backend(destination, create=True)
backend.version, backend.prev_version = 0, 0 backend.version, backend.prev_version = 0, 0
backend.offsets = [512, 0] backend.offsets = [512, 0]
backend.version_count = 0 backend.version_count = 0

View File

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