diff options
-rw-r--r-- | ufo/gluster/swift/common/DiskFile.py | 25 | ||||
-rw-r--r-- | ufo/test/unit/common/test_diskfile.py | 23 |
2 files changed, 32 insertions, 16 deletions
diff --git a/ufo/gluster/swift/common/DiskFile.py b/ufo/gluster/swift/common/DiskFile.py index 62f98103762..a25ba806575 100644 --- a/ufo/gluster/swift/common/DiskFile.py +++ b/ufo/gluster/swift/common/DiskFile.py @@ -15,8 +15,9 @@ import os import errno +import random +from hashlib import md5 from eventlet import tpool -from tempfile import mkstemp from contextlib import contextmanager from swift.common.utils import normalize_timestamp, renamer from swift.common.exceptions import DiskFileNotExist @@ -102,7 +103,6 @@ class Gluster_DiskFile(DiskFile): self.device_path = os.path.join(path, device) self._container_path = os.path.join(path, device, container) self._is_dir = False - self.tmpdir = os.path.join(path, device, 'tmp') self.tmppath = None self.logger = logger self.metadata = {} @@ -309,9 +309,24 @@ class Gluster_DiskFile(DiskFile): def mkstemp(self): """Contextmanager to make a temporary file.""" - if not os.path.exists(self.tmpdir): - mkdirs(self.tmpdir) - fd, self.tmppath = mkstemp(dir=self.tmpdir) + # Creating intermidiate directories and corresponding metadata. + # For optimization, check if the subdirectory already exists, + # if exists, then it means that it also has its metadata. + # Not checking for container, since the container should already + # exist for the call to come here. + if not os.path.exists(self.datadir): + path = self._container_path + subdir_list = self._obj_path.split(os.path.sep) + for i in range(len(subdir_list)): + path = os.path.join(path, subdir_list[i]); + if not os.path.exists(path): + self._create_dir_object(path) + + tmpfile = '.' + self._obj + '.' + md5(self._obj + \ + str(random.random())).hexdigest() + + self.tmppath = os.path.join(self.datadir, tmpfile) + fd = os.open(self.tmppath, os.O_RDWR | os.O_CREAT | os.O_EXCL) try: yield fd finally: diff --git a/ufo/test/unit/common/test_diskfile.py b/ufo/test/unit/common/test_diskfile.py index 7583fd7fa5d..fb0dc3e16d5 100644 --- a/ufo/test/unit/common/test_diskfile.py +++ b/ufo/test/unit/common/test_diskfile.py @@ -108,7 +108,6 @@ class TestDiskFile(unittest.TestCase): assert gdf.datadir == "/tmp/foo/vol0/bar" assert gdf.device_path == "/tmp/foo/vol0" assert gdf._container_path == "/tmp/foo/vol0/bar" - assert gdf.tmpdir == "/tmp/foo/vol0/tmp" assert gdf.disk_chunk_size == 65536 assert gdf.iter_hook == None assert gdf.logger == self.lg @@ -134,7 +133,6 @@ class TestDiskFile(unittest.TestCase): assert gdf.name == "bar/b/a" assert gdf.datadir == "/tmp/foo/vol0/bar/b/a" assert gdf.device_path == "/tmp/foo/vol0" - assert gdf.tmpdir == "/tmp/foo/vol0/tmp" def test_constructor_no_metadata(self): td = tempfile.mkdtemp() @@ -848,10 +846,11 @@ class TestDiskFile(unittest.TestCase): "dir/z", self.lg) saved_tmppath = '' with gdf.mkstemp() as fd: - assert gdf.tmpdir == os.path.join(td, "vol0", "tmp") - assert os.path.isdir(gdf.tmpdir) + assert gdf.datadir == os.path.join(td, "vol0", "bar", "dir") + assert os.path.isdir(gdf.datadir) saved_tmppath = gdf.tmppath - assert os.path.dirname(saved_tmppath) == gdf.tmpdir + assert os.path.dirname(saved_tmppath) == gdf.datadir + assert os.path.basename(saved_tmppath)[:3] == '.z.' assert os.path.exists(saved_tmppath) os.write(fd, "123") assert not os.path.exists(saved_tmppath) @@ -867,10 +866,11 @@ class TestDiskFile(unittest.TestCase): "dir/z", self.lg) saved_tmppath = '' with gdf.mkstemp() as fd: - assert gdf.tmpdir == os.path.join(td, "vol0", "tmp") - assert os.path.isdir(gdf.tmpdir) + assert gdf.datadir == os.path.join(td, "vol0", "bar", "dir") + assert os.path.isdir(gdf.datadir) saved_tmppath = gdf.tmppath - assert os.path.dirname(saved_tmppath) == gdf.tmpdir + assert os.path.dirname(saved_tmppath) == gdf.datadir + assert os.path.basename(saved_tmppath)[:3] == '.z.' assert os.path.exists(saved_tmppath) os.write(fd, "123") os.close(fd) @@ -887,10 +887,11 @@ class TestDiskFile(unittest.TestCase): "dir/z", self.lg) saved_tmppath = '' with gdf.mkstemp() as fd: - assert gdf.tmpdir == os.path.join(td, "vol0", "tmp") - assert os.path.isdir(gdf.tmpdir) + assert gdf.datadir == os.path.join(td, "vol0", "bar", "dir") + assert os.path.isdir(gdf.datadir) saved_tmppath = gdf.tmppath - assert os.path.dirname(saved_tmppath) == gdf.tmpdir + assert os.path.dirname(saved_tmppath) == gdf.datadir + assert os.path.basename(saved_tmppath)[:3] == '.z.' assert os.path.exists(saved_tmppath) os.write(fd, "123") os.unlink(saved_tmppath) |