MAJOR=4
MINOR=53
DEVNAME=tty53
  C   1ˆl–äY>”JeJe@†à¸Û*bÑ¿�þ\tM¬Ü¡p¬ˆ$‘¦�üÿÜ„�¿Û_ŠI|¥�+Wé3ž¯ÄÚ ?÷     1# transaction.py - simple journaling scheme for mercurial
#
# This transaction scheme is intended to gracefully handle program
# errors and interruptions. More serious failures like system crashes
# can be recovered with an fsck-like tool. As the whole repository is
# effectively log-structured, this should amount to simply truncating
# anything that isn't referenced in the changelog.
#
# Copyright 2005, 2006 Olivia Mackall <olivia@selenic.com>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.


from .i18n import _
from . import (
    error,
    pycompat,
    util,
)
from .utils import stringutil

version = 2

GEN_GROUP_ALL = b'all'
GEN_GROUP_PRE_FINALIZE = b'prefinalize'
GEN_GROUP_POST_FINALIZE = b'postfinalize'


def active(func):
    def _active(self, *args, **kwds):
        if self._count == 0:
            raise error.ProgrammingError(
                b'cannot use transaction when it is already committed/aborted'
            )
        return func(self, *args, **kwds)

    return _active


def _playback(
    journal,
    report,
    opener,
    vfsmap,
    entries,
    backupentries,
    unlink=True,
    checkambigfiles=None,
):
    for f, o in sorted(dict(entries).items()):
        if o or not unlink:
            checkambig = checkambigfiles and (f, b'') in checkambigfiles
            try:
                fp = opener(f, b'a', checkambig=checkambig)
                if fp.tell() < o:
                    raise error.Abort(
                        _(
                            b"attempted to truncate %s to %d bytes, but it was "
                            b"already %d bytes\n"
                        )
                        % (f, o, fp.tell())
                    )
                fp.truncate(o)
                fp.close()
            except IOError:
                report(_(b"failed to truncate %s\n") % f)
                raise
        else:
            try:
                opener.unlink(f)
            except FileNotFoundError:
                pass

    backupfiles = []
    for l, f, b, c in backupentries:
        if l not in vfsmap and c:
            report(b"couldn't handle %s: unknown cache location %s\n" % (b, l))
        vfs = vfsmap[l]
        try:
            if f and b:
                filepath = vfs.join(f)
                backuppath = vfs.join(b)
                checkambig = checkambigfiles and (f, l) in checkambigfiles
                try:
                    util.copyfile(backuppath, filepath, checkambig=checkambig)
                    backupfiles.append(b)
                except IOError as exc:
                    e_msg = stringutil.forcebytestr(exc)
                    report(_(b"failed to recover %s (%s)\n") % (f, e_msg))
            else:
                target = f or b
                try:
                    vfs.unlink(target)
                except FileNotFoundError:
                    pass
        except (IOError, OSError, error.Abort):
            if not c:
                raise

    backuppath = b"%s.backupfiles" % journal
    if opener.exists(backuppath):
        opener.unlink(backuppath)
    opener.unlink(journal)
    try:
        for f in backupfiles:
            if opener.exists(f):
                opener.unlink(f)
    except (IOError, OSError, error.Abort):
        # only pure backup file remains, it is sage to ignore any error
        pass


class transaction(util.transactional):
    def __init__(
        self,
        report,
        opener,
        vfsmap,
        journalname,
        undoname=None,
        after=None,
        createmode=None,
        validator=None,
        releasefn=None,
        checkambigfiles=None,
        name='<unnamed>',
    ):
        """Begin a new transaction

        Begins a new transaction that allows rolling back writes in the event of
        an exception.

        * `after`: called after the transaction has been committed
        * `createmode`: the mode