hgext/fsmonitor/state.py
changeset 43076 2372284d9457
parent 34463 718f7acd6d5e
child 43077 687b865b95ad
equal deleted inserted replaced
43075:57875cf423c9 43076:2372284d9457
    19 )
    19 )
    20 
    20 
    21 _version = 4
    21 _version = 4
    22 _versionformat = ">I"
    22 _versionformat = ">I"
    23 
    23 
       
    24 
    24 class state(object):
    25 class state(object):
    25     def __init__(self, repo):
    26     def __init__(self, repo):
    26         self._vfs = repo.vfs
    27         self._vfs = repo.vfs
    27         self._ui = repo.ui
    28         self._ui = repo.ui
    28         self._rootdir = pathutil.normasprefix(repo.root)
    29         self._rootdir = pathutil.normasprefix(repo.root)
    29         self._lastclock = None
    30         self._lastclock = None
    30         self._identity = util.filestat(None)
    31         self._identity = util.filestat(None)
    31 
    32 
    32         self.mode = self._ui.config('fsmonitor', 'mode')
    33         self.mode = self._ui.config('fsmonitor', 'mode')
    33         self.walk_on_invalidate = self._ui.configbool(
    34         self.walk_on_invalidate = self._ui.configbool(
    34             'fsmonitor', 'walk_on_invalidate')
    35             'fsmonitor', 'walk_on_invalidate'
       
    36         )
    35         self.timeout = float(self._ui.config('fsmonitor', 'timeout'))
    37         self.timeout = float(self._ui.config('fsmonitor', 'timeout'))
    36 
    38 
    37     def get(self):
    39     def get(self):
    38         try:
    40         try:
    39             file = self._vfs('fsmonitor.state', 'rb')
    41             file = self._vfs('fsmonitor.state', 'rb')
    46         self._identity = util.filestat.fromfp(file)
    48         self._identity = util.filestat.fromfp(file)
    47 
    49 
    48         versionbytes = file.read(4)
    50         versionbytes = file.read(4)
    49         if len(versionbytes) < 4:
    51         if len(versionbytes) < 4:
    50             self._ui.log(
    52             self._ui.log(
    51                 'fsmonitor', 'fsmonitor: state file only has %d bytes, '
    53                 'fsmonitor',
    52                 'nuking state\n' % len(versionbytes))
    54                 'fsmonitor: state file only has %d bytes, '
       
    55                 'nuking state\n' % len(versionbytes),
       
    56             )
    53             self.invalidate()
    57             self.invalidate()
    54             return None, None, None
    58             return None, None, None
    55         try:
    59         try:
    56             diskversion = struct.unpack(_versionformat, versionbytes)[0]
    60             diskversion = struct.unpack(_versionformat, versionbytes)[0]
    57             if diskversion != _version:
    61             if diskversion != _version:
    58                 # different version, nuke state and start over
    62                 # different version, nuke state and start over
    59                 self._ui.log(
    63                 self._ui.log(
    60                     'fsmonitor', 'fsmonitor: version switch from %d to '
    64                     'fsmonitor',
    61                     '%d, nuking state\n' % (diskversion, _version))
    65                     'fsmonitor: version switch from %d to '
       
    66                     '%d, nuking state\n' % (diskversion, _version),
       
    67                 )
    62                 self.invalidate()
    68                 self.invalidate()
    63                 return None, None, None
    69                 return None, None, None
    64 
    70 
    65             state = file.read().split('\0')
    71             state = file.read().split('\0')
    66             # state = hostname\0clock\0ignorehash\0 + list of files, each
    72             # state = hostname\0clock\0ignorehash\0 + list of files, each
    67             # followed by a \0
    73             # followed by a \0
    68             if len(state) < 3:
    74             if len(state) < 3:
    69                 self._ui.log(
    75                 self._ui.log(
    70                     'fsmonitor', 'fsmonitor: state file truncated (expected '
    76                     'fsmonitor',
    71                     '3 chunks, found %d), nuking state\n', len(state))
    77                     'fsmonitor: state file truncated (expected '
       
    78                     '3 chunks, found %d), nuking state\n',
       
    79                     len(state),
       
    80                 )
    72                 self.invalidate()
    81                 self.invalidate()
    73                 return None, None, None
    82                 return None, None, None
    74             diskhostname = state[0]
    83             diskhostname = state[0]
    75             hostname = socket.gethostname()
    84             hostname = socket.gethostname()
    76             if diskhostname != hostname:
    85             if diskhostname != hostname:
    77                 # file got moved to a different host
    86                 # file got moved to a different host
    78                 self._ui.log('fsmonitor', 'fsmonitor: stored hostname "%s" '
    87                 self._ui.log(
    79                              'different from current "%s", nuking state\n' %
    88                     'fsmonitor',
    80                              (diskhostname, hostname))
    89                     'fsmonitor: stored hostname "%s" '
       
    90                     'different from current "%s", nuking state\n'
       
    91                     % (diskhostname, hostname),
       
    92                 )
    81                 self.invalidate()
    93                 self.invalidate()
    82                 return None, None, None
    94                 return None, None, None
    83 
    95 
    84             clock = state[1]
    96             clock = state[1]
    85             ignorehash = state[2]
    97             ignorehash = state[2]
   102         if identity != self._identity:
   114         if identity != self._identity:
   103             self._ui.debug('skip updating fsmonitor.state: identity mismatch\n')
   115             self._ui.debug('skip updating fsmonitor.state: identity mismatch\n')
   104             return
   116             return
   105 
   117 
   106         try:
   118         try:
   107             file = self._vfs('fsmonitor.state', 'wb', atomictemp=True,
   119             file = self._vfs(
   108                 checkambig=True)
   120                 'fsmonitor.state', 'wb', atomictemp=True, checkambig=True
       
   121             )
   109         except (IOError, OSError):
   122         except (IOError, OSError):
   110             self._ui.warn(_("warning: unable to write out fsmonitor state\n"))
   123             self._ui.warn(_("warning: unable to write out fsmonitor state\n"))
   111             return
   124             return
   112 
   125 
   113         with file:
   126         with file: