cborutil: implement sans I/O decoder
The vendored CBOR package decodes by calling read(n) on an object.
There are a number of disadvantages to this:
* Uses blocking I/O. If sufficient data is not available, the decoder
will hang until it is.
* No support for partial reads. If the read(n) returns less data than
requested, the decoder raises an error.
* Requires the use of a file like object. If the original data is in
say a buffer, we need to "cast" it to e.g. a BytesIO to appease the
decoder.
In addition, the vendored CBOR decoder doesn't provide flexibility
that we desire. Specifically:
* It buffers indefinite length bytestrings instead of streaming them.
* It doesn't allow limiting the set of types that can be decoded. This
property is useful when implementing a "hardened" decoder that is
less susceptible to abusive input.
* It doesn't provide sufficient "hook points" and introspection to
institute checks around behavior. These are useful for implementing
a "hardened" decoder.
This all adds up to a reasonable set of justifications for writing our
own decoder.
So, this commit implements our own CBOR decoder.
At the heart of the decoder is a function that decodes a single "item"
from a buffer. This item can be a complete simple value or a special
value, such as "start of array." Using this function, we can build a
decoder that effectively iterates over the stream of decoded items and
builds up higher-level values, such as arrays, maps, sets, and indefinite
length bytestrings. And we can do this without performing I/O in the
decoder itself.
The core of the sans I/O decoder will probably not be used directly.
Instead, it is expected that we'll build utility functions for invoking
the decoder given specific input types. This will allow extreme
flexibility in how data is delivered to the decoder.
I'm pretty happy with the state of the decoder modulo the TODO items
to track wanted features to help with a "hardened" decoder. The one
thing I could be convinced to change is the handling of semantic tags.
Since we only support a single semantic tag (sets), I thought it would
be easier to handle them inline in decodeitem(). This is simpler now.
But if we add support for other semantic tags, it will likely be easier
to move semantic tag handling outside of decodeitem(). But, properly
supporting semantic tags opens up a whole can of worms, as many
semantic tags imply new types. I'm optimistic we won't need these in
Mercurial. But who knows.
I'm also pretty happy with the test coverage. Writing comprehensive
tests for partial decoding did flush out a handful of bugs. One
general improvement to testing would be fuzz testing for partial
decoding. I may implement that later. I also anticipate switching the
wire protocol code to this new decoder will flush out any lingering
bugs.
Differential Revision: https://phab.mercurial-scm.org/D4414
test stripping of filelogs where the linkrev doesn't always increase
$ echo '[extensions]' >> $HGRCPATH
$ echo 'strip =' >> $HGRCPATH
$ hg init orig
$ cd orig
$ commit()
> {
> hg up -qC null
> count=1
> for i in "$@"; do
> for f in $i; do
> echo $count > $f
> done
> count=`expr $count + 1`
> done
> hg commit -qAm "$*"
> }
2 1 0 2 0 1 2
$ commit '201 210'
$ commit '102 120' '210'
$ commit '021'
$ commit '201' '021 120'
$ commit '012 021' '102 201' '120 210'
$ commit 'manifest-file'
$ commit '102 120' '012 210' '021 201'
$ commit '201 210' '021 120' '012 102'
$ HGUSER=another-user; export HGUSER
$ commit 'manifest-file'
$ commit '012' 'manifest-file'
$ cd ..
$ hg clone -q -U -r -1 -r -2 -r -3 -r -4 -r -6 orig crossed
$ cd crossed
$ hg debugindex --manifest
rev linkrev nodeid p1 p2
0 0 6f105cbb914d 000000000000 000000000000
1 3 1b55917b3699 000000000000 000000000000
2 1 8f3d04e263e5 000000000000 000000000000
3 2 f0ef8726ac4f 000000000000 000000000000
4 4 0b76e38b4070 000000000000 000000000000
$ for i in 012 021 102 120 201 210 manifest-file; do
> echo $i
> hg debugindex $i
> echo
> done
012
rev linkrev nodeid p1 p2
0 0 b8e02f643373 000000000000 000000000000
1 1 5d9299349fc0 000000000000 000000000000
2 2 2661d26c6496 000000000000 000000000000
021
rev linkrev nodeid p1 p2
0 0 b8e02f643373 000000000000 000000000000
1 2 5d9299349fc0 000000000000 000000000000
2 1 2661d26c6496 000000000000 000000000000
102
rev linkrev nodeid p1 p2
0 1 b8e02f643373 000000000000 000000000000
1 0 5d9299349fc0 000000000000 000000000000
2 2 2661d26c6496 000000000000 000000000000
120
rev linkrev nodeid p1 p2
0 1 b8e02f643373 000000000000 000000000000
1 2 5d9299349fc0 000000000000 000000000000
2 0 2661d26c6496 000000000000 000000000000
201
rev linkrev nodeid p1 p2
0 2 b8e02f643373 000000000000 000000000000
1 0 5d9299349fc0 000000000000 000000000000
2 1 2661d26c6496 000000000000 000000000000
210
rev linkrev nodeid p1 p2
0 2 b8e02f643373 000000000000 000000000000
1 1 5d9299349fc0 000000000000 000000000000
2 0 2661d26c6496 000000000000 000000000000
manifest-file
rev linkrev nodeid p1 p2
0 3 b8e02f643373 000000000000 000000000000
1 4 5d9299349fc0 000000000000 000000000000
$ cd ..
$ for i in 0 1 2 3 4; do
> hg clone -q -U --pull crossed $i
> echo "% Trying to strip revision $i"
> hg --cwd $i strip $i
> echo "% Verifying"
> hg --cwd $i verify
> echo
> done
% Trying to strip revision 0
saved backup bundle to $TESTTMP/0/.hg/strip-backup/*-backup.hg (glob)
% Verifying
checking changesets
checking manifests
crosschecking files in changesets and manifests
checking files
7 files, 4 changesets, 15 total revisions
% Trying to strip revision 1
saved backup bundle to $TESTTMP/1/.hg/strip-backup/*-backup.hg (glob)
% Verifying
checking changesets
checking manifests
crosschecking files in changesets and manifests
checking files
7 files, 4 changesets, 14 total revisions
% Trying to strip revision 2
saved backup bundle to $TESTTMP/2/.hg/strip-backup/*-backup.hg (glob)
% Verifying
checking changesets
checking manifests
crosschecking files in changesets and manifests
checking files
7 files, 4 changesets, 14 total revisions
% Trying to strip revision 3
saved backup bundle to $TESTTMP/3/.hg/strip-backup/*-backup.hg (glob)
% Verifying
checking changesets
checking manifests
crosschecking files in changesets and manifests
checking files
7 files, 4 changesets, 19 total revisions
% Trying to strip revision 4
saved backup bundle to $TESTTMP/4/.hg/strip-backup/*-backup.hg (glob)
% Verifying
checking changesets
checking manifests
crosschecking files in changesets and manifests
checking files
7 files, 4 changesets, 19 total revisions