This file is indexed.

/usr/share/pyshared/obnamlib/repo.py is in obnam 0.24.1-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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
# Copyright (C) 2009-2011  Lars Wirzenius
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import errno
import hashlib
import larch
import logging
import os
import random
import re
import stat
import struct
import time
import tracing

import obnamlib


class LockFail(obnamlib.Error):

    pass


class BadFormat(obnamlib.Error):

    pass


class HookedFS(object):

    '''A class to filter read/written data through hooks.'''
    
    def __init__(self, repo, fs, hooks):
        self.repo = repo
        self.fs = fs
        self.hooks = hooks
        self.do_make_readonly = True
        
    def __getattr__(self, name):
        return getattr(self.fs, name)
        
    def _get_toplevel(self, filename):
        parts = filename.split(os.sep)
        if len(parts) > 1:
            return parts[0]
        else: # pragma: no cover
            raise obnamlib.Error('File at repository root: %s' % filename)
        
    def cat(self, filename):
        data = self.fs.cat(filename)
        toplevel = self._get_toplevel(filename)
        return self.hooks.call('repository-read-data', data,
                                repo=self.repo, toplevel=toplevel)
        
    def write_file(self, filename, data):
        tracing.trace('writing hooked %s' % filename)
        toplevel = self._get_toplevel(filename)
        data = self.hooks.call('repository-write-data', data,
                                repo=self.repo, toplevel=toplevel)
        self.fs.write_file(filename, data)
        self.make_readonly(filename)
        
    def overwrite_file(self, filename, data):
        tracing.trace('overwriting hooked %s' % filename)
        toplevel = self._get_toplevel(filename)
        data = self.hooks.call('repository-write-data', data,
                                repo=self.repo, toplevel=toplevel)
        self.fs.overwrite_file(filename, data)
        self.make_readonly(filename)
        
    def make_readonly(self, filename):
        if self.do_make_readonly:
            try:
                self.fs.chmod(filename, stat.S_IRUSR)
            except OSError, e: # pragma: no cover
                if e.errno != errno.EPERM:
                    raise
                else:
                    logging.debug('Ignoring chmod error in repository')
                    self.do_make_readonly = False
        

