Gentoo Archives: gentoo-portage-dev

From: Zac Medico <zmedico@g.o>
To: gentoo-portage-dev@l.g.o
Cc: Zac Medico <zmedico@g.o>
Subject: [gentoo-portage-dev] [PATCH v2] unpack: use chmod-lite helper for bug 554084
Date: Fri, 02 Oct 2015 18:57:50
Message-Id: 1443812246-31043-1-git-send-email-zmedico@gentoo.org
In Reply to: [gentoo-portage-dev] [PATCH] unpack: use chmod-lite helper for bug 554084 by Zac Medico
1 Use the apply_recursive_permissions function to minimize the number of
2 chmod calls.
3
4 Also, fix an UnboundLocalError triggered in portage.data._get_global
5 by chmod-lite.
6
7 X-Gentoo-Bug: 554084
8 X-Gentoo-Bug-URL: https://bugs.gentoo.org/show_bug.cgi?id=554084
9 ---
10 [PATCH v2] fixes apply_recursive_permissions to apply permissions to
11 a directory before that directory is traversed.
12
13 bin/chmod-lite | 10 ++++++++++
14 bin/chmod-lite.py | 30 ++++++++++++++++++++++++++++++
15 bin/phase-helpers.sh | 2 +-
16 pym/portage/data.py | 2 +-
17 pym/portage/util/__init__.py | 30 +++++++++++++++++-------------
18 5 files changed, 59 insertions(+), 15 deletions(-)
19 create mode 100755 bin/chmod-lite
20 create mode 100755 bin/chmod-lite.py
21
22 diff --git a/bin/chmod-lite b/bin/chmod-lite
23 new file mode 100755
24 index 0000000..ffa8d4d
25 --- /dev/null
26 +++ b/bin/chmod-lite
27 @@ -0,0 +1,10 @@
28 +#!/bin/bash
29 +# Copyright 2015 Gentoo Foundation
30 +# Distributed under the terms of the GNU General Public License v2
31 +
32 +export __PORTAGE_HELPER_CWD=${PWD}
33 +
34 +# Use safe cwd, avoiding unsafe import for bug #469338.
35 +cd "${PORTAGE_PYM_PATH}" || exit 1
36 +PYTHONPATH=${PORTAGE_PYTHONPATH:-${PORTAGE_PYM_PATH}} \
37 + exec "${PORTAGE_PYTHON:-/usr/bin/python}" "$PORTAGE_BIN_PATH/chmod-lite.py" "$@"
38 diff --git a/bin/chmod-lite.py b/bin/chmod-lite.py
39 new file mode 100755
40 index 0000000..a552b25
41 --- /dev/null
42 +++ b/bin/chmod-lite.py
43 @@ -0,0 +1,30 @@
44 +#!/usr/bin/python -b
45 +# Copyright 2015 Gentoo Foundation
46 +# Distributed under the terms of the GNU General Public License v2
47 +
48 +import os
49 +import sys
50 +
51 +from portage.util import apply_recursive_permissions
52 +
53 +# Change back to original cwd _after_ all imports (bug #469338).
54 +os.chdir(os.environ["__PORTAGE_HELPER_CWD"])
55 +
56 +def main(files):
57 +
58 + if sys.hexversion >= 0x3000000:
59 + # We can't trust that the filesystem encoding (locale dependent)
60 + # correctly matches the arguments, so use surrogateescape to
61 + # pass through the original argv bytes for Python 3.
62 + fs_encoding = sys.getfilesystemencoding()
63 + files = [x.encode(fs_encoding, 'surrogateescape') for x in files]
64 +
65 + for filename in files:
66 + # Emulate 'chmod -fR a+rX,u+w,g-w,o-w' with mininal chmod calls.
67 + apply_recursive_permissions(filename, filemode=0o644,
68 + filemask=0o022, dirmode=0o755, dirmask=0o022)
69 +
70 + return os.EX_OK
71 +
72 +if __name__ == "__main__":
73 + sys.exit(main(sys.argv[1:]))
74 diff --git a/bin/phase-helpers.sh b/bin/phase-helpers.sh
75 index efd2cfa..0c25ffe 100644
76 --- a/bin/phase-helpers.sh
77 +++ b/bin/phase-helpers.sh
78 @@ -532,7 +532,7 @@ unpack() {
79 # Do not chmod '.' since it's probably ${WORKDIR} and PORTAGE_WORKDIR_MODE
80 # should be preserved.
81 find . -mindepth 1 -maxdepth 1 ! -type l -print0 | \
82 - ${XARGS} -0 chmod -fR a+rX,u+w,g-w,o-w
83 + ${XARGS} -0 "${PORTAGE_BIN_PATH}/chmod-lite"
84 }
85
86 econf() {
87 diff --git a/pym/portage/data.py b/pym/portage/data.py
88 index 2fd287d..2c99548 100644
89 --- a/pym/portage/data.py
90 +++ b/pym/portage/data.py
91 @@ -139,7 +139,7 @@ def _get_global(k):
92 v = 2
93 elif unprivileged:
94 v = 2
95 - elif portage_gid in os.getgroups():
96 + elif _get_global('portage_gid') in os.getgroups():
97 v = 1
98
99 elif k in ('portage_gid', 'portage_uid'):
100 diff --git a/pym/portage/util/__init__.py b/pym/portage/util/__init__.py
101 index c0b509b..2dcd5b6 100644
102 --- a/pym/portage/util/__init__.py
103 +++ b/pym/portage/util/__init__.py
104 @@ -17,9 +17,9 @@ from copy import deepcopy
105 import errno
106 import io
107 try:
108 - from itertools import filterfalse
109 + from itertools import chain, filterfalse
110 except ImportError:
111 - from itertools import ifilterfalse as filterfalse
112 + from itertools import chain, ifilterfalse as filterfalse
113 import logging
114 import re
115 import shlex
116 @@ -1177,22 +1177,26 @@ def apply_recursive_permissions(top, uid=-1, gid=-1,
117 else:
118 raise
119
120 + # For bug 554084, always apply permissions to a directory before
121 + # that directory is traversed.
122 all_applied = True
123 - for dirpath, dirnames, filenames in os.walk(top):
124 - try:
125 - applied = apply_secpass_permissions(dirpath,
126 - uid=uid, gid=gid, mode=dirmode, mask=dirmask,
127 - follow_links=follow_links)
128 - if not applied:
129 - all_applied = False
130 - except PortageException as e:
131 +
132 + try:
133 + applied = apply_secpass_permissions(top,
134 + uid=uid, gid=gid, mode=dirmode, mask=dirmask,
135 + follow_links=follow_links)
136 + if not applied:
137 all_applied = False
138 - onerror(e)
139 + except PortageException as e:
140 + all_applied = False
141 + onerror(e)
142
143 - for name in filenames:
144 + for dirpath, dirnames, filenames in os.walk(top):
145 + for name, mode in chain(((x, filemode) for x in filenames),
146 + ((x, dirmode) for x in dirnames)):
147 try:
148 applied = apply_secpass_permissions(os.path.join(dirpath, name),
149 - uid=uid, gid=gid, mode=filemode, mask=filemask,
150 + uid=uid, gid=gid, mode=mode, mask=filemask,
151 follow_links=follow_links)
152 if not applied:
153 all_applied = False
154 --
155 2.4.6

Replies