This file is indexed.

/usr/lib/python2.7/dist-packages/pygccxml/parser/directory_cache.py is in python-pygccxml 1.8.0-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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
# Copyright 2014-2016 Insight Software Consortium.
# Copyright 2004-2008 Roman Yakovenko.
# Distributed under the Boost Software License, Version 1.0.
# See http://www.boost.org/LICENSE_1_0.txt
#
# The initial version of the directory_cache_t class was written
# by Matthias Baas (baas@ira.uka.de).

"""
directory cache implementation.

This module contains the implementation of a cache that uses individual
files, stored in a dedicated cache directory, to store the cached contents.

The :class:`parser.directory_cache_t` class instance could be passed as the
`cache` argument of the :func:`parser.parse` function.
"""

import os
import os.path
import gzip
import hashlib
try:
    import cPickle as pickle
except ImportError:
    import pickle
from . import declarations_cache


class index_entry_t(object):

    """
    Entry of the index table in the directory cache index.

    Each cached header file (i.e. each .cache file) has a corresponding
    index_entry_t object. This object is used to determine whether the
    cache file with the declarations is still valid or not.

    This class is a helper class for the directory_cache_t class.
    """

    def __init__(self, filesigs, configsig):
        """
        :param filesigs: a list of tuples( `fileid`, `sig`)...
        :param configsig: the signature of the configuration object.
        """

        self.filesigs = filesigs
        self.configsig = configsig

    def __getstate__(self):
        return self.filesigs, self.configsig

    def __setstate__(self, state):
        self.filesigs, self.configsig = state


