Show More
@@ -1,649 +1,649 | |||||
1 | # posix.py - Posix utility function implementations for Mercurial |
|
1 | # posix.py - Posix utility function implementations for Mercurial | |
2 | # |
|
2 | # | |
3 | # Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others |
|
3 | # Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms of the |
|
5 | # This software may be used and distributed according to the terms of the | |
6 | # GNU General Public License version 2 or any later version. |
|
6 | # GNU General Public License version 2 or any later version. | |
7 |
|
7 | |||
8 | from __future__ import absolute_import |
|
8 | from __future__ import absolute_import | |
9 |
|
9 | |||
10 | import errno |
|
10 | import errno | |
11 | import fcntl |
|
11 | import fcntl | |
12 | import getpass |
|
12 | import getpass | |
13 | import grp |
|
13 | import grp | |
14 | import os |
|
14 | import os | |
15 | import pwd |
|
15 | import pwd | |
16 | import re |
|
16 | import re | |
17 | import select |
|
17 | import select | |
18 | import stat |
|
18 | import stat | |
19 | import sys |
|
19 | import sys | |
20 | import tempfile |
|
20 | import tempfile | |
21 | import unicodedata |
|
21 | import unicodedata | |
22 |
|
22 | |||
23 | from .i18n import _ |
|
23 | from .i18n import _ | |
24 | from . import ( |
|
24 | from . import ( | |
25 | encoding, |
|
25 | encoding, | |
26 | pycompat, |
|
26 | pycompat, | |
27 | ) |
|
27 | ) | |
28 |
|
28 | |||
29 | posixfile = open |
|
29 | posixfile = open | |
30 | normpath = os.path.normpath |
|
30 | normpath = os.path.normpath | |
31 | samestat = os.path.samestat |
|
31 | samestat = os.path.samestat | |
32 | try: |
|
32 | try: | |
33 | oslink = os.link |
|
33 | oslink = os.link | |
34 | except AttributeError: |
|
34 | except AttributeError: | |
35 | # Some platforms build Python without os.link on systems that are |
|
35 | # Some platforms build Python without os.link on systems that are | |
36 | # vaguely unix-like but don't have hardlink support. For those |
|
36 | # vaguely unix-like but don't have hardlink support. For those | |
37 | # poor souls, just say we tried and that it failed so we fall back |
|
37 | # poor souls, just say we tried and that it failed so we fall back | |
38 | # to copies. |
|
38 | # to copies. | |
39 | def oslink(src, dst): |
|
39 | def oslink(src, dst): | |
40 | raise OSError(errno.EINVAL, |
|
40 | raise OSError(errno.EINVAL, | |
41 | 'hardlinks not supported: %s to %s' % (src, dst)) |
|
41 | 'hardlinks not supported: %s to %s' % (src, dst)) | |
42 | unlink = os.unlink |
|
42 | unlink = os.unlink | |
43 | rename = os.rename |
|
43 | rename = os.rename | |
44 | removedirs = os.removedirs |
|
44 | removedirs = os.removedirs | |
45 | expandglobs = False |
|
45 | expandglobs = False | |
46 |
|
46 | |||
47 | umask = os.umask(0) |
|
47 | umask = os.umask(0) | |
48 | os.umask(umask) |
|
48 | os.umask(umask) | |
49 |
|
49 | |||
50 | def split(p): |
|
50 | def split(p): | |
51 | '''Same as posixpath.split, but faster |
|
51 | '''Same as posixpath.split, but faster | |
52 |
|
52 | |||
53 | >>> import posixpath |
|
53 | >>> import posixpath | |
54 | >>> for f in ['/absolute/path/to/file', |
|
54 | >>> for f in ['/absolute/path/to/file', | |
55 | ... 'relative/path/to/file', |
|
55 | ... 'relative/path/to/file', | |
56 | ... 'file_alone', |
|
56 | ... 'file_alone', | |
57 | ... 'path/to/directory/', |
|
57 | ... 'path/to/directory/', | |
58 | ... '/multiple/path//separators', |
|
58 | ... '/multiple/path//separators', | |
59 | ... '/file_at_root', |
|
59 | ... '/file_at_root', | |
60 | ... '///multiple_leading_separators_at_root', |
|
60 | ... '///multiple_leading_separators_at_root', | |
61 | ... '']: |
|
61 | ... '']: | |
62 | ... assert split(f) == posixpath.split(f), f |
|
62 | ... assert split(f) == posixpath.split(f), f | |
63 | ''' |
|
63 | ''' | |
64 | ht = p.rsplit('/', 1) |
|
64 | ht = p.rsplit('/', 1) | |
65 | if len(ht) == 1: |
|
65 | if len(ht) == 1: | |
66 | return '', p |
|
66 | return '', p | |
67 | nh = ht[0].rstrip('/') |
|
67 | nh = ht[0].rstrip('/') | |
68 | if nh: |
|
68 | if nh: | |
69 | return nh, ht[1] |
|
69 | return nh, ht[1] | |
70 | return ht[0] + '/', ht[1] |
|
70 | return ht[0] + '/', ht[1] | |
71 |
|
71 | |||
72 | def openhardlinks(): |
|
72 | def openhardlinks(): | |
73 | '''return true if it is safe to hold open file handles to hardlinks''' |
|
73 | '''return true if it is safe to hold open file handles to hardlinks''' | |
74 | return True |
|
74 | return True | |
75 |
|
75 | |||
76 | def nlinks(name): |
|
76 | def nlinks(name): | |
77 | '''return number of hardlinks for the given file''' |
|
77 | '''return number of hardlinks for the given file''' | |
78 | return os.lstat(name).st_nlink |
|
78 | return os.lstat(name).st_nlink | |
79 |
|
79 | |||
80 | def parsepatchoutput(output_line): |
|
80 | def parsepatchoutput(output_line): | |
81 | """parses the output produced by patch and returns the filename""" |
|
81 | """parses the output produced by patch and returns the filename""" | |
82 | pf = output_line[14:] |
|
82 | pf = output_line[14:] | |
83 | if pycompat.sysplatform == 'OpenVMS': |
|
83 | if pycompat.sysplatform == 'OpenVMS': | |
84 | if pf[0] == '`': |
|
84 | if pf[0] == '`': | |
85 | pf = pf[1:-1] # Remove the quotes |
|
85 | pf = pf[1:-1] # Remove the quotes | |
86 | else: |
|
86 | else: | |
87 | if pf.startswith("'") and pf.endswith("'") and " " in pf: |
|
87 | if pf.startswith("'") and pf.endswith("'") and " " in pf: | |
88 | pf = pf[1:-1] # Remove the quotes |
|
88 | pf = pf[1:-1] # Remove the quotes | |
89 | return pf |
|
89 | return pf | |
90 |
|
90 | |||
91 | def sshargs(sshcmd, host, user, port): |
|
91 | def sshargs(sshcmd, host, user, port): | |
92 | '''Build argument list for ssh''' |
|
92 | '''Build argument list for ssh''' | |
93 | args = user and ("%s@%s" % (user, host)) or host |
|
93 | args = user and ("%s@%s" % (user, host)) or host | |
94 | return port and ("%s -p %s" % (args, port)) or args |
|
94 | return port and ("%s -p %s" % (args, port)) or args | |
95 |
|
95 | |||
96 | def isexec(f): |
|
96 | def isexec(f): | |
97 | """check whether a file is executable""" |
|
97 | """check whether a file is executable""" | |
98 | return (os.lstat(f).st_mode & 0o100 != 0) |
|
98 | return (os.lstat(f).st_mode & 0o100 != 0) | |
99 |
|
99 | |||
100 | def setflags(f, l, x): |
|
100 | def setflags(f, l, x): | |
101 | s = os.lstat(f).st_mode |
|
101 | s = os.lstat(f).st_mode | |
102 | if l: |
|
102 | if l: | |
103 | if not stat.S_ISLNK(s): |
|
103 | if not stat.S_ISLNK(s): | |
104 | # switch file to link |
|
104 | # switch file to link | |
105 | fp = open(f) |
|
105 | fp = open(f) | |
106 | data = fp.read() |
|
106 | data = fp.read() | |
107 | fp.close() |
|
107 | fp.close() | |
108 | unlink(f) |
|
108 | unlink(f) | |
109 | try: |
|
109 | try: | |
110 | os.symlink(data, f) |
|
110 | os.symlink(data, f) | |
111 | except OSError: |
|
111 | except OSError: | |
112 | # failed to make a link, rewrite file |
|
112 | # failed to make a link, rewrite file | |
113 | fp = open(f, "w") |
|
113 | fp = open(f, "w") | |
114 | fp.write(data) |
|
114 | fp.write(data) | |
115 | fp.close() |
|
115 | fp.close() | |
116 | # no chmod needed at this point |
|
116 | # no chmod needed at this point | |
117 | return |
|
117 | return | |
118 | if stat.S_ISLNK(s): |
|
118 | if stat.S_ISLNK(s): | |
119 | # switch link to file |
|
119 | # switch link to file | |
120 | data = os.readlink(f) |
|
120 | data = os.readlink(f) | |
121 | unlink(f) |
|
121 | unlink(f) | |
122 | fp = open(f, "w") |
|
122 | fp = open(f, "w") | |
123 | fp.write(data) |
|
123 | fp.write(data) | |
124 | fp.close() |
|
124 | fp.close() | |
125 | s = 0o666 & ~umask # avoid restatting for chmod |
|
125 | s = 0o666 & ~umask # avoid restatting for chmod | |
126 |
|
126 | |||
127 | sx = s & 0o100 |
|
127 | sx = s & 0o100 | |
128 | if x and not sx: |
|
128 | if x and not sx: | |
129 | # Turn on +x for every +r bit when making a file executable |
|
129 | # Turn on +x for every +r bit when making a file executable | |
130 | # and obey umask. |
|
130 | # and obey umask. | |
131 | os.chmod(f, s | (s & 0o444) >> 2 & ~umask) |
|
131 | os.chmod(f, s | (s & 0o444) >> 2 & ~umask) | |
132 | elif not x and sx: |
|
132 | elif not x and sx: | |
133 | # Turn off all +x bits |
|
133 | # Turn off all +x bits | |
134 | os.chmod(f, s & 0o666) |
|
134 | os.chmod(f, s & 0o666) | |
135 |
|
135 | |||
136 | def copymode(src, dst, mode=None): |
|
136 | def copymode(src, dst, mode=None): | |
137 | '''Copy the file mode from the file at path src to dst. |
|
137 | '''Copy the file mode from the file at path src to dst. | |
138 | If src doesn't exist, we're using mode instead. If mode is None, we're |
|
138 | If src doesn't exist, we're using mode instead. If mode is None, we're | |
139 | using umask.''' |
|
139 | using umask.''' | |
140 | try: |
|
140 | try: | |
141 | st_mode = os.lstat(src).st_mode & 0o777 |
|
141 | st_mode = os.lstat(src).st_mode & 0o777 | |
142 | except OSError as inst: |
|
142 | except OSError as inst: | |
143 | if inst.errno != errno.ENOENT: |
|
143 | if inst.errno != errno.ENOENT: | |
144 | raise |
|
144 | raise | |
145 | st_mode = mode |
|
145 | st_mode = mode | |
146 | if st_mode is None: |
|
146 | if st_mode is None: | |
147 | st_mode = ~umask |
|
147 | st_mode = ~umask | |
148 | st_mode &= 0o666 |
|
148 | st_mode &= 0o666 | |
149 | os.chmod(dst, st_mode) |
|
149 | os.chmod(dst, st_mode) | |
150 |
|
150 | |||
151 | def checkexec(path): |
|
151 | def checkexec(path): | |
152 | """ |
|
152 | """ | |
153 | Check whether the given path is on a filesystem with UNIX-like exec flags |
|
153 | Check whether the given path is on a filesystem with UNIX-like exec flags | |
154 |
|
154 | |||
155 | Requires a directory (like /foo/.hg) |
|
155 | Requires a directory (like /foo/.hg) | |
156 | """ |
|
156 | """ | |
157 |
|
157 | |||
158 | # VFAT on some Linux versions can flip mode but it doesn't persist |
|
158 | # VFAT on some Linux versions can flip mode but it doesn't persist | |
159 | # a FS remount. Frequently we can detect it if files are created |
|
159 | # a FS remount. Frequently we can detect it if files are created | |
160 | # with exec bit on. |
|
160 | # with exec bit on. | |
161 |
|
161 | |||
162 | try: |
|
162 | try: | |
163 | EXECFLAGS = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH |
|
163 | EXECFLAGS = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH | |
164 | cachedir = os.path.join(path, '.hg', 'cache') |
|
164 | cachedir = os.path.join(path, '.hg', 'cache') | |
165 | if os.path.isdir(cachedir): |
|
165 | if os.path.isdir(cachedir): | |
166 | checkisexec = os.path.join(cachedir, 'checkisexec') |
|
166 | checkisexec = os.path.join(cachedir, 'checkisexec') | |
167 | checknoexec = os.path.join(cachedir, 'checknoexec') |
|
167 | checknoexec = os.path.join(cachedir, 'checknoexec') | |
168 |
|
168 | |||
169 | try: |
|
169 | try: | |
170 | m = os.stat(checkisexec).st_mode |
|
170 | m = os.stat(checkisexec).st_mode | |
171 | except OSError as e: |
|
171 | except OSError as e: | |
172 | if e.errno != errno.ENOENT: |
|
172 | if e.errno != errno.ENOENT: | |
173 | raise |
|
173 | raise | |
174 | # checkisexec does not exist - fall through ... |
|
174 | # checkisexec does not exist - fall through ... | |
175 | else: |
|
175 | else: | |
176 | # checkisexec exists, check if it actually is exec |
|
176 | # checkisexec exists, check if it actually is exec | |
177 | if m & EXECFLAGS != 0: |
|
177 | if m & EXECFLAGS != 0: | |
178 | # ensure checkisexec exists, check it isn't exec |
|
178 | # ensure checkisexec exists, check it isn't exec | |
179 | try: |
|
179 | try: | |
180 | m = os.stat(checknoexec).st_mode |
|
180 | m = os.stat(checknoexec).st_mode | |
181 | except OSError as e: |
|
181 | except OSError as e: | |
182 | if e.errno != errno.ENOENT: |
|
182 | if e.errno != errno.ENOENT: | |
183 | raise |
|
183 | raise | |
184 | open(checknoexec, 'w').close() # might fail |
|
184 | open(checknoexec, 'w').close() # might fail | |
185 | m = os.stat(checknoexec).st_mode |
|
185 | m = os.stat(checknoexec).st_mode | |
186 | if m & EXECFLAGS == 0: |
|
186 | if m & EXECFLAGS == 0: | |
187 | # check-exec is exec and check-no-exec is not exec |
|
187 | # check-exec is exec and check-no-exec is not exec | |
188 | return True |
|
188 | return True | |
189 | # checknoexec exists but is exec - delete it |
|
189 | # checknoexec exists but is exec - delete it | |
190 | unlink(checknoexec) |
|
190 | unlink(checknoexec) | |
191 | # checkisexec exists but is not exec - delete it |
|
191 | # checkisexec exists but is not exec - delete it | |
192 | unlink(checkisexec) |
|
192 | unlink(checkisexec) | |
193 |
|
193 | |||
194 | # check using one file, leave it as checkisexec |
|
194 | # check using one file, leave it as checkisexec | |
195 | checkdir = cachedir |
|
195 | checkdir = cachedir | |
196 | else: |
|
196 | else: | |
197 | # check directly in path and don't leave checkisexec behind |
|
197 | # check directly in path and don't leave checkisexec behind | |
198 | checkdir = path |
|
198 | checkdir = path | |
199 | checkisexec = None |
|
199 | checkisexec = None | |
200 | fh, fn = tempfile.mkstemp(dir=checkdir, prefix='hg-checkexec-') |
|
200 | fh, fn = tempfile.mkstemp(dir=checkdir, prefix='hg-checkexec-') | |
201 | try: |
|
201 | try: | |
202 | os.close(fh) |
|
202 | os.close(fh) | |
203 | m = os.stat(fn).st_mode |
|
203 | m = os.stat(fn).st_mode | |
204 | if m & EXECFLAGS == 0: |
|
204 | if m & EXECFLAGS == 0: | |
205 | os.chmod(fn, m & 0o777 | EXECFLAGS) |
|
205 | os.chmod(fn, m & 0o777 | EXECFLAGS) | |
206 | if os.stat(fn).st_mode & EXECFLAGS != 0: |
|
206 | if os.stat(fn).st_mode & EXECFLAGS != 0: | |
207 | if checkisexec is not None: |
|
207 | if checkisexec is not None: | |
208 | os.rename(fn, checkisexec) |
|
208 | os.rename(fn, checkisexec) | |
209 | fn = None |
|
209 | fn = None | |
210 | return True |
|
210 | return True | |
211 | finally: |
|
211 | finally: | |
212 | if fn is not None: |
|
212 | if fn is not None: | |
213 | unlink(fn) |
|
213 | unlink(fn) | |
214 | except (IOError, OSError): |
|
214 | except (IOError, OSError): | |
215 | # we don't care, the user probably won't be able to commit anyway |
|
215 | # we don't care, the user probably won't be able to commit anyway | |
216 | return False |
|
216 | return False | |
217 |
|
217 | |||
218 | def checklink(path): |
|
218 | def checklink(path): | |
219 | """check whether the given path is on a symlink-capable filesystem""" |
|
219 | """check whether the given path is on a symlink-capable filesystem""" | |
220 | # mktemp is not racy because symlink creation will fail if the |
|
220 | # mktemp is not racy because symlink creation will fail if the | |
221 | # file already exists |
|
221 | # file already exists | |
222 | while True: |
|
222 | while True: | |
223 | cachedir = os.path.join(path, '.hg', 'cache') |
|
223 | cachedir = os.path.join(path, '.hg', 'cache') | |
224 | checklink = os.path.join(cachedir, 'checklink') |
|
224 | checklink = os.path.join(cachedir, 'checklink') | |
225 | # try fast path, read only |
|
225 | # try fast path, read only | |
226 | if os.path.islink(checklink): |
|
226 | if os.path.islink(checklink): | |
227 | return True |
|
227 | return True | |
228 | if os.path.isdir(cachedir): |
|
228 | if os.path.isdir(cachedir): | |
229 | checkdir = cachedir |
|
229 | checkdir = cachedir | |
230 | else: |
|
230 | else: | |
231 | checkdir = path |
|
231 | checkdir = path | |
232 | cachedir = None |
|
232 | cachedir = None | |
233 | fscheckdir = pycompat.fsdecode(checkdir) |
|
233 | fscheckdir = pycompat.fsdecode(checkdir) | |
234 | name = tempfile.mktemp(dir=fscheckdir, |
|
234 | name = tempfile.mktemp(dir=fscheckdir, | |
235 | prefix=r'checklink-') |
|
235 | prefix=r'checklink-') | |
236 | name = pycompat.fsencode(name) |
|
236 | name = pycompat.fsencode(name) | |
237 | try: |
|
237 | try: | |
238 | fd = None |
|
238 | fd = None | |
239 | if cachedir is None: |
|
239 | if cachedir is None: | |
240 | fd = tempfile.NamedTemporaryFile(dir=fscheckdir, |
|
240 | fd = tempfile.NamedTemporaryFile(dir=fscheckdir, | |
241 | prefix=r'hg-checklink-') |
|
241 | prefix=r'hg-checklink-') | |
242 | target = pycompat.fsencode(os.path.basename(fd.name)) |
|
242 | target = pycompat.fsencode(os.path.basename(fd.name)) | |
243 | else: |
|
243 | else: | |
244 | # create a fixed file to link to; doesn't matter if it |
|
244 | # create a fixed file to link to; doesn't matter if it | |
245 | # already exists. |
|
245 | # already exists. | |
246 | target = 'checklink-target' |
|
246 | target = 'checklink-target' | |
247 | open(os.path.join(cachedir, target), 'w').close() |
|
247 | open(os.path.join(cachedir, target), 'w').close() | |
248 | try: |
|
248 | try: | |
249 | os.symlink(target, name) |
|
249 | os.symlink(target, name) | |
250 | if cachedir is None: |
|
250 | if cachedir is None: | |
251 | unlink(name) |
|
251 | unlink(name) | |
252 | else: |
|
252 | else: | |
253 | try: |
|
253 | try: | |
254 | os.rename(name, checklink) |
|
254 | os.rename(name, checklink) | |
255 | except OSError: |
|
255 | except OSError: | |
256 | unlink(name) |
|
256 | unlink(name) | |
257 | return True |
|
257 | return True | |
258 | except OSError as inst: |
|
258 | except OSError as inst: | |
259 | # link creation might race, try again |
|
259 | # link creation might race, try again | |
260 | if inst[0] == errno.EEXIST: |
|
260 | if inst[0] == errno.EEXIST: | |
261 | continue |
|
261 | continue | |
262 | raise |
|
262 | raise | |
263 | finally: |
|
263 | finally: | |
264 | if fd is not None: |
|
264 | if fd is not None: | |
265 | fd.close() |
|
265 | fd.close() | |
266 | except AttributeError: |
|
266 | except AttributeError: | |
267 | return False |
|
267 | return False | |
268 | except OSError as inst: |
|
268 | except OSError as inst: | |
269 | # sshfs might report failure while successfully creating the link |
|
269 | # sshfs might report failure while successfully creating the link | |
270 | if inst[0] == errno.EIO and os.path.exists(name): |
|
270 | if inst[0] == errno.EIO and os.path.exists(name): | |
271 | unlink(name) |
|
271 | unlink(name) | |
272 | return False |
|
272 | return False | |
273 |
|
273 | |||
274 | def checkosfilename(path): |
|
274 | def checkosfilename(path): | |
275 | '''Check that the base-relative path is a valid filename on this platform. |
|
275 | '''Check that the base-relative path is a valid filename on this platform. | |
276 | Returns None if the path is ok, or a UI string describing the problem.''' |
|
276 | Returns None if the path is ok, or a UI string describing the problem.''' | |
277 | pass # on posix platforms, every path is ok |
|
277 | pass # on posix platforms, every path is ok | |
278 |
|
278 | |||
279 | def setbinary(fd): |
|
279 | def setbinary(fd): | |
280 | pass |
|
280 | pass | |
281 |
|
281 | |||
282 | def pconvert(path): |
|
282 | def pconvert(path): | |
283 | return path |
|
283 | return path | |
284 |
|
284 | |||
285 | def localpath(path): |
|
285 | def localpath(path): | |
286 | return path |
|
286 | return path | |
287 |
|
287 | |||
288 | def samefile(fpath1, fpath2): |
|
288 | def samefile(fpath1, fpath2): | |
289 | """Returns whether path1 and path2 refer to the same file. This is only |
|
289 | """Returns whether path1 and path2 refer to the same file. This is only | |
290 | guaranteed to work for files, not directories.""" |
|
290 | guaranteed to work for files, not directories.""" | |
291 | return os.path.samefile(fpath1, fpath2) |
|
291 | return os.path.samefile(fpath1, fpath2) | |
292 |
|
292 | |||
293 | def samedevice(fpath1, fpath2): |
|
293 | def samedevice(fpath1, fpath2): | |
294 | """Returns whether fpath1 and fpath2 are on the same device. This is only |
|
294 | """Returns whether fpath1 and fpath2 are on the same device. This is only | |
295 | guaranteed to work for files, not directories.""" |
|
295 | guaranteed to work for files, not directories.""" | |
296 | st1 = os.lstat(fpath1) |
|
296 | st1 = os.lstat(fpath1) | |
297 | st2 = os.lstat(fpath2) |
|
297 | st2 = os.lstat(fpath2) | |
298 | return st1.st_dev == st2.st_dev |
|
298 | return st1.st_dev == st2.st_dev | |
299 |
|
299 | |||
300 | # os.path.normcase is a no-op, which doesn't help us on non-native filesystems |
|
300 | # os.path.normcase is a no-op, which doesn't help us on non-native filesystems | |
301 | def normcase(path): |
|
301 | def normcase(path): | |
302 | return path.lower() |
|
302 | return path.lower() | |
303 |
|
303 | |||
304 | # what normcase does to ASCII strings |
|
304 | # what normcase does to ASCII strings | |
305 | normcasespec = encoding.normcasespecs.lower |
|
305 | normcasespec = encoding.normcasespecs.lower | |
306 | # fallback normcase function for non-ASCII strings |
|
306 | # fallback normcase function for non-ASCII strings | |
307 | normcasefallback = normcase |
|
307 | normcasefallback = normcase | |
308 |
|
308 | |||
309 | if pycompat.sysplatform == 'darwin': |
|
309 | if pycompat.sysplatform == 'darwin': | |
310 |
|
310 | |||
311 | def normcase(path): |
|
311 | def normcase(path): | |
312 | ''' |
|
312 | ''' | |
313 | Normalize a filename for OS X-compatible comparison: |
|
313 | Normalize a filename for OS X-compatible comparison: | |
314 | - escape-encode invalid characters |
|
314 | - escape-encode invalid characters | |
315 | - decompose to NFD |
|
315 | - decompose to NFD | |
316 | - lowercase |
|
316 | - lowercase | |
317 | - omit ignored characters [200c-200f, 202a-202e, 206a-206f,feff] |
|
317 | - omit ignored characters [200c-200f, 202a-202e, 206a-206f,feff] | |
318 |
|
318 | |||
319 | >>> normcase('UPPER') |
|
319 | >>> normcase('UPPER') | |
320 | 'upper' |
|
320 | 'upper' | |
321 | >>> normcase('Caf\xc3\xa9') |
|
321 | >>> normcase('Caf\xc3\xa9') | |
322 | 'cafe\\xcc\\x81' |
|
322 | 'cafe\\xcc\\x81' | |
323 | >>> normcase('\xc3\x89') |
|
323 | >>> normcase('\xc3\x89') | |
324 | 'e\\xcc\\x81' |
|
324 | 'e\\xcc\\x81' | |
325 | >>> normcase('\xb8\xca\xc3\xca\xbe\xc8.JPG') # issue3918 |
|
325 | >>> normcase('\xb8\xca\xc3\xca\xbe\xc8.JPG') # issue3918 | |
326 | '%b8%ca%c3\\xca\\xbe%c8.jpg' |
|
326 | '%b8%ca%c3\\xca\\xbe%c8.jpg' | |
327 | ''' |
|
327 | ''' | |
328 |
|
328 | |||
329 | try: |
|
329 | try: | |
330 | return encoding.asciilower(path) # exception for non-ASCII |
|
330 | return encoding.asciilower(path) # exception for non-ASCII | |
331 | except UnicodeDecodeError: |
|
331 | except UnicodeDecodeError: | |
332 | return normcasefallback(path) |
|
332 | return normcasefallback(path) | |
333 |
|
333 | |||
334 | normcasespec = encoding.normcasespecs.lower |
|
334 | normcasespec = encoding.normcasespecs.lower | |
335 |
|
335 | |||
336 | def normcasefallback(path): |
|
336 | def normcasefallback(path): | |
337 | try: |
|
337 | try: | |
338 | u = path.decode('utf-8') |
|
338 | u = path.decode('utf-8') | |
339 | except UnicodeDecodeError: |
|
339 | except UnicodeDecodeError: | |
340 | # OS X percent-encodes any bytes that aren't valid utf-8 |
|
340 | # OS X percent-encodes any bytes that aren't valid utf-8 | |
341 | s = '' |
|
341 | s = '' | |
342 | pos = 0 |
|
342 | pos = 0 | |
343 | l = len(path) |
|
343 | l = len(path) | |
344 | while pos < l: |
|
344 | while pos < l: | |
345 | try: |
|
345 | try: | |
346 | c = encoding.getutf8char(path, pos) |
|
346 | c = encoding.getutf8char(path, pos) | |
347 | pos += len(c) |
|
347 | pos += len(c) | |
348 | except ValueError: |
|
348 | except ValueError: | |
349 | c = '%%%02X' % ord(path[pos]) |
|
349 | c = '%%%02X' % ord(path[pos]) | |
350 | pos += 1 |
|
350 | pos += 1 | |
351 | s += c |
|
351 | s += c | |
352 |
|
352 | |||
353 | u = s.decode('utf-8') |
|
353 | u = s.decode('utf-8') | |
354 |
|
354 | |||
355 | # Decompose then lowercase (HFS+ technote specifies lower) |
|
355 | # Decompose then lowercase (HFS+ technote specifies lower) | |
356 | enc = unicodedata.normalize('NFD', u).lower().encode('utf-8') |
|
356 | enc = unicodedata.normalize('NFD', u).lower().encode('utf-8') | |
357 | # drop HFS+ ignored characters |
|
357 | # drop HFS+ ignored characters | |
358 | return encoding.hfsignoreclean(enc) |
|
358 | return encoding.hfsignoreclean(enc) | |
359 |
|
359 | |||
360 | if pycompat.sysplatform == 'cygwin': |
|
360 | if pycompat.sysplatform == 'cygwin': | |
361 | # workaround for cygwin, in which mount point part of path is |
|
361 | # workaround for cygwin, in which mount point part of path is | |
362 | # treated as case sensitive, even though underlying NTFS is case |
|
362 | # treated as case sensitive, even though underlying NTFS is case | |
363 | # insensitive. |
|
363 | # insensitive. | |
364 |
|
364 | |||
365 | # default mount points |
|
365 | # default mount points | |
366 | cygwinmountpoints = sorted([ |
|
366 | cygwinmountpoints = sorted([ | |
367 | "/usr/bin", |
|
367 | "/usr/bin", | |
368 | "/usr/lib", |
|
368 | "/usr/lib", | |
369 | "/cygdrive", |
|
369 | "/cygdrive", | |
370 | ], reverse=True) |
|
370 | ], reverse=True) | |
371 |
|
371 | |||
372 | # use upper-ing as normcase as same as NTFS workaround |
|
372 | # use upper-ing as normcase as same as NTFS workaround | |
373 | def normcase(path): |
|
373 | def normcase(path): | |
374 | pathlen = len(path) |
|
374 | pathlen = len(path) | |
375 | if (pathlen == 0) or (path[0] != pycompat.ossep): |
|
375 | if (pathlen == 0) or (path[0] != pycompat.ossep): | |
376 | # treat as relative |
|
376 | # treat as relative | |
377 | return encoding.upper(path) |
|
377 | return encoding.upper(path) | |
378 |
|
378 | |||
379 | # to preserve case of mountpoint part |
|
379 | # to preserve case of mountpoint part | |
380 | for mp in cygwinmountpoints: |
|
380 | for mp in cygwinmountpoints: | |
381 | if not path.startswith(mp): |
|
381 | if not path.startswith(mp): | |
382 | continue |
|
382 | continue | |
383 |
|
383 | |||
384 | mplen = len(mp) |
|
384 | mplen = len(mp) | |
385 | if mplen == pathlen: # mount point itself |
|
385 | if mplen == pathlen: # mount point itself | |
386 | return mp |
|
386 | return mp | |
387 | if path[mplen] == pycompat.ossep: |
|
387 | if path[mplen] == pycompat.ossep: | |
388 | return mp + encoding.upper(path[mplen:]) |
|
388 | return mp + encoding.upper(path[mplen:]) | |
389 |
|
389 | |||
390 | return encoding.upper(path) |
|
390 | return encoding.upper(path) | |
391 |
|
391 | |||
392 | normcasespec = encoding.normcasespecs.other |
|
392 | normcasespec = encoding.normcasespecs.other | |
393 | normcasefallback = normcase |
|
393 | normcasefallback = normcase | |
394 |
|
394 | |||
395 | # Cygwin translates native ACLs to POSIX permissions, |
|
395 | # Cygwin translates native ACLs to POSIX permissions, | |
396 | # but these translations are not supported by native |
|
396 | # but these translations are not supported by native | |
397 | # tools, so the exec bit tends to be set erroneously. |
|
397 | # tools, so the exec bit tends to be set erroneously. | |
398 | # Therefore, disable executable bit access on Cygwin. |
|
398 | # Therefore, disable executable bit access on Cygwin. | |
399 | def checkexec(path): |
|
399 | def checkexec(path): | |
400 | return False |
|
400 | return False | |
401 |
|
401 | |||
402 | # Similarly, Cygwin's symlink emulation is likely to create |
|
402 | # Similarly, Cygwin's symlink emulation is likely to create | |
403 | # problems when Mercurial is used from both Cygwin and native |
|
403 | # problems when Mercurial is used from both Cygwin and native | |
404 | # Windows, with other native tools, or on shared volumes |
|
404 | # Windows, with other native tools, or on shared volumes | |
405 | def checklink(path): |
|
405 | def checklink(path): | |
406 | return False |
|
406 | return False | |
407 |
|
407 | |||
408 | _needsshellquote = None |
|
408 | _needsshellquote = None | |
409 | def shellquote(s): |
|
409 | def shellquote(s): | |
410 | if pycompat.sysplatform == 'OpenVMS': |
|
410 | if pycompat.sysplatform == 'OpenVMS': | |
411 | return '"%s"' % s |
|
411 | return '"%s"' % s | |
412 | global _needsshellquote |
|
412 | global _needsshellquote | |
413 | if _needsshellquote is None: |
|
413 | if _needsshellquote is None: | |
414 | _needsshellquote = re.compile(br'[^a-zA-Z0-9._/+-]').search |
|
414 | _needsshellquote = re.compile(br'[^a-zA-Z0-9._/+-]').search | |
415 | if s and not _needsshellquote(s): |
|
415 | if s and not _needsshellquote(s): | |
416 | # "s" shouldn't have to be quoted |
|
416 | # "s" shouldn't have to be quoted | |
417 | return s |
|
417 | return s | |
418 | else: |
|
418 | else: | |
419 | return "'%s'" % s.replace("'", "'\\''") |
|
419 | return "'%s'" % s.replace("'", "'\\''") | |
420 |
|
420 | |||
421 | def quotecommand(cmd): |
|
421 | def quotecommand(cmd): | |
422 | return cmd |
|
422 | return cmd | |
423 |
|
423 | |||
424 | def popen(command, mode='r'): |
|
424 | def popen(command, mode='r'): | |
425 | return os.popen(command, mode) |
|
425 | return os.popen(command, mode) | |
426 |
|
426 | |||
427 | def testpid(pid): |
|
427 | def testpid(pid): | |
428 | '''return False if pid dead, True if running or not sure''' |
|
428 | '''return False if pid dead, True if running or not sure''' | |
429 | if pycompat.sysplatform == 'OpenVMS': |
|
429 | if pycompat.sysplatform == 'OpenVMS': | |
430 | return True |
|
430 | return True | |
431 | try: |
|
431 | try: | |
432 | os.kill(pid, 0) |
|
432 | os.kill(pid, 0) | |
433 | return True |
|
433 | return True | |
434 | except OSError as inst: |
|
434 | except OSError as inst: | |
435 | return inst.errno != errno.ESRCH |
|
435 | return inst.errno != errno.ESRCH | |
436 |
|
436 | |||
437 | def explainexit(code): |
|
437 | def explainexit(code): | |
438 | """return a 2-tuple (desc, code) describing a subprocess status |
|
438 | """return a 2-tuple (desc, code) describing a subprocess status | |
439 | (codes from kill are negative - not os.system/wait encoding)""" |
|
439 | (codes from kill are negative - not os.system/wait encoding)""" | |
440 | if code >= 0: |
|
440 | if code >= 0: | |
441 | return _("exited with status %d") % code, code |
|
441 | return _("exited with status %d") % code, code | |
442 | return _("killed by signal %d") % -code, -code |
|
442 | return _("killed by signal %d") % -code, -code | |
443 |
|
443 | |||
444 | def isowner(st): |
|
444 | def isowner(st): | |
445 | """Return True if the stat object st is from the current user.""" |
|
445 | """Return True if the stat object st is from the current user.""" | |
446 | return st.st_uid == os.getuid() |
|
446 | return st.st_uid == os.getuid() | |
447 |
|
447 | |||
448 | def findexe(command): |
|
448 | def findexe(command): | |
449 | '''Find executable for command searching like which does. |
|
449 | '''Find executable for command searching like which does. | |
450 | If command is a basename then PATH is searched for command. |
|
450 | If command is a basename then PATH is searched for command. | |
451 | PATH isn't searched if command is an absolute or relative path. |
|
451 | PATH isn't searched if command is an absolute or relative path. | |
452 | If command isn't found None is returned.''' |
|
452 | If command isn't found None is returned.''' | |
453 | if pycompat.sysplatform == 'OpenVMS': |
|
453 | if pycompat.sysplatform == 'OpenVMS': | |
454 | return command |
|
454 | return command | |
455 |
|
455 | |||
456 | def findexisting(executable): |
|
456 | def findexisting(executable): | |
457 | 'Will return executable if existing file' |
|
457 | 'Will return executable if existing file' | |
458 | if os.path.isfile(executable) and os.access(executable, os.X_OK): |
|
458 | if os.path.isfile(executable) and os.access(executable, os.X_OK): | |
459 | return executable |
|
459 | return executable | |
460 | return None |
|
460 | return None | |
461 |
|
461 | |||
462 | if pycompat.ossep in command: |
|
462 | if pycompat.ossep in command: | |
463 | return findexisting(command) |
|
463 | return findexisting(command) | |
464 |
|
464 | |||
465 | if pycompat.sysplatform == 'plan9': |
|
465 | if pycompat.sysplatform == 'plan9': | |
466 | return findexisting(os.path.join('/bin', command)) |
|
466 | return findexisting(os.path.join('/bin', command)) | |
467 |
|
467 | |||
468 | for path in encoding.environ.get('PATH', '').split(pycompat.ospathsep): |
|
468 | for path in encoding.environ.get('PATH', '').split(pycompat.ospathsep): | |
469 | executable = findexisting(os.path.join(path, command)) |
|
469 | executable = findexisting(os.path.join(path, command)) | |
470 | if executable is not None: |
|
470 | if executable is not None: | |
471 | return executable |
|
471 | return executable | |
472 | return None |
|
472 | return None | |
473 |
|
473 | |||
474 | def setsignalhandler(): |
|
474 | def setsignalhandler(): | |
475 | pass |
|
475 | pass | |
476 |
|
476 | |||
477 | _wantedkinds = set([stat.S_IFREG, stat.S_IFLNK]) |
|
477 | _wantedkinds = set([stat.S_IFREG, stat.S_IFLNK]) | |
478 |
|
478 | |||
479 | def statfiles(files): |
|
479 | def statfiles(files): | |
480 | '''Stat each file in files. Yield each stat, or None if a file does not |
|
480 | '''Stat each file in files. Yield each stat, or None if a file does not | |
481 | exist or has a type we don't care about.''' |
|
481 | exist or has a type we don't care about.''' | |
482 | lstat = os.lstat |
|
482 | lstat = os.lstat | |
483 | getkind = stat.S_IFMT |
|
483 | getkind = stat.S_IFMT | |
484 | for nf in files: |
|
484 | for nf in files: | |
485 | try: |
|
485 | try: | |
486 | st = lstat(nf) |
|
486 | st = lstat(nf) | |
487 | if getkind(st.st_mode) not in _wantedkinds: |
|
487 | if getkind(st.st_mode) not in _wantedkinds: | |
488 | st = None |
|
488 | st = None | |
489 | except OSError as err: |
|
489 | except OSError as err: | |
490 | if err.errno not in (errno.ENOENT, errno.ENOTDIR): |
|
490 | if err.errno not in (errno.ENOENT, errno.ENOTDIR): | |
491 | raise |
|
491 | raise | |
492 | st = None |
|
492 | st = None | |
493 | yield st |
|
493 | yield st | |
494 |
|
494 | |||
495 | def getuser(): |
|
495 | def getuser(): | |
496 | '''return name of current user''' |
|
496 | '''return name of current user''' | |
497 | return getpass.getuser() |
|
497 | return pycompat.fsencode(getpass.getuser()) | |
498 |
|
498 | |||
499 | def username(uid=None): |
|
499 | def username(uid=None): | |
500 | """Return the name of the user with the given uid. |
|
500 | """Return the name of the user with the given uid. | |
501 |
|
501 | |||
502 | If uid is None, return the name of the current user.""" |
|
502 | If uid is None, return the name of the current user.""" | |
503 |
|
503 | |||
504 | if uid is None: |
|
504 | if uid is None: | |
505 | uid = os.getuid() |
|
505 | uid = os.getuid() | |
506 | try: |
|
506 | try: | |
507 | return pwd.getpwuid(uid)[0] |
|
507 | return pwd.getpwuid(uid)[0] | |
508 | except KeyError: |
|
508 | except KeyError: | |
509 | return str(uid) |
|
509 | return str(uid) | |
510 |
|
510 | |||
511 | def groupname(gid=None): |
|
511 | def groupname(gid=None): | |
512 | """Return the name of the group with the given gid. |
|
512 | """Return the name of the group with the given gid. | |
513 |
|
513 | |||
514 | If gid is None, return the name of the current group.""" |
|
514 | If gid is None, return the name of the current group.""" | |
515 |
|
515 | |||
516 | if gid is None: |
|
516 | if gid is None: | |
517 | gid = os.getgid() |
|
517 | gid = os.getgid() | |
518 | try: |
|
518 | try: | |
519 | return grp.getgrgid(gid)[0] |
|
519 | return grp.getgrgid(gid)[0] | |
520 | except KeyError: |
|
520 | except KeyError: | |
521 | return str(gid) |
|
521 | return str(gid) | |
522 |
|
522 | |||
523 | def groupmembers(name): |
|
523 | def groupmembers(name): | |
524 | """Return the list of members of the group with the given |
|
524 | """Return the list of members of the group with the given | |
525 | name, KeyError if the group does not exist. |
|
525 | name, KeyError if the group does not exist. | |
526 | """ |
|
526 | """ | |
527 | return list(grp.getgrnam(name).gr_mem) |
|
527 | return list(grp.getgrnam(name).gr_mem) | |
528 |
|
528 | |||
529 | def spawndetached(args): |
|
529 | def spawndetached(args): | |
530 | return os.spawnvp(os.P_NOWAIT | getattr(os, 'P_DETACH', 0), |
|
530 | return os.spawnvp(os.P_NOWAIT | getattr(os, 'P_DETACH', 0), | |
531 | args[0], args) |
|
531 | args[0], args) | |
532 |
|
532 | |||
533 | def gethgcmd(): |
|
533 | def gethgcmd(): | |
534 | return sys.argv[:1] |
|
534 | return sys.argv[:1] | |
535 |
|
535 | |||
536 | def makedir(path, notindexed): |
|
536 | def makedir(path, notindexed): | |
537 | os.mkdir(path) |
|
537 | os.mkdir(path) | |
538 |
|
538 | |||
539 | def lookupreg(key, name=None, scope=None): |
|
539 | def lookupreg(key, name=None, scope=None): | |
540 | return None |
|
540 | return None | |
541 |
|
541 | |||
542 | def hidewindow(): |
|
542 | def hidewindow(): | |
543 | """Hide current shell window. |
|
543 | """Hide current shell window. | |
544 |
|
544 | |||
545 | Used to hide the window opened when starting asynchronous |
|
545 | Used to hide the window opened when starting asynchronous | |
546 | child process under Windows, unneeded on other systems. |
|
546 | child process under Windows, unneeded on other systems. | |
547 | """ |
|
547 | """ | |
548 | pass |
|
548 | pass | |
549 |
|
549 | |||
550 | class cachestat(object): |
|
550 | class cachestat(object): | |
551 | def __init__(self, path): |
|
551 | def __init__(self, path): | |
552 | self.stat = os.stat(path) |
|
552 | self.stat = os.stat(path) | |
553 |
|
553 | |||
554 | def cacheable(self): |
|
554 | def cacheable(self): | |
555 | return bool(self.stat.st_ino) |
|
555 | return bool(self.stat.st_ino) | |
556 |
|
556 | |||
557 | __hash__ = object.__hash__ |
|
557 | __hash__ = object.__hash__ | |
558 |
|
558 | |||
559 | def __eq__(self, other): |
|
559 | def __eq__(self, other): | |
560 | try: |
|
560 | try: | |
561 | # Only dev, ino, size, mtime and atime are likely to change. Out |
|
561 | # Only dev, ino, size, mtime and atime are likely to change. Out | |
562 | # of these, we shouldn't compare atime but should compare the |
|
562 | # of these, we shouldn't compare atime but should compare the | |
563 | # rest. However, one of the other fields changing indicates |
|
563 | # rest. However, one of the other fields changing indicates | |
564 | # something fishy going on, so return False if anything but atime |
|
564 | # something fishy going on, so return False if anything but atime | |
565 | # changes. |
|
565 | # changes. | |
566 | return (self.stat.st_mode == other.stat.st_mode and |
|
566 | return (self.stat.st_mode == other.stat.st_mode and | |
567 | self.stat.st_ino == other.stat.st_ino and |
|
567 | self.stat.st_ino == other.stat.st_ino and | |
568 | self.stat.st_dev == other.stat.st_dev and |
|
568 | self.stat.st_dev == other.stat.st_dev and | |
569 | self.stat.st_nlink == other.stat.st_nlink and |
|
569 | self.stat.st_nlink == other.stat.st_nlink and | |
570 | self.stat.st_uid == other.stat.st_uid and |
|
570 | self.stat.st_uid == other.stat.st_uid and | |
571 | self.stat.st_gid == other.stat.st_gid and |
|
571 | self.stat.st_gid == other.stat.st_gid and | |
572 | self.stat.st_size == other.stat.st_size and |
|
572 | self.stat.st_size == other.stat.st_size and | |
573 | self.stat.st_mtime == other.stat.st_mtime and |
|
573 | self.stat.st_mtime == other.stat.st_mtime and | |
574 | self.stat.st_ctime == other.stat.st_ctime) |
|
574 | self.stat.st_ctime == other.stat.st_ctime) | |
575 | except AttributeError: |
|
575 | except AttributeError: | |
576 | return False |
|
576 | return False | |
577 |
|
577 | |||
578 | def __ne__(self, other): |
|
578 | def __ne__(self, other): | |
579 | return not self == other |
|
579 | return not self == other | |
580 |
|
580 | |||
581 | def executablepath(): |
|
581 | def executablepath(): | |
582 | return None # available on Windows only |
|
582 | return None # available on Windows only | |
583 |
|
583 | |||
584 | def statislink(st): |
|
584 | def statislink(st): | |
585 | '''check whether a stat result is a symlink''' |
|
585 | '''check whether a stat result is a symlink''' | |
586 | return st and stat.S_ISLNK(st.st_mode) |
|
586 | return st and stat.S_ISLNK(st.st_mode) | |
587 |
|
587 | |||
588 | def statisexec(st): |
|
588 | def statisexec(st): | |
589 | '''check whether a stat result is an executable file''' |
|
589 | '''check whether a stat result is an executable file''' | |
590 | return st and (st.st_mode & 0o100 != 0) |
|
590 | return st and (st.st_mode & 0o100 != 0) | |
591 |
|
591 | |||
592 | def poll(fds): |
|
592 | def poll(fds): | |
593 | """block until something happens on any file descriptor |
|
593 | """block until something happens on any file descriptor | |
594 |
|
594 | |||
595 | This is a generic helper that will check for any activity |
|
595 | This is a generic helper that will check for any activity | |
596 | (read, write. exception) and return the list of touched files. |
|
596 | (read, write. exception) and return the list of touched files. | |
597 |
|
597 | |||
598 | In unsupported cases, it will raise a NotImplementedError""" |
|
598 | In unsupported cases, it will raise a NotImplementedError""" | |
599 | try: |
|
599 | try: | |
600 | while True: |
|
600 | while True: | |
601 | try: |
|
601 | try: | |
602 | res = select.select(fds, fds, fds) |
|
602 | res = select.select(fds, fds, fds) | |
603 | break |
|
603 | break | |
604 | except select.error as inst: |
|
604 | except select.error as inst: | |
605 | if inst.args[0] == errno.EINTR: |
|
605 | if inst.args[0] == errno.EINTR: | |
606 | continue |
|
606 | continue | |
607 | raise |
|
607 | raise | |
608 | except ValueError: # out of range file descriptor |
|
608 | except ValueError: # out of range file descriptor | |
609 | raise NotImplementedError() |
|
609 | raise NotImplementedError() | |
610 | return sorted(list(set(sum(res, [])))) |
|
610 | return sorted(list(set(sum(res, [])))) | |
611 |
|
611 | |||
612 | def readpipe(pipe): |
|
612 | def readpipe(pipe): | |
613 | """Read all available data from a pipe.""" |
|
613 | """Read all available data from a pipe.""" | |
614 | # We can't fstat() a pipe because Linux will always report 0. |
|
614 | # We can't fstat() a pipe because Linux will always report 0. | |
615 | # So, we set the pipe to non-blocking mode and read everything |
|
615 | # So, we set the pipe to non-blocking mode and read everything | |
616 | # that's available. |
|
616 | # that's available. | |
617 | flags = fcntl.fcntl(pipe, fcntl.F_GETFL) |
|
617 | flags = fcntl.fcntl(pipe, fcntl.F_GETFL) | |
618 | flags |= os.O_NONBLOCK |
|
618 | flags |= os.O_NONBLOCK | |
619 | oldflags = fcntl.fcntl(pipe, fcntl.F_SETFL, flags) |
|
619 | oldflags = fcntl.fcntl(pipe, fcntl.F_SETFL, flags) | |
620 |
|
620 | |||
621 | try: |
|
621 | try: | |
622 | chunks = [] |
|
622 | chunks = [] | |
623 | while True: |
|
623 | while True: | |
624 | try: |
|
624 | try: | |
625 | s = pipe.read() |
|
625 | s = pipe.read() | |
626 | if not s: |
|
626 | if not s: | |
627 | break |
|
627 | break | |
628 | chunks.append(s) |
|
628 | chunks.append(s) | |
629 | except IOError: |
|
629 | except IOError: | |
630 | break |
|
630 | break | |
631 |
|
631 | |||
632 | return ''.join(chunks) |
|
632 | return ''.join(chunks) | |
633 | finally: |
|
633 | finally: | |
634 | fcntl.fcntl(pipe, fcntl.F_SETFL, oldflags) |
|
634 | fcntl.fcntl(pipe, fcntl.F_SETFL, oldflags) | |
635 |
|
635 | |||
636 | def bindunixsocket(sock, path): |
|
636 | def bindunixsocket(sock, path): | |
637 | """Bind the UNIX domain socket to the specified path""" |
|
637 | """Bind the UNIX domain socket to the specified path""" | |
638 | # use relative path instead of full path at bind() if possible, since |
|
638 | # use relative path instead of full path at bind() if possible, since | |
639 | # AF_UNIX path has very small length limit (107 chars) on common |
|
639 | # AF_UNIX path has very small length limit (107 chars) on common | |
640 | # platforms (see sys/un.h) |
|
640 | # platforms (see sys/un.h) | |
641 | dirname, basename = os.path.split(path) |
|
641 | dirname, basename = os.path.split(path) | |
642 | bakwdfd = None |
|
642 | bakwdfd = None | |
643 | if dirname: |
|
643 | if dirname: | |
644 | bakwdfd = os.open('.', os.O_DIRECTORY) |
|
644 | bakwdfd = os.open('.', os.O_DIRECTORY) | |
645 | os.chdir(dirname) |
|
645 | os.chdir(dirname) | |
646 | sock.bind(basename) |
|
646 | sock.bind(basename) | |
647 | if bakwdfd: |
|
647 | if bakwdfd: | |
648 | os.fchdir(bakwdfd) |
|
648 | os.fchdir(bakwdfd) | |
649 | os.close(bakwdfd) |
|
649 | os.close(bakwdfd) |
General Comments 0
You need to be logged in to leave comments.
Login now