From: Armin Ronacher Date: Fri, 6 Jun 2014 15:28:09 +0000 (+0600) Subject: More resilient template directory handling for bytecode cache. X-Git-Tag: 2.8~90 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=888e6755d4ecd552f2fab0827056287e9f417685;p=thirdparty%2Fjinja.git More resilient template directory handling for bytecode cache. --- diff --git a/jinja2/bccache.py b/jinja2/bccache.py index fff65dae..7b368aa6 100644 --- a/jinja2/bccache.py +++ b/jinja2/bccache.py @@ -211,6 +211,10 @@ class FileSystemBytecodeCache(BytecodeCache): self.pattern = pattern def _get_default_cache_dir(self): + def _unsafe_dir(): + raise RuntimeError('Cannot determine safe temp directory. You ' + 'need to explicitly provide one.') + tmpdir = tempfile.gettempdir() # On windows the temporary directory is used specific unless @@ -218,18 +222,23 @@ class FileSystemBytecodeCache(BytecodeCache): if os.name == 'nt': return tmpdir if not hasattr(os, 'getuid'): - raise RuntimeError('Cannot determine safe temp directory. You ' - 'need to explicitly provide one.') + _unsafe_dir() dirname = '_jinja2-cache-%d' % os.getuid() actual_dir = os.path.join(tmpdir, dirname) + + # 448 == 0700 try: - # 448 == 0700 os.mkdir(actual_dir, 448) except OSError as e: if e.errno != errno.EEXIST: raise - + try: + os.chmod(actual_dir, 448) + if os.stat(actual_dir).st_uid != os.getuid(): + _unsafe_dir() + except OSError: + _unsafe_dir() return actual_dir def _get_cache_filename(self, bucket):