From 01444e6b6de9fc32e5927064760faf906c6c66fc Mon Sep 17 00:00:00 2001 From: Peter Portante Date: Tue, 20 Aug 2013 19:13:42 -0400 Subject: Try to ensure we'll have a truly random postfix While it is very improbable, it seemed fairly straight forward to add a small bit of code to ensure the temporary file name is random and won't clash with another host, pid or thread potentially working on the same object. BUG: XXXXXX (https://bugzilla.redhat.com/show_bug.cgi?id=XXXXXX) Change-Id: I862021b725efbfe58b98754c4470aef4882eb8f7 Signed-off-by: Peter Portante Reviewed-on: http://review.gluster.org/5671 Reviewed-by: Luis Pabon Tested-by: Luis Pabon --- gluster/swift/obj/diskfile.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'gluster') diff --git a/gluster/swift/obj/diskfile.py b/gluster/swift/obj/diskfile.py index ea39659..4d50488 100644 --- a/gluster/swift/obj/diskfile.py +++ b/gluster/swift/obj/diskfile.py @@ -17,10 +17,16 @@ import os import stat import fcntl import errno -import random +try: + from random import SystemRandom + random = SystemRandom() +except ImportError: + import random import logging +from socket import gethostname from hashlib import md5 from eventlet import sleep +from greenlet import getcurrent from contextlib import contextmanager from swift.common.utils import TRUE_VALUES, drop_buffer_cache, ThreadPool from swift.common.exceptions import DiskFileNotExist, DiskFileError, \ @@ -53,6 +59,9 @@ DISALLOWED_HEADERS = set('content-length content-type deleted etag'.split()) MAX_RENAME_ATTEMPTS = 10 MAX_OPEN_ATTEMPTS = 10 +_cur_pid = str(os.getpid()) +_cur_host = str(gethostname()) + def _random_sleep(): sleep(random.uniform(0.5, 0.15)) @@ -623,9 +632,11 @@ class DiskFile(SwiftDiskFile): # Assume the full directory path exists to the file already, and # construct the proper name for the temporary file. attempts = 1 + cur_thread = str(getcurrent()) while True: - tmpfile = '.' + self._obj + '.' + md5(self._obj + - str(random.random())).hexdigest() + postfix = md5(self._obj + _cur_host + _cur_pid + cur_thread + + str(random.random())).hexdigest() + tmpfile = '.' + self._obj + '.' + postfix tmppath = os.path.join(self.put_datadir, tmpfile) try: fd = do_open(tmppath, -- cgit