FileStorage: abort creating over an existing file
authorGuido Trotter <ultrotter@google.com>
Wed, 11 Feb 2009 18:29:25 +0000 (18:29 +0000)
committerGuido Trotter <ultrotter@google.com>
Wed, 11 Feb 2009 18:29:25 +0000 (18:29 +0000)
In FileStorage there is a TODO:
 decide whether we should check for existing files and
 abort or not
After Ganeti ate my instance data I decided. Let's abort.
In general there is no reason we should overwrite existing files, and
doing it can be very harmful for preexisting files on the host.

Reviewed-by: iustinp

lib/bdev.py

index bb1b2bd..d68f39e 100644 (file)
@@ -1629,11 +1629,11 @@ class FileStorage(BlockDev):
     @return: an instance of FileStorage
 
     """
-    # TODO: decide whether we should check for existing files and
-    # abort or not
     if not isinstance(unique_id, (tuple, list)) or len(unique_id) != 2:
       raise ValueError("Invalid configuration data %s" % str(unique_id))
     dev_path = unique_id[1]
+    if os.path.exists(dev_path):
+      _ThrowError("File already existing: %s", dev_path)
     try:
       f = open(dev_path, 'w')
       f.truncate(size * 1024 * 1024)