annotate mercurial/posix.py @ 40626:87a872555e90

revlog: detect incomplete revlog reads _readsegment() is supposed to return N bytes of revlog revision data starting at a file offset. Surprisingly, its behavior before this patch never verified that it actually read and returned N bytes! Instead, it would perform the read(), then return whatever data was available. And even more surprisingly, nothing in the call chain appears to have been validating that it received all the data it was expecting. This behavior could lead to partial or incomplete revision chunks being operated on. This could result in e.g. cached deltas being applied against incomplete base revisions. The delta application process would happily perform this operation. Only hash verification would detect the corruption and save us. This commit changes the behavior of raw revlog reading to validate that we actually read() the number of bytes that were requested. We will raise a more specific error faster, rather than possibly have it go undetected or manifest later in the call stack, at delta application or hash verification. Differential Revision: https://phab.mercurial-scm.org/D5266
author Gregory Szorc <gregory.szorc@gmail.com>
date Tue, 13 Nov 2018 12:30:59 -0800
parents 5fe0b880200e
children bd0874977a5e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8226
8b2cd04a6e97 put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
1 # posix.py - Posix utility function implementations for Mercurial
8b2cd04a6e97 put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
2 #
8b2cd04a6e97 put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
3 # Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others
8b2cd04a6e97 put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
4 #
8b2cd04a6e97 put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
5 # This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 10218
diff changeset
6 # GNU General Public License version 2 or any later version.
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
7
25967
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
8 from __future__ import absolute_import
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
9
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
10 import errno
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
11 import fcntl
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
12 import getpass
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
13 import grp
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
14 import os
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
15 import pwd
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
16 import re
25420
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
17 import select
25967
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
18 import stat
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
19 import sys
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
20 import tempfile
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
21 import unicodedata
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
22
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
23 from .i18n import _
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
24 from . import (
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
25 encoding,
33651
739cc0f9cbb4 ssh: ban any username@host or host that starts with - (SEC)
Augie Fackler <augie@google.com>
parents: 32721
diff changeset
26 error,
35509
beede158ea8a util: move getfstype() to the platform modules
Matt Harbison <matt_harbison@yahoo.com>
parents: 34647
diff changeset
27 policy,
30612
d623cc6b3742 py3: replace os.pathsep with pycompat.ospathsep
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30555
diff changeset
28 pycompat,
25967
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
29 )
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
30
35509
beede158ea8a util: move getfstype() to the platform modules
Matt Harbison <matt_harbison@yahoo.com>
parents: 34647
diff changeset
31 osutil = policy.importmod(r'osutil')
beede158ea8a util: move getfstype() to the platform modules
Matt Harbison <matt_harbison@yahoo.com>
parents: 34647
diff changeset
32
9031
3b76321aa0de compat: use open() instead of file() everywhere
Alejandro Santos <alejolp@alejolp.com>
parents: 8761
diff changeset
33 posixfile = open
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
34 normpath = os.path.normpath
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
35 samestat = os.path.samestat
27236
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
36 try:
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
37 oslink = os.link
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
38 except AttributeError:
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
39 # Some platforms build Python without os.link on systems that are
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
40 # vaguely unix-like but don't have hardlink support. For those
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
41 # poor souls, just say we tried and that it failed so we fall back
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
42 # to copies.
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
43 def oslink(src, dst):
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
44 raise OSError(errno.EINVAL,
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
45 'hardlinks not supported: %s to %s' % (src, dst))
39904
5fe0b880200e py3: convert os.readlink() path to native strings on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 38252
diff changeset
46 readlink = os.readlink
13280
6052bbc7aabd reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents: 13007
diff changeset
47 unlink = os.unlink
9549
8b8920209317 util: move rename into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 9517
diff changeset
48 rename = os.rename
24692
144883a8d0d4 util: add removedirs as platform depending function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24596
diff changeset
49 removedirs = os.removedirs
8614
573734e7e6d0 cmdutils: Take over glob expansion duties from util
Matt Mackall <mpm@selenic.com>
parents: 8312
diff changeset
50 expandglobs = False
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
51
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
52 umask = os.umask(0)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
53 os.umask(umask)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
54
17560
9ee25d7b1aed util: implement a faster os.path.split for posix systems
Bryan O'Sullivan <bryano@fb.com>
parents: 17391
diff changeset
55 def split(p):
18288
0d5a22f73a1f posix: fix split() for the case where the path is at the root of the filesystem
Remy Blank <remy.blank@pobox.com>
parents: 18143
diff changeset
56 '''Same as posixpath.split, but faster
0d5a22f73a1f posix: fix split() for the case where the path is at the root of the filesystem
Remy Blank <remy.blank@pobox.com>
parents: 18143
diff changeset
57
0d5a22f73a1f posix: fix split() for the case where the path is at the root of the filesystem
Remy Blank <remy.blank@pobox.com>
parents: 18143
diff changeset
58 >>> import posixpath
34131
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 33659
diff changeset
59 >>> for f in [b'/absolute/path/to/file',
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 33659
diff changeset
60 ... b'relative/path/to/file',
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 33659
diff changeset
61 ... b'file_alone',
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 33659
diff changeset
62 ... b'path/to/directory/',
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 33659
diff changeset
63 ... b'/multiple/path//separators',
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 33659
diff changeset
64 ... b'/file_at_root',
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 33659
diff changeset
65 ... b'///multiple_leading_separators_at_root',
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 33659
diff changeset
66 ... b'']:
18288
0d5a22f73a1f posix: fix split() for the case where the path is at the root of the filesystem
Remy Blank <remy.blank@pobox.com>
parents: 18143
diff changeset
67 ... assert split(f) == posixpath.split(f), f
0d5a22f73a1f posix: fix split() for the case where the path is at the root of the filesystem
Remy Blank <remy.blank@pobox.com>
parents: 18143
diff changeset
68 '''
17560
9ee25d7b1aed util: implement a faster os.path.split for posix systems
Bryan O'Sullivan <bryano@fb.com>
parents: 17391
diff changeset
69 ht = p.rsplit('/', 1)
9ee25d7b1aed util: implement a faster os.path.split for posix systems
Bryan O'Sullivan <bryano@fb.com>
parents: 17391
diff changeset
70 if len(ht) == 1:
9ee25d7b1aed util: implement a faster os.path.split for posix systems
Bryan O'Sullivan <bryano@fb.com>
parents: 17391
diff changeset
71 return '', p
9ee25d7b1aed util: implement a faster os.path.split for posix systems
Bryan O'Sullivan <bryano@fb.com>
parents: 17391
diff changeset
72 nh = ht[0].rstrip('/')
9ee25d7b1aed util: implement a faster os.path.split for posix systems
Bryan O'Sullivan <bryano@fb.com>
parents: 17391
diff changeset
73 if nh:
9ee25d7b1aed util: implement a faster os.path.split for posix systems
Bryan O'Sullivan <bryano@fb.com>
parents: 17391
diff changeset
74 return nh, ht[1]
18288
0d5a22f73a1f posix: fix split() for the case where the path is at the root of the filesystem
Remy Blank <remy.blank@pobox.com>
parents: 18143
diff changeset
75 return ht[0] + '/', ht[1]
17560
9ee25d7b1aed util: implement a faster os.path.split for posix systems
Bryan O'Sullivan <bryano@fb.com>
parents: 17391
diff changeset
76
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
77 def openhardlinks():
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
78 '''return true if it is safe to hold open file handles to hardlinks'''
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
79 return True
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
80
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13280
diff changeset
81 def nlinks(name):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13280
diff changeset
82 '''return number of hardlinks for the given file'''
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13280
diff changeset
83 return os.lstat(name).st_nlink
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13280
diff changeset
84
14231
8abe4db2d162 rename util.parse_patch_output to parsepatchoutput
Adrian Buehlmann <adrian@cadifra.com>
parents: 14165
diff changeset
85 def parsepatchoutput(output_line):
8761
0289f384e1e5 Generally replace "file name" with "filename" in help and comments.
timeless <timeless@gmail.com>
parents: 8657
diff changeset
86 """parses the output produced by patch and returns the filename"""
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
87 pf = output_line[14:]
30642
e995f00a9e9a py3: replace sys.platform with pycompat.sysplatform (part 2 of 2)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30641
diff changeset
88 if pycompat.sysplatform == 'OpenVMS':
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
89 if pf[0] == '`':
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
90 pf = pf[1:-1] # Remove the quotes
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
91 else:
8219
21cf74ff2deb whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 7943
diff changeset
92 if pf.startswith("'") and pf.endswith("'") and " " in pf:
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
93 pf = pf[1:-1] # Remove the quotes
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
94 return pf
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
95
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
96 def sshargs(sshcmd, host, user, port):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
97 '''Build argument list for ssh'''
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
98 args = user and ("%s@%s" % (user, host)) or host
33659
8cb9e921ef8c ssh: quote parameters using shellquote (SEC)
Jun Wu <quark@fb.com>
parents: 33651
diff changeset
99 if '-' in args[:1]:
33651
739cc0f9cbb4 ssh: ban any username@host or host that starts with - (SEC)
Augie Fackler <augie@google.com>
parents: 32721
diff changeset
100 raise error.Abort(
739cc0f9cbb4 ssh: ban any username@host or host that starts with - (SEC)
Augie Fackler <augie@google.com>
parents: 32721
diff changeset
101 _('illegal ssh hostname or username starting with -: %s') % args)
33659
8cb9e921ef8c ssh: quote parameters using shellquote (SEC)
Jun Wu <quark@fb.com>
parents: 33651
diff changeset
102 args = shellquote(args)
8cb9e921ef8c ssh: quote parameters using shellquote (SEC)
Jun Wu <quark@fb.com>
parents: 33651
diff changeset
103 if port:
8cb9e921ef8c ssh: quote parameters using shellquote (SEC)
Jun Wu <quark@fb.com>
parents: 33651
diff changeset
104 args = '-p %s %s' % (shellquote(port), args)
8cb9e921ef8c ssh: quote parameters using shellquote (SEC)
Jun Wu <quark@fb.com>
parents: 33651
diff changeset
105 return args
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
106
14273
38af0f514134 rename util.is_exec to isexec
Adrian Buehlmann <adrian@cadifra.com>
parents: 14272
diff changeset
107 def isexec(f):
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
108 """check whether a file is executable"""
25658
e93036747902 global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25420
diff changeset
109 return (os.lstat(f).st_mode & 0o100 != 0)
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
110
14232
df2399663392 rename util.set_flags to setflags
Adrian Buehlmann <adrian@cadifra.com>
parents: 14231
diff changeset
111 def setflags(f, l, x):
32721
c2cb0de25120 chmod: create a new file when flags are set on a hardlinked file
Koen Van Hoof <koen.van_hoof@nokia.com>
parents: 32394
diff changeset
112 st = os.lstat(f)
c2cb0de25120 chmod: create a new file when flags are set on a hardlinked file
Koen Van Hoof <koen.van_hoof@nokia.com>
parents: 32394
diff changeset
113 s = st.st_mode
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
114 if l:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
115 if not stat.S_ISLNK(s):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
116 # switch file to link
36303
ed95758addf3 py3: make sure we open the files in bytes mode
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35513
diff changeset
117 fp = open(f, 'rb')
13400
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13375
diff changeset
118 data = fp.read()
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13375
diff changeset
119 fp.close()
31537
c6cbe5720353 posix: use local reference to unlink
Ryan McElroy <rmcelroy@fb.com>
parents: 31506
diff changeset
120 unlink(f)
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
121 try:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
122 os.symlink(data, f)
14004
97ed99d1f419 eliminate various naked except clauses
Idan Kamara <idankk86@gmail.com>
parents: 13986
diff changeset
123 except OSError:
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
124 # failed to make a link, rewrite file
36303
ed95758addf3 py3: make sure we open the files in bytes mode
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35513
diff changeset
125 fp = open(f, "wb")
13400
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13375
diff changeset
126 fp.write(data)
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13375
diff changeset
127 fp.close()
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
128 # no chmod needed at this point
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
129 return
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
130 if stat.S_ISLNK(s):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
131 # switch link to file
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
132 data = os.readlink(f)
31537
c6cbe5720353 posix: use local reference to unlink
Ryan McElroy <rmcelroy@fb.com>
parents: 31506
diff changeset
133 unlink(f)
36303
ed95758addf3 py3: make sure we open the files in bytes mode
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35513
diff changeset
134 fp = open(f, "wb")
13400
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13375
diff changeset
135 fp.write(data)
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13375
diff changeset
136 fp.close()
25658
e93036747902 global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25420
diff changeset
137 s = 0o666 & ~umask # avoid restatting for chmod
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
138
25658
e93036747902 global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25420
diff changeset
139 sx = s & 0o100
32721
c2cb0de25120 chmod: create a new file when flags are set on a hardlinked file
Koen Van Hoof <koen.van_hoof@nokia.com>
parents: 32394
diff changeset
140 if st.st_nlink > 1 and bool(x) != bool(sx):
c2cb0de25120 chmod: create a new file when flags are set on a hardlinked file
Koen Van Hoof <koen.van_hoof@nokia.com>
parents: 32394
diff changeset
141 # the file is a hardlink, break it
c2cb0de25120 chmod: create a new file when flags are set on a hardlinked file
Koen Van Hoof <koen.van_hoof@nokia.com>
parents: 32394
diff changeset
142 with open(f, "rb") as fp:
c2cb0de25120 chmod: create a new file when flags are set on a hardlinked file
Koen Van Hoof <koen.van_hoof@nokia.com>
parents: 32394
diff changeset
143 data = fp.read()
c2cb0de25120 chmod: create a new file when flags are set on a hardlinked file
Koen Van Hoof <koen.van_hoof@nokia.com>
parents: 32394
diff changeset
144 unlink(f)
c2cb0de25120 chmod: create a new file when flags are set on a hardlinked file
Koen Van Hoof <koen.van_hoof@nokia.com>
parents: 32394
diff changeset
145 with open(f, "wb") as fp:
c2cb0de25120 chmod: create a new file when flags are set on a hardlinked file
Koen Van Hoof <koen.van_hoof@nokia.com>
parents: 32394
diff changeset
146 fp.write(data)
c2cb0de25120 chmod: create a new file when flags are set on a hardlinked file
Koen Van Hoof <koen.van_hoof@nokia.com>
parents: 32394
diff changeset
147
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
148 if x and not sx:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
149 # Turn on +x for every +r bit when making a file executable
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
150 # and obey umask.
25658
e93036747902 global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25420
diff changeset
151 os.chmod(f, s | (s & 0o444) >> 2 & ~umask)
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
152 elif not x and sx:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
153 # Turn off all +x bits
25658
e93036747902 global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25420
diff changeset
154 os.chmod(f, s & 0o666)
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
155
15011
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
156 def copymode(src, dst, mode=None):
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
157 '''Copy the file mode from the file at path src to dst.
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
158 If src doesn't exist, we're using mode instead. If mode is None, we're
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
159 using umask.'''
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
160 try:
25658
e93036747902 global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25420
diff changeset
161 st_mode = os.lstat(src).st_mode & 0o777
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25658
diff changeset
162 except OSError as inst:
15011
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
163 if inst.errno != errno.ENOENT:
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
164 raise
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
165 st_mode = mode
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
166 if st_mode is None:
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
167 st_mode = ~umask
25658
e93036747902 global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25420
diff changeset
168 st_mode &= 0o666
15011
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
169 os.chmod(dst, st_mode)
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
170
13879
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
171 def checkexec(path):
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
172 """
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
173 Check whether the given path is on a filesystem with UNIX-like exec flags
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
174
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
175 Requires a directory (like /foo/.hg)
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
176 """
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
177
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
178 # VFAT on some Linux versions can flip mode but it doesn't persist
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
179 # a FS remount. Frequently we can detect it if files are created
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
180 # with exec bit on.
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
181
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
182 try:
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
183 EXECFLAGS = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
30444
b1ce25a40826 posix: move checkexec test file to .hg/cache
Mads Kiilerich <madski@unity3d.com>
parents: 30309
diff changeset
184 cachedir = os.path.join(path, '.hg', 'cache')
30446
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
185 if os.path.isdir(cachedir):
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
186 checkisexec = os.path.join(cachedir, 'checkisexec')
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
187 checknoexec = os.path.join(cachedir, 'checknoexec')
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
188
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
189 try:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
190 m = os.stat(checkisexec).st_mode
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
191 except OSError as e:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
192 if e.errno != errno.ENOENT:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
193 raise
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
194 # checkisexec does not exist - fall through ...
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
195 else:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
196 # checkisexec exists, check if it actually is exec
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
197 if m & EXECFLAGS != 0:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
198 # ensure checkisexec exists, check it isn't exec
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
199 try:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
200 m = os.stat(checknoexec).st_mode
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
201 except OSError as e:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
202 if e.errno != errno.ENOENT:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
203 raise
31505
d96d010be4fa posix: use open() instead of file()
Augie Fackler <augie@google.com>
parents: 31491
diff changeset
204 open(checknoexec, 'w').close() # might fail
30446
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
205 m = os.stat(checknoexec).st_mode
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
206 if m & EXECFLAGS == 0:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
207 # check-exec is exec and check-no-exec is not exec
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
208 return True
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
209 # checknoexec exists but is exec - delete it
31537
c6cbe5720353 posix: use local reference to unlink
Ryan McElroy <rmcelroy@fb.com>
parents: 31506
diff changeset
210 unlink(checknoexec)
30446
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
211 # checkisexec exists but is not exec - delete it
31537
c6cbe5720353 posix: use local reference to unlink
Ryan McElroy <rmcelroy@fb.com>
parents: 31506
diff changeset
212 unlink(checkisexec)
30446
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
213
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
214 # check using one file, leave it as checkisexec
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
215 checkdir = cachedir
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
216 else:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
217 # check directly in path and don't leave checkisexec behind
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
218 checkdir = path
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
219 checkisexec = None
38164
aac4be30e250 py3: wrap tempfile.mkstemp() to use bytes path
Yuya Nishihara <yuya@tcha.org>
parents: 37934
diff changeset
220 fh, fn = pycompat.mkstemp(dir=checkdir, prefix='hg-checkexec-')
13879
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
221 try:
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
222 os.close(fh)
30445
1ce4c2062ab0 posix: simplify checkexec check
Mads Kiilerich <madski@unity3d.com>
parents: 30444
diff changeset
223 m = os.stat(fn).st_mode
30446
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
224 if m & EXECFLAGS == 0:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
225 os.chmod(fn, m & 0o777 | EXECFLAGS)
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
226 if os.stat(fn).st_mode & EXECFLAGS != 0:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
227 if checkisexec is not None:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
228 os.rename(fn, checkisexec)
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
229 fn = None
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
230 return True
13879
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
231 finally:
30446
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
232 if fn is not None:
31537
c6cbe5720353 posix: use local reference to unlink
Ryan McElroy <rmcelroy@fb.com>
parents: 31506
diff changeset
233 unlink(fn)
13879
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
234 except (IOError, OSError):
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
235 # we don't care, the user probably won't be able to commit anyway
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
236 return False
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
237
13890
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
238 def checklink(path):
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
239 """check whether the given path is on a symlink-capable filesystem"""
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
240 # mktemp is not racy because symlink creation will fail if the
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
241 # file already exists
26883
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
242 while True:
30447
0d87b1caed92 posix: move checklink test file to .hg/cache
Mads Kiilerich <madski@unity3d.com>
parents: 30446
diff changeset
243 cachedir = os.path.join(path, '.hg', 'cache')
30448
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30447
diff changeset
244 checklink = os.path.join(cachedir, 'checklink')
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30447
diff changeset
245 # try fast path, read only
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30447
diff changeset
246 if os.path.islink(checklink):
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30447
diff changeset
247 return True
30447
0d87b1caed92 posix: move checklink test file to .hg/cache
Mads Kiilerich <madski@unity3d.com>
parents: 30446
diff changeset
248 if os.path.isdir(cachedir):
0d87b1caed92 posix: move checklink test file to .hg/cache
Mads Kiilerich <madski@unity3d.com>
parents: 30446
diff changeset
249 checkdir = cachedir
0d87b1caed92 posix: move checklink test file to .hg/cache
Mads Kiilerich <madski@unity3d.com>
parents: 30446
diff changeset
250 else:
0d87b1caed92 posix: move checklink test file to .hg/cache
Mads Kiilerich <madski@unity3d.com>
parents: 30446
diff changeset
251 checkdir = path
0d87b1caed92 posix: move checklink test file to .hg/cache
Mads Kiilerich <madski@unity3d.com>
parents: 30446
diff changeset
252 cachedir = None
38166
cc9aa88792fe py3: wrap tempfile.NamedTemporaryFile() to return bytes fp.name
Yuya Nishihara <yuya@tcha.org>
parents: 38164
diff changeset
253 name = tempfile.mktemp(dir=pycompat.fsdecode(checkdir),
31506
53575feed7c0 posix: tiptoe around tempfile module more delicately
Augie Fackler <augie@google.com>
parents: 31505
diff changeset
254 prefix=r'checklink-')
53575feed7c0 posix: tiptoe around tempfile module more delicately
Augie Fackler <augie@google.com>
parents: 31505
diff changeset
255 name = pycompat.fsencode(name)
22946
77c121da6143 checklink: always close the NamedTemporaryFile
Augie Fackler <raf@durin42.com>
parents: 22781
diff changeset
256 try:
30555
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30448
diff changeset
257 fd = None
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30448
diff changeset
258 if cachedir is None:
38166
cc9aa88792fe py3: wrap tempfile.NamedTemporaryFile() to return bytes fp.name
Yuya Nishihara <yuya@tcha.org>
parents: 38164
diff changeset
259 fd = pycompat.namedtempfile(dir=checkdir,
cc9aa88792fe py3: wrap tempfile.NamedTemporaryFile() to return bytes fp.name
Yuya Nishihara <yuya@tcha.org>
parents: 38164
diff changeset
260 prefix='hg-checklink-')
cc9aa88792fe py3: wrap tempfile.NamedTemporaryFile() to return bytes fp.name
Yuya Nishihara <yuya@tcha.org>
parents: 38164
diff changeset
261 target = os.path.basename(fd.name)
30555
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30448
diff changeset
262 else:
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30448
diff changeset
263 # create a fixed file to link to; doesn't matter if it
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30448
diff changeset
264 # already exists.
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30448
diff changeset
265 target = 'checklink-target'
32394
38a2b9d90131 checklink: degrade gracefully on posix when fs is readonly (issue5511)
Augie Fackler <augie@google.com>
parents: 32291
diff changeset
266 try:
36948
0585337ea787 cleanup: fix some latent open(path).read() et al calls we previously missed
Augie Fackler <augie@google.com>
parents: 36781
diff changeset
267 fullpath = os.path.join(cachedir, target)
0585337ea787 cleanup: fix some latent open(path).read() et al calls we previously missed
Augie Fackler <augie@google.com>
parents: 36781
diff changeset
268 open(fullpath, 'w').close()
32394
38a2b9d90131 checklink: degrade gracefully on posix when fs is readonly (issue5511)
Augie Fackler <augie@google.com>
parents: 32291
diff changeset
269 except IOError as inst:
38a2b9d90131 checklink: degrade gracefully on posix when fs is readonly (issue5511)
Augie Fackler <augie@google.com>
parents: 32291
diff changeset
270 if inst[0] == errno.EACCES:
38a2b9d90131 checklink: degrade gracefully on posix when fs is readonly (issue5511)
Augie Fackler <augie@google.com>
parents: 32291
diff changeset
271 # If we can't write to cachedir, just pretend
38a2b9d90131 checklink: degrade gracefully on posix when fs is readonly (issue5511)
Augie Fackler <augie@google.com>
parents: 32291
diff changeset
272 # that the fs is readonly and by association
38a2b9d90131 checklink: degrade gracefully on posix when fs is readonly (issue5511)
Augie Fackler <augie@google.com>
parents: 32291
diff changeset
273 # that the fs won't support symlinks. This
38a2b9d90131 checklink: degrade gracefully on posix when fs is readonly (issue5511)
Augie Fackler <augie@google.com>
parents: 32291
diff changeset
274 # seems like the least dangerous way to avoid
38a2b9d90131 checklink: degrade gracefully on posix when fs is readonly (issue5511)
Augie Fackler <augie@google.com>
parents: 32291
diff changeset
275 # data loss.
38a2b9d90131 checklink: degrade gracefully on posix when fs is readonly (issue5511)
Augie Fackler <augie@google.com>
parents: 32291
diff changeset
276 return False
38a2b9d90131 checklink: degrade gracefully on posix when fs is readonly (issue5511)
Augie Fackler <augie@google.com>
parents: 32291
diff changeset
277 raise
26883
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
278 try:
30555
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30448
diff changeset
279 os.symlink(target, name)
30448
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30447
diff changeset
280 if cachedir is None:
31537
c6cbe5720353 posix: use local reference to unlink
Ryan McElroy <rmcelroy@fb.com>
parents: 31506
diff changeset
281 unlink(name)
30448
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30447
diff changeset
282 else:
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30447
diff changeset
283 try:
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30447
diff changeset
284 os.rename(name, checklink)
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30447
diff changeset
285 except OSError:
31537
c6cbe5720353 posix: use local reference to unlink
Ryan McElroy <rmcelroy@fb.com>
parents: 31506
diff changeset
286 unlink(name)
26883
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
287 return True
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
288 except OSError as inst:
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
289 # link creation might race, try again
37934
2d919ab6c5b4 posix: use inst.errno instead of inst[0] on OSError instances
Augie Fackler <augie@google.com>
parents: 37460
diff changeset
290 if inst.errno == errno.EEXIST:
26883
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
291 continue
26889
1aa5083cbebb posix: fix test-permissions regression
Matt Mackall <mpm@selenic.com>
parents: 26883
diff changeset
292 raise
26883
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
293 finally:
30555
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30448
diff changeset
294 if fd is not None:
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30448
diff changeset
295 fd.close()
26883
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
296 except AttributeError:
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
297 return False
26889
1aa5083cbebb posix: fix test-permissions regression
Matt Mackall <mpm@selenic.com>
parents: 26883
diff changeset
298 except OSError as inst:
1aa5083cbebb posix: fix test-permissions regression
Matt Mackall <mpm@selenic.com>
parents: 26883
diff changeset
299 # sshfs might report failure while successfully creating the link
37934
2d919ab6c5b4 posix: use inst.errno instead of inst[0] on OSError instances
Augie Fackler <augie@google.com>
parents: 37460
diff changeset
300 if inst.errno == errno.EIO and os.path.exists(name):
31537
c6cbe5720353 posix: use local reference to unlink
Ryan McElroy <rmcelroy@fb.com>
parents: 31506
diff changeset
301 unlink(name)
26889
1aa5083cbebb posix: fix test-permissions regression
Matt Mackall <mpm@selenic.com>
parents: 26883
diff changeset
302 return False
13890
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
303
13916
98ee3dd5bab4 path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13890
diff changeset
304 def checkosfilename(path):
98ee3dd5bab4 path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13890
diff changeset
305 '''Check that the base-relative path is a valid filename on this platform.
98ee3dd5bab4 path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13890
diff changeset
306 Returns None if the path is ok, or a UI string describing the problem.'''
34383
ab687e06fe02 posix: replace superfluous pass statement with explicit return
Augie Fackler <augie@google.com>
parents: 34202
diff changeset
307 return None # on posix platforms, every path is ok
13916
98ee3dd5bab4 path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13890
diff changeset
308
35513
c4caf530b1c7 util: add a function to show the mount point of the filesystem
Matt Harbison <matt_harbison@yahoo.com>
parents: 35509
diff changeset
309 def getfsmountpoint(dirpath):
c4caf530b1c7 util: add a function to show the mount point of the filesystem
Matt Harbison <matt_harbison@yahoo.com>
parents: 35509
diff changeset
310 '''Get the filesystem mount point from a directory (best-effort)
c4caf530b1c7 util: add a function to show the mount point of the filesystem
Matt Harbison <matt_harbison@yahoo.com>
parents: 35509
diff changeset
311
c4caf530b1c7 util: add a function to show the mount point of the filesystem
Matt Harbison <matt_harbison@yahoo.com>
parents: 35509
diff changeset
312 Returns None if we are unsure. Raises OSError on ENOENT, EPERM, etc.
c4caf530b1c7 util: add a function to show the mount point of the filesystem
Matt Harbison <matt_harbison@yahoo.com>
parents: 35509
diff changeset
313 '''
c4caf530b1c7 util: add a function to show the mount point of the filesystem
Matt Harbison <matt_harbison@yahoo.com>
parents: 35509
diff changeset
314 return getattr(osutil, 'getfsmountpoint', lambda x: None)(dirpath)
c4caf530b1c7 util: add a function to show the mount point of the filesystem
Matt Harbison <matt_harbison@yahoo.com>
parents: 35509
diff changeset
315
35509
beede158ea8a util: move getfstype() to the platform modules
Matt Harbison <matt_harbison@yahoo.com>
parents: 34647
diff changeset
316 def getfstype(dirpath):
beede158ea8a util: move getfstype() to the platform modules
Matt Harbison <matt_harbison@yahoo.com>
parents: 34647
diff changeset
317 '''Get the filesystem type name from a directory (best-effort)
beede158ea8a util: move getfstype() to the platform modules
Matt Harbison <matt_harbison@yahoo.com>
parents: 34647
diff changeset
318
beede158ea8a util: move getfstype() to the platform modules
Matt Harbison <matt_harbison@yahoo.com>
parents: 34647
diff changeset
319 Returns None if we are unsure. Raises OSError on ENOENT, EPERM, etc.
beede158ea8a util: move getfstype() to the platform modules
Matt Harbison <matt_harbison@yahoo.com>
parents: 34647
diff changeset
320 '''
beede158ea8a util: move getfstype() to the platform modules
Matt Harbison <matt_harbison@yahoo.com>
parents: 34647
diff changeset
321 return getattr(osutil, 'getfstype', lambda x: None)(dirpath)
beede158ea8a util: move getfstype() to the platform modules
Matt Harbison <matt_harbison@yahoo.com>
parents: 34647
diff changeset
322
14233
659f34b833b9 rename util.set_binary to setbinary
Adrian Buehlmann <adrian@cadifra.com>
parents: 14232
diff changeset
323 def setbinary(fd):
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
324 pass
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
325
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
326 def pconvert(path):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
327 return path
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
328
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
329 def localpath(path):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
330 return path
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
331
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
332 def samefile(fpath1, fpath2):
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
333 """Returns whether path1 and path2 refer to the same file. This is only
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
334 guaranteed to work for files, not directories."""
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
335 return os.path.samefile(fpath1, fpath2)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
336
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
337 def samedevice(fpath1, fpath2):
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
338 """Returns whether fpath1 and fpath2 are on the same device. This is only
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
339 guaranteed to work for files, not directories."""
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
340 st1 = os.lstat(fpath1)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
341 st2 = os.lstat(fpath2)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
342 return st1.st_dev == st2.st_dev
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
343
15488
6eff984d8e76 dirstate: fix case-folding identity for traditional Unix
Matt Mackall <mpm@selenic.com>
parents: 15353
diff changeset
344 # os.path.normcase is a no-op, which doesn't help us on non-native filesystems
6eff984d8e76 dirstate: fix case-folding identity for traditional Unix
Matt Mackall <mpm@selenic.com>
parents: 15353
diff changeset
345 def normcase(path):
6eff984d8e76 dirstate: fix case-folding identity for traditional Unix
Matt Mackall <mpm@selenic.com>
parents: 15353
diff changeset
346 return path.lower()
6eff984d8e76 dirstate: fix case-folding identity for traditional Unix
Matt Mackall <mpm@selenic.com>
parents: 15353
diff changeset
347
24594
609aa973c01d posix: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24108
diff changeset
348 # what normcase does to ASCII strings
609aa973c01d posix: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24108
diff changeset
349 normcasespec = encoding.normcasespecs.lower
609aa973c01d posix: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24108
diff changeset
350 # fallback normcase function for non-ASCII strings
609aa973c01d posix: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24108
diff changeset
351 normcasefallback = normcase
609aa973c01d posix: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24108
diff changeset
352
34647
dacfcdd8b94e codemod: use pycompat.isdarwin
Jun Wu <quark@fb.com>
parents: 34383
diff changeset
353 if pycompat.isdarwin:
15551
1fa41d1f1351 posix: add extended support for OS X path folding
Matt Mackall <mpm@selenic.com>
parents: 15499
diff changeset
354
1fa41d1f1351 posix: add extended support for OS X path folding
Matt Mackall <mpm@selenic.com>
parents: 15499
diff changeset
355 def normcase(path):
19131
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
356 '''
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
357 Normalize a filename for OS X-compatible comparison:
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
358 - escape-encode invalid characters
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
359 - decompose to NFD
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
360 - lowercase
23597
7a5bcd471f2e darwin: omit ignorable codepoints when normcase()ing a file path
Augie Fackler <raf@durin42.com>
parents: 22946
diff changeset
361 - omit ignored characters [200c-200f, 202a-202e, 206a-206f,feff]
19131
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
362
34131
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 33659
diff changeset
363 >>> normcase(b'UPPER')
19131
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
364 'upper'
34199
90b0e1639fd4 posix: fix HFS+ normcase doctest to produce valid bytes literals in Python 3
Augie Fackler <raf@durin42.com>
parents: 34131
diff changeset
365 >>> normcase(b'Caf\\xc3\\xa9')
19131
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
366 'cafe\\xcc\\x81'
34199
90b0e1639fd4 posix: fix HFS+ normcase doctest to produce valid bytes literals in Python 3
Augie Fackler <raf@durin42.com>
parents: 34131
diff changeset
367 >>> normcase(b'\\xc3\\x89')
19131
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
368 'e\\xcc\\x81'
34199
90b0e1639fd4 posix: fix HFS+ normcase doctest to produce valid bytes literals in Python 3
Augie Fackler <raf@durin42.com>
parents: 34131
diff changeset
369 >>> normcase(b'\\xb8\\xca\\xc3\\xca\\xbe\\xc8.JPG') # issue3918
19131
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
370 '%b8%ca%c3\\xca\\xbe%c8.jpg'
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
371 '''
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
372
15551
1fa41d1f1351 posix: add extended support for OS X path folding
Matt Mackall <mpm@selenic.com>
parents: 15499
diff changeset
373 try:
22781
70624fda193d normcase: for darwin, use fast ASCII lower
Siddharth Agarwal <sid0@fb.com>
parents: 22246
diff changeset
374 return encoding.asciilower(path) # exception for non-ASCII
18501
a3b2dc1aa909 OS X: try cheap ascii .lower() in normcase before making full unicode dance
Mads Kiilerich <madski@unity3d.com>
parents: 18442
diff changeset
375 except UnicodeDecodeError:
24595
136ab89d61cb darwin: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24594
diff changeset
376 return normcasefallback(path)
136ab89d61cb darwin: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24594
diff changeset
377
136ab89d61cb darwin: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24594
diff changeset
378 normcasespec = encoding.normcasespecs.lower
136ab89d61cb darwin: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24594
diff changeset
379
136ab89d61cb darwin: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24594
diff changeset
380 def normcasefallback(path):
18501
a3b2dc1aa909 OS X: try cheap ascii .lower() in normcase before making full unicode dance
Mads Kiilerich <madski@unity3d.com>
parents: 18442
diff changeset
381 try:
15551
1fa41d1f1351 posix: add extended support for OS X path folding
Matt Mackall <mpm@selenic.com>
parents: 15499
diff changeset
382 u = path.decode('utf-8')
1fa41d1f1351 posix: add extended support for OS X path folding
Matt Mackall <mpm@selenic.com>
parents: 15499
diff changeset
383 except UnicodeDecodeError:
19131
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
384 # OS X percent-encodes any bytes that aren't valid utf-8
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
385 s = ''
26876
b8381832ce2b posix: use getutf8char to handle OS X filename percent-escaping
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
386 pos = 0
27380
c7129ed280b8 mac: fix percent-encoding of non-utf-8 characters (issue4999)
Matt Mackall <mpm@selenic.com>
parents: 27362
diff changeset
387 l = len(path)
26876
b8381832ce2b posix: use getutf8char to handle OS X filename percent-escaping
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
388 while pos < l:
b8381832ce2b posix: use getutf8char to handle OS X filename percent-escaping
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
389 try:
b8381832ce2b posix: use getutf8char to handle OS X filename percent-escaping
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
390 c = encoding.getutf8char(path, pos)
b8381832ce2b posix: use getutf8char to handle OS X filename percent-escaping
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
391 pos += len(c)
b8381832ce2b posix: use getutf8char to handle OS X filename percent-escaping
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
392 except ValueError:
34201
310c151a0705 posix: use slicing to grab a single byte out of a bytes in HFS+ normcase code
Augie Fackler <raf@durin42.com>
parents: 34199
diff changeset
393 c = '%%%02X' % ord(path[pos:pos + 1])
26876
b8381832ce2b posix: use getutf8char to handle OS X filename percent-escaping
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
394 pos += 1
b8381832ce2b posix: use getutf8char to handle OS X filename percent-escaping
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
395 s += c
19131
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
396
15551
1fa41d1f1351 posix: add extended support for OS X path folding
Matt Mackall <mpm@selenic.com>
parents: 15499
diff changeset
397 u = s.decode('utf-8')
1fa41d1f1351 posix: add extended support for OS X path folding
Matt Mackall <mpm@selenic.com>
parents: 15499
diff changeset
398
1fa41d1f1351 posix: add extended support for OS X path folding
Matt Mackall <mpm@selenic.com>
parents: 15499
diff changeset
399 # Decompose then lowercase (HFS+ technote specifies lower)
34202
d47749807464 posix: always pass a native str to unicodedata.normalize's first arg
Augie Fackler <raf@durin42.com>
parents: 34201
diff changeset
400 enc = unicodedata.normalize(r'NFD', u).lower().encode('utf-8')
23597
7a5bcd471f2e darwin: omit ignorable codepoints when normcase()ing a file path
Augie Fackler <raf@durin42.com>
parents: 22946
diff changeset
401 # drop HFS+ ignored characters
7a5bcd471f2e darwin: omit ignorable codepoints when normcase()ing a file path
Augie Fackler <raf@durin42.com>
parents: 22946
diff changeset
402 return encoding.hfsignoreclean(enc)
15551
1fa41d1f1351 posix: add extended support for OS X path folding
Matt Mackall <mpm@selenic.com>
parents: 15499
diff changeset
403
30641
16b5df5792a8 py3: replace sys.platform with pycompat.sysplatform (part 1 of 2)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30634
diff changeset
404 if pycompat.sysplatform == 'cygwin':
15711
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
405 # workaround for cygwin, in which mount point part of path is
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
406 # treated as case sensitive, even though underlying NTFS is case
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
407 # insensitive.
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
408
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
409 # default mount points
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
410 cygwinmountpoints = sorted([
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
411 "/usr/bin",
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
412 "/usr/lib",
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
413 "/cygdrive",
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
414 ], reverse=True)
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
415
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
416 # use upper-ing as normcase as same as NTFS workaround
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
417 def normcase(path):
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
418 pathlen = len(path)
30614
cfe66dcf45c0 py3: replace os.sep with pycompat.ossep (part 2 of 4)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30612
diff changeset
419 if (pathlen == 0) or (path[0] != pycompat.ossep):
15711
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
420 # treat as relative
17203
0cb55b5c19a3 util, posix: eliminate encodinglower and encodingupper
Adrian Buehlmann <adrian@cadifra.com>
parents: 16726
diff changeset
421 return encoding.upper(path)
15711
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
422
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
423 # to preserve case of mountpoint part
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
424 for mp in cygwinmountpoints:
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
425 if not path.startswith(mp):
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
426 continue
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
427
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
428 mplen = len(mp)
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
429 if mplen == pathlen: # mount point itself
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
430 return mp
30614
cfe66dcf45c0 py3: replace os.sep with pycompat.ossep (part 2 of 4)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30612
diff changeset
431 if path[mplen] == pycompat.ossep:
17203
0cb55b5c19a3 util, posix: eliminate encodinglower and encodingupper
Adrian Buehlmann <adrian@cadifra.com>
parents: 16726
diff changeset
432 return mp + encoding.upper(path[mplen:])
15711
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
433
17203
0cb55b5c19a3 util, posix: eliminate encodinglower and encodingupper
Adrian Buehlmann <adrian@cadifra.com>
parents: 16726
diff changeset
434 return encoding.upper(path)
15711
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
435
24596
75ea27f1711d cygwin: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24595
diff changeset
436 normcasespec = encoding.normcasespecs.other
75ea27f1711d cygwin: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24595
diff changeset
437 normcasefallback = normcase
75ea27f1711d cygwin: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24595
diff changeset
438
16240
95e45abe7e8e posix: ignore execution bit in cygwin (issue3301)
A. S. Budden <abudden@gmail.com>
parents: 15791
diff changeset
439 # Cygwin translates native ACLs to POSIX permissions,
95e45abe7e8e posix: ignore execution bit in cygwin (issue3301)
A. S. Budden <abudden@gmail.com>
parents: 15791
diff changeset
440 # but these translations are not supported by native
95e45abe7e8e posix: ignore execution bit in cygwin (issue3301)
A. S. Budden <abudden@gmail.com>
parents: 15791
diff changeset
441 # tools, so the exec bit tends to be set erroneously.
95e45abe7e8e posix: ignore execution bit in cygwin (issue3301)
A. S. Budden <abudden@gmail.com>
parents: 15791
diff changeset
442 # Therefore, disable executable bit access on Cygwin.
95e45abe7e8e posix: ignore execution bit in cygwin (issue3301)
A. S. Budden <abudden@gmail.com>
parents: 15791
diff changeset
443 def checkexec(path):
95e45abe7e8e posix: ignore execution bit in cygwin (issue3301)
A. S. Budden <abudden@gmail.com>
parents: 15791
diff changeset
444 return False
95e45abe7e8e posix: ignore execution bit in cygwin (issue3301)
A. S. Budden <abudden@gmail.com>
parents: 15791
diff changeset
445
16241
60cc3a0d2249 posix: disable cygwin's symlink emulation
Matt Mackall <mpm@selenic.com>
parents: 16240
diff changeset
446 # Similarly, Cygwin's symlink emulation is likely to create
60cc3a0d2249 posix: disable cygwin's symlink emulation
Matt Mackall <mpm@selenic.com>
parents: 16240
diff changeset
447 # problems when Mercurial is used from both Cygwin and native
60cc3a0d2249 posix: disable cygwin's symlink emulation
Matt Mackall <mpm@selenic.com>
parents: 16240
diff changeset
448 # Windows, with other native tools, or on shared volumes
60cc3a0d2249 posix: disable cygwin's symlink emulation
Matt Mackall <mpm@selenic.com>
parents: 16240
diff changeset
449 def checklink(path):
60cc3a0d2249 posix: disable cygwin's symlink emulation
Matt Mackall <mpm@selenic.com>
parents: 16240
diff changeset
450 return False
60cc3a0d2249 posix: disable cygwin's symlink emulation
Matt Mackall <mpm@selenic.com>
parents: 16240
diff changeset
451
23683
5edb387158a1 posix: quote the specified string only when it may have to be quoted
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23597
diff changeset
452 _needsshellquote = None
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
453 def shellquote(s):
30642
e995f00a9e9a py3: replace sys.platform with pycompat.sysplatform (part 2 of 2)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30641
diff changeset
454 if pycompat.sysplatform == 'OpenVMS':
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
455 return '"%s"' % s
23683
5edb387158a1 posix: quote the specified string only when it may have to be quoted
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23597
diff changeset
456 global _needsshellquote
5edb387158a1 posix: quote the specified string only when it may have to be quoted
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23597
diff changeset
457 if _needsshellquote is None:
31491
492c64afc54c py3: make the regular expression bytes to prevent TypeError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30697
diff changeset
458 _needsshellquote = re.compile(br'[^a-zA-Z0-9._/+-]').search
24108
d65ecb814fc0 shellquote: fix missing quotes for empty string
Yuya Nishihara <yuya@tcha.org>
parents: 23683
diff changeset
459 if s and not _needsshellquote(s):
23683
5edb387158a1 posix: quote the specified string only when it may have to be quoted
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23597
diff changeset
460 # "s" shouldn't have to be quoted
5edb387158a1 posix: quote the specified string only when it may have to be quoted
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23597
diff changeset
461 return s
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
462 else:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
463 return "'%s'" % s.replace("'", "'\\''")
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
464
36415
0cb09c322647 util: factor out shellsplit() function
Yuya Nishihara <yuya@tcha.org>
parents: 36303
diff changeset
465 def shellsplit(s):
0cb09c322647 util: factor out shellsplit() function
Yuya Nishihara <yuya@tcha.org>
parents: 36303
diff changeset
466 """Parse a command string in POSIX shell way (best-effort)"""
0cb09c322647 util: factor out shellsplit() function
Yuya Nishihara <yuya@tcha.org>
parents: 36303
diff changeset
467 return pycompat.shlexsplit(s, posix=True)
0cb09c322647 util: factor out shellsplit() function
Yuya Nishihara <yuya@tcha.org>
parents: 36303
diff changeset
468
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
469 def quotecommand(cmd):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
470 return cmd
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
471
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
472 def testpid(pid):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
473 '''return False if pid dead, True if running or not sure'''
30642
e995f00a9e9a py3: replace sys.platform with pycompat.sysplatform (part 2 of 2)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30641
diff changeset
474 if pycompat.sysplatform == 'OpenVMS':
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
475 return True
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
476 try:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
477 os.kill(pid, 0)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
478 return True
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25658
diff changeset
479 except OSError as inst:
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
480 return inst.errno != errno.ESRCH
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
481
8657
3fa92c618624 posix: do not use fstat in isowner
Martin Geisler <mg@lazybytes.net>
parents: 8614
diff changeset
482 def isowner(st):
3fa92c618624 posix: do not use fstat in isowner
Martin Geisler <mg@lazybytes.net>
parents: 8614
diff changeset
483 """Return True if the stat object st is from the current user."""
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
484 return st.st_uid == os.getuid()
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
485
14271
4030630fb59c rename util.find_exe to findexe
Adrian Buehlmann <adrian@cadifra.com>
parents: 14237
diff changeset
486 def findexe(command):
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
487 '''Find executable for command searching like which does.
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
488 If command is a basename then PATH is searched for command.
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
489 PATH isn't searched if command is an absolute or relative path.
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
490 If command isn't found None is returned.'''
30641
16b5df5792a8 py3: replace sys.platform with pycompat.sysplatform (part 1 of 2)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30634
diff changeset
491 if pycompat.sysplatform == 'OpenVMS':
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
492 return command
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
493
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
494 def findexisting(executable):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
495 'Will return executable if existing file'
15499
58f96703a9ab posix: fix findexe() to check for file type and access
Marc-Antoine Ruel <maruel@google.com>
parents: 15488
diff changeset
496 if os.path.isfile(executable) and os.access(executable, os.X_OK):
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
497 return executable
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
498 return None
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
499
30614
cfe66dcf45c0 py3: replace os.sep with pycompat.ossep (part 2 of 4)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30612
diff changeset
500 if pycompat.ossep in command:
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
501 return findexisting(command)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
502
30641
16b5df5792a8 py3: replace sys.platform with pycompat.sysplatform (part 1 of 2)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30634
diff changeset
503 if pycompat.sysplatform == 'plan9':
16383
f5dd179bfa4a plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents: 16241
diff changeset
504 return findexisting(os.path.join('/bin', command))
f5dd179bfa4a plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents: 16241
diff changeset
505
30634
ad15646dc61c py3: replace os.environ with encoding.environ (part 1 of 5)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30614
diff changeset
506 for path in encoding.environ.get('PATH', '').split(pycompat.ospathsep):
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
507 executable = findexisting(os.path.join(path, command))
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
508 if executable is not None:
15499
58f96703a9ab posix: fix findexe() to check for file type and access
Marc-Antoine Ruel <maruel@google.com>
parents: 15488
diff changeset
509 return executable
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
510 return None
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
511
14237
4d684d8210a1 rename util.set_signal_handler to setsignalhandler
Adrian Buehlmann <adrian@cadifra.com>
parents: 14235
diff changeset
512 def setsignalhandler():
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
513 pass
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
514
32291
bd872f64a8ba cleanup: use set literals
Martin von Zweigbergk <martinvonz@google.com>
parents: 32129
diff changeset
515 _wantedkinds = {stat.S_IFREG, stat.S_IFLNK}
18017
74912fe3d718 dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents: 17560
diff changeset
516
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
517 def statfiles(files):
18017
74912fe3d718 dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents: 17560
diff changeset
518 '''Stat each file in files. Yield each stat, or None if a file does not
74912fe3d718 dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents: 17560
diff changeset
519 exist or has a type we don't care about.'''
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
520 lstat = os.lstat
18017
74912fe3d718 dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents: 17560
diff changeset
521 getkind = stat.S_IFMT
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
522 for nf in files:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
523 try:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
524 st = lstat(nf)
18017
74912fe3d718 dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents: 17560
diff changeset
525 if getkind(st.st_mode) not in _wantedkinds:
74912fe3d718 dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents: 17560
diff changeset
526 st = None
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25658
diff changeset
527 except OSError as err:
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
528 if err.errno not in (errno.ENOENT, errno.ENOTDIR):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
529 raise
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
530 st = None
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
531 yield st
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
532
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
533 def getuser():
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
534 '''return name of current user'''
32129
f0ea0f642627 py3: make posix.getuser return a bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents: 31539
diff changeset
535 return pycompat.fsencode(getpass.getuser())
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
536
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
537 def username(uid=None):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
538 """Return the name of the user with the given uid.
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
539
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
540 If uid is None, return the name of the current user."""
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
541
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
542 if uid is None:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
543 uid = os.getuid()
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
544 try:
38252
a4aa77b84efd py3: make sure util.username() always returns bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents: 38166
diff changeset
545 return pycompat.fsencode(pwd.getpwuid(uid)[0])
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
546 except KeyError:
38252
a4aa77b84efd py3: make sure util.username() always returns bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents: 38166
diff changeset
547 return b'%d' % uid
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
548
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
549 def groupname(gid=None):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
550 """Return the name of the group with the given gid.
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
551
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
552 If gid is None, return the name of the current group."""
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
553
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
554 if gid is None:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
555 gid = os.getgid()
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
556 try:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
557 return grp.getgrgid(gid)[0]
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
558 except KeyError:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
559 return str(gid)
10237
2f7a38f336f4 serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents: 10218
diff changeset
560
11138
99eee847beaa acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents: 11011
diff changeset
561 def groupmembers(name):
99eee847beaa acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents: 11011
diff changeset
562 """Return the list of members of the group with the given
99eee847beaa acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents: 11011
diff changeset
563 name, KeyError if the group does not exist.
99eee847beaa acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents: 11011
diff changeset
564 """
99eee847beaa acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents: 11011
diff changeset
565 return list(grp.getgrnam(name).gr_mem)
99eee847beaa acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents: 11011
diff changeset
566
10237
2f7a38f336f4 serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents: 10218
diff changeset
567 def spawndetached(args):
2f7a38f336f4 serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents: 10218
diff changeset
568 return os.spawnvp(os.P_NOWAIT | getattr(os, 'P_DETACH', 0),
2f7a38f336f4 serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents: 10218
diff changeset
569 args[0], args)
2f7a38f336f4 serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents: 10218
diff changeset
570
10239
8e4be44a676f Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
571 def gethgcmd():
8e4be44a676f Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
572 return sys.argv[:1]
11010
18e81d42ee5c util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10264
diff changeset
573
14908
e2b5605501df util: move "default" makedir to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14273
diff changeset
574 def makedir(path, notindexed):
e2b5605501df util: move "default" makedir to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14273
diff changeset
575 os.mkdir(path)
14909
c627fe32c923 util: move "default" unlinkpath to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14908
diff changeset
576
14910
570ea0259b0a util: move "default" lookupreg to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14909
diff changeset
577 def lookupreg(key, name=None, scope=None):
570ea0259b0a util: move "default" lookupreg to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14909
diff changeset
578 return None
14911
5b39503157fd util: move "default" hidewindow to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14910
diff changeset
579
5b39503157fd util: move "default" hidewindow to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14910
diff changeset
580 def hidewindow():
5b39503157fd util: move "default" hidewindow to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14910
diff changeset
581 """Hide current shell window.
5b39503157fd util: move "default" hidewindow to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14910
diff changeset
582
5b39503157fd util: move "default" hidewindow to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14910
diff changeset
583 Used to hide the window opened when starting asynchronous
5b39503157fd util: move "default" hidewindow to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14910
diff changeset
584 child process under Windows, unneeded on other systems.
5b39503157fd util: move "default" hidewindow to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14910
diff changeset
585 """
5b39503157fd util: move "default" hidewindow to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14910
diff changeset
586 pass
14926
4e7e63fc685a util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents: 14911
diff changeset
587
14927
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
588 class cachestat(object):
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
589 def __init__(self, path):
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
590 self.stat = os.stat(path)
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
591
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
592 def cacheable(self):
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
593 return bool(self.stat.st_ino)
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
594
15791
a814f8fcc65a Use explicit integer division
Martin Geisler <mg@aragost.com>
parents: 15711
diff changeset
595 __hash__ = object.__hash__
a814f8fcc65a Use explicit integer division
Martin Geisler <mg@aragost.com>
parents: 15711
diff changeset
596
14927
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
597 def __eq__(self, other):
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
598 try:
18442
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
599 # Only dev, ino, size, mtime and atime are likely to change. Out
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
600 # of these, we shouldn't compare atime but should compare the
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
601 # rest. However, one of the other fields changing indicates
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
602 # something fishy going on, so return False if anything but atime
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
603 # changes.
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
604 return (self.stat.st_mode == other.stat.st_mode and
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
605 self.stat.st_ino == other.stat.st_ino and
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
606 self.stat.st_dev == other.stat.st_dev and
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
607 self.stat.st_nlink == other.stat.st_nlink and
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
608 self.stat.st_uid == other.stat.st_uid and
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
609 self.stat.st_gid == other.stat.st_gid and
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
610 self.stat.st_size == other.stat.st_size and
36781
ffa3026d4196 cleanup: use stat_result[stat.ST_MTIME] instead of stat_result.st_mtime
Augie Fackler <augie@google.com>
parents: 36415
diff changeset
611 self.stat[stat.ST_MTIME] == other.stat[stat.ST_MTIME] and
ffa3026d4196 cleanup: use stat_result[stat.ST_MTIME] instead of stat_result.st_mtime
Augie Fackler <augie@google.com>
parents: 36415
diff changeset
612 self.stat[stat.ST_CTIME] == other.stat[stat.ST_CTIME])
14927
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
613 except AttributeError:
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
614 return False
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
615
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
616 def __ne__(self, other):
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
617 return not self == other
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
618
18868
cafa447a7d3b util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents: 18501
diff changeset
619 def statislink(st):
cafa447a7d3b util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents: 18501
diff changeset
620 '''check whether a stat result is a symlink'''
cafa447a7d3b util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents: 18501
diff changeset
621 return st and stat.S_ISLNK(st.st_mode)
cafa447a7d3b util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents: 18501
diff changeset
622
cafa447a7d3b util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents: 18501
diff changeset
623 def statisexec(st):
cafa447a7d3b util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents: 18501
diff changeset
624 '''check whether a stat result is an executable file'''
25658
e93036747902 global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25420
diff changeset
625 return st and (st.st_mode & 0o100 != 0)
22245
234e4c24b980 platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20202
diff changeset
626
25420
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
627 def poll(fds):
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
628 """block until something happens on any file descriptor
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
629
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
630 This is a generic helper that will check for any activity
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
631 (read, write. exception) and return the list of touched files.
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
632
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
633 In unsupported cases, it will raise a NotImplementedError"""
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
634 try:
30654
5f33116cd787 posix: make poll() restart on interruption by signal (issue5452)
Yuya Nishihara <yuya@tcha.org>
parents: 29530
diff changeset
635 while True:
5f33116cd787 posix: make poll() restart on interruption by signal (issue5452)
Yuya Nishihara <yuya@tcha.org>
parents: 29530
diff changeset
636 try:
5f33116cd787 posix: make poll() restart on interruption by signal (issue5452)
Yuya Nishihara <yuya@tcha.org>
parents: 29530
diff changeset
637 res = select.select(fds, fds, fds)
5f33116cd787 posix: make poll() restart on interruption by signal (issue5452)
Yuya Nishihara <yuya@tcha.org>
parents: 29530
diff changeset
638 break
5f33116cd787 posix: make poll() restart on interruption by signal (issue5452)
Yuya Nishihara <yuya@tcha.org>
parents: 29530
diff changeset
639 except select.error as inst:
5f33116cd787 posix: make poll() restart on interruption by signal (issue5452)
Yuya Nishihara <yuya@tcha.org>
parents: 29530
diff changeset
640 if inst.args[0] == errno.EINTR:
5f33116cd787 posix: make poll() restart on interruption by signal (issue5452)
Yuya Nishihara <yuya@tcha.org>
parents: 29530
diff changeset
641 continue
5f33116cd787 posix: make poll() restart on interruption by signal (issue5452)
Yuya Nishihara <yuya@tcha.org>
parents: 29530
diff changeset
642 raise
25420
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
643 except ValueError: # out of range file descriptor
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
644 raise NotImplementedError()
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
645 return sorted(list(set(sum(res, []))))
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
646
22245
234e4c24b980 platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20202
diff changeset
647 def readpipe(pipe):
234e4c24b980 platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20202
diff changeset
648 """Read all available data from a pipe."""
22246
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
649 # We can't fstat() a pipe because Linux will always report 0.
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
650 # So, we set the pipe to non-blocking mode and read everything
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
651 # that's available.
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
652 flags = fcntl.fcntl(pipe, fcntl.F_GETFL)
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
653 flags |= os.O_NONBLOCK
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
654 oldflags = fcntl.fcntl(pipe, fcntl.F_SETFL, flags)
22245
234e4c24b980 platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20202
diff changeset
655
22246
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
656 try:
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
657 chunks = []
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
658 while True:
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
659 try:
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
660 s = pipe.read()
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
661 if not s:
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
662 break
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
663 chunks.append(s)
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
664 except IOError:
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
665 break
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
666
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
667 return ''.join(chunks)
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
668 finally:
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
669 fcntl.fcntl(pipe, fcntl.F_SETFL, oldflags)
29530
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
670
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
671 def bindunixsocket(sock, path):
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
672 """Bind the UNIX domain socket to the specified path"""
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
673 # use relative path instead of full path at bind() if possible, since
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
674 # AF_UNIX path has very small length limit (107 chars) on common
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
675 # platforms (see sys/un.h)
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
676 dirname, basename = os.path.split(path)
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
677 bakwdfd = None
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
678 if dirname:
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
679 bakwdfd = os.open('.', os.O_DIRECTORY)
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
680 os.chdir(dirname)
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
681 sock.bind(basename)
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
682 if bakwdfd:
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
683 os.fchdir(bakwdfd)
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
684 os.close(bakwdfd)