[PATCH 1 of 5] util: add filestat class to detect ambiguity of file stat

Yuya Nishihara yuya at tcha.org
Tue May 24 08:58:11 EDT 2016


On Thu, 19 May 2016 00:26:27 +0900, FUJIWARA Katsunori wrote:
> # HG changeset patch
> # User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
> # Date 1463584837 -32400
> #      Thu May 19 00:20:37 2016 +0900
> # Node ID dc731ebd60613cf3f08799a8d8dc48435798665b
> # Parent  8c8442523eefac2d53e3f10ff1ebf37f4d3c63c3
> util: add filestat class to detect ambiguity of file stat

> +class filestat(object):
> +    """help to exactly detect change of a file
> +
> +    'stat' attribute is result of 'os.stat()' if specified 'path'
> +    exists. Otherwise, it is None. This can avoid preparative
> +    'exists()' examination on client side of this class.
> +    """
> +    def __init__(self, path):
> +        try:
> +            self.stat = os.stat(path)
> +        except OSError as err:
> +            if err.errno != errno.ENOENT:
> +                raise
> +            self.stat = None
> +
> +    __hash__ = object.__hash__
> +
> +    def __eq__(self, old):
> +        try:
> +            # if ambiguity between stat of new and old file is
> +            # avoided, comparision of size, ctime and mtime is enough
> +            # to exactly detect change of a file regardless of platform
> +            return (self.stat.st_size == old.stat.st_size and
> +                    self.stat.st_ctime == old.stat.st_ctime and
> +                    self.stat.st_mtime == old.stat.st_mtime)
> +        except AttributeError:
> +            return False

You have to implement __ne__ to avoid troubles. I have no idea about __hash__.

https://docs.python.org/2/reference/datamodel.html#object.__eq__

> +    def isambig(self, old):
> +        """Examine whether new (= self) stat is ambiguous against old one
> +
> +        "S[N]" below means stat of a file at N-th change:
> +
> +        - S[n-1].ctime  < S[n].ctime: can detect change of a file
> +        - S[n-1].ctime == S[n].ctime
> +          - S[n-1].ctime  < S[n].mtime: means natural advancing (*1)
> +          - S[n-1].ctime == S[n].mtime: is ambiguous (*2)
> +          - S[n-1].ctime  > S[n].mtime: never occurs naturally (don't care)
> +        - S[n-1].ctime  > S[n].ctime: never occurs naturally (don't care)

Does it work well on Windows? I'm not skeptical about it, I just question
because ctime is platform dependent.


More information about the Mercurial-devel mailing list