Commit cdeefd9b authored by Guido Trotter's avatar Guido Trotter
Browse files

Remove race condition in FileStorage.Create



Rather than checking that the file doesn't exist, and then creating it,
we create it with O_CREAT | O_EXCL, making sure the checking/creation is
atomic.
Signed-off-by: default avatarGuido Trotter <ultrotter@google.com>
Reviewed-by: default avatarMichael Hanselmann <hansmi@google.com>
parent 263b8de6
...@@ -1915,13 +1915,14 @@ class FileStorage(BlockDev): ...@@ -1915,13 +1915,14 @@ class FileStorage(BlockDev):
if not isinstance(unique_id, (tuple, list)) or len(unique_id) != 2: if not isinstance(unique_id, (tuple, list)) or len(unique_id) != 2:
raise ValueError("Invalid configuration data %s" % str(unique_id)) raise ValueError("Invalid configuration data %s" % str(unique_id))
dev_path = unique_id[1] dev_path = unique_id[1]
if os.path.exists(dev_path):
_ThrowError("File already existing: %s", dev_path)
try: try:
f = open(dev_path, 'w') fd = os.open(dev_path, os.O_RDWR | os.O_CREAT | os.O_EXCL)
f = os.fdopen(fd, "w")
f.truncate(size * 1024 * 1024) f.truncate(size * 1024 * 1024)
f.close() f.close()
except IOError, err: except EnvironmentError, err:
if err.errno == errno.EEXIST:
_ThrowError("File already existing: %s", dev_path)
_ThrowError("Error in file creation: %", str(err)) _ThrowError("Error in file creation: %", str(err))
return FileStorage(unique_id, children, size) return FileStorage(unique_id, children, size)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment