Skip to content

Commit

Permalink
Make FileForWriteOnceCompareSame respect umask.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdswinbank committed Apr 14, 2017
1 parent f146911 commit a8cd350
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions python/lsst/daf/persistence/safeFileIo.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ def FileForWriteOnceCompareSame(name):
# If the symlink was created then this is the process that created the first instance of the
# file, and we know its contents match. Move the temp file over the symlink.
os.rename(temp.name, name)
# At this point, we know the file has just been created. Set permissions according to the
# current umask.
setFileMode(name)
except OSError as e:
if e.errno != errno.EEXIST:
raise e
Expand Down
15 changes: 15 additions & 0 deletions tests/safeFileIo.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import os
import shutil
import stat
import unittest

import lsst.daf.persistence as dp
Expand Down Expand Up @@ -75,6 +76,20 @@ def writeNonMatchingFile():
f.write('fop\n')
self.assertRaises(RuntimeError, writeNonMatchingFile)

def testPermissions(self):
"""Check that the file is created with the current umask."""
# The only way to get the umask is to set it.
umask = os.umask(0)
os.umask(umask)

fileName = os.path.join(ROOT, 'safeFileIo/test.txt')
with dp.safeFileIo.FileForWriteOnceCompareSame(fileName) as f:
f.write('bar\n')
f.write('baz\n')

filePerms = stat.S_IMODE(os.lstat(fileName).st_mode)
self.assertEqual(~umask & 0o666, filePerms)


class MemoryTester(lsst.utils.tests.MemoryTestCase):
pass
Expand Down

0 comments on commit a8cd350

Please sign in to comment.