[PATCH 2 of 2 STABLE] store: add auto detection for hardlink count blindness (issue1866)
Matt Mackall
mpm at selenic.com
Mon Nov 1 03:18:27 CDT 2010
On Sun, 2010-10-31 at 20:26 +0100, Adrian Buehlmann wrote:
> > - Test function is too heavy-weight - do the simplest fastest test
> > possible. Probably something like:
> >
> > f, fn = getasecurefilehandleandname(path)
> > fn2 = fn + "l"
> > link(fn, fn2)
> > stat
> >
> > Even this is pretty expensive.
>
> Near as I can tell, there's no test in my code that is not needed.
>
> But I guess if even the above is too heavyweight, there's not much point
> in explaining in detail every case I covered.
Here's a quick test I made. It catches the Linux bug and should work on
Windows shares too.
def checknlink(testfile):
rm = None
try:
for x in xrange(10):
try:
f = "%s.hgtmp%s" % (testfile, x)
os.link(testfile, f)
rm = f
except OSError, inst:
if inst == errno.EEXISTS:
continue
elif inst == errno.EPERM:
return false
raise
break
else:
return False
try:
fd = open(f, "r", 0)
s = os.stat(f)
if s.st_nlink > 1:
return True
finally:
fd.close()
finally:
if rm:
os.unlink(rm)
return False
Typically, this will do the bare minimum of syscalls: link, open, stat,
close, and unlink. Note how it uses an existing file (which we should
always have when doing this test) to avoid the tempfile issues (and the
poking around at /dev/urandom that tempfile.mkstemp wants to do).
--
Mathematics is the supreme nostalgia of our time.
More information about the Mercurial-devel
mailing list