Debian Codegraph @

hgext/fsmonitor/state.py

raw
1 # state.py - fsmonitor persistent state
2 #
3 # Copyright 2013-2016 Facebook, Inc.
4 #
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
7
8 from __future__ import annotations
9
10 import errno
11 import os
12 import socket
13 import struct
14
15 from mercurial.i18n import _
16 from mercurial import (
17 encoding,
18 pathutil,
19 util,
20 )
21
22 _version = 4
23 _versionformat = b">I"
24
25
26 class state:
27 def __init__(self, repo):
28 self._vfs = repo.vfs
29 self._ui = repo.ui
30 self._rootdir = pathutil.normasprefix(repo.root)
31 self._lastclock = None
32 self._identity = util.filestat(None)
33
34 self.mode = self._ui.config(b'fsmonitor', b'mode')
35 self.walk_on_invalidate = self._ui.configbool(
36 b'fsmonitor', b'walk_on_invalidate'
37 )
38 self.timeout = float(self._ui.config(b'fsmonitor', b'timeout'))
39
40 def get(self):
41 try:
42 file = self._vfs(b'fsmonitor.state', b'rb')
43 except OSError as inst:
44 self._identity = util.filestat(None)
45 if inst.errno != errno.ENOENT:
46 raise
47 return None, None, None
48
49 self._identity = util.filestat.fromfp(file)
50
51 versionbytes = file.read(4)
52 if len(versionbytes) < 4:
53 self._ui.log(
54 b'fsmonitor',
55 b'fsmonitor: state file only has %d bytes, '
56 b'nuking state\n' % len(versionbytes),
57 )
58 self.invalidate()
59 return None, None, None
60 try:
61 diskversion = struct.unpack(_versionformat, versionbytes)[0]
62 if diskversion != _version:
63 # different version, nuke state and start over
64 self._ui.log(
65 b'fsmonitor',
66 b'fsmonitor: version switch from %d to '
67 b'%d, nuking state\n' % (diskversion, _version),
68 )
69 self.invalidate()
70 return None, None, None
71
72 state = file.read().split(b'\0')
73 # state = hostname\0clock\0ignorehash\0 + list of files, each
74 # followed by a \0
75 if len(state) < 3:
76 self._ui.log(
77 b'fsmonitor',
78 b'fsmonitor: state file truncated (expected '
79 b'3 chunks, found %d), nuking state\n',
80 len(state),
81 )
82 self.invalidate()
83 return None, None, None
84 diskhostname = state[0]
85 hostname = encoding.strtolocal(socket.gethostname())
86 if diskhostname != hostname:
87 # file got moved to a different host
88 self._ui.log(
89 b'fsmonitor',
90 b'fsmonitor: stored hostname "%s" '
91 b'different from current "%s", nuking state\n'
92 % (diskhostname, hostname),
93 )
94 self.invalidate()
95 return None, None, None
96
97 clock = state[1]
98 ignorehash = state[2]
99 # discard the value after the last \0
100 notefiles = state[3:-1]
101
102 finally:
103 file.close()
104
105 return clock, ignorehash, notefiles
106
107 def set(self, clock, ignorehash, notefiles):
108 if clock is None:
109 self.invalidate()
110 return
111
112 # Read the identity from the file on disk rather than from the open file
113 # pointer below, because the latter is actually a brand new file.
114 identity = util.filestat.frompath(self._vfs.join(b'fsmonitor.state'))
115 if identity != self._identity:
116 self._ui.debug(
117 b'skip updating fsmonitor.state: identity mismatch\n'
118 )
119 return
120
121 try:
122 file = self._vfs(
123 b'fsmonitor.state', b'wb', atomictemp=True, checkambig=True
124 )
125 except OSError:
126 self._ui.warn(_(b"warning: unable to write out fsmonitor state\n"))
127 return
128
129 with file:
130 file.write(struct.pack(_versionformat, _version))
131 file.write(encoding.strtolocal(socket.gethostname()) + b'\0')
132 file.write(clock + b'\0')
133 file.write(ignorehash + b'\0')
134 if notefiles:
135 file.write(b'\0'.join(notefiles))
136 file.write(b'\0')
137
138 def invalidate(self):
139 try:
140 os.unlink(os.path.join(self._rootdir, b'.hg', b'fsmonitor.state'))
141 except FileNotFoundError:
142 pass
143 self._identity = util.filestat(None)
144
145 def setlastclock(self, clock):
146 self._lastclock = clock
147
148 def getlastclock(self):
149 return self._lastclock