posix: give checklink a fast path that cache the check file and is read only
util.checklink would create a symlink and remove it again. That would sometimes
happen multiple times. Write operations are relatively expensive and give disk
tear and noise for applications monitoring file system activity.
Instead of creating a symlink and deleting it again, just create it once and
leave it in .hg/cache/check-link . If the file exists, just verify that
os.islink reports true. We will assume that this check is as good as symlink
creation not failing.
Note: The symlink left in .hg/cache has to resolve to a file - otherwise 'make
dist' will fail ...
test-symlink-os-yes-fs-no.py does some monkey patching to simulate a platform
without symlink support. The slightly different testing method requires
additional monkeying.
--- a/mercurial/posix.py Thu Nov 17 12:59:36 2016 +0100
+++ b/mercurial/posix.py Wed Jan 14 01:15:26 2015 +0100
@@ -220,6 +220,10 @@
# file already exists
while True:
cachedir = os.path.join(path, '.hg', 'cache')
+ checklink = os.path.join(cachedir, 'checklink')
+ # try fast path, read only
+ if os.path.islink(checklink):
+ return True
if os.path.isdir(cachedir):
checkdir = cachedir
else:
@@ -231,7 +235,13 @@
prefix='hg-checklink-')
try:
os.symlink(os.path.basename(fd.name), name)
- os.unlink(name)
+ if cachedir is None:
+ os.unlink(name)
+ else:
+ try:
+ os.rename(name, checklink)
+ except OSError:
+ os.unlink(name)
return True
except OSError as inst:
# link creation might race, try again
--- a/tests/test-clone.t Thu Nov 17 12:59:36 2016 +0100
+++ b/tests/test-clone.t Wed Jan 14 01:15:26 2015 +0100
@@ -32,6 +32,7 @@
$ ls .hg/cache
branch2-served
checkisexec
+ checklink
checknoexec
rbc-names-v1
rbc-revs-v1
@@ -48,6 +49,7 @@
$ ls .hg/cache
branch2-served
checkisexec
+ checklink
$ cat a
a
--- a/tests/test-symlink-os-yes-fs-no.py Thu Nov 17 12:59:36 2016 +0100
+++ b/tests/test-symlink-os-yes-fs-no.py Wed Jan 14 01:15:26 2015 +0100
@@ -35,6 +35,9 @@
def symlink_failure(src, dst):
raise OSError(1, "Operation not permitted")
os.symlink = symlink_failure
+def islink_failure(path):
+ return False
+os.path.islink = islink_failure
# dereference links as if a Samba server has exported this to a
# Windows client
--- a/tests/test-tags.t Thu Nov 17 12:59:36 2016 +0100
+++ b/tests/test-tags.t Wed Jan 14 01:15:26 2015 +0100
@@ -673,6 +673,7 @@
$ ls tagsclient/.hg/cache
branch2-served
checkisexec
+ checklink
hgtagsfnodes1
rbc-names-v1
rbc-revs-v1
@@ -698,6 +699,7 @@
$ ls tagsclient/.hg/cache
branch2-served
checkisexec
+ checklink
hgtagsfnodes1
rbc-names-v1
rbc-revs-v1