class Repository(object):

    '''Repository for backup data.
    
    Backup data is put on a virtual file system
    (obnamlib.VirtualFileSystem instance), in some form that
    the API of this class does not care about.
    
    The repository may contain data for several clients that share 
    encryption keys. Each client is identified by a name.
    
    The repository has a "root" object, which is conceptually a list of
    client names.
    
    Each client in turn is conceptually a list of generations,
    which correspond to snapshots of the user data that existed
    when the generation was created.
    
    Read-only access to the repository does not require locking.
    Write access may affect only the root object, or only a client's
    own data, and thus locking may affect only the root, or only
    the client.
    
    When a new generation is started, it is a copy-on-write clone
    of the previous generation, and the caller needs to modify
    the new generation to match the current state of user data.
    
    The file 'metadata/format' at the root of the repository contains the
    version of the repository format it uses. The version is
    specified using a single integer.

    '''
    
    format_version = 5

    def __init__(self, fs, node_size, upload_queue_size, lru_size, hooks,
                 idpath_depth, idpath_bits, idpath_skip):
        self.setup_hooks(hooks or obnamlib.HookManager())
        self.fs = HookedFS(self, fs, self.hooks)
        self.node_size = node_size
        self.upload_queue_size = upload_queue_size
        self.lru_size = lru_size
        self.got_root_lock = False
        self.clientlist = obnamlib.ClientList(self.fs, node_size, 
                                              upload_queue_size, 
                                              lru_size, self)
        self.got_client_lock = False
        self.client_lockfile = None
        self.current_client = None
        self.current_client_id = None
        self.new_generation = None
        self.added_clients = []
        self.removed_clients = []
        self.removed_generations = []
        self.client = None
        self.chunklist = obnamlib.ChunkList(self.fs, node_size, 
                                            upload_queue_size, 
                                            lru_size, self)
        self.chunksums = obnamlib.ChecksumTree(self.fs, 'chunksums', 
                                               len(self.checksum('')),
                                               node_size, upload_queue_size, 
                                               lru_size, self)
        self.prev_chunkid = None
        self.chunk_idpath = larch.IdPath('chunks', idpath_depth, 
                                         idpath_bits, idpath_skip)

    def setup_hooks(self, hooks):
        self.hooks = hooks
        
        self.hooks.new('repository-toplevel-init')
        self.hooks.new_filter('repository-read-data')
        self.hooks.new_filter('repository-write-data')
        self.hooks.new('repository-add-client')
        
    def checksum(self, data):
        '''Return checksum of data.
        
        The checksum is (currently) MD5.
        
        Return a non-binary string (hexdigest) form of the checksum
        so that it can easily be used for filenames, or printed to
        log files, or whatever.
        
        '''

        checksummer = self.new_checksummer()
        checksummer.update(data)
        return checksummer.hexdigest()

    def new_checksummer(self):
        '''Return a new checksum algorithm.'''
        return hashlib.md5()

    def acceptable_version(self, version):
        '''Are we compatible with on-disk format?'''
        return self.format_version == version

    def client_dir(self, client_id):
        '''Return name of sub-directory for a given client.'''
        return str(client_id)

    def list_clients(self):
        '''Return list of names of clients using this repository.'''

        self.check_format_version()
        listed = set(self.clientlist.list_clients())
        added = set(self.added_clients)
        removed = set(self.removed_clients)
        clients = listed.union(added).difference(removed)
        return list(clients)

    def require_root_lock(self):
        '''Ensure we have the lock on the repository's root node.'''
        if not self.got_root_lock:
            raise LockFail('have not got lock on root node')

    def require_client_lock(self):
        '''Ensure we have the lock on the currently open client.'''
        if not self.got_client_lock:
                raise LockFail('have not got lock on client')

    def require_open_client(self):
        '''Ensure we have opened the client (r/w or r/o).'''
        if self.current_client is None:
            raise obnamlib.Error('client is not open')

    def require_started_generation(self):
        '''Ensure we have started a new generation.'''
        if self.new_generation is None:
            raise obnamlib.Error('new generation has not started')

    def lock_root(self):
        '''Lock root node.
        
        Raise obnamlib.LockFail if locking fails. Lock will be released
        by commit_root() or unlock_root().
        
        '''

        tracing.trace('locking root')        
        self.check_format_version()
        try:
            self.fs.fs.write_file('root.lock', '')
        except OSError, e:
            if e.errno == errno.EEXIST:
                raise LockFail('Lock file root.lock already exists')
        self.got_root_lock = True
        self.added_clients = []
        self.removed_clients = []
        self._write_format_version(self.format_version)

    def unlock_root(self):
        '''Unlock root node without committing changes made.'''
        tracing.trace('unlocking root')
        self.require_root_lock()
        self.added_clients = []
        self.removed_clients = []
        self.fs.remove('root.lock')
        self.got_root_lock = False
        
    def commit_root(self):
        '''Commit changes to root node, and unlock it.'''
        tracing.trace('committing root')
        self.require_root_lock()
        for client_name in self.added_clients:
            self.clientlist.add_client(client_name)
            self.hooks.call('repository-add-client', 
                            self.clientlist, client_name)
        self.added_clients = []
        for client_name in self.removed_clients:
            client_id = self.clientlist.get_client_id(client_name)
            client_dir = self.client_dir(client_id)
            if client_id is not None and self.fs.exists(client_dir):
                self.fs.rmtree(client_dir)
            self.clientlist.remove_client(client_name)
        self.clientlist.commit()
        self.unlock_root()
        
    def get_format_version(self):
        '''Return (major, minor) of the on-disk format version.
        
        If on-disk repository does not have a version yet, return None.
        
        '''
        
        if self.fs.exists('metadata/format'):
            data = self.fs.cat('metadata/format')
            lines = data.splitlines()
            line = lines[0]
            try:
                version = int(line)
            except ValueError, e: # pragma: no cover
                msg = ('Invalid repository format version (%s) -- '
                            'forgot encryption?' %
                       repr(line))
                raise obnamlib.Error(msg)
            return version
        else:
            return None
        
    def _write_format_version(self, version):
        '''Write the desired format version to the repository.'''
        tracing.trace('write format version')
        if not self.fs.exists('metadata'):
            self.fs.mkdir('metadata')
            self.hooks.call('repository-toplevel-init', self, 'metadata')
        self.fs.overwrite_file('metadata/format', '%s\n' % version)

    def check_format_version(self):
        '''Verify that on-disk format version is compatbile.
        
        If not, raise BadFormat.
        
        '''
        
        on_disk = self.get_format_version()
        if on_disk is not None and not self.acceptable_version(on_disk):
            raise BadFormat('On-disk format %s is incompatible '
                            'with program format %s' %
                                (on_disk, self.format_version))
        
    def add_client(self, client_name):
        '''Add a new client to the repository.'''
        tracing.trace('client_name=%s', client_name)
        self.require_root_lock()
        if client_name in self.list_clients():
            raise obnamlib.Error('client %s already exists in repository' % 
                                 client_name)
        self.added_clients.append(client_name)
        
    def remove_client(self, client_name):
        '''Remove a client from the repository.
        
        This removes all data related to the client, including all
        actual file data unless other clients also use it.
        
        '''
        
        tracing.trace('client_name=%s', client_name)
        self.require_root_lock()
        if client_name not in self.list_clients():
            raise obnamlib.Error('client %s does not exist' % client_name)
        self.removed_clients.append(client_name)
        
    def lock_client(self, client_name):
        '''Lock a client for exclusive write access.
        
        Raise obnamlib.LockFail if locking fails. Lock will be released
        by commit_client() or unlock_client().

        '''

        tracing.trace('client_name=%s', client_name)
        self.check_format_version()
        client_id = self.clientlist.get_client_id(client_name)
        if client_id is None:
            raise LockFail('client %s does not exist' % client_name)

        client_dir = self.client_dir(client_id)
        if not self.fs.exists(client_dir):
            self.fs.mkdir(client_dir)
            self.hooks.call('repository-toplevel-init', self, client_dir)
        lockname = os.path.join(client_dir, 'lock')
        try:
            self.fs.write_file(lockname, '')
        except OSError, e:
            if e.errno == errno.EEXIST:
                raise LockFail('client %s is already locked' % client_name)
        self.got_client_lock = True
        self.client_lockfile = lockname
        self.current_client = client_name
        self.current_client_id = client_id
        self.added_generations = []
        self.removed_generations = []
        self.client = obnamlib.ClientMetadataTree(self.fs, client_dir, 
                                                  self.node_size,
                                                  self.upload_queue_size, 
                                                  self.lru_size, self)
        self.client.init_forest()

    def unlock_client(self):
        '''Unlock currently locked client, without committing changes.'''
        tracing.trace('unlocking client')
        self.require_client_lock()
        self.new_generation = None
        for genid in self.added_generations:
            self._really_remove_generation(genid)
        self.client = None # FIXME: This should remove uncommitted data.
        self.added_generations = []
        self.removed_generations = []
        self.fs.remove(self.client_lockfile)
        self.client_lockfile = None
        self.got_client_lock = False
        self.current_client = None
        self.current_client_id = None

    def commit_client(self, checkpoint=False):
        '''Commit changes to and unlock currently locked client.'''
        tracing.trace('committing client (checkpoint=%s)', checkpoint)
        self.require_client_lock()
        if self.new_generation:
            self.client.set_current_generation_is_checkpoint(checkpoint)
        self.added_generations = []
        for genid in self.removed_generations:
            self._really_remove_generation(genid)
        self.client.commit()
        self.chunklist.commit()
        self.chunksums.commit()
        self.unlock_client()
        
    def open_client(self, client_name):
        '''Open a client for read-only operation.'''
        tracing.trace('open r/o client_name=%s' % client_name)
        self.check_format_version()
        client_id = self.clientlist.get_client_id(client_name)
        if client_id is None:
            raise obnamlib.Error('%s is not an existing client' % client_name)
        self.current_client = client_name
        self.current_client_id = client_id
        client_dir = self.client_dir(client_id)
        self.client = obnamlib.ClientMetadataTree(self.fs, client_dir, 
                                                  self.node_size, 
                                                  self.upload_queue_size, 
                                                  self.lru_size, self)
        self.client.init_forest()
        
    def list_generations(self):
        '''List existing generations for currently open client.'''
        self.require_open_client()
        return self.client.list_generations()
        
    def get_is_checkpoint(self, genid):
        '''Is a generation a checkpoint one?'''
        self.require_open_client()
        return self.client.get_is_checkpoint(genid)
        
    def start_generation(self):
        '''Start a new generation.
        
        The new generation is a copy-on-write clone of the previous
        one (or empty, if first generation).
        
        '''
        tracing.trace('start new generation')
        self.require_client_lock()
        if self.new_generation is not None:
            raise obnamlib.Error('Cannot start two new generations')
        self.client.start_generation()
        self.new_generation = \
            self.client.get_generation_id(self.client.tree)
        self.added_generations.append(self.new_generation)
        return self.new_generation

    def _really_remove_generation(self, gen_id):
        '''Really remove a committed generation.
        
        This is not part of the public API.
        
        This does not make any safety checks.
        
        '''


        def filter_away_chunks_used_by_other_gens(chunk_ids, gen_id):
            for other_id in self.list_generations():
                if other_id != gen_id:
                    other_chunks = self.client.list_chunks_in_generation(
                                        other_id)
                    other_chunks = set(other_chunks)
                    chunk_ids = chunk_ids.difference(other_chunks)

            return chunk_ids

        def remove_unused_chunks(chunk_ids):
            for chunk_id in chunk_ids:
                checksum = self.chunklist.get_checksum(chunk_id)
                self.chunksums.remove(checksum, chunk_id, 
                                      self.current_client_id)
                if not self.chunksums.chunk_is_used(checksum, chunk_id):
                    self.remove_chunk(chunk_id)

        self.require_client_lock()
        logging.debug('_really_remove_generation: %d' % gen_id)
        chunk_ids = set(self.client.list_chunks_in_generation(gen_id))
        chunk_ids = filter_away_chunks_used_by_other_gens(chunk_ids, gen_id)
        remove_unused_chunks(chunk_ids)
        self.client.remove_generation(gen_id)

    def remove_generation(self, gen):
        '''Remove a committed generation.'''
        self.require_client_lock()
        if gen == self.new_generation:
            raise obnamlib.Error('cannot remove started generation')
        self.removed_generations.append(gen)

    def get_generation_times(self, gen):
        '''Return start and end times of a generation.
        
        An unfinished generation has no end time, so None is returned.
        
        '''

        self.require_open_client()
        return self.client.get_generation_times(gen)

    def listdir(self, gen, dirname):
        '''Return list of basenames in a directory within generation.'''
        self.require_open_client()
        return self.client.listdir(gen, dirname)
        
    def get_metadata(self, gen, filename):
        '''Return metadata for a file in a generation.'''

        self.require_open_client()
        try:
            encoded = self.client.get_metadata(gen, filename)
        except KeyError:
            raise obnamlib.Error('%s does not exist' % filename)
        return obnamlib.decode_metadata(encoded)

    def create(self, filename, metadata):
        '''Create a new (empty) file in the new generation.'''
        self.require_started_generation()
        encoded = obnamlib.encode_metadata(metadata)
        self.client.create(filename, encoded)

    def remove(self, filename):
        '''Remove file or directory or directory tree from generation.'''
        self.require_started_generation()
        self.client.remove(filename)

    def _chunk_filename(self, chunkid):
        return self.chunk_idpath.convert(chunkid)

    def put_chunk(self, data, checksum):
        '''Put chunk of data into repository.
        
        checksum is the checksum of the data, and must be the same
        value as returned by self.checksum(data). However, since all
        known use cases require the caller to know the checksum before
        calling this method, and since computing checksums is
        expensive, we micro-optimize a little bit by passing it as
        an argument.
        
        If the same data is already in the repository, it will be put there
        a second time. It is the caller's responsibility to check
        that the data is not already in the repository.
        
        Return the unique identifier of the new chunk.
        
        '''
        
        def random_chunkid():
            return random.randint(0, obnamlib.MAX_ID)
        
        tracing.trace('putting chunk (checksum=%s)', repr(checksum))
        self.require_started_generation()
        if self.prev_chunkid is None:
            self.prev_chunkid = random_chunkid()
        while True:
            chunkid = (self.prev_chunkid + 1) % obnamlib.MAX_ID
            filename = self._chunk_filename(chunkid)
            if not self.fs.exists(filename):
                break
            self.prev_chunkid = random_chunkid() # pragma: no cover
        tracing.trace('chunkid=%s', chunkid)
        self.prev_chunkid = chunkid
        if not self.fs.exists('chunks'):
            self.fs.mkdir('chunks')
            self.hooks.call('repository-toplevel-init', self, 'chunks')
        dirname = os.path.dirname(filename)
        if not self.fs.exists(dirname):
            self.fs.makedirs(dirname)
        self.fs.write_file(filename, data)
        checksum = self.checksum(data)
        self.chunklist.add(chunkid, checksum)
        self.chunksums.add(checksum, chunkid, self.current_client_id)
        return chunkid
        
    def get_chunk(self, chunkid):
        '''Return data of chunk with given id.'''
        self.require_open_client()
        return self.fs.cat(self._chunk_filename(chunkid))
        
    def chunk_exists(self, chunkid):
        '''Does a chunk exist in the repository?'''
        self.require_open_client()
        return self.fs.exists(self._chunk_filename(chunkid))
        
    def find_chunks(self, checksum):
        '''Return identifiers of chunks with given checksum.
        
        Because of hash collisions, the list may be longer than one.
        
        '''

        self.require_open_client()
        return self.chunksums.find(checksum)

    def list_chunks(self):
        '''Return list of ids of all chunks in repository.'''
        result = []
        pat = re.compile(r'^.*/.*/[0-9a-fA-F]+$')
        if self.fs.exists('chunks'):
            for pathname, st in self.fs.scan_tree('chunks'):
                if stat.S_ISREG(st.st_mode) and pat.match(pathname):
                    basename = os.path.basename(pathname)
                    result.append(int(basename, 16))
        return result

    def remove_chunk(self, chunk_id):
        '''Remove a chunk from the repository.
        
        Note that this does _not_ remove the chunk from the chunk
        checksum forest. The caller is not supposed to call us until
        the chunk is not there anymore.
        
        However, it does remove the chunk from the chunk list forest.
        
        '''

        tracing.trace('chunk_id=%s', chunk_id)
        self.require_open_client()
        self.chunklist.remove(chunk_id)
        filename = self._chunk_filename(chunk_id)
        try:
            self.fs.remove(filename)
        except OSError:
            pass

    def get_file_chunks(self, gen, filename):
        '''Return list of ids of chunks belonging to a file.'''
        self.require_open_client()
        return self.client.get_file_chunks(gen, filename)

    def set_file_chunks(self, filename, chunkids):
        '''Set ids of chunks belonging to a file.
        
        File must be in the started generation.
        
        '''
        
        self.require_started_generation()
        self.client.set_file_chunks(filename, chunkids)

    def append_file_chunks(self, filename, chunkids):
        '''Append to list of ids of chunks belonging to a file.
        
        File must be in the started generation.
        
        '''
        
        self.require_started_generation()
        self.client.append_file_chunks(filename, chunkids)
        
    def genspec(self, spec):
        '''Interpret a generation specification.'''

        self.require_open_client()
        gens = self.list_generations()
        if not gens:
            raise obnamlib.Error('No generations')
        if spec == 'latest':
            return gens[-1]
        else:
            try:
                intspec = int(spec)
            except ValueError:
                raise obnamlib.Error('Generation %s is not an integer' % spec)
            if intspec in gens:
                return intspec
            else:
                raise obnamlib.Error('Generation %s not found' % spec)

    def walk(self, gen, arg, depth_first=False):
        '''Iterate over each pathname specified by argument.
        
        This is a generator. Each return value is a tuple consisting
        of a pathname and its corresponding metadata. Directories are
        recursed into.
        
        '''
        
        arg = os.path.normpath(arg)
        metadata = self.get_metadata(gen, arg)
        if metadata.isdir():
            if not depth_first:
                yield arg, metadata
            kids = self.listdir(gen, arg)
            kidpaths = [os.path.join(arg, kid) for kid in kids]
            for kidpath in kidpaths:
                for x in self.walk(gen, kidpath, depth_first=depth_first):
                    yield x
            if depth_first:
                yield arg, metadata
        else:
            yield arg, metadata