Rev 5184: (vila) Only chown() the .bzr.log when creating it (Parth Malwankar) in file:///home/pqm/archives/thelove/bzr/%2Btrunk/
Canonical.com Patch Queue Manager
pqm at pqm.ubuntu.com
Sat Apr 24 17:54:54 BST 2010
At file:///home/pqm/archives/thelove/bzr/%2Btrunk/
------------------------------------------------------------
revno: 5184 [merge]
revision-id: pqm at pqm.ubuntu.com-20100424165450-2jfbk8ta0hhznynx
parent: pqm at pqm.ubuntu.com-20100423202719-c83nw1kvef32fmkl
parent: v.ladeuil+lp at free.fr-20100424135045-o3zewyurh5n4jtjn
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Sat 2010-04-24 17:54:50 +0100
message:
(vila) Only chown() the .bzr.log when creating it (Parth Malwankar)
modified:
NEWS NEWS-20050323055033-4e00b5db738777ff
bzrlib/config.py config.py-20051011043216-070c74f4e9e338e8
bzrlib/osutils.py osutils.py-20050309040759-eeaff12fbf77ac86
bzrlib/tests/test_osutils.py test_osutils.py-20051201224856-e48ee24c12182989
bzrlib/trace.py trace.py-20050309040759-c8ed824bdcd4748a
=== modified file 'NEWS'
--- a/NEWS 2010-04-23 14:15:23 +0000
+++ b/NEWS 2010-04-24 09:08:27 +0000
@@ -29,6 +29,11 @@
Bug Fixes
*********
+* ``.bazaar``, ``.bazaar/bazaar.conf`` and ``.bzr.log`` inherit user and
+ group ownership from the containing directory. This allow bzr to work
+ better with sudo.
+ (Martin <gzlist at googlemail.com>, Parth Malwankar, #376388)
+
* ``bzr selftest --parallel=fork`` wait for its children avoiding zombies.
(Vincent Ladeuil, #566670)
@@ -369,11 +374,6 @@
mainline (i.e. it supports dotted revisions).
(Parth Malwankar, #517800)
-* ``.bazaar``, ``.bazaar/bazaar.conf`` and ``.bzr.log`` inherit user and
- group ownership from the containing directory. This allow bzr to work
- better with sudo.
- (Parth Malwankar, #376388)
-
* Use first apparent author not committer in GNU Changelog format.
(Martin von Gagern, #513322)
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py 2010-04-23 08:51:52 +0000
+++ b/bzrlib/config.py 2010-04-24 09:08:27 +0000
@@ -519,7 +519,8 @@
def _write_config_file(self):
path = self._get_filename()
- f = osutils.open_with_ownership(path, 'wb')
+ f = open(path, 'wb')
+ osutils.copy_ownership_from_path(path)
self._get_parser().write(f)
f.close()
@@ -818,7 +819,8 @@
trace.mutter('creating config parent directory: %r', parent_dir)
os.mkdir(parent_dir)
trace.mutter('creating config directory: %r', path)
- osutils.mkdir_with_ownership(path)
+ os.mkdir(path)
+ osutils.copy_ownership_from_path(path)
def config_dir():
=== modified file 'bzrlib/osutils.py'
--- a/bzrlib/osutils.py 2010-04-23 09:28:29 +0000
+++ b/bzrlib/osutils.py 2010-04-24 09:08:27 +0000
@@ -1827,7 +1827,7 @@
real_handlers[kind](abspath, relpath)
-def copy_ownership(dst, src=None):
+def copy_ownership_from_path(dst, src=None):
"""Copy usr/grp ownership from src file/dir to dst file/dir.
If src is None, the containing directory is used as source. If chown
@@ -1849,30 +1849,6 @@
trace.warning("Unable to copy ownership from '%s' to '%s': IOError: %s." % (src, dst, e))
-def mkdir_with_ownership(path, ownership_src=None):
- """Create the directory 'path' with specified ownership.
-
- If ownership_src is given, copies (chown) usr/grp ownership
- from 'ownership_src' to 'path'. If ownership_src is None, use the
- containing dir ownership.
- """
- os.mkdir(path)
- copy_ownership(path, ownership_src)
-
-
-def open_with_ownership(filename, mode='r', bufsize=-1, ownership_src=None):
- """Open the file 'filename' with the specified ownership.
-
- If ownership_src is specified, copy usr/grp ownership from ownership_src
- to filename. If ownership_src is None, copy ownership from containing
- directory.
- Returns the opened file object.
- """
- f = open(filename, mode, bufsize)
- copy_ownership(filename, ownership_src)
- return f
-
-
def path_prefix_key(path):
"""Generate a prefix-order path key for path.
=== modified file 'bzrlib/tests/test_osutils.py'
--- a/bzrlib/tests/test_osutils.py 2010-03-11 06:24:00 +0000
+++ b/bzrlib/tests/test_osutils.py 2010-04-24 13:48:58 +0000
@@ -1990,29 +1990,24 @@
def _dummy_chown(self, path, uid, gid):
self.path, self.uid, self.gid = path, uid, gid
- def test_mkdir_with_ownership_chown(self):
- """Ensure that osutils.mkdir_with_ownership chowns correctly with ownership_src.
- """
- ownsrc = '/'
- osutils.mkdir_with_ownership('foo', ownsrc)
-
- s = os.stat(ownsrc)
- self.assertEquals(self.path, 'foo')
- self.assertEquals(self.uid, s.st_uid)
- self.assertEquals(self.gid, s.st_gid)
-
- def test_open_with_ownership_chown(self):
- """Ensure that osutils.open_with_ownership chowns correctly with ownership_src.
- """
- ownsrc = '/'
- f = osutils.open_with_ownership('foo', 'w', ownership_src=ownsrc)
-
- # do a test write and close
- f.write('hello')
- f.close()
-
- s = os.stat(ownsrc)
- self.assertEquals(self.path, 'foo')
- self.assertEquals(self.uid, s.st_uid)
- self.assertEquals(self.gid, s.st_gid)
-
+ def test_copy_ownership_from_path(self):
+ """copy_ownership_from_path test with specified src."""
+ ownsrc = '/'
+ f = open('test_file', 'wt')
+ osutils.copy_ownership_from_path('test_file', ownsrc)
+
+ s = os.stat(ownsrc)
+ self.assertEquals(self.path, 'test_file')
+ self.assertEquals(self.uid, s.st_uid)
+ self.assertEquals(self.gid, s.st_gid)
+
+ def test_copy_ownership_nonesrc(self):
+ """copy_ownership_from_path test with src=None."""
+ f = open('test_file', 'wt')
+ # should use parent dir for permissions
+ osutils.copy_ownership_from_path('test_file')
+
+ s = os.stat('..')
+ self.assertEquals(self.path, 'test_file')
+ self.assertEquals(self.uid, s.st_uid)
+ self.assertEquals(self.gid, s.st_gid)
=== modified file 'bzrlib/trace.py'
--- a/bzrlib/trace.py 2010-03-25 11:08:55 +0000
+++ b/bzrlib/trace.py 2010-03-30 14:44:30 +0000
@@ -238,12 +238,37 @@
This sets the global _bzr_log_filename.
"""
global _bzr_log_filename
+
+ def _open_or_create_log_file(filename):
+ """Open existing log file, or create with ownership and permissions
+
+ It inherits the ownership and permissions (masked by umask) from
+ the containing directory to cope better with being run under sudo
+ with $HOME still set to the user's homedir.
+ """
+ flags = os.O_WRONLY | os.O_APPEND | osutils.O_TEXT
+ while True:
+ try:
+ fd = os.open(filename, flags)
+ break
+ except OSError, e:
+ if e.errno != errno.ENOENT:
+ raise
+ try:
+ fd = os.open(filename, flags | os.O_CREAT | os.O_EXCL, 0666)
+ except OSError, e:
+ if e.errno != errno.EEXIST:
+ raise
+ else:
+ osutils.copy_ownership_from_path(filename)
+ break
+ return os.fdopen(fd, 'at', 0) # unbuffered
+
+
_bzr_log_filename = _get_bzr_log_filename()
_rollover_trace_maybe(_bzr_log_filename)
try:
- buffering = 0 # unbuffered
- bzr_log_file = osutils.open_with_ownership(_bzr_log_filename, 'at', buffering)
- # bzr_log_file.tell() on windows always return 0 until some writing done
+ bzr_log_file = _open_or_create_log_file(_bzr_log_filename)
bzr_log_file.write('\n')
if bzr_log_file.tell() <= 2:
bzr_log_file.write("this is a debug log for diagnosing/reporting problems in bzr\n")
@@ -252,7 +277,7 @@
return bzr_log_file
- except IOError, e:
+ except EnvironmentError, e:
# If we are failing to open the log, then most likely logging has not
# been set up yet. So we just write to stderr rather than using
# 'warning()'. If we using warning(), users get the unhelpful 'no
More information about the bazaar-commits
mailing list