From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 5BBDB13832E for ; Sun, 24 Jul 2016 23:22:13 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 73A00E0B03; Sun, 24 Jul 2016 23:22:12 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 0DB76E0B03 for ; Sun, 24 Jul 2016 23:22:12 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id E1AE9340CC9 for ; Sun, 24 Jul 2016 23:22:10 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id A06257CD for ; Sun, 24 Jul 2016 23:22:06 +0000 (UTC) From: "Zac Medico" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Zac Medico" Message-ID: <1469401801.5652bc88514bdb36b36b544f7fc7e623cf25caae.zmedico@gentoo> Subject: [gentoo-commits] proj/portage:master commit in: pym/portage/cache/ X-VCS-Repository: proj/portage X-VCS-Files: pym/portage/cache/flat_hash.py X-VCS-Directories: pym/portage/cache/ X-VCS-Committer: zmedico X-VCS-Committer-Name: Zac Medico X-VCS-Revision: 5652bc88514bdb36b36b544f7fc7e623cf25caae X-VCS-Branch: master Date: Sun, 24 Jul 2016 23:22:06 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: 32095dd7-3ffc-414a-878c-7d33042eb255 X-Archives-Hash: 53e9920eef42235fea2deafa191bd501 commit: 5652bc88514bdb36b36b544f7fc7e623cf25caae Author: Zac Medico gentoo org> AuthorDate: Sun Jul 24 22:44:31 2016 +0000 Commit: Zac Medico gentoo org> CommitDate: Sun Jul 24 23:10:01 2016 +0000 URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=5652bc88 flat_hash: use mkstemp in _setitem Fix the _setitem method to use mkstemp in order to avoid a race condition when multiple pid namespaces share the same cache directory. Reported-by: Mike Frysinger chromium.org> X-Chromium-Bug: 477727 X-Chromium-Bug-url: https://bugs.chromium.org/p/chromium/issues/detail?id=477727 pym/portage/cache/flat_hash.py | 49 +++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/pym/portage/cache/flat_hash.py b/pym/portage/cache/flat_hash.py index 3a899c0..7978324 100644 --- a/pym/portage/cache/flat_hash.py +++ b/pym/portage/cache/flat_hash.py @@ -1,4 +1,4 @@ -# Copyright 2005-2014 Gentoo Foundation +# Copyright 2005-2016 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 # Author(s): Brian Harring (ferringb@gentoo.org) @@ -10,6 +10,7 @@ import errno import io import stat import sys +import tempfile import os as _os from portage import os from portage import _encodings @@ -66,27 +67,14 @@ class database(fs_template.FsBased): raise cache_errors.CacheCorruption(cpv, e) def _setitem(self, cpv, values): - s = cpv.rfind("/") - fp = os.path.join(self.location,cpv[:s],".update.%i.%s" % (os.getpid(), cpv[s+1:])) try: - myf = io.open(_unicode_encode(fp, - encoding=_encodings['fs'], errors='strict'), - mode='w', encoding=_encodings['repo.content'], - errors='backslashreplace') - except (IOError, OSError) as e: - if errno.ENOENT == e.errno: - try: - self._ensure_dirs(cpv) - myf = io.open(_unicode_encode(fp, - encoding=_encodings['fs'], errors='strict'), - mode='w', encoding=_encodings['repo.content'], - errors='backslashreplace') - except (OSError, IOError) as e: - raise cache_errors.CacheCorruption(cpv, e) - else: - raise cache_errors.CacheCorruption(cpv, e) + fd, fp = tempfile.mkstemp(dir=self.location) + except EnvironmentError as e: + raise cache_errors.CacheCorruption(cpv, e) - try: + with io.open(fd, mode='w', + encoding=_encodings['repo.content'], + errors='backslashreplace') as myf: for k in self._write_keys: v = values.get(k) if not v: @@ -95,8 +83,7 @@ class database(fs_template.FsBased): # k and v are coerced to unicode, in order to prevent TypeError # when writing raw bytes to TextIOWrapper with Python 2. myf.write("%s=%s\n" % (k, v)) - finally: - myf.close() + self._ensure_access(fp) #update written. now we move it. @@ -104,9 +91,21 @@ class database(fs_template.FsBased): new_fp = os.path.join(self.location,cpv) try: os.rename(fp, new_fp) - except (OSError, IOError) as e: - os.remove(fp) - raise cache_errors.CacheCorruption(cpv, e) + except EnvironmentError as e: + success = False + try: + if errno.ENOENT == e.errno: + try: + self._ensure_dirs(cpv) + os.rename(fp, new_fp) + success = True + except EnvironmentError as e: + raise cache_errors.CacheCorruption(cpv, e) + else: + raise cache_errors.CacheCorruption(cpv, e) + finally: + if not success: + os.remove(fp) def _delitem(self, cpv): # import pdb;pdb.set_trace()