class directory_cache_t (declarations_cache.cache_base_t):

    """cache class that stores its data as multiple files inside a directory.

    The cache stores one index file called `index.dat` which is always
    read by the cache when the cache object is created. Each header file
    will have its corresponding .cache file that stores the declarations
    found in the header file. The index file is used to determine whether
    a .cache file is still valid or not (by checking if one of the dependent
    files (i.e. the header file itself and all included files) have been
    modified since the last run).
    """

    def __init__(self, dir="cache", compression=False, sha1_sigs=True):
        """
        :param dir: cache directory path, it is created, if it does not exist

        :param compression: if `True`, the cache files will be compressed
                            using `gzip`

        :param sha1_sigs: `sha1_sigs` determines whether file modifications is
                         checked by computing a `sha1` digest or by checking
                         the modification date
        """

        declarations_cache.cache_base_t.__init__(self)

        # Cache directory
        self.__dir = os.path.abspath(dir)

        # Flag that determines whether the cache files will be compressed
        self.__compression = compression

        # Flag that determines whether the signature is a sha1 digest or
        # the modification time
        # (this flag is passed to the filename_repository_t class)
        self.__sha1_sigs = sha1_sigs

        # Filename repository
        self.__filename_rep = filename_repository_t(self.__sha1_sigs)

        # Index dictionary (Key is the value returned by _create_cache_key()
        # (which is based on the header file name) and value is an
        # index_entry_t object)
        self.__index = {}

        # Flag that indicates whether the index was modified
        self.__modified_flag = False

        # Check if dir refers to an existing file...
        if os.path.isfile(self.__dir):
            raise ValueError((
                "Cannot use %s as cache directory. There is already a file " +
                "with that name.") % self.__dir)

        # Load the cache or create the cache directory...
        if os.path.isdir(self.__dir):
            self._load()
        else:
            # Create the cache directory...
            os.mkdir(self.__dir)

    def flush(self):
        """Save the index table to disk."""
        self._save()

    def update(self, source_file, configuration, declarations, included_files):
        """Replace a cache entry by a new value.

        :param source_file: a C++ source file name.
        :type source_file: str
        :param configuration: configuration  object.
        :type configuration: :class:`xml_generator_configuration_t`
        :param declarations: declarations contained in the `source_file`
        :type declarations: pickable object
        :param included_files: included files
        :type included_files: list of str
        """

        # Normlize all paths...
        source_file = os.path.normpath(source_file)
        included_files = [os.path.normpath(p) for p in included_files]

        # Create the list of dependent files. This is the included_files list
        # + the source file. Duplicate names are removed.
        dependent_files = {}
        for name in [source_file] + included_files:
            dependent_files[name] = 1
        dependent_files = list(dependent_files.keys())

        key = self._create_cache_key(source_file)
        # Remove an existing entry (if there is one)
        # After calling this method, it is guaranteed that __index[key]
        # does not exist anymore.
        self._remove_entry(source_file, key)

        # Create a new entry...

        # Create the sigs of all dependent files...
        filesigs = []
        for filename in dependent_files:
            id_, sig = self.__filename_rep.acquire_filename(filename)
            filesigs.append((id_, sig))

        configsig = self._create_config_signature(configuration)
        entry = index_entry_t(filesigs, configsig)
        self.__index[key] = entry
        self.__modified_flag = True

        # Write the declarations into the cache file...
        cachefilename = self._create_cache_filename(source_file)
        self._write_file(cachefilename, declarations)

    def cached_value(self, source_file, configuration):
        """Return the cached declarations or None.

        :param source_file: Header file name
        :type source_file: str
        :param configuration: Configuration object
        :type configuration: :class:`parser.xml_generator_configuration_t`
        :rtype: Cached declarations or None
        """

        # Check if the cache contains an entry for source_file
        key = self._create_cache_key(source_file)
        entry = self.__index.get(key)
        if entry is None:
            # print "CACHE: %s: Not cached"%source_file
            return None

        # Check if the entry is still valid. It is not valid if:
        #  - the source_file has been updated
        #  - the configuration object has changed (i.e. the header is parsed
        #    by gccxml with different settings which may influence the
        #    declarations)
        #  - the included files have been updated
        #    (this list is part of the cache entry as it cannot be known
        #    by the caller when cached_value() is called. It was instead
        #    passed to update())

        # Check if the config is different...
        configsig = self._create_config_signature(configuration)
        if configsig != entry.configsig:
            # print "CACHE: %s: Config mismatch"%source_file
            return None

        # Check if any of the dependent files has been modified...
        for id_, sig in entry.filesigs:
            if self.__filename_rep.is_file_modified(id_, sig):
                # print "CACHE: %s: Entry not up to date"%source_file
                return None

        # Load and return the cached declarations
        cachefilename = self._create_cache_filename(source_file)
        decls = self._read_file(cachefilename)

        # print "CACHE: Using cached decls for",source_file
        return decls

    def _load(self):
        """Load the cache.

        Loads the `index.dat` file, which contains the index table and the
        file name repository.

        This method is called by the :meth:`__init__`
        """

        indexfilename = os.path.join(self.__dir, "index.dat")
        if os.path.exists(indexfilename):
            data = self._read_file(indexfilename)
            self.__index = data[0]
            self.__filename_rep = data[1]
            if self.__filename_rep._sha1_sigs != self.__sha1_sigs:
                print((
                    "CACHE: Warning: sha1_sigs stored in the cache is set " +
                    "to %s.") % self.__filename_rep._sha1_sigs)
                print("Please remove the cache to change this setting.")
                self.__sha1_sigs = self.__filename_rep._sha1_sigs
        else:
            self.__index = {}
            self.__filename_rep = filename_repository_t(self.__sha1_sigs)

        self.__modified_flag = False

    def _save(self):
        """
        save the cache index, in case it was modified.

        Saves the index table and the file name repository in the file
        `index.dat`
        """

        if self.__modified_flag:
            self.__filename_rep.update_id_counter()
            indexfilename = os.path.join(self.__dir, "index.dat")
            self._write_file(
                indexfilename,
                (self.__index,
                 self.__filename_rep))
            self.__modified_flag = False

    def _read_file(self, filename):
        """
        read a Python object from a cache file.

        Reads a pickled object from disk and returns it.

        :param filename: Name of the file that should be read.
        :type filename: str
        :rtype: object
        """

        if self.__compression:
            f = gzip.GzipFile(filename, "rb")
        else:
            f = open(filename, "rb")
        res = pickle.load(f)
        f.close()
        return res

    def _write_file(self, filename, data):
        """Write a data item into a file.

        The data object is written to a file using the pickle mechanism.

        :param filename: Output file name
        :type filename: str
        :param data: A Python object that will be pickled
        """

        if self.__compression:
            f = gzip.GzipFile(filename, "wb")
        else:
            f = open(filename, "wb")
        pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
        f.close()

    def _remove_entry(self, source_file, key):
        """Remove an entry from the cache.

        source_file is the name of the header and key is its corresponding
        cache key (obtained by a call to :meth:_create_cache_key ).
        The entry is removed from the index table, any referenced file
        name is released and the cache file is deleted.

        If key references a non-existing entry, the method returns
        immediately.

        :param source_file: Header file name
        :type source_file: str
        :param key: Key value for the specified header file
        :type key: hash table object
        """

        entry = self.__index.get(key)
        if entry is None:
            return

        # Release the referenced files...
        for id_, sig in entry.filesigs:
            self.__filename_rep.release_filename(id_)

        # Remove the cache entry...
        del self.__index[key]
        self.__modified_flag = True

        # Delete the corresponding cache file...
        cachefilename = self._create_cache_filename(source_file)
        try:
            os.remove(cachefilename)
        except OSError as e:
            print("Could not remove cache file (%s)" % e)

    @staticmethod
    def _create_cache_key(source_file):
        """
        return the cache key for a header file.

        :param source_file: Header file name
        :type source_file: str
        :rtype: str
        """
        path, name = os.path.split(source_file)
        return name + str(hash(path))

    def _create_cache_filename(self, source_file):
        """
        return the cache file name for a header file.

        :param source_file: Header file name
        :type source_file: str
        :rtype: str
        """
        res = self._create_cache_key(source_file) + ".cache"
        return os.path.join(self.__dir, res)

    @staticmethod
    def _create_config_signature(config):
        """
        return the signature for a config object.

        The signature is computed as sha1 digest of the contents of
        working_directory, include_paths, define_symbols and
        undefine_symbols.

        :param config: Configuration object
        :type config: :class:`parser.xml_generator_configuration_t`
        :rtype: str
        """
        m = hashlib.sha1()
        m.update(config.working_directory)
        for p in config.include_paths:
            m.update(p)
        for p in config.define_symbols:
            m.update(p)
        for p in config.undefine_symbols:
            m.update(p)
        for p in config.cflags:
            m.update(p)
        return m.digest()


