view mercurial/ @ 2162:dac432a521d8

author Colin McMillen <>
date Sun, 30 Apr 2006 18:52:34 +0200
parents c62112815801
children d821918e3bee 9b42304d9896
line wrap: on
line source

# - special classes to make repo updates atomic
# Copyright 2006 Vadim Gelfer <>
# This software may be used and distributed according to the terms
# of the GNU General Public License, incorporated herein by reference.

from demandload import *
demandload(globals(), "cStringIO changelog errno manifest os tempfile")

# writes to metadata files are ordered.  reads: changelog, manifest,
# normal files.  writes: normal files, manifest, changelog.

# manifest contains pointers to offsets in normal files.  changelog
# contains pointers to offsets in manifest.  if reader reads old
# changelog while manifest or normal files are written, it has no
# pointers into new parts of those files that are maybe not consistent
# yet, so will not read them.

# localrepo.addchangegroup thinks it writes changelog first, then
# manifest, then normal files (this is order they are available, and
# needed for computing linkrev fields), but uses appendfile to hide
# updates from readers.  data not written to manifest or changelog
# until all normal files updated.  write manifest first, then
# changelog.

# with this write ordering, readers cannot see inconsistent view of
# repo during update.

class appendfile(object):
    '''implement enough of file protocol to append to revlog file.
    appended data is written to temp file.  reads and seeks span real
    file and temp file.  readers cannot see appended data until
    writedata called.'''

    def __init__(self, fp, tmpname):
        if tmpname:
            self.tmpname = tmpname
            self.tmpfp = open(self.tmpname, 'ab+')
            fd, self.tmpname = tempfile.mkstemp()
            self.tmpfp = os.fdopen(fd, 'ab+')
        self.realfp = fp
        self.offset = fp.tell()
        # real file is not written by anyone else. cache its size so
        # seek and read can be fast.
        self.realsize = os.fstat(fp.fileno()).st_size

    def end(self):
        self.tmpfp.flush() # make sure the stat is correct
        return self.realsize + os.fstat(self.tmpfp.fileno()).st_size

    def tell(self):
        return self.offset

    def flush(self):

    def close(self):

    def seek(self, offset, whence=0):
        '''virtual file offset spans real file and temp file.'''
        if whence == 0:
            self.offset = offset
        elif whence == 1:
            self.offset += offset
        elif whence == 2:
            self.offset = self.end() + offset

        if self.offset < self.realsize:
   - self.realsize)

    def read(self, count=-1):
        '''only trick here is reads that span real file and temp file.'''
        fp = cStringIO.StringIO()
        old_offset = self.offset
        if self.offset < self.realsize:
            s =
            self.offset += len(s)
            if count > 0:
                count -= len(s)
        if count != 0:
            if old_offset != self.offset:
       - self.realsize)
            s =
            self.offset += len(s)
        return fp.getvalue()

    def write(self, s):
        '''append to temp file.''', 2)
        # all writes are appends, so offset must go to end of file.
        self.offset = self.realsize + self.tmpfp.tell()

class appendopener(object):
    '''special opener for files that only read or append.'''

    def __init__(self, opener):
        self.realopener = opener
        # key: file name, value: appendfile name
        self.tmpnames = {}

    def __call__(self, name, mode='r'):
        '''open file.'''

        assert mode in 'ra+'
            realfp = self.realopener(name, 'r')
        except IOError, err:
            if err.errno != errno.ENOENT: raise
            realfp = self.realopener(name, 'w+')
        tmpname = self.tmpnames.get(name)
        fp = appendfile(realfp, tmpname)
        if tmpname is None:
            self.tmpnames[name] = fp.tmpname
        return fp

    def writedata(self):
        '''copy data from temp files to real files.'''
        # write .d file before .i file.
        tmpnames = self.tmpnames.items()
        for name, tmpname in tmpnames:
            fp = open(tmpname, 'rb')
            s =
            fp = self.realopener(name, 'a')

# files for changelog and manifest are in different appendopeners, so
# not mixed up together.

class appendchangelog(changelog.changelog, appendopener):
    def __init__(self, opener, version):
        appendopener.__init__(self, opener)
        changelog.changelog.__init__(self, self, version)
    def checkinlinesize(self, fp, tr):

class appendmanifest(manifest.manifest, appendopener):
    def __init__(self, opener, version):
        appendopener.__init__(self, opener)
        manifest.manifest.__init__(self, self, version)
    def checkinlinesize(self, fp, tr):