Commit 8b177917 authored by Julien Muchembled's avatar Julien Muchembled

jinja2: safer code for FS accesses

Although very unlikely here, it's good pratice to write code without possible
race conditions. The only remaining one here is when anouther process create
a file (with the same output path) between its deletion and its creation.
parent 594d4bfc
......@@ -24,6 +24,7 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
import errno
import os
import json
import zc.buildout
......@@ -37,6 +38,9 @@ DEFAULT_IMPORT_DELIMITER = '/'
@contextmanager
def umask(mask):
if mask is None:
yield
return
original = os.umask(mask)
try:
yield original
......@@ -124,8 +128,9 @@ LOADER_TYPE_DICT = {
}
class Recipe(object):
mode = None
mode = 0777 # BBB: 0666 may have been a better default value
loader = None
umask = None
def __init__(self, buildout, name, options):
self.template = zc.buildout.download.Download(
......@@ -175,26 +180,23 @@ class Recipe(object):
umask_value = options.get('umask')
if umask_value:
self.umask = int(umask_value, 8)
else:
self.umask = os.umask(0)
os.umask(self.umask)
def install(self):
if os.path.lexists(self.rendered):
# Unlink any existing file, so umask is always applied.
try:
os.unlink(self.rendered)
except OSError, e:
if e.errno != errno.ENOENT:
raise
with umask(self.umask):
outdir = os.path.dirname(self.rendered)
if outdir and not os.path.exists(outdir):
os.makedirs(outdir)
# XXX: open doesn't allow providing a filesystem mode, so use
# os.open and os.fdopen instead.
out_fd = os.open(self.rendered,
os.O_CREAT | os.O_TRUNC | os.O_WRONLY,
)
if self.mode is not None:
os.fchmod(out_fd, self.mode)
with os.fdopen(out_fd, 'w') as out:
with os.fdopen(os.open(self.rendered,
os.O_CREAT | os.O_EXCL | os.O_WRONLY,
self.mode), 'w') as out:
out.write(
Environment(
extensions=self.extension_list,
......
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