hgext/fsmonitor/pywatchman/load.py
changeset 30656 16f4b341288d
child 43385 6469c23a40a2
equal deleted inserted replaced
30655:f35397fe0c04 30656:16f4b341288d
       
     1 # Copyright 2016 Facebook, Inc.
       
     2 # All rights reserved.
       
     3 #
       
     4 # Redistribution and use in source and binary forms, with or without
       
     5 # modification, are permitted provided that the following conditions are met:
       
     6 #
       
     7 #  * Redistributions of source code must retain the above copyright notice,
       
     8 #    this list of conditions and the following disclaimer.
       
     9 #
       
    10 #  * Redistributions in binary form must reproduce the above copyright notice,
       
    11 #    this list of conditions and the following disclaimer in the documentation
       
    12 #    and/or other materials provided with the distribution.
       
    13 #
       
    14 #  * Neither the name Facebook nor the names of its contributors may be used to
       
    15 #    endorse or promote products derived from this software without specific
       
    16 #    prior written permission.
       
    17 #
       
    18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
       
    19 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    20 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
       
    21 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
       
    22 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
       
    23 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
       
    24 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
       
    25 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
       
    26 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    28 
       
    29 from __future__ import absolute_import
       
    30 from __future__ import division
       
    31 from __future__ import print_function
       
    32 # no unicode literals
       
    33 
       
    34 try:
       
    35     from . import bser
       
    36 except ImportError:
       
    37     from . import pybser as bser
       
    38 
       
    39 import ctypes
       
    40 
       
    41 EMPTY_HEADER = b"\x00\x01\x05\x00\x00\x00\x00"
       
    42 
       
    43 
       
    44 def _read_bytes(fp, buf):
       
    45     """Read bytes from a file-like object
       
    46 
       
    47     @param fp: File-like object that implements read(int)
       
    48     @type fp: file
       
    49 
       
    50     @param buf: Buffer to read into
       
    51     @type buf: bytes
       
    52 
       
    53     @return: buf
       
    54     """
       
    55 
       
    56     # Do the first read without resizing the input buffer
       
    57     offset = 0
       
    58     remaining = len(buf)
       
    59     while remaining > 0:
       
    60         l = fp.readinto((ctypes.c_char * remaining).from_buffer(buf, offset))
       
    61         if l is None or l == 0:
       
    62             return offset
       
    63         offset += l
       
    64         remaining -= l
       
    65     return offset
       
    66 
       
    67 
       
    68 def load(fp, mutable=True, value_encoding=None, value_errors=None):
       
    69     """Deserialize a BSER-encoded blob.
       
    70 
       
    71     @param fp: The file-object to deserialize.
       
    72     @type file:
       
    73 
       
    74     @param mutable: Whether to return mutable results.
       
    75     @type mutable: bool
       
    76 
       
    77     @param value_encoding: Optional codec to use to decode values. If
       
    78                            unspecified or None, return values as bytestrings.
       
    79     @type value_encoding: str
       
    80 
       
    81     @param value_errors: Optional error handler for codec. 'strict' by default.
       
    82                          The other most common argument is 'surrogateescape' on
       
    83                          Python 3. If value_encoding is None, this is ignored.
       
    84     @type value_errors: str
       
    85     """
       
    86     buf = ctypes.create_string_buffer(8192)
       
    87     SNIFF_BUFFER_SIZE = len(EMPTY_HEADER)
       
    88     header = (ctypes.c_char * SNIFF_BUFFER_SIZE).from_buffer(buf)
       
    89     read_len = _read_bytes(fp, header)
       
    90     if read_len < len(header):
       
    91         return None
       
    92 
       
    93     total_len = bser.pdu_len(buf)
       
    94     if total_len > len(buf):
       
    95         ctypes.resize(buf, total_len)
       
    96 
       
    97     body = (ctypes.c_char * (total_len - len(header))).from_buffer(
       
    98         buf, len(header))
       
    99     read_len = _read_bytes(fp, body)
       
   100     if read_len < len(body):
       
   101         raise RuntimeError('bser data ended early')
       
   102 
       
   103     return bser.loads(
       
   104         (ctypes.c_char * total_len).from_buffer(buf, 0),
       
   105         mutable,
       
   106         value_encoding,
       
   107         value_errors)