This file is indexed.

/usr/lib/python2.7/dist-packages/zake/fake_storage.py is in python-zake 0.1.6-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# -*- coding: utf-8 -*-

# vim: tabstop=4 shiftwidth=4 softtabstop=4

#    Copyright (C) 2013 Yahoo! Inc. All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import contextlib
import itertools
import os
import sys

import six

from zake import utils

from kazoo import exceptions as k_exceptions
from kazoo.protocol import states as k_states


# See: https://issues.apache.org/jira/browse/ZOOKEEPER-243
SEQ_ROLLOVER = 2147483647
SEQ_ROLLOVER_TO = -2147483647
ROOT_PATH = '/'


class FakeStorage(object):
    """A place too place fake zookeeper paths + data + connected clients."""

    def __init__(self, handler, paths=None, sequences=None):
        if paths:
            self._paths = dict(paths)
        else:
            self._paths = {}
        if sequences:
            self._sequences = dict(sequences)
        else:
            self._sequences = {}
        self._lock = handler.rlock_object()
        # Ensure the root path *always* exists.
        if ROOT_PATH not in self._paths:
            self._paths[ROOT_PATH] = {
                'created_on': 0,
                'updated_on': 0,
                'version': 0,
                # Not supported for now...
                'aversion': -1,
                'cversion': -1,
                'data': b"",
                'ephemeral': False,
            }
        self._clients = set()
        self._client_lock = handler.rlock_object()

    @property
    def lock(self):
        return self._lock

    def attach(self, client):
        with self._client_lock:
            if client not in self._clients:
                self._clients.add(client)

    def _make_znode(self, node, child_count):
        # Not exactly right, but good enough...
        translated = {
            'czxid': node['version'],
            'mzxid': node['version'],
            'pzxid': node['version'],
            'ctime': node['created_on'],
            'mtime': node['updated_on'],
            'version': node['version'],
            'aversion': node['aversion'],
            'cversion': node['cversion'],
            'dataLength': len(node['data']),
            'numChildren': int(child_count),
        }
        if node['ephemeral']:
            translated['ephemeralOwner'] = node['ephemeral_owner']
        else:
            translated['ephemeralOwner'] = 0
        return k_states.ZnodeStat(**translated)

    @property
    def paths(self):
        return self._paths

    @property
    def sequences(self):
        return self._sequences

    @property
    def clients(self):
        return self._clients

    def __getitem__(self, path):
        return self._paths[path]

    def __setitem__(self, path, value):
        with self.lock:
            self._paths[path] = value

    def set(self, path, value, version=-1):
        with self.lock:
            if version != -1:
                stat = self.get(path)[1]
                if stat.version != version:
                    raise k_exceptions.BadVersionError("Version mismatch %s "
                                                       "!= %s" % (stat.version,
                                                                  version))
                self._paths[path]['data'] = value
                self._paths[path]['updated_on'] = utils.millitime()
                self._paths[path]['version'] += 1
            else:
                self._paths[path]['data'] = value
                self._paths[path]['updated_on'] = utils.millitime()
                self._paths[path]['version'] += 1
            return self.get(path)[1]

    def purge(self, client):
        if not client.session_id:
            return 0
        with self._client_lock:
            if client in self._clients:
                self._clients.discard(client)
            else:
                return 0
        removals = []
        with self.lock:
            for path, data in six.iteritems(self._paths):
                if data['ephemeral'] \
                   and data['ephemeral_owner'] == client.session_id:
                    removals.append(path)
            data_watches = []
            for path in removals:
                event = k_states.WatchedEvent(
                    type=k_states.EventType.DELETED,
                    state=k_states.KeeperState.CONNECTED,
                    path=path)
                data_watches.append(([path], event))
            fire_paths = []
            for path in removals:
                parents = sorted(six.iterkeys(self.get_parents(path)))
                for parent_path in parents:
                    if parent_path in fire_paths:
                        continue
                    fire_paths.append(parent_path)
            child_watches = []
            for path in fire_paths:
                event = k_states.WatchedEvent(
                    type=k_states.EventType.DELETED,
                    state=k_states.KeeperState.CONNECTED,
                    path=path)
                child_watches.append(([path], event))
            for path in removals:
                del self._paths[path]
        self.inform(client, child_watches, data_watches, inform_self=False)
        return len(removals)

    def inform(self, client, child_watches, data_watches, inform_self=True):
        with self._client_lock:
            clients = self._clients.copy()
        for other_client in clients:
            if not inform_self and other_client is client:
                continue
            other_client.fire_child_watches(child_watches)
            other_client.fire_data_watches(data_watches)

    def create(self, path, value=b"", sequence=False,
               ephemeral=False, session_id=None):

        def sequence_iter(path, parent_path):
            for i in itertools.count(0):
                sequence_id = self._sequences.get(parent_path, 0)
                if sequence_id == SEQ_ROLLOVER:
                    self._sequences[parent_path] = SEQ_ROLLOVER_TO
                else:
                    self._sequences[parent_path] = sequence_id + 1
                yield path + '%010d' % (sequence_id)

        parent_path, _node_name = os.path.split(path)
        with self.lock:
            if sequence:
                for possible_path in sequence_iter(path, parent_path):
                    if possible_path not in self:
                        path = possible_path
                        break
            parents = sorted(six.iterkeys(self.get_parents(path)))
            if parent_path not in self:
                if sequence:
                    self._sequences.pop(parent_path, None)
                raise k_exceptions.NoNodeError("Parent node %s does not exist"
                                               % (parent_path))
            if ephemeral and not session_id:
                raise k_exceptions.SystemZookeeperError("Ephemeral node %s can"
                                                        " not be created"
                                                        " without a session"
                                                        " id" % path)
            if path in self:
                raise k_exceptions.NodeExistsError("Node %s already"
                                                   " exists" % (path))
            for parent_path in reversed(parents):
                if self._paths[parent_path]['ephemeral']:
                    raise k_exceptions.NoChildrenForEphemeralsError(
                        "Parent %s is ephemeral" % parent_path)
            path_data = {
                # Kazoo clients expect in milliseconds
                'created_on': utils.millitime(),
                'updated_on': utils.millitime(),
                'version': 0,
                # Not supported for now...
                'aversion': -1,
                'cversion': -1,
                'data': value,
            }
            if ephemeral:
                path_data['ephemeral_owner'] = session_id
                path_data['ephemeral'] = True
            else:
                path_data['ephemeral'] = False
            self._paths[path] = path_data
            return (True, parents, path)

    def pop(self, path):
        if path == ROOT_PATH:
            raise k_exceptions.BadArgumentsError("Can not delete %s"
                                                 % ROOT_PATH)
        with self.lock:
            self._paths.pop(path)

    def get(self, path):
        with self.lock:
            node = self._paths[path]
            children_count = len(self.get_children(path))
            return (node['data'], self._make_znode(node, children_count))

    def __contains__(self, path):
        return path in self._paths

    @contextlib.contextmanager
    def transaction(self):
        with self.lock:
            # Keep the before the transaction information and reset to that
            # data if the context manager fails (this makes it appear that the
            # operations done during the transaction either complete as a
            # group or do not complete).
            paths = self._paths.copy()
            sequences = self._sequences.copy()
            try:
                yield
            except Exception:
                cause = sys.exc_info()
                try:
                    self._paths = paths
                    self._sequences = sequences
                finally:
                    six.reraise(*cause)

    def get_children(self, path, only_direct=True):
        paths = {}
        with self.lock:
            for (other_path, data) in list(six.iteritems(self._paths)):
                if utils.is_child_path(path, other_path,
                                       only_direct=only_direct):
                    paths[other_path] = data
        return paths

    def get_parents(self, path):
        paths = {}
        with self.lock:
            for (other_path, data) in list(six.iteritems(self._paths)):
                if utils.is_child_path(other_path, path, only_direct=False):
                    paths[other_path] = data
        return paths