MAJOR=4
MINOR=18
DEVNAME=tty18
  C   ˆl–äY>”JeJe@†à¸Û*bÑ¿�þ]�,Ü¡p¬ˆ$‘¦�üÿÃ„Îº/Ê_ŠI|¥�+Wé3ž¯ÉÈ ?÷     # blobstore.py - local and remote (speaking Git-LFS protocol) blob storages
#
# Copyright 2017 Facebook, Inc.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.


import contextlib
import errno
import hashlib
import json
import os
import re
import socket

from mercurial.i18n import _
from mercurial.pycompat import getattr
from mercurial.node import hex

from mercurial import (
    encoding,
    error,
    httpconnection as httpconnectionmod,
    pathutil,
    pycompat,
    url as urlmod,
    util,
    vfs as vfsmod,
    worker,
)

from mercurial.utils import (
    stringutil,
    urlutil,
)

from ..largefiles import lfutil

# 64 bytes for SHA256
_lfsre = re.compile(br'\A[a-f0-9]{64}\Z')


class lfsvfs(vfsmod.vfs):
    def join(self, path):
        """split the path at first two characters, like: XX/XXXXX..."""
        if not _lfsre.match(path):
            raise error.ProgrammingError(b'unexpected lfs path: %s' % path)
        return super(lfsvfs, self).join(path[0:2], path[2:])

    def walk(self, path=None, onerror=None):
        """Yield (dirpath, [], oids) tuple for blobs under path

        Oids only exist in the root of this vfs, so dirpath is always ''.
        """
        root = os.path.normpath(self.base)
        # when dirpath == root, dirpath[prefixlen:] becomes empty
        # because len(dirpath) < prefixlen.
        prefixlen = len(pathutil.normasprefix(root))
        oids = []

        for dirpath, dirs, files in os.walk(
            self.reljoin(self.base, path or b''), onerror=onerror
        ):
            dirpath = dirpath[prefixlen:]

            # Silently skip unexpected files and directories
            if len(dirpath) == 2:
                oids.extend(
                    [dirpath + f for f in files if _lfsre.match(dirpath + f)]
                )

        yield (b'', [], oids)


class nullvfs(lfsvfs):
    def __init__(self):
        pass

    def exists(self, oid):
        return False

    def read(self, oid):
        # store.read() calls into here if the blob doesn't exist in its
        # self.vfs.  Raise the same error as a normal vfs when asked to read a
        # file that doesn't exist.  The only difference is the full file path
        # isn't available in the error.
        raise IOError(
            errno.ENOENT,
            pycompat.sysstr(b'%s: No such file or directory' % oid),
        )

    def walk(self, path=None, onerror=None):
        return (b'', [], [])

    def write(self, oid, data):
        pass


class lfsuploadfile(httpconnectionmod.httpsendfile):
    """a file-like object that supports keepalive."""

    def __init__(self, ui, filename):
        super(lfsuploadfile, self).__init__(ui, filename, b'rb')
        self.read = self._data.read

    def _makeprogress(self):
        return None  # progress is handled by the worker client


class local:
    """Local blobstore for large file contents.

    This blobstore is used both as a cache and as a staging area for large blobs
    to be uploaded to the remote blobstore.
    """

    def __init__(self, repo):
        fullpath = repo.svfs.join(b'lfs/objects')
        self.vfs = lfsvfs(fullpath)

        if repo.ui.configbool(b'experimental', b'lfs.disableusercache'):
            self.cachevfs = nullvfs()
        else:
            usercache = lfutil._usercachedir(repo.ui, b'lfs')
            self.cachevfs = lfsvfs(usercache)
        self.ui = repo.ui

    def open(self, oid):
        """Open a read-only file descriptor to the named blob, in either the
        usercache or the local store."""
        return open(self.path(oid), 'rb')

    def path(self, oid):
        """Build the path for the given blob ``oid``.

        If the blob exists locally, the path may point to either the usercache
        or the local store.  If it doesn't, it will point to the local store.
        This is meant for situations where existing code that isn'