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