This file is indexed.

/usr/lib/python2.7/dist-packages/larch/journal.py is in python-larch 1.20151025-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
# Copyright 2012  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 logging
import os
import tracing

import larch


class ReadOnlyMode(larch.Error): # pragma: no cover

    def __init__(self):
        self.msg = 'Larch B-tree is in read-only mode, no changes allowed'


class Journal(object):

    '''A journal layer on top of a virtual filesystem.
    
    The journal solves the problem of updating on-disk data structures
    atomically. Changes are first written to a journal, and then moved
    from there to the real location. If the program or system crashes,
    the changes can be completed later on, or rolled back, depending
    on what's needed for consistency.
    
    The journal works as follows:
    
    * ``x`` is the real filename
    * ``new/x`` is a new or modified file
    * ``delete/x`` is a deleted file, created there as a flag file
    
    Commit does this:
    
    * for every ``delete/x``, remove ``x``
    * for every ``new/x`` except ``new/metadata``, move to ``x``
    * move ``new/metadata`` to ``metadata``
    
    Rollback does this:
    
    * remove every ``new/x``
    * remove every ``delete/x``
    
    When a journalled node store is opened, if ``new/metadata`` exists,
    the commit happens. Otherwise a rollback happens. This guarantees
    that the on-disk state is consistent.
    
    We only provide enough of a filesystem interface as is needed by
    NodeStoreDisk. For example, we do not care about directory removal.
    
    The journal can be opened in read-only mode, in which case it ignores
    any changes in ``new`` and ``delete``, and does not try to rollback or
    commit at start.

    '''
    
    flag_basename = 'metadata'
    
    def __init__(self, allow_writes, fs, storedir):
        logging.debug('Initializing Journal for %s' % storedir)
        self.allow_writes = allow_writes
        self.fs = fs
        self.storedir = storedir
        if not self.storedir.endswith(os.sep):
            self.storedir += os.sep
        self.newdir = os.path.join(self.storedir, 'new/')
        self.deletedir = os.path.join(self.storedir, 'delete/')
        self.flag_file = os.path.join(self.storedir, self.flag_basename)
        self.new_flag = os.path.join(self.newdir, self.flag_basename)

        self.new_flag_seen = self.fs.exists(self.new_flag)
        tracing.trace('self.new_flag_seen: %s' % self.new_flag_seen)
        if self.allow_writes:
            if self.new_flag_seen:
                logging.debug('Automatically committing remaining changes')
                self.commit()
            else:
                logging.debug('Automatically rolling back remaining changes')
                self.rollback()
        else:
            logging.debug('Not committing/rolling back since read-only')
        self.new_files = set()
        self.deleted_files = set()

    def _require_rw(self):
        '''Raise error if modifications are not allowed.'''
        if not self.allow_writes:
            raise ReadOnlyMode()

    def _relative(self, filename):
        '''Return the part of filename that is relative to storedir.'''
        assert filename.startswith(self.storedir)
        return filename[len(self.storedir):]

    def _new(self, filename):
        '''Return name for a new file whose final name is filename.'''
        return os.path.join(self.newdir, self._relative(filename))

    def _deleted(self, filename):
        '''Return name for temporary name for file to be deleted.'''
        return os.path.join(self.deletedir, self._relative(filename))

    def _realname(self, journaldir, filename):
        '''Return real name for a file in a journal temporary directory.'''
        assert filename.startswith(journaldir)
        return os.path.join(self.storedir, filename[len(journaldir):])

    def _is_in_new(self, filename):
        new = self._new(filename)
        return new in self.new_files or self.fs.exists(new)

    def _is_in_deleted(self, filename):
        deleted = self._deleted(filename)
        return deleted in self.deleted_files or self.fs.exists(deleted)

    def exists(self, filename):
        if self.allow_writes or self.new_flag_seen:
            if self._is_in_new(filename):
                return True
            elif self._is_in_deleted(filename):
                return False
        return self.fs.exists(filename)
        
    def makedirs(self, dirname):
        tracing.trace(dirname)
        self._require_rw()
        x = self._new(dirname)
        self.fs.makedirs(x)
        self.new_files.add(x)

    def overwrite_file(self, filename, contents):
        tracing.trace(filename)
        self._require_rw()
        new = self._new(filename)
        self.fs.overwrite_file(new, contents)
        self.new_files.add(new)

    def cat(self, filename):
        tracing.trace('filename=%s' % filename)
        tracing.trace('allow_writes=%s' % self.allow_writes)
        tracing.trace('new_flag_seen=%s' % self.new_flag_seen)
        if self.allow_writes or self.new_flag_seen:
            if self._is_in_new(filename):
                return self.fs.cat(self._new(filename))
            elif self._is_in_deleted(filename):
                raise OSError(
                    errno.ENOENT, os.strerror(errno.ENOENT), filename)
        return self.fs.cat(filename)

    def remove(self, filename):
        tracing.trace(filename)
        self._require_rw()

        new = self._new(filename)
        deleted = self._deleted(filename)
        
        if new in self.new_files:
            self.fs.remove(new)
            self.new_files.remove(new)
        elif deleted in self.deleted_files:
            raise OSError(errno.ENOENT, os.strerror(errno.ENOENT), filename)
        else:
            self.fs.overwrite_file(deleted, '')
            self.deleted_files.add(deleted)

    def list_files(self, dirname):
        '''List all files.
        
        Files only, no directories.
        
        '''

        assert not dirname.startswith(self.newdir)
        assert not dirname.startswith(self.deletedir)

        if self.allow_writes or self.new_flag_seen:
            if self.fs.exists(dirname):
                for x in self.climb(dirname, files_only=True):
                    if self.exists(x):
                        yield x
            new = self._new(dirname)
            if self.fs.exists(new):
                for x in self.climb(new, files_only=True):
                    yield self._realname(self.newdir, x)
        else:
            if self.fs.exists(dirname):
                for x in self.climb(dirname, files_only=True):
                    in_new = x.startswith(self.newdir)
                    in_deleted = x.startswith(self.deletedir)
                    if not in_new and not in_deleted:
                        yield x

    def climb(self, dirname, files_only=False):
        basenames = self.fs.listdir(dirname)
        filenames = []
        for basename in basenames:
            pathname = os.path.join(dirname, basename)
            if self.fs.isdir(pathname):
                for x in self.climb(pathname, files_only=files_only):
                    yield x
            else:
                filenames.append(pathname)
        for filename in filenames:
            yield filename
        if not files_only:
            yield dirname

    def _clear_directory(self, dirname):
        tracing.trace(dirname)
        for pathname in self.climb(dirname):
            if pathname != dirname:
                if self.fs.isdir(pathname):
                    self.fs.rmdir(pathname)
                else:
                    self.fs.remove(pathname)

    def _vivify(self, dirname, exclude):
        tracing.trace('dirname: %s' % dirname)
        tracing.trace('exclude: %s' % repr(exclude))
        all_excludes = [dirname] + exclude
        for pathname in self.climb(dirname):
            if pathname not in all_excludes:
                r = self._realname(dirname, pathname)
                parent = os.path.dirname(r)
                if self.fs.isdir(pathname):
                    if not self.fs.exists(r):
                        if not self.fs.exists(parent):
                            self.fs.makedirs(parent)
                        self.fs.rename(pathname, r)
                else:
                    if not self.fs.exists(parent):
                        self.fs.makedirs(parent)
                    self.fs.rename(pathname, r)

    def rollback(self):
        tracing.trace('%s start' % self.storedir)
        self._require_rw()

        if self.fs.exists(self.newdir):
            self._clear_directory(self.newdir)

        if self.fs.exists(self.deletedir):
            self._clear_directory(self.deletedir)

        self.new_files = set()
        self.deleted_files = set()

        tracing.trace('%s done' % self.storedir)

    def _really_delete(self, deletedir):
        tracing.trace(deletedir)
        for pathname in self.climb(deletedir, files_only=True):
            if pathname != deletedir:
                realname = self._realname(deletedir, pathname)
                try:
                    self.fs.remove(realname)
                except OSError, e: # pragma: no cover
                    if e.errno not in (errno.ENOENT, errno.EISDIR):
                        raise
                self.fs.remove(pathname)

    def commit(self, skip=[]):
        tracing.trace('%s start' % self.storedir)
        self._require_rw()

        if self.fs.exists(self.deletedir):
            self._really_delete(self.deletedir)

        if self.fs.exists(self.newdir):
            skip = [self._new(x) for x in skip]
            self._vivify(self.newdir, [self.new_flag] + skip)
        if not skip and self.fs.exists(self.new_flag):
            self.fs.rename(self.new_flag, self.flag_file)

        self.new_files = set()
        self.deleted_files = set()

        tracing.trace('%s done' % self.storedir)