class filename_entry_t(object):

    """This is a record stored in the filename_repository_t class.

    The class is an internal class used in the implementation of the
    filename_repository_t class and it just serves as a container for
    the file name and the reference count.
    """

    def __init__(self, filename):
        """Constructor.

        The reference count is initially set to 0.
        """
        # Filename
        self.filename = filename
        # Reference count
        self.refcount = 0

        # Cached signature value for the file.
        # If sig_valid flag is False, the signature still has to be computed,
        # otherwise the cached value can be used.
        # These attributes must not be pickled!
        self.sig_valid = False
        self.signature = None

    def __getstate__(self):
        # Only pickle filename and refcount
        return self.filename, self.refcount

    def __setstate__(self, state):
        self.filename, self.refcount = state
        self.sig_valid = False
        self.signature = None

    def inc_ref_count(self):
        """Increase the reference count by 1."""

        self.refcount += 1

    def dec_ref_count(self):
        """Decrease the reference count by 1 and return the new count."""

        self.refcount -= 1
        return self.refcount


class filename_repository_t(object):

    """File name repository.

    This class stores file names and can check whether a file has been
    modified or not since a previous call.
    A file name is stored by calling acquire_filename() which returns
    an ID and a signature of the file. The signature can later be used
    to check if the file was modified by calling is_file_modified().
    If the file name is no longer required release_filename() should be
    called so that the entry can be removed from the repository.
    """

    def __init__(self, sha1_sigs):
        """Constructor.
        """

        # Flag that determines whether the signature is a sha1 digest or
        # the modification time
        # (this flag is passed to the filename_repository_t class)
        self._sha1_sigs = sha1_sigs

        # ID lookup table (key: filename / value: id_)
        self.__id_lut = {}

        # Entry dictionary (key: id_ / value: filename_entry_t)
        # This dictionary contains the actual data.
        # It must always hold that each entry in __entries has a corresponding
        # entry in __id_lut (i.e. the keys in __id_lut must be the names
        # stored in __entries)
        self.__entries = {}

        # A counter for new ids
        self.__next_id = 1

    def acquire_filename(self, name):
        """Acquire a file name and return its id and its signature.
        """

        id_ = self.__id_lut.get(name)
        # Is this a new entry?
        if id_ is None:
            # then create one...
            id_ = self.__next_id
            self.__next_id += 1
            self.__id_lut[name] = id_
            entry = filename_entry_t(name)
            self.__entries[id_] = entry
        else:
            # otherwise obtain the entry...
            entry = self.__entries[id_]

        entry.inc_ref_count()
        return id_, self._get_signature(entry)

    def release_filename(self, id_):
        """Release a file name.
        """

        entry = self.__entries.get(id_)
        if entry is None:
            raise ValueError("Invalid filename id (%d)" % id_)

        # Decrease reference count and check if the entry has to be removed...
        if entry.dec_ref_count() == 0:
            del self.__entries[id_]
            del self.__id_lut[entry.filename]

    def is_file_modified(self, id_, signature):
        """Check if the file referred to by `id_` has been modified.
        """

        entry = self.__entries.get(id_)
        if entry is None:
            raise ValueError("Invalid filename id_ (%d)" % id_)

        # Is the signature already known?
        if entry.sig_valid:
            # use the cached signature
            filesig = entry.signature
        else:
            # compute the signature and store it
            filesig = self._get_signature(entry)
            entry.signature = filesig
            entry.sig_valid = True

        return filesig != signature

    def update_id_counter(self):
        """Update the `id_` counter so that it doesn't grow forever.
        """

        if not self.__entries:
            self.__next_id = 1
        else:
            self.__next_id = max(self.__entries.keys()) + 1

    def _get_signature(self, entry):
        """Return the signature of the file stored in entry.
        """

        if self._sha1_sigs:
            # return sha1 digest of the file content...
            if not os.path.exists(entry.filename):
                return None
            try:
                f = open(entry.filename)
            except IOError as e:
                print("Cannot determine sha1 digest:", e)
                return None
            data = f.read()
            f.close()
            return hashlib.sha1(data).digest()
        else:
            # return file modification date...
            try:
                return os.path.getmtime(entry.filename)
            except OSError:
                return None

    def _dump(self):
        """Dump contents for debugging/testing.
        """

        print(70 * "-")
        print("ID lookup table:")
        for name in self.__id_lut:
            id_ = self.__id_lut[name]
            print("  %s -> %d" % (name, id_))

        print(70 * "-")
        print("%-4s %-60s %s" % ("ID", "Filename", "Refcount"))
        print(70 * "-")
        for id_ in self.__entries:
            entry = self.__entries[id_]
            print("%04d %-60s %d" % (id_, entry.filename, entry.refcount))