<commit_before>package pgtype_test

import (
	"testing"

	"github.com/jackc/pgtype"
	"github.com/jackc/pgtype/binary"
	errors "golang.org/x/xerrors"
)

type MyCompositeRaw struct {
	a int32
	b *string
}

func (src MyCompositeRaw) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) (newBuf []byte, err error) {
	a := pgtype.Int4{src.a, pgtype.Present}

	fieldBytes := make([]byte, 0, 64)
	fieldBytes, _ = a.EncodeBinary(ci, fieldBytes[:0])

	newBuf = binary.RecordStart(buf, 2)
	newBuf = binary.RecordAdd(newBuf, pgtype.Int4OID, fieldBytes)

	if src.b != nil {
		fieldBytes, _ = pgtype.Text{*src.b, pgtype.Present}.EncodeBinary(ci, fieldBytes[:0])
		newBuf = binary.RecordAdd(newBuf, pgtype.TextOID, fieldBytes)
	} else {
		newBuf = binary.RecordAddNull(newBuf, pgtype.TextOID)
	}
	return
}

func (dst *MyCompositeRaw) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error {
	a := pgtype.Int4{}
	b := pgtype.Text{}

	fieldIter, fieldCount, err := binary.NewRecordFieldIterator(src)
	if err != nil {
		return err
	}

	if 2 != fieldCount {
		return errors.Errorf("can't scan row value, number of fields don't match: found=%d expected=2", fieldCount)
	}

	_, fieldBytes, eof, err := fieldIter.Next()
	if eof || err != nil {
		return errors.New("Bad record")
	}
	if err = a.DecodeBinary(ci, fieldBytes); err != nil {
		return err
	}

	_, fieldBytes, eof, err = fieldIter.Next()
	if eof || err != nil {
		return errors.New("Bad record")
	}
	if err = b.DecodeBinary(ci, fieldBytes); err != nil {
		return err
	}

	dst.a = a.Int
	if b.Status == pgtype.Present {
		dst.b = &b.String
	} else {
		dst.b = nil
	}

	return nil
}

var x []byte

func BenchmarkBinaryEncodingManual(b *testing.B) {
	buf := make([]byte, 0, 128)
	ci := pgtype.NewConnInfo()
	v := MyCompositeRaw{4, ptrS("ABCDEFG")}

	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		buf, _ = v.EncodeBinary(ci, buf[:0])
	}
	x = buf
}

func BenchmarkBinaryEncodingHelper(b *testing.B) {
	buf := make([]byte, 0, 128)
	ci := pgtype.NewConnInfo()
	v := MyType{4, ptrS("ABCDEFG")}

	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		buf, _ = v.EncodeBinary(ci, buf[:0])
	}
	x = buf
}

func BenchmarkBinaryEncodingRow(b *testing.B) {
	buf := make([]byte, 0, 128)
	ci := pgtype.NewConnInfo()
	f1 := 2
	f2 := ptrS("bar")
	c := pgtype.NewComposite(&pgtype.Int4{}, &pgtype.Text{})

	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		c.SetFields(f1, f2)
		buf, _ = c.EncodeBinary(ci, buf[:0])
	}
	x = buf
}

var dstRaw MyCompositeRaw

func BenchmarkBinaryDecodingManual(b *testing.B) {
	ci := pgtype.NewConnInfo()
	buf, _ := MyType{4, ptrS("ABCDEFG")}.EncodeBinary(ci, nil)
	dst := MyCompositeRaw{}

	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		err := dst.DecodeBinary(ci, buf)
		E(err)
	}
	dstRaw = dst
}

var dstMyType MyType

func BenchmarkBinaryDecodingHelpers(b *testing.B) {
	ci := pgtype.NewConnInfo()
	buf, _ := MyType{4, ptrS("ABCDEFG")}.EncodeBinary(ci, nil)
	dst := MyType{}

	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		err := dst.DecodeBinary(ci, buf)
		E(err)
	}
	dstMyType = dst
}

var gf1 int
var gf2 *string

func BenchmarkBinaryDecodingCompositeScan(b *testing.B) {
	ci := pgtype.NewConnInfo()
	buf, _ := MyType{4, ptrS("ABCDEFG")}.EncodeBinary(ci, nil)
	var isNull bool
	var f1 int
	var f2 *string

	c := pgtype.NewComposite(&pgtype.Int4{}, &pgtype.Text{})

	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		err := c.Scan(&isNull, &f1, &f2).DecodeBinary(ci, buf)
		E(err)
	}
	gf1 = f1
	gf2 = f2
}
<commit_msg>Add JSON  benchmarks<commit_after>package pgtype_test

import (
	"testing"

	"github.com/jackc/pgtype"
	"github.com/jackc/pgtype/binary"
	errors "golang.org/x/xerrors"
)

type MyCompositeRaw struct {
	A int32
	B *string
}

func (src MyCompositeRaw) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) (newBuf []byte, err error) {
	a := pgtype.Int4{src.A, pgtype.Present}

	fieldBytes := make([]byte, 0, 64)
	fieldBytes, _ = a.EncodeBinary(ci, fieldBytes[:0])

	newBuf = binary.RecordStart(buf, 2)
	newBuf = binary.RecordAdd(newBuf, pgtype.Int4OID, fieldBytes)

	if src.B != nil {
		fieldBytes, _ = pgtype.Text{*src.B, pgtype.Present}.EncodeBinary(ci, fieldBytes[:0])
		newBuf = binary.RecordAdd(newBuf, pgtype.TextOID, fieldBytes)
	} else {
		newBuf = binary.RecordAddNull(newBuf, pgtype.TextOID)
	}
	return
}

func (dst *MyCompositeRaw) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error {
	a := pgtype.Int4{}
	b := pgtype.Text{}

	fieldIter, fieldCount, err := binary.NewRecordFieldIterator(src)
	if err != nil {
		return err
	}

	if 2 != fieldCount {
		return errors.Errorf("can't scan row value, number of fields don't match: found=%d expected=2", fieldCount)
	}

	_, fieldBytes, eof, err := fieldIter.Next()
	if eof || err != nil {
		return errors.New("Bad record")
	}
	if err = a.DecodeBinary(ci, fieldBytes); err != nil {
		return err
	}

	_, fieldBytes, eof, err = fieldIter.Next()
	if eof || err != nil {
		return errors.New("Bad record")
	}
	if err = b.DecodeBinary(ci, fieldBytes); err != nil {
		return err
	}

	dst.A = a.Int
	if b.Status == pgtype.Present {
		dst.B = &b.String
	} else {
		dst.B = nil
	}

	return nil
}

var x []byte

func BenchmarkBinaryEncodingManual(b *testing.B) {
	buf := make([]byte, 0, 128)
	ci := pgtype.NewConnInfo()
	v := MyCompositeRaw{4, ptrS("ABCDEFG")}

	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		buf, _ = v.EncodeBinary(ci, buf[:0])
	}
	x = buf
}

func BenchmarkBinaryEncodingHelper(b *testing.B) {
	buf := make([]byte, 0, 128)
	ci := pgtype.NewConnInfo()
	v := MyType{4, ptrS("ABCDEFG")}

	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		buf, _ = v.EncodeBinary(ci, buf[:0])
	}
	x = buf
}

func BenchmarkBinaryEncodingComposite(b *testing.B) {
	buf := make([]byte, 0, 128)
	ci := pgtype.NewConnInfo()
	f1 := 2
	f2 := ptrS("bar")
	c := pgtype.NewComposite(&pgtype.Int4{}, &pgtype.Text{})

	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		c.SetFields(f1, f2)
		buf, _ = c.EncodeBinary(ci, buf[:0])
	}
	x = buf
}

func BenchmarkBinaryEncodingJSON(b *testing.B) {
	buf := make([]byte, 0, 128)
	ci := pgtype.NewConnInfo()
	v := MyCompositeRaw{4, ptrS("ABCDEFG")}
	j := pgtype.JSON{}

	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		j.Set(v)
		buf, _ = j.EncodeBinary(ci, buf[:0])
	}
	x = buf
}

var dstRaw MyCompositeRaw

func BenchmarkBinaryDecodingManual(b *testing.B) {
	ci := pgtype.NewConnInfo()
	buf, _ := MyType{4, ptrS("ABCDEFG")}.EncodeBinary(ci, nil)
	dst := MyCompositeRaw{}

	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		err := dst.DecodeBinary(ci, buf)
		E(err)
	}
	dstRaw = dst
}

var dstMyType MyType

func BenchmarkBinaryDecodingHelpers(b *testing.B) {
	ci := pgtype.NewConnInfo()
	buf, _ := MyType{4, ptrS("ABCDEFG")}.EncodeBinary(ci, nil)
	dst := MyType{}

	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		err := dst.DecodeBinary(ci, buf)
		E(err)
	}
	dstMyType = dst
}

var gf1 int
var gf2 *string

func BenchmarkBinaryDecodingCompositeScan(b *testing.B) {
	ci := pgtype.NewConnInfo()
	buf, _ := MyType{4, ptrS("ABCDEFG")}.EncodeBinary(ci, nil)
	var isNull bool
	var f1 int
	var f2 *string

	c := pgtype.NewComposite(&pgtype.Int4{}, &pgtype.Text{})

	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		err := c.Scan(&isNull, &f1, &f2).DecodeBinary(ci, buf)
		E(err)
	}
	gf1 = f1
	gf2 = f2
}

func BenchmarkBinaryDecodingJSON(b *testing.B) {
	ci := pgtype.NewConnInfo()
	j := pgtype.JSON{}
	j.Set(MyCompositeRaw{4, ptrS("ABCDEFG")})
	buf, _ := j.EncodeBinary(ci, nil)

	j = pgtype.JSON{}
	dst := MyCompositeRaw{}

	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		err := j.DecodeBinary(ci, buf)
		E(err)
		err = j.AssignTo(&dst)
		E(err)
	}
	dstRaw = dst
}
<|endoftext|>


<commit_before>package dog

import "testing"

func TestValidDogfileName(t *testing.T) {
	for i, test := range []struct {
		input  string
		expect bool
	}{
		{"Dogfile.yml", true},
		{"Dogfile.yaml", true},
		{"Dogfile", true},
		{"🐕.yml", true},
		{"Dogfile-foo.yml", true},
		{"dogfile.yml", false},
		{"DogFile.yml:", false},
	} {
		if got, want := validDogfileName(test.input), test.expect; got != want {
			t.Errorf("Test %d (%s): expected %v but was %v", i, test.input, want, got)
		}
	}
}

func TestValidTaskName(t *testing.T) {
	for i, test := range []struct {
		input  string
		expect bool
	}{
		{"foo", true},
		{"foo-bar", true},
		{"01-with-02-numbers-03", true},
		{"-foo", false},
		{"foo-", false},
		{"-", false},
		{"camelCase", false},
		{"snake_case:", false},
		{"Some-Caps", false},
	} {
		if got, want := validTaskName(test.input), test.expect; got != want {
			t.Errorf("Test %d (%s): expected %v but was %v", i, test.input, want, got)
		}
	}
}

func TestDogfileParse(t *testing.T) {
	if _, err := Parse([]byte(`
- task: foo
  description: Foo task
  post: bar
  code: echo "foo"

- task: bar
  description: Bar task
  code: echo "bar"
`)); err != nil {
		t.Errorf("Failed parsing Dogfile from YAML")
	}
}

func TestDogfileParseDuplicatedTask(t *testing.T) {
	if _, err := Parse([]byte(`
- task: foo
  description: Foo task
  code: echo "foo"

- task: foo
  description: Foo task
  code: echo "foo"
`)); err == nil {
		t.Errorf("Failed to detect duplicated task name")
	}
}

func TestDogfileParsePreTasksArray(t *testing.T) {
	if _, err := Parse([]byte(`
- task: lorem
  description: Foo task
  pre:
    - ipsum
    - dolor
  code: echo "lorem"

- task: ipsum
  code: echo "ipsum"

- task: dolor
  code: echo "dolor"
`)); err != nil {
		t.Errorf("Failed to parse pre tasks array")
	}
}

func TestDogfileValidatePost(t *testing.T) {
	dogfile := Dogfile{
		Tasks: map[string]*Task{
			"foo": {
				Name:        "foo",
				Description: "Foo is a task that, it will trigger Bar when it finishes",
				Post:        []string{"bar"},
				Code:        "echo foo",
			},
			"bar": {
				Name: "bar",
				Code: "echo bar",
			},
		},
	}
	err := dogfile.Validate()
	if err != nil {
		t.Errorf("Failed validating a Dogfile with a post task")
	}
}

func TestDogfileValidatePostError(t *testing.T) {
	dogfile := Dogfile{
		Tasks: map[string]*Task{
			"foo": {
				Name:        "foo",
				Description: "Foo is a task that, it will trigger Bar when it finishes",
				Post:        []string{"bar"},
				Code:        "echo foo",
			},
		},
	}
	err := dogfile.Validate()
	if err == nil {
		t.Errorf("Failed, should have errored validating a Dogfile with an unexistent post task")
	}
}
<commit_msg>Print the actual error when test fail<commit_after>package dog

import "testing"

func TestValidDogfileName(t *testing.T) {
	for i, test := range []struct {
		input  string
		expect bool
	}{
		{"Dogfile.yml", true},
		{"Dogfile.yaml", true},
		{"Dogfile", true},
		{"🐕.yml", true},
		{"Dogfile-foo.yml", true},
		{"dogfile.yml", false},
		{"DogFile.yml:", false},
	} {
		if got, want := validDogfileName(test.input), test.expect; got != want {
			t.Errorf("Test %d (%s): expected %v but was %v", i, test.input, want, got)
		}
	}
}

func TestValidTaskName(t *testing.T) {
	for i, test := range []struct {
		input  string
		expect bool
	}{
		{"foo", true},
		{"foo-bar", true},
		{"01-with-02-numbers-03", true},
		{"-foo", false},
		{"foo-", false},
		{"-", false},
		{"camelCase", false},
		{"snake_case:", false},
		{"Some-Caps", false},
	} {
		if got, want := validTaskName(test.input), test.expect; got != want {
			t.Errorf("Test %d (%s): expected %v but was %v", i, test.input, want, got)
		}
	}
}

func TestDogfileParse(t *testing.T) {
	if _, err := Parse([]byte(`
- task: foo
  description: Foo task
  post: bar
  code: echo "foo"

- task: bar
  description: Bar task
  code: echo "bar"
`)); err != nil {
		t.Errorf("Failed parsing Dogfile from YAML: %s", err)
	}
}

func TestDogfileParseDuplicatedTask(t *testing.T) {
	if _, err := Parse([]byte(`
- task: foo
  description: Foo task
  code: echo "foo"

- task: foo
  description: Foo task
  code: echo "foo"
`)); err == nil {
		t.Errorf("Failed to detect duplicated task name")
	}
}

func TestDogfileParsePreTasksArray(t *testing.T) {
	if _, err := Parse([]byte(`
- task: lorem
  description: Foo task
  pre:
    - ipsum
    - dolor
  code: echo "lorem"

- task: ipsum
  code: echo "ipsum"

- task: dolor
  code: echo "dolor"
`)); err != nil {
		t.Errorf("Failed to parse pre tasks array: %s", err)
	}
}

func TestDogfileValidatePost(t *testing.T) {
	dogfile := Dogfile{
		Tasks: map[string]*Task{
			"foo": {
				Name:        "foo",
				Description: "Foo is a task that, it will trigger Bar when it finishes",
				Post:        []string{"bar"},
				Code:        "echo foo",
			},
			"bar": {
				Name: "bar",
				Code: "echo bar",
			},
		},
	}
	err := dogfile.Validate()
	if err != nil {
		t.Errorf("Failed validating a Dogfile with a post task: %s", err)
	}
}

func TestDogfileValidatePostError(t *testing.T) {
	dogfile := Dogfile{
		Tasks: map[string]*Task{
			"foo": {
				Name:        "foo",
				Description: "Foo is a task that, it will trigger Bar when it finishes",
				Post:        []string{"bar"},
				Code:        "echo foo",
			},
		},
	}
	err := dogfile.Validate()
	if err == nil {
		t.Errorf("Failed, should have errored validating a Dogfile with an unexistent post task")
	}
}
<|endoftext|>


<commit_before>package dns

import (
	"os"
	"time"
	"bufio"
	"strings"
	"testing"
	"crypto/rsa"
)

func TestSign(t *testing.T) {
	pub := "miek.nl. IN DNSKEY 256 3 5 AwEAAb+8lGNCxJgLS8rYVer6EnHVuIkQDghdjdtewDzU3G5R7PbMbKVRvH2Ma7pQyYceoaqWZQirSj72euPWfPxQnMy9ucCylA+FuH9cSjIcPf4PqJfdupHk9X6EBYjxrCLY4p1/yBwgyBIRJtZtAqM3ceAH2WovEJD6rTtOuHo5AluJ"

	priv := `Private-key-format: v1.3
Algorithm: 5 (RSASHA1)
Modulus: v7yUY0LEmAtLythV6voScdW4iRAOCF2N217APNTcblHs9sxspVG8fYxrulDJhx6hqpZlCKtKPvZ649Z8/FCczL25wLKUD4W4f1xKMhw9/g+ol926keT1foQFiPGsItjinX/IHCDIEhEm1m0Cozdx4AfZai8QkPqtO064ejkCW4k=
PublicExponent: AQAB
PrivateExponent: YPwEmwjk5HuiROKU4xzHQ6l1hG8Iiha4cKRG3P5W2b66/EN/GUh07ZSf0UiYB67o257jUDVEgwCuPJz776zfApcCB4oGV+YDyEu7Hp/rL8KcSN0la0k2r9scKwxTp4BTJT23zyBFXsV/1wRDK1A5NxsHPDMYi2SoK63Enm/1ptk=
Prime1: /wjOG+fD0ybNoSRn7nQ79udGeR1b0YhUA5mNjDx/x2fxtIXzygYk0Rhx9QFfDy6LOBvz92gbNQlzCLz3DJt5hw==
Prime2: wHZsJ8OGhkp5p3mrJFZXMDc2mbYusDVTA+t+iRPdS797Tj0pjvU2HN4vTnTj8KBQp6hmnY7dLp9Y1qserySGbw==
Exponent1: N0A7FsSRIg+IAN8YPQqlawoTtG1t1OkJ+nWrurPootScApX6iMvn8fyvw3p2k51rv84efnzpWAYiC8SUaQDNxQ==
Exponent2: SvuYRaGyvo0zemE3oS+WRm2scxR8eiA8WJGeOc+obwOKCcBgeZblXzfdHGcEC1KaOcetOwNW/vwMA46lpLzJNw==
Coefficient: 8+7ZN/JgByqv0NfULiFKTjtyegUcijRuyij7yNxYbCBneDvZGxJwKNi4YYXWx743pcAj4Oi4Oh86gcmxLs+hGw==
Created: 20110302104537
Publish: 20110302104537
Activate: 20110302104537`

	k := new(RR_DNSKEY)
	k.Read(strings.NewReader(pub))
	p, err := k.ReadPrivateKey(strings.NewReader(priv))
	if err != nil {
		t.Logf("%v\n", err)
		t.Fail()
	}
	switch priv := p.(type) {
	case *rsa.PrivateKey:
		if 65537 != priv.PublicKey.E {
			t.Log("Exponenent should be 65537")
			t.Fail()
		}
	default:
		t.Logf("We should have read an RSA key: %v", priv)
		t.Fail()
	}
	if k.KeyTag() != 37350 {
		t.Logf("%d %v\n", k.KeyTag(), k)
		t.Log("Keytag should be 37350")
		t.Fail()
	}

	soa := new(RR_SOA)
	soa.Hdr = RR_Header{"miek.nl.", TypeSOA, ClassINET, 14400, 0}
	soa.Ns = "open.nlnetlabs.nl."
	soa.Mbox = "miekg.atoom.net."
	soa.Serial = 1293945905
	soa.Refresh = 14400
	soa.Retry = 3600
	soa.Expire = 604800
	soa.Minttl = 86400

	sig := new(RR_RRSIG)
	sig.Hdr = RR_Header{"miek.nl.", TypeRRSIG, ClassINET, 14400, 0}
	sig.Expiration = 1296534305 // date -u '+%s' -d"2011-02-01 04:25:05"
	sig.Inception = 1293942305  // date -u '+%s' -d"2011-01-02 04:25:05"
	sig.KeyTag = k.KeyTag()
	sig.SignerName = k.Hdr.Name
	sig.Algorithm = k.Algorithm

	sig.Sign(p, []RR{soa})
	if sig.Signature != "D5zsobpQcmMmYsUMLxCVEtgAdCvTu8V/IEeP4EyLBjqPJmjt96bwM9kqihsccofA5LIJ7DN91qkCORjWSTwNhzCv7bMyr2o5vBZElrlpnRzlvsFIoAZCD9xg6ZY7ZyzUJmU6IcTwG4v3xEYajcpbJJiyaw/RqR90MuRdKPiBzSo=" {
		t.Log("Signature is not correct")
		t.Logf("%v\n", sig)
		t.Fail()
	}
}

func TestDotInName(t *testing.T) {
	buf := make([]byte, 20)
	packDomainName("aa\\.bb.nl.", buf, 0)
	// index 3 must be a real dot
	if buf[3] != '.' {
		t.Log("Dot should be a real dot")
		t.Fail()
	}

	if buf[6] != 2 {
		t.Log("This must have the value 2")
		t.Fail()
	}
	dom, _, _ := unpackDomainName(buf, 0)
	// printing it should yield the backspace again
	if dom != "aa\\.bb.nl." {
		t.Log("Dot should have been escaped: " + dom)
		t.Fail()
	}
}

// Make this a decend test case. For now, good enough
// New style (Ragel) parsing
func TestParse(t *testing.T) {
	tests := map[string]string{
		"miek.nl. 3600 IN A 127.0.0.1":               "miek.nl.\t3600\tIN\tA\t127.0.0.1",
		"miek.nl. 3600 IN MX 10 elektron.atoom.net.": "miek.nl.\t3600\tIN\tMX\t10 elektron.atoom.net.",
		"miek.nl. IN 3600 A 127.0.0.1":               "miek.nl.\t3600\tIN\tA\t127.0.0.1",
		"miek.nl. A 127.0.0.1":                       "miek.nl.\t0\tCLASS0\tA\t127.0.0.1",
		"miek.nl. IN AAAA ::1":                       "miek.nl.\t0\tIN\tAAAA\t::1",
		"miek.nl. IN A 127.0.0.1":                    "miek.nl.\t0\tIN\tA\t127.0.0.1",
		"miek.nl. IN DNSKEY 256 3 5 AwEAAb+8lGNCxJgLS8rYVer6EnHVuIkQDghdjdtewDzU3G5R7PbMbKVRvH2Ma7pQyYceoaqWZQirSj72euPWfPxQnMy9ucCylA+FuH9cSjIcPf4PqJfdupHk9X6EBYjxrCLY4p1/yBwgyBIRJtZtAqM3ceAH2WovEJD6rTtOuHo5AluJ":           "miek.nl.\t0\tIN\tDNSKEY\t256 3 5 AwEAAb+8lGNCxJgLS8rYVer6EnHVuIkQDghdjdtewDzU3G5R7PbMbKVRvH2Ma7pQyYceoaqWZQirSj72euPWfPxQnMy9ucCylA+FuH9cSjIcPf4PqJfdupHk9X6EBYjxrCLY4p1/yBwgyBIRJtZtAqM3ceAH2WovEJD6rTtOuHo5AluJ",
		"nlnetlabs.nl. 3175 IN DNSKEY 256 3 8 AwEAAdR7XR95OaAN9Rz7TbtPalQ9guQk7zfxTHYNKhsiwTZA9z+F16nD0VeBlk7dNik3ETpT2GLAwr9sntG898JwurCDe353wHPvjZtMCdiTVp3cRCrjuCEvoFpmZNN82H0gaH/4v8mkv/QBDAkDSncYjz/FqHKAeYy3cMcjY6RyVweh": "nlnetlabs.nl.\t3175\tIN\tDNSKEY\t256 3 8 AwEAAdR7XR95OaAN9Rz7TbtPalQ9guQk7zfxTHYNKhsiwTZA9z+F16nD0VeBlk7dNik3ETpT2GLAwr9sntG898JwurCDe353wHPvjZtMCdiTVp3cRCrjuCEvoFpmZNN82H0gaH/4v8mkv/QBDAkDSncYjz/FqHKAeYy3cMcjY6RyVweh",
                "dnsex.nl.  86400 IN SOA  elektron.atoom.net. miekg.atoom.net. 1299256910 14400 3600 604800 86400": "dnsex.nl.\t86400\tIN\tSOA\telektron.atoom.net. miekg.atoom.net. 1299256910 14400 3600 604800 86400",
                "dnsex.nl.  86400 IN RRSIG    SOA 8 2 86400 20110403154150 20110304154150 23334 dnsex.nl. QN6hwJQLEBqRVKmO2LgkuRSx9bkKIZxXlTVtHg5SaiN+8RCTckGtUXkQ vmZiBt3RdIWAjaabQYpEZHgvyjfy4Wwu/9RPDYnLt/qoyr4QKAdujchc m+fMDSbbcC7AN08i5D/dUWfNOHXjRJLY7t7AYB9DBt32LazIb0EU9QiW 5Cg=": "dnsex.nl.\t86400\tIN\tRRSIG\tSOA 8 2 86400 20110403154150 20110304154150 23334 dnsex.nl. QN6hwJQLEBqRVKmO2LgkuRSx9bkKIZxXlTVtHg5SaiN+8RCTckGtUXkQvmZiBt3RdIWAjaabQYpEZHgvyjfy4Wwu/9RPDYnLt/qoyr4QKAdujchcm+fMDSbbcC7AN08i5D/dUWfNOHXjRJLY7t7AYB9DBt32LazIb0EU9QiW5Cg=",
	}
	for test, result := range tests {
		p := NewParser(strings.NewReader(test))
		z, err := p.Zone()
		if err != nil {
			t.Logf("Error of nil r %v %s\n", err, test)
			t.Fail()
		}
                r := z.PopRR()
                if r == nil {
                        t.Log("Empty RR")
                        t.Fail()
                } else {
                        if r.(RR).String() != result {
                                t.Logf("\"%s\" should be equal to\n\"%s\"\n", r, result)
                                t.Fail()
                        }
                }
	}
}

func TestParseFailure(t *testing.T) {
        tests := []string{"miek.nl. IN A 327.0.0.1",
	        "miek.nl. IN AAAA ::x",
	        "miek.nl. IN MX a0 miek.nl.",
        }
// Tests that make me crash
//                "miek.nl aap IN MX mx.miek.nl.",
//	        "miek.nl. IN CNAME ",
//	        "miek.nl. PA MX 10 miek.nl.",

        for _, t1 := range tests {
	        _, err := NewRRString(t1)
                if err == nil {
                        t.Log("Should have triggered an error")
                        t.Fail()
                }
                t.Logf("%s: %s\n", t1, err.String())
        }
}

func BenchmarkZoneParsing(b *testing.B) {
	file, err := os.Open("miek.nl.signed_test")
	defer file.Close()
	if err != nil {
		return
	}
	p := NewParser(bufio.NewReader(file))
	p.Zone()
}

func TestZoneParsing(t *testing.T) {
	file, err := os.Open("miek.nl.signed_test")
	defer file.Close()
	if err != nil {
		return
	}
	p := NewParser(bufio.NewReader(file))
	start := time.Nanoseconds()
	z, err := p.Zone()
	if err != nil {
		t.Logf("error %v\n", err.String())
		t.Fail()
	}
	delta := time.Nanoseconds() - start
        t.Logf("%s", z.Nxt)
	t.Logf("%d RRs parsed in %.2f s (%.2f RR/s)", z.Len(), float32(delta)/1e9, float32(z.Len())/(float32(delta)/1e9))
}
<commit_msg>Tests fails, but dont crash<commit_after>package dns

import (
	"os"
	"time"
	"bufio"
	"strings"
	"testing"
	"crypto/rsa"
)

func TestSign(t *testing.T) {
	pub := "miek.nl. IN DNSKEY 256 3 5 AwEAAb+8lGNCxJgLS8rYVer6EnHVuIkQDghdjdtewDzU3G5R7PbMbKVRvH2Ma7pQyYceoaqWZQirSj72euPWfPxQnMy9ucCylA+FuH9cSjIcPf4PqJfdupHk9X6EBYjxrCLY4p1/yBwgyBIRJtZtAqM3ceAH2WovEJD6rTtOuHo5AluJ"

	priv := `Private-key-format: v1.3
Algorithm: 5 (RSASHA1)
Modulus: v7yUY0LEmAtLythV6voScdW4iRAOCF2N217APNTcblHs9sxspVG8fYxrulDJhx6hqpZlCKtKPvZ649Z8/FCczL25wLKUD4W4f1xKMhw9/g+ol926keT1foQFiPGsItjinX/IHCDIEhEm1m0Cozdx4AfZai8QkPqtO064ejkCW4k=
PublicExponent: AQAB
PrivateExponent: YPwEmwjk5HuiROKU4xzHQ6l1hG8Iiha4cKRG3P5W2b66/EN/GUh07ZSf0UiYB67o257jUDVEgwCuPJz776zfApcCB4oGV+YDyEu7Hp/rL8KcSN0la0k2r9scKwxTp4BTJT23zyBFXsV/1wRDK1A5NxsHPDMYi2SoK63Enm/1ptk=
Prime1: /wjOG+fD0ybNoSRn7nQ79udGeR1b0YhUA5mNjDx/x2fxtIXzygYk0Rhx9QFfDy6LOBvz92gbNQlzCLz3DJt5hw==
Prime2: wHZsJ8OGhkp5p3mrJFZXMDc2mbYusDVTA+t+iRPdS797Tj0pjvU2HN4vTnTj8KBQp6hmnY7dLp9Y1qserySGbw==
Exponent1: N0A7FsSRIg+IAN8YPQqlawoTtG1t1OkJ+nWrurPootScApX6iMvn8fyvw3p2k51rv84efnzpWAYiC8SUaQDNxQ==
Exponent2: SvuYRaGyvo0zemE3oS+WRm2scxR8eiA8WJGeOc+obwOKCcBgeZblXzfdHGcEC1KaOcetOwNW/vwMA46lpLzJNw==
Coefficient: 8+7ZN/JgByqv0NfULiFKTjtyegUcijRuyij7yNxYbCBneDvZGxJwKNi4YYXWx743pcAj4Oi4Oh86gcmxLs+hGw==
Created: 20110302104537
Publish: 20110302104537
Activate: 20110302104537`

	k := new(RR_DNSKEY)
	k.Read(strings.NewReader(pub))
	p, err := k.ReadPrivateKey(strings.NewReader(priv))
	if err != nil {
		t.Logf("%v\n", err)
		t.Fail()
	}
	switch priv := p.(type) {
	case *rsa.PrivateKey:
		if 65537 != priv.PublicKey.E {
			t.Log("Exponenent should be 65537")
			t.Fail()
		}
	default:
		t.Logf("We should have read an RSA key: %v", priv)
		t.Fail()
	}
	if k.KeyTag() != 37350 {
		t.Logf("%d %v\n", k.KeyTag(), k)
		t.Log("Keytag should be 37350")
		t.Fail()
	}

	soa := new(RR_SOA)
	soa.Hdr = RR_Header{"miek.nl.", TypeSOA, ClassINET, 14400, 0}
	soa.Ns = "open.nlnetlabs.nl."
	soa.Mbox = "miekg.atoom.net."
	soa.Serial = 1293945905
	soa.Refresh = 14400
	soa.Retry = 3600
	soa.Expire = 604800
	soa.Minttl = 86400

	sig := new(RR_RRSIG)
	sig.Hdr = RR_Header{"miek.nl.", TypeRRSIG, ClassINET, 14400, 0}
	sig.Expiration = 1296534305 // date -u '+%s' -d"2011-02-01 04:25:05"
	sig.Inception = 1293942305  // date -u '+%s' -d"2011-01-02 04:25:05"
	sig.KeyTag = k.KeyTag()
	sig.SignerName = k.Hdr.Name
	sig.Algorithm = k.Algorithm

	sig.Sign(p, []RR{soa})
	if sig.Signature != "D5zsobpQcmMmYsUMLxCVEtgAdCvTu8V/IEeP4EyLBjqPJmjt96bwM9kqihsccofA5LIJ7DN91qkCORjWSTwNhzCv7bMyr2o5vBZElrlpnRzlvsFIoAZCD9xg6ZY7ZyzUJmU6IcTwG4v3xEYajcpbJJiyaw/RqR90MuRdKPiBzSo=" {
		t.Log("Signature is not correct")
		t.Logf("%v\n", sig)
		t.Fail()
	}
}

func TestDotInName(t *testing.T) {
	buf := make([]byte, 20)
	packDomainName("aa\\.bb.nl.", buf, 0)
	// index 3 must be a real dot
	if buf[3] != '.' {
		t.Log("Dot should be a real dot")
		t.Fail()
	}

	if buf[6] != 2 {
		t.Log("This must have the value 2")
		t.Fail()
	}
	dom, _, _ := unpackDomainName(buf, 0)
	// printing it should yield the backspace again
	if dom != "aa\\.bb.nl." {
		t.Log("Dot should have been escaped: " + dom)
		t.Fail()
	}
}

// Make this a decend test case. For now, good enough
// New style (Ragel) parsing
func TestParse(t *testing.T) {
	tests := map[string]string{
		"miek.nl. 3600 IN A 127.0.0.1":               "miek.nl.\t3600\tIN\tA\t127.0.0.1",
		"miek.nl. 3600 IN MX 10 elektron.atoom.net.": "miek.nl.\t3600\tIN\tMX\t10 elektron.atoom.net.",
		"miek.nl. IN 3600 A 127.0.0.1":               "miek.nl.\t3600\tIN\tA\t127.0.0.1",
		"miek.nl. A 127.0.0.1":                       "miek.nl.\t0\tCLASS0\tA\t127.0.0.1",
		"miek.nl. IN AAAA ::1":                       "miek.nl.\t0\tIN\tAAAA\t::1",
		"miek.nl. IN A 127.0.0.1":                    "miek.nl.\t0\tIN\tA\t127.0.0.1",
		"miek.nl. IN DNSKEY 256 3 5 AwEAAb+8lGNCxJgLS8rYVer6EnHVuIkQDghdjdtewDzU3G5R7PbMbKVRvH2Ma7pQyYceoaqWZQirSj72euPWfPxQnMy9ucCylA+FuH9cSjIcPf4PqJfdupHk9X6EBYjxrCLY4p1/yBwgyBIRJtZtAqM3ceAH2WovEJD6rTtOuHo5AluJ":           "miek.nl.\t0\tIN\tDNSKEY\t256 3 5 AwEAAb+8lGNCxJgLS8rYVer6EnHVuIkQDghdjdtewDzU3G5R7PbMbKVRvH2Ma7pQyYceoaqWZQirSj72euPWfPxQnMy9ucCylA+FuH9cSjIcPf4PqJfdupHk9X6EBYjxrCLY4p1/yBwgyBIRJtZtAqM3ceAH2WovEJD6rTtOuHo5AluJ",
		"nlnetlabs.nl. 3175 IN DNSKEY 256 3 8 AwEAAdR7XR95OaAN9Rz7TbtPalQ9guQk7zfxTHYNKhsiwTZA9z+F16nD0VeBlk7dNik3ETpT2GLAwr9sntG898JwurCDe353wHPvjZtMCdiTVp3cRCrjuCEvoFpmZNN82H0gaH/4v8mkv/QBDAkDSncYjz/FqHKAeYy3cMcjY6RyVweh": "nlnetlabs.nl.\t3175\tIN\tDNSKEY\t256 3 8 AwEAAdR7XR95OaAN9Rz7TbtPalQ9guQk7zfxTHYNKhsiwTZA9z+F16nD0VeBlk7dNik3ETpT2GLAwr9sntG898JwurCDe353wHPvjZtMCdiTVp3cRCrjuCEvoFpmZNN82H0gaH/4v8mkv/QBDAkDSncYjz/FqHKAeYy3cMcjY6RyVweh",
                "dnsex.nl.  86400 IN SOA  elektron.atoom.net. miekg.atoom.net. 1299256910 14400 3600 604800 86400": "dnsex.nl.\t86400\tIN\tSOA\telektron.atoom.net. miekg.atoom.net. 1299256910 14400 3600 604800 86400",
                "dnsex.nl.  86400 IN RRSIG    SOA 8 2 86400 20110403154150 20110304154150 23334 dnsex.nl. QN6hwJQLEBqRVKmO2LgkuRSx9bkKIZxXlTVtHg5SaiN+8RCTckGtUXkQ vmZiBt3RdIWAjaabQYpEZHgvyjfy4Wwu/9RPDYnLt/qoyr4QKAdujchc m+fMDSbbcC7AN08i5D/dUWfNOHXjRJLY7t7AYB9DBt32LazIb0EU9QiW 5Cg=": "dnsex.nl.\t86400\tIN\tRRSIG\tSOA 8 2 86400 20110403154150 20110304154150 23334 dnsex.nl. QN6hwJQLEBqRVKmO2LgkuRSx9bkKIZxXlTVtHg5SaiN+8RCTckGtUXkQvmZiBt3RdIWAjaabQYpEZHgvyjfy4Wwu/9RPDYnLt/qoyr4QKAdujchcm+fMDSbbcC7AN08i5D/dUWfNOHXjRJLY7t7AYB9DBt32LazIb0EU9QiW5Cg=",
	}
	for test, result := range tests {
		p := NewParser(strings.NewReader(test))
		z, err := p.Zone()
		if err != nil {
			t.Logf("Error of nil r %v %s\n", err, test)
			t.Fail()
		}
                r := z.PopRR()
                if r == nil {
                        t.Log("Empty RR")
                        t.Fail()
                } else {
                        if r.(RR).String() != result {
                                t.Logf("\"%s\" should be equal to\n\"%s\"\n", r, result)
                                t.Fail()
                        }
                }
	}
}

func TestParseFailure(t *testing.T) {
        tests := []string{"miek.nl. IN A 327.0.0.1",
	        "miek.nl. IN AAAA ::x",
	        "miek.nl. IN MX a0 miek.nl.",
        }
// Tests that make me crash
//                "miek.nl aap IN MX mx.miek.nl.",
//	        "miek.nl. IN CNAME ",
//	        "miek.nl. PA MX 10 miek.nl.",

        for _, t1 := range tests {
	        _, err := NewRRString(t1)
                if err == nil {
                        t.Log("Should have triggered an error")
                        t.Fail()
                } else {
                        t.Logf("%s: %s\n", t1, err.String())
                }
        }
}

func BenchmarkZoneParsing(b *testing.B) {
	file, err := os.Open("miek.nl.signed_test")
	defer file.Close()
	if err != nil {
		return
	}
	p := NewParser(bufio.NewReader(file))
	p.Zone()
}

func TestZoneParsing(t *testing.T) {
	file, err := os.Open("miek.nl.signed_test")
	defer file.Close()
	if err != nil {
		return
	}
	p := NewParser(bufio.NewReader(file))
	start := time.Nanoseconds()
	z, err := p.Zone()
	if err != nil {
		t.Logf("error %v\n", err.String())
		t.Fail()
	}
	delta := time.Nanoseconds() - start
        if z != nil {
                t.Logf("%s", z.Nxt)
                t.Logf("%d RRs parsed in %.2f s (%.2f RR/s)", z.Len(), float32(delta)/1e9, float32(z.Len())/(float32(delta)/1e9))
        }
}
<|endoftext|>


<commit_before>// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package gce

import (
	"code.google.com/p/google-api-go-client/compute/v1"
	"github.com/juju/errors"

	"github.com/juju/juju/instance"
	"github.com/juju/juju/network"
)

type environInstance struct {
	id   instance.Id
	env  *environ
	zone string

	gce *compute.Instance
}

var _ instance.Instance = (*environInstance)(nil)

func newInstance(raw *compute.Instance, env *environ) *environInstance {
	inst := environInstance{
		id:   instance.Id(raw.Name),
		env:  env,
		zone: zoneName(raw),
	}
	inst.update(env, raw)
	return &inst
}

func (inst *environInstance) Id() instance.Id {
	return inst.id
}

func (inst *environInstance) Status() string {
	return inst.gce.Status
}

func (inst *environInstance) update(env *environ, newInst *compute.Instance) {
	inst.gce = newInst
}

func (inst *environInstance) Refresh() error {
	env := inst.env.getSnapshot()

	gInst, err := env.gce.instance(inst.zone, string(inst.id))
	if err != nil {
		return errors.Trace(err)
	}

	inst.update(env, gInst)
	return nil
}

func (inst *environInstance) Addresses() ([]network.Address, error) {
	var addresses []network.Address

	for _, netif := range inst.gce.NetworkInterfaces {
		// Add public addresses.
		for _, accessConfig := range netif.AccessConfigs {
			if accessConfig.NatIP == "" {
				continue
			}
			address := network.Address{
				Value: accessConfig.NatIP,
				Type:  network.IPv4Address,
				Scope: network.ScopePublic,
			}
			addresses = append(addresses, address)

		}

		// Add private address.
		if netif.NetworkIP == "" {
			continue
		}
		address := network.Address{
			Value: netif.NetworkIP,
			Type:  network.IPv4Address,
			Scope: network.ScopeCloudLocal,
		}
		addresses = append(addresses, address)
	}

	return addresses, nil
}

func findInst(id instance.Id, instances []instance.Instance) instance.Instance {
	for _, inst := range instances {
		if id == inst.Id() {
			return inst
		}
	}
	return nil
}

// firewall stuff

// OpenPorts opens the given ports on the instance, which
// should have been started with the given machine id.
func (inst *environInstance) OpenPorts(machineId string, ports []network.PortRange) error {
	env := inst.env.getSnapshot()
	err := env.openPorts(machineId, ports)
	return errors.Trace(err)
}

// ClosePorts closes the given ports on the instance, which
// should have been started with the given machine id.
func (inst *environInstance) ClosePorts(machineId string, ports []network.PortRange) error {
	env := inst.env.getSnapshot()
	err := env.closePorts(machineId, ports)
	return errors.Trace(err)
}

// Ports returns the set of ports open on the instance, which
// should have been started with the given machine id.
// The ports are returned as sorted by SortPorts.
func (inst *environInstance) Ports(machineId string) ([]network.PortRange, error) {
	env := inst.env.getSnapshot()
	ports, err := env.ports(machineId)
	return ports, errors.Trace(err)
}
<commit_msg>provider/gce: Drop the env parameter.<commit_after>// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package gce

import (
	"code.google.com/p/google-api-go-client/compute/v1"
	"github.com/juju/errors"

	"github.com/juju/juju/instance"
	"github.com/juju/juju/network"
)

type environInstance struct {
	id   instance.Id
	env  *environ
	zone string

	gce *compute.Instance
}

var _ instance.Instance = (*environInstance)(nil)

func newInstance(raw *compute.Instance, env *environ) *environInstance {
	inst := environInstance{
		id:   instance.Id(raw.Name),
		env:  env,
		zone: zoneName(raw),
	}
	inst.update(raw)
	return &inst
}

func (inst *environInstance) Id() instance.Id {
	return inst.id
}

func (inst *environInstance) Status() string {
	return inst.gce.Status
}

func (inst *environInstance) update(newInst *compute.Instance) {
	inst.gce = newInst
}

func (inst *environInstance) Refresh() error {
	env := inst.env.getSnapshot()

	raw, err := env.gce.instance(inst.zone, string(inst.id))
	if err != nil {
		return errors.Trace(err)
	}

	inst.update(raw)
	return nil
}

func (inst *environInstance) Addresses() ([]network.Address, error) {
	var addresses []network.Address

	for _, netif := range inst.gce.NetworkInterfaces {
		// Add public addresses.
		for _, accessConfig := range netif.AccessConfigs {
			if accessConfig.NatIP == "" {
				continue
			}
			address := network.Address{
				Value: accessConfig.NatIP,
				Type:  network.IPv4Address,
				Scope: network.ScopePublic,
			}
			addresses = append(addresses, address)

		}

		// Add private address.
		if netif.NetworkIP == "" {
			continue
		}
		address := network.Address{
			Value: netif.NetworkIP,
			Type:  network.IPv4Address,
			Scope: network.ScopeCloudLocal,
		}
		addresses = append(addresses, address)
	}

	return addresses, nil
}

func findInst(id instance.Id, instances []instance.Instance) instance.Instance {
	for _, inst := range instances {
		if id == inst.Id() {
			return inst
		}
	}
	return nil
}

// firewall stuff

// OpenPorts opens the given ports on the instance, which
// should have been started with the given machine id.
func (inst *environInstance) OpenPorts(machineId string, ports []network.PortRange) error {
	env := inst.env.getSnapshot()
	err := env.openPorts(machineId, ports)
	return errors.Trace(err)
}

// ClosePorts closes the given ports on the instance, which
// should have been started with the given machine id.
func (inst *environInstance) ClosePorts(machineId string, ports []network.PortRange) error {
	env := inst.env.getSnapshot()
	err := env.closePorts(machineId, ports)
	return errors.Trace(err)
}

// Ports returns the set of ports open on the instance, which
// should have been started with the given machine id.
// The ports are returned as sorted by SortPorts.
func (inst *environInstance) Ports(machineId string) ([]network.PortRange, error) {
	env := inst.env.getSnapshot()
	ports, err := env.ports(machineId)
	return ports, errors.Trace(err)
}
<|endoftext|>


<commit_before>// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

// +build !gccgo

package vsphere

import (
	"github.com/juju/utils/featureflag"

	"github.com/juju/juju/environs"
	"github.com/juju/juju/feature"
	"github.com/juju/juju/juju/osenv"
	"github.com/juju/juju/storage/provider/registry"
)

const (
	providerType = "vsphere"
)

func init() {
	featureflag.SetFlagsFromEnvironment(osenv.JujuFeatureFlagEnvKey)
	if featureflag.Enabled(feature.VSphereProvider) {
		environs.RegisterProvider(providerType, providerInstance)
		registry.RegisterEnvironStorageProviders(providerType)
	}
}
<commit_msg>Drop feature flag handling for vsphere.<commit_after>// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

// +build !gccgo

package vsphere

import (
	"github.com/juju/juju/environs"
	"github.com/juju/juju/storage/provider/registry"
)

const (
	providerType = "vsphere"
)

func init() {
	environs.RegisterProvider(providerType, providerInstance)
	registry.RegisterEnvironStorageProviders(providerType)
}
<|endoftext|>


<commit_before>package proxy

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"net"
	"net/http"
	"net/http/httputil"
	"sync"

	"github.com/fsouza/go-dockerclient"
)

func (proxy *Proxy) Intercept(i interceptor, w http.ResponseWriter, r *http.Request) {
	if err := i.InterceptRequest(r); err != nil {
		switch err.(type) {
		case *docker.NoSuchContainer:
			http.Error(w, err.Error(), http.StatusNotFound)
		case *ErrNoSuchImage:
			http.Error(w, err.Error(), http.StatusNotFound)
		default:
			http.Error(w, err.Error(), http.StatusInternalServerError)
			Log.Warning("Error intercepting request: ", err)
		}
		return
	}

	conn, err := proxy.Dial()
	if err != nil {
		http.Error(w, "Could not connect to target", http.StatusInternalServerError)
		Log.Warning(err)
		return
	}
	client := httputil.NewClientConn(conn, nil)
	defer client.Close()

	resp, err := client.Do(r)
	if err != nil && err != httputil.ErrPersistEOF {
		http.Error(w, fmt.Sprintf("Could not make request to target: %v", err), http.StatusInternalServerError)
		Log.Warning("Error forwarding request: ", err)
		return
	}
	err = i.InterceptResponse(resp)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		Log.Warning("Error intercepting response: ", err)
		return
	}

	hdr := w.Header()
	for k, vs := range resp.Header {
		for _, v := range vs {
			hdr.Add(k, v)
		}
	}
	Log.Debugf("Response from target: %s %v", resp.Status, w.Header())

	if resp.Header.Get("Content-Type") == "application/vnd.docker.raw-stream" {
		doRawStream(w, resp, client)
	} else if resp.TransferEncoding != nil && resp.TransferEncoding[0] == "chunked" {
		doChunkedResponse(w, resp, client)
	} else {
		w.WriteHeader(resp.StatusCode)
		if _, err := io.Copy(w, resp.Body); err != nil {
			Log.Warning(err)
		}
	}
}

func doRawStream(w http.ResponseWriter, resp *http.Response, client *httputil.ClientConn) {
	down, downBuf, up, remaining, err := hijack(w, client)
	if err != nil {
		http.Error(w, "Unable to hijack connection for raw stream mode", http.StatusInternalServerError)
		return
	}
	defer down.Close()
	defer up.Close()

	if _, err := downBuf.Write([]byte("HTTP/1.1 " + resp.Status + "\n")); err != nil {
		Log.Warning(err)
		return
	}

	if err := resp.Header.Write(downBuf); err != nil {
		Log.Warning(err)
		return
	}

	if _, err := downBuf.Write([]byte("\n")); err != nil {
		Log.Warning(err)
		return
	}

	if err = downBuf.Flush(); err != nil {
		Log.Warning(err)
		return
	}

	var wg sync.WaitGroup
	wg.Add(2)
	go copyStream(down, io.MultiReader(remaining, up), &wg)
	go copyStream(up, downBuf, &wg)
	wg.Wait()
}

type closeWriter interface {
	CloseWrite() error
}

func copyStream(dst io.WriteCloser, src io.Reader, wg *sync.WaitGroup) {
	defer wg.Done()
	if _, err := io.Copy(dst, src); err != nil {
		Log.Warning(err)
	}
	var err error
	if c, ok := dst.(closeWriter); ok {
		err = c.CloseWrite()
	} else {
		err = dst.Close()
	}
	if err != nil {
		Log.Warningf("Error closing connection: %s", err)
	}
}

type writeFlusher interface {
	io.Writer
	http.Flusher
}

func doChunkedResponse(w http.ResponseWriter, resp *http.Response, client *httputil.ClientConn) {
	wf, ok := w.(writeFlusher)
	if !ok {
		http.Error(w, "Error forwarding chunked response body: flush not available", http.StatusInternalServerError)
		return
	}

	w.WriteHeader(resp.StatusCode)
	wf.Flush()

	up, remaining := client.Hijack()
	defer up.Close()

	var err error
	chunks := NewChunkedReader(io.MultiReader(remaining, up))
	for chunks.Next() && err == nil {
		_, err = io.Copy(wf, chunks.Chunk())
		wf.Flush()
	}
	if err == nil {
		err = chunks.Err()
	}
	if err != nil {
		Log.Errorf("Error forwarding chunked response body: %s", err)
	}
}

func hijack(w http.ResponseWriter, client *httputil.ClientConn) (down net.Conn, downBuf *bufio.ReadWriter, up net.Conn, remaining io.Reader, err error) {
	hj, ok := w.(http.Hijacker)
	if !ok {
		err = errors.New("Unable to cast to Hijack")
		return
	}
	down, downBuf, err = hj.Hijack()
	if err != nil {
		return
	}
	up, remaining = client.Hijack()
	return
}
<commit_msg>Squash up the code via 'defer'<commit_after>package proxy

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"net"
	"net/http"
	"net/http/httputil"
	"sync"

	"github.com/fsouza/go-dockerclient"
)

func (proxy *Proxy) Intercept(i interceptor, w http.ResponseWriter, r *http.Request) {
	if err := i.InterceptRequest(r); err != nil {
		switch err.(type) {
		case *docker.NoSuchContainer:
			http.Error(w, err.Error(), http.StatusNotFound)
		case *ErrNoSuchImage:
			http.Error(w, err.Error(), http.StatusNotFound)
		default:
			http.Error(w, err.Error(), http.StatusInternalServerError)
			Log.Warning("Error intercepting request: ", err)
		}
		return
	}

	conn, err := proxy.Dial()
	if err != nil {
		http.Error(w, "Could not connect to target", http.StatusInternalServerError)
		Log.Warning(err)
		return
	}
	client := httputil.NewClientConn(conn, nil)
	defer client.Close()

	resp, err := client.Do(r)
	if err != nil && err != httputil.ErrPersistEOF {
		http.Error(w, fmt.Sprintf("Could not make request to target: %v", err), http.StatusInternalServerError)
		Log.Warning("Error forwarding request: ", err)
		return
	}
	err = i.InterceptResponse(resp)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		Log.Warning("Error intercepting response: ", err)
		return
	}

	hdr := w.Header()
	for k, vs := range resp.Header {
		for _, v := range vs {
			hdr.Add(k, v)
		}
	}
	Log.Debugf("Response from target: %s %v", resp.Status, w.Header())

	if resp.Header.Get("Content-Type") == "application/vnd.docker.raw-stream" {
		doRawStream(w, resp, client)
	} else if resp.TransferEncoding != nil && resp.TransferEncoding[0] == "chunked" {
		doChunkedResponse(w, resp, client)
	} else {
		w.WriteHeader(resp.StatusCode)
		if _, err := io.Copy(w, resp.Body); err != nil {
			Log.Warning(err)
		}
	}
}

func doRawStream(w http.ResponseWriter, resp *http.Response, client *httputil.ClientConn) {
	down, downBuf, up, remaining, err := hijack(w, client)
	if err != nil {
		http.Error(w, "Unable to hijack connection for raw stream mode", http.StatusInternalServerError)
		return
	}
	defer down.Close()
	defer up.Close()
	defer func() {
		if err != nil {
			Log.Warning(err)
		}
	}()

	if _, err = downBuf.Write([]byte("HTTP/1.1 " + resp.Status + "\n")); err != nil {
		return
	}
	if err = resp.Header.Write(downBuf); err != nil {
		return
	}
	if _, err = downBuf.Write([]byte("\n")); err != nil {
		return
	}
	if err = downBuf.Flush(); err != nil {
		return
	}

	var wg sync.WaitGroup
	wg.Add(2)
	go copyStream(down, io.MultiReader(remaining, up), &wg)
	go copyStream(up, downBuf, &wg)
	wg.Wait()
}

type closeWriter interface {
	CloseWrite() error
}

func copyStream(dst io.WriteCloser, src io.Reader, wg *sync.WaitGroup) {
	defer wg.Done()
	if _, err := io.Copy(dst, src); err != nil {
		Log.Warning(err)
	}
	var err error
	if c, ok := dst.(closeWriter); ok {
		err = c.CloseWrite()
	} else {
		err = dst.Close()
	}
	if err != nil {
		Log.Warningf("Error closing connection: %s", err)
	}
}

type writeFlusher interface {
	io.Writer
	http.Flusher
}

func doChunkedResponse(w http.ResponseWriter, resp *http.Response, client *httputil.ClientConn) {
	wf, ok := w.(writeFlusher)
	if !ok {
		http.Error(w, "Error forwarding chunked response body: flush not available", http.StatusInternalServerError)
		return
	}

	w.WriteHeader(resp.StatusCode)
	wf.Flush()

	up, remaining := client.Hijack()
	defer up.Close()

	var err error
	chunks := NewChunkedReader(io.MultiReader(remaining, up))
	for chunks.Next() && err == nil {
		_, err = io.Copy(wf, chunks.Chunk())
		wf.Flush()
	}
	if err == nil {
		err = chunks.Err()
	}
	if err != nil {
		Log.Errorf("Error forwarding chunked response body: %s", err)
	}
}

func hijack(w http.ResponseWriter, client *httputil.ClientConn) (down net.Conn, downBuf *bufio.ReadWriter, up net.Conn, remaining io.Reader, err error) {
	hj, ok := w.(http.Hijacker)
	if !ok {
		err = errors.New("Unable to cast to Hijack")
		return
	}
	down, downBuf, err = hj.Hijack()
	if err != nil {
		return
	}
	up, remaining = client.Hijack()
	return
}
<|endoftext|>


<commit_before>package jsondot

import (
	"testing"
)

const JSON_TEXT = `{
	"name": "Bill",
	"age": 18,
	"hobby": ["sport", "music", "game"],
	"lucknum": [1,3,7,12],
	"card": {
		"tel": 13311223344,
		"bank": [
			{
				"bankname": "ICBC",
				"cardnum": 3568111100002231987,
				"credit": 100000
			},
			{
				"bankname": "CCB",
				"cardnum": 8782266352638784672,
				"credit": 50000
			}
		]
	}
}`

func initDot(t *testing.T) *JsonDot {
	byteData := []byte(JSON_TEXT)
	if byteData == nil {
		t.Error("byteData is NIL")
		panic("convert string to []byte error!")
	}
	dot, err := NewDot(byteData)
	if err != nil {
		t.Error(err)
		panic("NewDot error !")
	}
	return dot
}

func Test_NewDot(t *testing.T) {
	dot := initDot(t)
	if dot != nil {
		t.Log("Test_NewDot success!")
	} else {
		t.Log("Test_NewDot error!")
	}
}

func Test_GetValue(t *testing.T) {
	dot := initDot(t)

	t.Log(dot.GetString("name"))
	t.Log(dot.GetNumber("age"))

	t.Log("Test_GetValue success!")
}

func Test_SetValue(t *testing.T) {
	dot := initDot(t)

	t.Log("name=>", dot.GetString("name"))
	dot.SetString("name", "Frank")
	t.Log("name=>", dot.GetString("name"))

	t.Log("age=>", dot.GetNumber("age"))
	dot.SetNumber("age", 99)
	t.Log("age=>", dot.GetNumber("age"))

	t.Log("Test_SetValue success!")
}

func Test_GetStringArray(t *testing.T) {
	dot := initDot(t)

	t.Log("hobby=>", dot.GetStringArray("hobby"))

	slice := dot.GetStringArray("hobby")

	t.Log("[0]=>", slice[0])

	t.Log("[2]=>", slice[2])

	t.Log("Test_GetStringArray success!")
}

func Test_GetIntArray(t *testing.T) {
	dot := initDot(t)

	t.Log("lucknum=>", dot.GetNumberArray("lucknum"))

	slice := dot.GetNumberArray("lucknum")

	t.Log("[0]=>", slice[0])

	t.Log("[2]=>", slice[2])

	t.Log("Test_GetIntArray success!")
}

func Test_SetStringArray(t *testing.T) {
	dot := initDot(t)

	t.Log("before Change: hobby=>", dot.GetStringArray("hobby"))

	slice := dot.GetStringArray("hobby")

	t.Log("[0]=>", slice[0])

	slice[0] = "swimming"

	t.Log("[0]=>", slice[0])

	t.Log("after reassign: hobby=>", dot.GetStringArray("hobby"))

	dot.SetStringArray("hobby", slice)

	t.Log("after Set: hobby=>", dot.GetStringArray("hobby"))

	t.Log("Test_SetStringArray success!")
}

func Test_SetNumberArray(t *testing.T) {
	dot := initDot(t)

	t.Log("lucknum=>", dot.GetNumberArray("lucknum"))

	slice := dot.GetNumberArray("lucknum")

	t.Log("[0]=>", slice[0])

	slice[0] = 123

	t.Log("[0]=>", slice[0])

	t.Log("after reassign: lucknum=>", dot.GetNumberArray("lucknum"))

	dot.SetNumberArray("lucknum", slice)

	t.Log("after Set: lucknum=>", dot.GetNumberArray("lucknum"))

	t.Log("Test_SetStringArray success!")
}
<commit_msg>Update jsondot_test.go<commit_after>package jsondot

import (
	"testing"
)

const JSON_TEXT = `{
	"name": "Bill",
	"age": 18,
	"hobby": ["sport", "music", "game"],
	"lucknum": [1,3,7,12],
	"card": {
		"tel": 13311223344,
		"bank": [
			{
				"bankname": "ICBC",
				"cardnum": 3568111100002231987,
				"credit": 100000
			},
			{
				"bankname": "CCB",
				"cardnum": 8782266352638784672,
				"credit": 50000
			}
		]
	}
}`

func initDot(t *testing.T) *JsonDot {
	byteData := []byte(JSON_TEXT)
	if byteData == nil {
		t.Error("byteData is NIL")
		panic("convert string to []byte error!")
	}
	dot, err := NewDot(byteData)
	if err != nil {
		t.Error(err)
		panic("NewDot error !")
	}
	return dot
}

func Test_NewDot(t *testing.T) {
	dot := initDot(t)
	if dot != nil {
		t.Log("Test_NewDot success!")
	} else {
		t.Log("Test_NewDot error!")
	}
}

func Test_GetValue(t *testing.T) {
	dot := initDot(t)

	t.Log(dot.GetString("name"))
	t.Log(dot.GetNumber("age"))

	t.Log("Test_GetValue success!")
}

func Test_SetValue(t *testing.T) {
	dot := initDot(t)

	t.Log("name=>", dot.GetString("name"))
	dot.SetString("name", "Frank")
	t.Log("name=>", dot.GetString("name"))

	t.Log("age=>", dot.GetNumber("age"))
	dot.SetNumber("age", 99)
	t.Log("age=>", dot.GetNumber("age"))

	t.Log("Test_SetValue success!")
}

func Test_GetStringArray(t *testing.T) {
	dot := initDot(t)

	t.Log("hobby=>", dot.GetStringArray("hobby"))

	slice := dot.GetStringArray("hobby")

	t.Log("[0]=>", slice[0])

	t.Log("[2]=>", slice[2])

	t.Log("Test_GetStringArray success!")
}

func Test_GetIntArray(t *testing.T) {
	dot := initDot(t)

	t.Log("lucknum=>", dot.GetNumberArray("lucknum"))

	slice := dot.GetNumberArray("lucknum")

	t.Log("[0]=>", slice[0])

	t.Log("[2]=>", slice[2])

	t.Log("Test_GetIntArray success!")
}

func Test_SetStringArray(t *testing.T) {
	dot := initDot(t)

	t.Log("before Change: hobby=>", dot.GetStringArray("hobby"))

	slice := dot.GetStringArray("hobby")

	t.Log("[0]=>", slice[0])

	slice[0] = "swimming"

	t.Log("[0]=>", slice[0])

	t.Log("after reassign: hobby=>", dot.GetStringArray("hobby"))

	dot.SetStringArray("hobby", slice)

	t.Log("after Set: hobby=>", dot.GetStringArray("hobby"))

	t.Log("Test_SetStringArray success!")
}

func Test_SetNumberArray(t *testing.T) {
	dot := initDot(t)

	t.Log("lucknum=>", dot.GetNumberArray("lucknum"))

	slice := dot.GetNumberArray("lucknum")

	t.Log("[0]=>", slice[0])

	slice[0] = 123

	t.Log("[0]=>", slice[0])

	t.Log("after reassign: lucknum=>", dot.GetNumberArray("lucknum"))

	dot.SetNumberArray("lucknum", slice)

	t.Log("after Set: lucknum=>", dot.GetNumberArray("lucknum"))

	t.Log("Test_SetNumberArray success!")
}
<|endoftext|>


<commit_before>package l10n

// Term constants
const (
	DefReadMore      = "Read more..."
	DefPublishedWith = "published with write.as"
	DefOlder         = "Older"
	DefNewer         = "Newer"
)

// Default phrases
var phrases = map[string]string{
	"Anonymous post":                 "Anonymous post",
	"Blogs":                          "Blogs",
	"Enter":                          "Enter",
	"Newer":                          "Newer",
	"Next":                           "Next",
	"Older":                          "Older",
	"Posts":                          "Posts",
	"Previous":                       "Previous",
	"Publish to...":                  "Publish to...",
	"Publish":                        "Publish",
	"Read more...":                   "Read more...",
	"Subscribe":                      "Subscribe",
	"This blog requires a password.": "This blog requires a password.",
	"Toggle theme":                   "Toggle theme",
	"View posts":                     "View Posts",
	"delete":                         "delete",
	"edit":                           "edit",
	"email subscription confirm":     "Please check your email and click the confirmation link to subscribe.",
	"email subscription prompt":      "Enter your email to subscribe to updates.",
	"email subscription success":     "Subscribed. You'll now receive future blog posts via email.",
	"move to...":                     "move to...",
	"pin":                            "pin",
	"published with write.as":        "published with write.as",
	"share modal ending":             "Send it to a friend, share it across the web, or maybe tweet it. Learn more.",
	"share modal introduction":       "Each published post has a secret, unique URL you can share with anyone. This is that URL:",
	"share modal title":              "Share this post",
	"share":                          "share",
	"unpin":                          "unpin",
}
<commit_msg>Restore default title dash<commit_after>package l10n

// Term constants
const (
	DefReadMore      = "Read more..."
	DefPublishedWith = "published with write.as"
	DefOlder         = "Older"
	DefNewer         = "Newer"
)

// Default phrases
var phrases = map[string]string{
	"Anonymous post":                 "Anonymous post",
	"Blogs":                          "Blogs",
	"Enter":                          "Enter",
	"Newer":                          "Newer",
	"Next":                           "Next",
	"Older":                          "Older",
	"Posts":                          "Posts",
	"Previous":                       "Previous",
	"Publish to...":                  "Publish to...",
	"Publish":                        "Publish",
	"Read more...":                   "Read more...",
	"Subscribe":                      "Subscribe",
	"This blog requires a password.": "This blog requires a password.",
	"Toggle theme":                   "Toggle theme",
	"View posts":                     "View Posts",
	"delete":                         "delete",
	"edit":                           "edit",
	"email subscription confirm":     "Please check your email and click the confirmation link to subscribe.",
	"email subscription prompt":      "Enter your email to subscribe to updates.",
	"email subscription success":     "Subscribed. You'll now receive future blog posts via email.",
	"move to...":                     "move to...",
	"pin":                            "pin",
	"published with write.as":        "published with write.as",
	"share modal ending":             "Send it to a friend, share it across the web, or maybe tweet it. Learn more.",
	"share modal introduction":       "Each published post has a secret, unique URL you can share with anyone. This is that URL:",
	"share modal title":              "Share this post",
	"share":                          "share",
	"unpin":                          "unpin",
	"title dash":                     "—",
}
<|endoftext|>


<commit_before>package db

import (
	"bytes"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"net/url"
	"strings"
	"time"

	"github.com/bosssauce/ponzu/system/admin/config"

	"github.com/boltdb/bolt"
	"github.com/gorilla/schema"
)

var configCache url.Values

func init() {
	configCache = make(url.Values)
}

// SetConfig sets key:value pairs in the db for configuration settings
func SetConfig(data url.Values) error {
	err := store.Update(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte("_config"))

		// check for any multi-value fields (ex. checkbox fields)
		// and correctly format for db storage. Essentially, we need
		// fieldX.0: value1, fieldX.1: value2 => fieldX: []string{value1, value2}
		var discardKeys []string
		for k, v := range data {
			if strings.Contains(k, ".") {
				key := strings.Split(k, ".")[0]

				if data.Get(key) == "" {
					data.Set(key, v[0])
					discardKeys = append(discardKeys, k)
				} else {
					data.Add(key, v[0])
				}
			}
		}

		for _, discardKey := range discardKeys {
			data.Del(discardKey)
		}

		cfg := &config.Config{}
		dec := schema.NewDecoder()
		dec.SetAliasTag("json")     // allows simpler struct tagging when creating a content type
		dec.IgnoreUnknownKeys(true) // will skip over form values submitted, but not in struct
		err := dec.Decode(cfg, data)
		if err != nil {
			return err
		}

		// check for "invalidate" value to reset the Etag
		fmt.Println(len(cfg.CacheInvalidate))
		if len(cfg.CacheInvalidate) > 0 && cfg.CacheInvalidate[0] == "invalidate" {
			fmt.Println(cfg.CacheInvalidate)
			cfg.Etag = NewEtag()
			cfg.CacheInvalidate = []string{}
		}

		j, err := json.Marshal(cfg)
		if err != nil {
			return err
		}

		err = b.Put([]byte("settings"), j)
		if err != nil {
			return err
		}

		return nil
	})
	if err != nil {
		return err
	}

	configCache = data

	return nil
}

// Config gets the value of a key in the configuration from the db
func Config(key string) ([]byte, error) {
	kv := make(map[string]interface{})

	cfg, err := ConfigAll()
	if err != nil {
		return nil, err
	}

	if len(cfg) < 1 {
		return nil, nil
	}

	err = json.Unmarshal(cfg, &kv)
	if err != nil {
		return nil, err
	}

	return []byte(kv[key].(string)), nil
}

// ConfigAll gets the configuration from the db
func ConfigAll() ([]byte, error) {
	val := &bytes.Buffer{}
	err := store.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte("_config"))
		val.Write(b.Get([]byte("settings")))

		return nil
	})
	if err != nil {
		return nil, err
	}

	return val.Bytes(), nil
}

// ConfigCache is a in-memory cache of the Configs for quicker lookups
func ConfigCache(key string) string {
	return configCache.Get(key)
}

// NewEtag generates a new Etag for response caching
func NewEtag() string {
	now := fmt.Sprintf("%d", time.Now().Unix())
	etag := base64.StdEncoding.EncodeToString([]byte(now))

	return etag
}
<commit_msg>testing check for cache invalidation to after values are decoded<commit_after>package db

import (
	"bytes"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"net/url"
	"strings"
	"time"

	"github.com/bosssauce/ponzu/system/admin/config"

	"github.com/boltdb/bolt"
	"github.com/gorilla/schema"
)

var configCache url.Values

func init() {
	configCache = make(url.Values)
}

// SetConfig sets key:value pairs in the db for configuration settings
func SetConfig(data url.Values) error {
	err := store.Update(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte("_config"))

		// check for any multi-value fields (ex. checkbox fields)
		// and correctly format for db storage. Essentially, we need
		// fieldX.0: value1, fieldX.1: value2 => fieldX: []string{value1, value2}
		var discardKeys []string
		for k, v := range data {
			if strings.Contains(k, ".") {
				key := strings.Split(k, ".")[0]

				if data.Get(key) == "" {
					data.Set(key, v[0])
					discardKeys = append(discardKeys, k)
				} else {
					data.Add(key, v[0])
				}
			}
		}

		for _, discardKey := range discardKeys {
			data.Del(discardKey)
		}

		cfg := &config.Config{}
		dec := schema.NewDecoder()
		dec.SetAliasTag("json")     // allows simpler struct tagging when creating a content type
		dec.IgnoreUnknownKeys(true) // will skip over form values submitted, but not in struct
		err := dec.Decode(cfg, data)
		if err != nil {
			return err
		}

		// check for "invalidate" value to reset the Etag
		if cfg.CacheInvalidate[0] == "invalidate" {
			cfg.Etag = NewEtag()
			cfg.CacheInvalidate = []string{}
		}

		j, err := json.Marshal(cfg)
		if err != nil {
			return err
		}

		err = b.Put([]byte("settings"), j)
		if err != nil {
			return err
		}

		return nil
	})
	if err != nil {
		return err
	}

	configCache = data

	return nil
}

// Config gets the value of a key in the configuration from the db
func Config(key string) ([]byte, error) {
	kv := make(map[string]interface{})

	cfg, err := ConfigAll()
	if err != nil {
		return nil, err
	}

	if len(cfg) < 1 {
		return nil, nil
	}

	err = json.Unmarshal(cfg, &kv)
	if err != nil {
		return nil, err
	}

	return []byte(kv[key].(string)), nil
}

// ConfigAll gets the configuration from the db
func ConfigAll() ([]byte, error) {
	val := &bytes.Buffer{}
	err := store.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte("_config"))
		val.Write(b.Get([]byte("settings")))

		return nil
	})
	if err != nil {
		return nil, err
	}

	return val.Bytes(), nil
}

// ConfigCache is a in-memory cache of the Configs for quicker lookups
func ConfigCache(key string) string {
	return configCache.Get(key)
}

// NewEtag generates a new Etag for response caching
func NewEtag() string {
	now := fmt.Sprintf("%d", time.Now().Unix())
	etag := base64.StdEncoding.EncodeToString([]byte(now))

	return etag
}
<|endoftext|>


<commit_before>package lib

import "github.com/jh-bate/fantail-bot/Godeps/_workspace/src/github.com/tucnak/telebot"

const (
	default_script = "default"
	stickers_chat  = "chat"
)

type (
	Info struct {
		App       []string `json:"appInfo"`
		Reminders []string `json:"remindersInfo"`
		Chat      []string `json:"chatInfo"`
		Said      []string `json:"saidInfo"`
	}

	QProcess struct {
		s *session
	}
)

func NewQProcess(b *telebot.Bot, s *Storage) *QProcess {
	q := &QProcess{s: newSession(b, s)}
	return q
}

func (this *QProcess) Run(input <-chan telebot.Message) {

	prevActionName := ""

	for msg := range input {
		in := newIncoming(msg)

		in.getAction(this.s, prevActionName).firstUp().askQuestion()
		prevActionName = in.action.getName()
	}
}
<commit_msg>update prev name if valid<commit_after>package lib

import (
	"strings"

	"github.com/jh-bate/fantail-bot/Godeps/_workspace/src/github.com/tucnak/telebot"
)

const (
	default_script = "default"
	stickers_chat  = "chat"
)

type (
	Info struct {
		App       []string `json:"appInfo"`
		Reminders []string `json:"remindersInfo"`
		Chat      []string `json:"chatInfo"`
		Said      []string `json:"saidInfo"`
	}

	QProcess struct {
		s *session
	}
)

func NewQProcess(b *telebot.Bot, s *Storage) *QProcess {
	q := &QProcess{s: newSession(b, s)}
	return q
}

func getActionName(msg telebot.Message) (bool, string) {
	if strings.Contains(msg.Text, "/") {
		return true, strings.Fields(msg.Text)[0]
	}
	return false, ""
}

func (this *QProcess) Run(input <-chan telebot.Message) {

	prevActionName := ""

	for msg := range input {

		in := newIncoming(msg)
		in.getAction(this.s, prevActionName).firstUp().askQuestion()
		if update, name := getActionName(msg); update {
			prevActionName = name
		}
	}
}
<|endoftext|>


<commit_before>package telemetry

import (
	"fmt"
	"net"
	"os"

	"github.com/bahadley/esp/log"
	"github.com/bahadley/esp/operator"
)

const (
	defaultAddr = "127.0.0.1"
	defaultPort = "22221"

	envsaddr = "ESP_ADDR"
	envport  = "ESP_PORT"
)

var (
	op chan string
)

func Ingest() {
	saddr, err := resolveAddr()
	if err != nil {
		log.Logfatalerror(err)
	}

	conn, err := net.ListenUDP("udp", saddr)
	if err != nil {
		log.Logoutput(log.ErrPrefix, err.Error())
	}
	defer conn.Close()

	buf := make([]byte, 1024)
	for {
		n, caddr, err := conn.ReadFromUDP(buf)
		if err != nil {
			log.Logoutput(log.ErrPrefix, err.Error())
			continue
		}

		msg := string(buf[0:n])
		log.Logoutput(log.InfoPrefix, fmt.Sprintf("Recv(%s): %s", caddr, msg))
		op <- msg
	}
}

func init() {
	op = make(chan string, 10)
	go operator.Window(op)
}

func resolveAddr() (*net.UDPAddr, error) {
	addr := os.Getenv(envsaddr)
	if len(addr) == 0 {
		addr = defaultAddr
	}

	port := os.Getenv(envport)
	if len(port) == 0 {
		port = defaultPort
	}

	return net.ResolveUDPAddr("udp", addr+":"+port)
}
<commit_msg>Channel buffer size set with constant value now<commit_after>package telemetry

import (
	"fmt"
	"net"
	"os"

	"github.com/bahadley/esp/log"
	"github.com/bahadley/esp/operator"
)

const (
	defaultAddr = "127.0.0.1"
	defaultPort = "22221"

	envsaddr = "ESP_ADDR"
	envport  = "ESP_PORT"

    chanbufsz = 10
)

var (
	op chan string
)

func Ingest() {
	saddr, err := resolveAddr()
	if err != nil {
		log.Logfatalerror(err)
	}

	conn, err := net.ListenUDP("udp", saddr)
	if err != nil {
		log.Logoutput(log.ErrPrefix, err.Error())
	}
	defer conn.Close()

	buf := make([]byte, 1024)
	for {
		n, caddr, err := conn.ReadFromUDP(buf)
		if err != nil {
			log.Logoutput(log.ErrPrefix, err.Error())
			continue
		}

		msg := string(buf[0:n])
		log.Logoutput(log.InfoPrefix, fmt.Sprintf("Recv(%s): %s", caddr, msg))
		op <- msg
	}
}

func init() {
	op = make(chan string, chanbufsz)
	go operator.Window(op)
}

func resolveAddr() (*net.UDPAddr, error) {
	addr := os.Getenv(envsaddr)
	if len(addr) == 0 {
		addr = defaultAddr
	}

	port := os.Getenv(envport)
	if len(port) == 0 {
		port = defaultPort
	}

	return net.ResolveUDPAddr("udp", addr+":"+port)
}
<|endoftext|>


<commit_before>package zenodb

import (
	"fmt"
	"path/filepath"
	"strings"
	"sync"
	"time"

	"github.com/dustin/go-humanize"
	"github.com/getlantern/goexpr/geo"
	"github.com/getlantern/goexpr/isp"
	"github.com/getlantern/golog"
	"github.com/getlantern/vtime"
)

var (
	log = golog.LoggerFor("zenodb")
)

// DBOpts provides options for configuring the database.
type DBOpts struct {
	// Dir points at the directory that contains the data files.
	Dir string
	// SchemaFile points at a YAML schema file that configures the tables and
	// views in the database.
	SchemaFile string
	// ISPDatabase points at an ISP database like the one from here:
	// https://lite.ip2location.com/database/ip-asn. Specify this to allow the use
	// of ISP functions.
	ISPDatabase string
	// DiscardOnBackPressure, when true, tells zenodb to discard new inserts if it
	// is unable to keep up with the insert rate.
	DiscardOnBackPressure bool
	// IncludeMemStoreInQuery, when true, tells zenodb to include the current
	// memstore when performing queries. This requires the memstore to be copied
	// which can dramatically impact performance.
	IncludeMemStoreInQuery bool
	// VirtualTime, if true, tells zenodb to use a virtual clock that advances
	// based on the timestamps of Points received via inserts.
	VirtualTime bool
}

// DB is a zenodb database.
type DB struct {
	opts        *DBOpts
	clock       vtime.Clock
	streams     map[string][]*table
	tables      map[string]*table
	tablesMutex sync.RWMutex
}

// NewDB creates a database using the given options.
func NewDB(opts *DBOpts) (*DB, error) {
	var err error
	db := &DB{opts: opts, clock: vtime.RealClock, tables: make(map[string]*table), streams: make(map[string][]*table)}
	if opts.VirtualTime {
		db.clock = vtime.NewVirtualClock(time.Time{})
	}
	if opts.SchemaFile != "" {
		err = db.pollForSchema(opts.SchemaFile)
	}
	log.Debug("Enabling geolocation functions")
	err = geo.Init(filepath.Join(opts.Dir, "geoip.dat.gz"))
	if err != nil {
		return nil, fmt.Errorf("Unable to initialize geo: %v", err)
	}
	if opts.ISPDatabase != "" {
		log.Debugf("Enabling ISP functions using file at %v", opts.ISPDatabase)
		err = isp.Init(opts.ISPDatabase)
		if err != nil {
			return nil, fmt.Errorf("Unable to initialize ISP functions from file at %v: %v", opts.ISPDatabase, err)
		}
	}
	log.Debugf("Dir: %v    SchemaFile: %v", opts.Dir, opts.SchemaFile)
	return db, err
}

// TableStats returns the TableStats for the named table.
func (db *DB) TableStats(table string) TableStats {
	t := db.getTable(table)
	if t == nil {
		return TableStats{}
	}
	t.statsMutex.RLock()
	defer t.statsMutex.RUnlock()
	return t.stats
}

// AllTableStats returns all TableStats for all tables, keyed to the table
// names.
func (db *DB) AllTableStats() map[string]TableStats {
	m := make(map[string]TableStats)
	tables := make(map[string]*table, 0)
	db.tablesMutex.RLock()
	for name, t := range db.tables {
		tables[name] = t
	}
	db.tablesMutex.RUnlock()
	for name, t := range tables {
		t.statsMutex.RLock()
		m[name] = t.stats
		t.statsMutex.RUnlock()
	}
	return m
}

// PrintTableStats prints the stats for the named table to a string.
func (db *DB) PrintTableStats(table string) string {
	stats := db.TableStats(table)
	now := db.clock.Now()
	return fmt.Sprintf("%v (%v)\tFiltered: %v    Queued: %v    Inserted: %v    Dropped: %v    Expired: %v",
		table,
		now.In(time.UTC),
		humanize.Comma(stats.FilteredPoints),
		humanize.Comma(stats.QueuedPoints),
		humanize.Comma(stats.InsertedPoints),
		humanize.Comma(stats.DroppedPoints),
		humanize.Comma(stats.ExpiredValues))
}

func (db *DB) getTable(table string) *table {
	db.tablesMutex.RLock()
	t := db.tables[strings.ToLower(table)]
	db.tablesMutex.RUnlock()
	return t
}
<commit_msg>Handling error from schema initialization<commit_after>package zenodb

import (
	"fmt"
	"path/filepath"
	"strings"
	"sync"
	"time"

	"github.com/dustin/go-humanize"
	"github.com/getlantern/goexpr/geo"
	"github.com/getlantern/goexpr/isp"
	"github.com/getlantern/golog"
	"github.com/getlantern/vtime"
)

var (
	log = golog.LoggerFor("zenodb")
)

// DBOpts provides options for configuring the database.
type DBOpts struct {
	// Dir points at the directory that contains the data files.
	Dir string
	// SchemaFile points at a YAML schema file that configures the tables and
	// views in the database.
	SchemaFile string
	// ISPDatabase points at an ISP database like the one from here:
	// https://lite.ip2location.com/database/ip-asn. Specify this to allow the use
	// of ISP functions.
	ISPDatabase string
	// DiscardOnBackPressure, when true, tells zenodb to discard new inserts if it
	// is unable to keep up with the insert rate.
	DiscardOnBackPressure bool
	// IncludeMemStoreInQuery, when true, tells zenodb to include the current
	// memstore when performing queries. This requires the memstore to be copied
	// which can dramatically impact performance.
	IncludeMemStoreInQuery bool
	// VirtualTime, if true, tells zenodb to use a virtual clock that advances
	// based on the timestamps of Points received via inserts.
	VirtualTime bool
}

// DB is a zenodb database.
type DB struct {
	opts        *DBOpts
	clock       vtime.Clock
	streams     map[string][]*table
	tables      map[string]*table
	tablesMutex sync.RWMutex
}

// NewDB creates a database using the given options.
func NewDB(opts *DBOpts) (*DB, error) {
	var err error
	db := &DB{opts: opts, clock: vtime.RealClock, tables: make(map[string]*table), streams: make(map[string][]*table)}
	if opts.VirtualTime {
		db.clock = vtime.NewVirtualClock(time.Time{})
	}
	if opts.SchemaFile != "" {
		err = db.pollForSchema(opts.SchemaFile)
		if err != nil {
			return nil, fmt.Errorf("Unable to apply schema: %v", err)
		}
	}
	log.Debug("Enabling geolocation functions")
	err = geo.Init(filepath.Join(opts.Dir, "geoip.dat.gz"))
	if err != nil {
		return nil, fmt.Errorf("Unable to initialize geo: %v", err)
	}
	if opts.ISPDatabase != "" {
		log.Debugf("Enabling ISP functions using file at %v", opts.ISPDatabase)
		err = isp.Init(opts.ISPDatabase)
		if err != nil {
			return nil, fmt.Errorf("Unable to initialize ISP functions from file at %v: %v", opts.ISPDatabase, err)
		}
	}
	log.Debugf("Dir: %v    SchemaFile: %v", opts.Dir, opts.SchemaFile)
	return db, err
}

// TableStats returns the TableStats for the named table.
func (db *DB) TableStats(table string) TableStats {
	t := db.getTable(table)
	if t == nil {
		return TableStats{}
	}
	t.statsMutex.RLock()
	defer t.statsMutex.RUnlock()
	return t.stats
}

// AllTableStats returns all TableStats for all tables, keyed to the table
// names.
func (db *DB) AllTableStats() map[string]TableStats {
	m := make(map[string]TableStats)
	tables := make(map[string]*table, 0)
	db.tablesMutex.RLock()
	for name, t := range db.tables {
		tables[name] = t
	}
	db.tablesMutex.RUnlock()
	for name, t := range tables {
		t.statsMutex.RLock()
		m[name] = t.stats
		t.statsMutex.RUnlock()
	}
	return m
}

// PrintTableStats prints the stats for the named table to a string.
func (db *DB) PrintTableStats(table string) string {
	stats := db.TableStats(table)
	now := db.clock.Now()
	return fmt.Sprintf("%v (%v)\tFiltered: %v    Queued: %v    Inserted: %v    Dropped: %v    Expired: %v",
		table,
		now.In(time.UTC),
		humanize.Comma(stats.FilteredPoints),
		humanize.Comma(stats.QueuedPoints),
		humanize.Comma(stats.InsertedPoints),
		humanize.Comma(stats.DroppedPoints),
		humanize.Comma(stats.ExpiredValues))
}

func (db *DB) getTable(table string) *table {
	db.tablesMutex.RLock()
	t := db.tables[strings.ToLower(table)]
	db.tablesMutex.RUnlock()
	return t
}
<|endoftext|>


<commit_before>package pulet

import (
	"crypto/rand"
	"encoding/hex"
	"fmt"
	"io"
	"os/exec"
	"time"
)

type ImportSpec struct {
	SourceHost string `json:"src_host"`
	SourcePort int    `json:"src_port"`
	SourceId   string `json:"src_id"`
}

type ImageSpec struct {
	Repo string `json:"repo"`
	Tag  string `json:"tag"`
}

func randomUniqString() string {
	var d [8]byte
	io.ReadFull(rand.Reader, d[:])
	str := hex.EncodeToString(d[:])
	return fmt.Sprintf("%x-%v", time.Now().Unix(), str)
}

type Pulet struct {
}

func NewPulet() *Pulet {
	return &Pulet{}
}

func (self *Pulet) Import(spec *ImportSpec) (*ImageSpec, error) {
	importUrl := fmt.Sprintf("http://%v:%v/export/%v", spec.SourceHost, spec.SourcePort, spec.SourceId)
	imgSpec := &ImageSpec{
		Repo: randomUniqString(),
		Tag:  randomUniqString(),
	}
	alias := fmt.Sprintf("%v:%v", imgSpec.Repo, imgSpec.Tag)
	fmt.Printf("docker import %v %v", importUrl, alias)
	cmd := exec.Command("docker", "import", importUrl, alias)
	err := cmd.Run()
	if err != nil {
		return nil, err
	}
	return imgSpec, nil
}

// runArgs: arguments sends after docker run. Something like -p -v, etc.
// cmdList: command runs in the container
func (self *Pulet) RunImage(img *ImageSpec, runArgs []string, cmdList []string) error {
	alias := fmt.Sprintf("%v:%v", img.Repo, img.Tag)
	args := make([]string, 0, len(runArgs)+len(cmdList)+2)
	args = append(args, "run")
	for _, arg := range runArgs {
		args = append(args, arg)
	}
	args = append(args, alias)
	for _, arg := range cmdList {
		args = append(args, arg)
	}

	fmt.Printf("docker %+v", args)
	cmd := exec.Command("docker", args...)
	err := cmd.Run()
	if err != nil {
		return err
	}
	return nil
}
<commit_msg>check root<commit_after>package pulet

import (
	"crypto/rand"
	"encoding/hex"
	"fmt"
	"io"
	"os/exec"
	"os/user"
	"time"
)

type ImportSpec struct {
	SourceHost string `json:"src_host"`
	SourcePort int    `json:"src_port"`
	SourceId   string `json:"src_id"`
}

type ImageSpec struct {
	Repo string `json:"repo"`
	Tag  string `json:"tag"`
}

func randomUniqString() string {
	var d [8]byte
	io.ReadFull(rand.Reader, d[:])
	str := hex.EncodeToString(d[:])
	return fmt.Sprintf("%x-%v", time.Now().Unix(), str)
}

type Pulet struct {
}

func NewPulet() *Pulet {
	u, err := user.Current()
	if err != nil {
		panic(err.Error())
	}
	if u.Username != "root" {
		panic("must be root!")
	}
	return &Pulet{}
}

func (self *Pulet) Import(spec *ImportSpec) (*ImageSpec, error) {
	importUrl := fmt.Sprintf("http://%v:%v/export/%v", spec.SourceHost, spec.SourcePort, spec.SourceId)
	imgSpec := &ImageSpec{
		Repo: randomUniqString(),
		Tag:  randomUniqString(),
	}
	alias := fmt.Sprintf("%v:%v", imgSpec.Repo, imgSpec.Tag)
	fmt.Printf("docker import %v %v", importUrl, alias)
	cmd := exec.Command("docker", "import", importUrl, alias)
	err := cmd.Run()
	if err != nil {
		return nil, err
	}
	return imgSpec, nil
}

// runArgs: arguments sends after docker run. Something like -p -v, etc.
// cmdList: command runs in the container
func (self *Pulet) RunImage(img *ImageSpec, runArgs []string, cmdList []string) error {
	alias := fmt.Sprintf("%v:%v", img.Repo, img.Tag)
	args := make([]string, 0, len(runArgs)+len(cmdList)+2)
	args = append(args, "run")
	for _, arg := range runArgs {
		args = append(args, arg)
	}
	args = append(args, alias)
	for _, arg := range cmdList {
		args = append(args, arg)
	}

	fmt.Printf("docker %+v", args)
	cmd := exec.Command("docker", args...)
	err := cmd.Run()
	if err != nil {
		return err
	}
	return nil
}
<|endoftext|>


<commit_before>package snapshot

import (
	"context"
	"path/filepath"
	"strconv"
	"strings"
	"sync"

	"github.com/containerd/containerd/mount"
	"github.com/containerd/containerd/snapshots"
	"github.com/docker/docker/daemon/graphdriver"
	"github.com/docker/docker/layer"
	"github.com/docker/docker/pkg/idtools"
	"github.com/moby/buildkit/identity"
	"github.com/moby/buildkit/snapshot"
	digest "github.com/opencontainers/go-digest"
	"github.com/pkg/errors"
	bolt "go.etcd.io/bbolt"
)

var keyParent = []byte("parent")
var keyCommitted = []byte("committed")
var keyChainID = []byte("chainid")
var keySize = []byte("size")

// Opt defines options for creating the snapshotter
type Opt struct {
	GraphDriver     graphdriver.Driver
	LayerStore      layer.Store
	Root            string
	IdentityMapping *idtools.IdentityMapping
}

type graphIDRegistrar interface {
	RegisterByGraphID(string, layer.ChainID, layer.DiffID, string, int64) (layer.Layer, error)
	Release(layer.Layer) ([]layer.Metadata, error)
	checksumCalculator
}

type checksumCalculator interface {
	ChecksumForGraphID(id, parent, oldTarDataPath, newTarDataPath string) (diffID layer.DiffID, size int64, err error)
}

type snapshotter struct {
	opt Opt

	refs map[string]layer.Layer
	db   *bolt.DB
	mu   sync.Mutex
	reg  graphIDRegistrar
}

var _ snapshot.SnapshotterBase = &snapshotter{}

// NewSnapshotter creates a new snapshotter
func NewSnapshotter(opt Opt) (snapshot.SnapshotterBase, error) {
	dbPath := filepath.Join(opt.Root, "snapshots.db")
	db, err := bolt.Open(dbPath, 0600, nil)
	if err != nil {
		return nil, errors.Wrapf(err, "failed to open database file %s", dbPath)
	}

	reg, ok := opt.LayerStore.(graphIDRegistrar)
	if !ok {
		return nil, errors.Errorf("layerstore doesn't support graphID registration")
	}

	s := &snapshotter{
		opt:  opt,
		db:   db,
		refs: map[string]layer.Layer{},
		reg:  reg,
	}
	return s, nil
}

func (s *snapshotter) Name() string {
	return "default"
}

func (s *snapshotter) IdentityMapping() *idtools.IdentityMapping {
	return s.opt.IdentityMapping
}

func (s *snapshotter) Prepare(ctx context.Context, key, parent string, opts ...snapshots.Opt) error {
	origParent := parent
	if parent != "" {
		if l, err := s.getLayer(parent, false); err != nil {
			return err
		} else if l != nil {
			parent, err = getGraphID(l)
			if err != nil {
				return err
			}
		} else {
			parent, _ = s.getGraphDriverID(parent)
		}
	}
	if err := s.opt.GraphDriver.Create(key, parent, nil); err != nil {
		return err
	}
	return s.db.Update(func(tx *bolt.Tx) error {
		b, err := tx.CreateBucketIfNotExists([]byte(key))
		if err != nil {
			return err
		}
		return b.Put(keyParent, []byte(origParent))
	})
}

func (s *snapshotter) chainID(key string) (layer.ChainID, bool) {
	if strings.HasPrefix(key, "sha256:") {
		dgst, err := digest.Parse(key)
		if err != nil {
			return "", false
		}
		return layer.ChainID(dgst), true
	}
	return "", false
}

func (s *snapshotter) GetLayer(key string) (layer.Layer, error) {
	return s.getLayer(key, true)
}

func (s *snapshotter) getLayer(key string, withCommitted bool) (layer.Layer, error) {
	s.mu.Lock()
	l, ok := s.refs[key]
	if !ok {
		id, ok := s.chainID(key)
		if !ok {
			if !withCommitted {
				s.mu.Unlock()
				return nil, nil
			}
			if err := s.db.View(func(tx *bolt.Tx) error {
				b := tx.Bucket([]byte(key))
				if b == nil {
					return nil
				}
				v := b.Get(keyChainID)
				if v != nil {
					id = layer.ChainID(v)
				}
				return nil
			}); err != nil {
				s.mu.Unlock()
				return nil, err
			}
			if id == "" {
				s.mu.Unlock()
				return nil, nil
			}
		}
		var err error
		l, err = s.opt.LayerStore.Get(id)
		if err != nil {
			s.mu.Unlock()
			return nil, err
		}
		s.refs[key] = l
		if err := s.db.Update(func(tx *bolt.Tx) error {
			_, err := tx.CreateBucketIfNotExists([]byte(key))
			return err
		}); err != nil {
			s.mu.Unlock()
			return nil, err
		}
	}
	s.mu.Unlock()

	return l, nil
}

func (s *snapshotter) getGraphDriverID(key string) (string, bool) {
	var gdID string
	if err := s.db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(key))
		if b == nil {
			return errors.Errorf("not found") // TODO: typed
		}
		v := b.Get(keyCommitted)
		if v != nil {
			gdID = string(v)
		}
		return nil
	}); err != nil || gdID == "" {
		return key, false
	}
	return gdID, true
}

func (s *snapshotter) Stat(ctx context.Context, key string) (snapshots.Info, error) {
	inf := snapshots.Info{
		Kind: snapshots.KindActive,
	}

	l, err := s.getLayer(key, false)
	if err != nil {
		return snapshots.Info{}, err
	}
	if l != nil {
		if p := l.Parent(); p != nil {
			inf.Parent = p.ChainID().String()
		}
		inf.Kind = snapshots.KindCommitted
		inf.Name = key
		return inf, nil
	}

	l, err = s.getLayer(key, true)
	if err != nil {
		return snapshots.Info{}, err
	}

	id, committed := s.getGraphDriverID(key)
	if committed {
		inf.Kind = snapshots.KindCommitted
	}

	if err := s.db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(id))
		if b == nil && l == nil {
			return errors.Errorf("snapshot %s not found", id) // TODO: typed
		}
		inf.Name = key
		if b != nil {
			v := b.Get(keyParent)
			if v != nil {
				inf.Parent = string(v)
				return nil
			}
		}
		if l != nil {
			if p := l.Parent(); p != nil {
				inf.Parent = p.ChainID().String()
			}
			inf.Kind = snapshots.KindCommitted
		}
		return nil
	}); err != nil {
		return snapshots.Info{}, err
	}
	return inf, nil
}

func (s *snapshotter) Mounts(ctx context.Context, key string) (snapshot.Mountable, error) {
	l, err := s.getLayer(key, true)
	if err != nil {
		return nil, err
	}
	if l != nil {
		id := identity.NewID()
		var rwlayer layer.RWLayer
		return &mountable{
			idmap: s.opt.IdentityMapping,
			acquire: func() ([]mount.Mount, error) {
				rwlayer, err = s.opt.LayerStore.CreateRWLayer(id, l.ChainID(), nil)
				if err != nil {
					return nil, err
				}
				rootfs, err := rwlayer.Mount("")
				if err != nil {
					return nil, err
				}
				return []mount.Mount{{
					Source:  rootfs.Path(),
					Type:    "bind",
					Options: []string{"rbind"},
				}}, nil
			},
			release: func() error {
				_, err := s.opt.LayerStore.ReleaseRWLayer(rwlayer)
				return err
			},
		}, nil
	}

	id, _ := s.getGraphDriverID(key)

	return &mountable{
		idmap: s.opt.IdentityMapping,
		acquire: func() ([]mount.Mount, error) {
			rootfs, err := s.opt.GraphDriver.Get(id, "")
			if err != nil {
				return nil, err
			}
			return []mount.Mount{{
				Source:  rootfs.Path(),
				Type:    "bind",
				Options: []string{"rbind"},
			}}, nil
		},
		release: func() error {
			return s.opt.GraphDriver.Put(id)
		},
	}, nil
}

func (s *snapshotter) Remove(ctx context.Context, key string) error {
	l, err := s.getLayer(key, true)
	if err != nil {
		return err
	}

	id, _ := s.getGraphDriverID(key)

	var found bool
	if err := s.db.Update(func(tx *bolt.Tx) error {
		found = tx.Bucket([]byte(key)) != nil
		if found {
			tx.DeleteBucket([]byte(key))
			if id != key {
				tx.DeleteBucket([]byte(id))
			}
		}
		return nil
	}); err != nil {
		return err
	}

	if l != nil {
		s.mu.Lock()
		delete(s.refs, key)
		s.mu.Unlock()
		_, err := s.opt.LayerStore.Release(l)
		return err
	}

	if !found { // this happens when removing views
		return nil
	}

	return s.opt.GraphDriver.Remove(id)
}

func (s *snapshotter) Commit(ctx context.Context, name, key string, opts ...snapshots.Opt) error {
	return s.db.Update(func(tx *bolt.Tx) error {
		b, err := tx.CreateBucketIfNotExists([]byte(name))
		if err != nil {
			return err
		}
		return b.Put(keyCommitted, []byte(key))
	})
}

func (s *snapshotter) View(ctx context.Context, key, parent string, opts ...snapshots.Opt) (snapshot.Mountable, error) {
	return s.Mounts(ctx, parent)
}

func (s *snapshotter) Walk(ctx context.Context, fn func(context.Context, snapshots.Info) error) error {
	return errors.Errorf("not-implemented")
}

func (s *snapshotter) Update(ctx context.Context, info snapshots.Info, fieldpaths ...string) (snapshots.Info, error) {
	// not implemented
	return s.Stat(ctx, info.Name)
}

func (s *snapshotter) Usage(ctx context.Context, key string) (us snapshots.Usage, retErr error) {
	usage := snapshots.Usage{}
	if l, err := s.getLayer(key, true); err != nil {
		return usage, err
	} else if l != nil {
		s, err := l.DiffSize()
		if err != nil {
			return usage, err
		}
		usage.Size = s
		return usage, nil
	}

	size := int64(-1)
	if err := s.db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(key))
		if b == nil {
			return nil
		}
		v := b.Get(keySize)
		if v != nil {
			s, err := strconv.Atoi(string(v))
			if err != nil {
				return err
			}
			size = int64(s)
		}
		return nil
	}); err != nil {
		return usage, err
	}

	if size != -1 {
		usage.Size = size
		return usage, nil
	}

	id, _ := s.getGraphDriverID(key)

	info, err := s.Stat(ctx, key)
	if err != nil {
		return usage, err
	}
	var parent string
	if info.Parent != "" {
		if l, err := s.getLayer(info.Parent, false); err != nil {
			return usage, err
		} else if l != nil {
			parent, err = getGraphID(l)
			if err != nil {
				return usage, err
			}
		} else {
			parent, _ = s.getGraphDriverID(info.Parent)
		}
	}

	diffSize, err := s.opt.GraphDriver.DiffSize(id, parent)
	if err != nil {
		return usage, err
	}

	if err := s.db.Update(func(tx *bolt.Tx) error {
		b, err := tx.CreateBucketIfNotExists([]byte(key))
		if err != nil {
			return err
		}
		return b.Put(keySize, []byte(strconv.Itoa(int(diffSize))))
	}); err != nil {
		return usage, err
	}
	usage.Size = diffSize
	return usage, nil
}

func (s *snapshotter) Close() error {
	return s.db.Close()
}

type mountable struct {
	mu       sync.Mutex
	mounts   []mount.Mount
	acquire  func() ([]mount.Mount, error)
	release  func() error
	refCount int
	idmap    *idtools.IdentityMapping
}

func (m *mountable) Mount() ([]mount.Mount, error) {
	m.mu.Lock()
	defer m.mu.Unlock()

	if m.mounts != nil {
		m.refCount++
		return m.mounts, nil
	}

	mounts, err := m.acquire()
	if err != nil {
		return nil, err
	}
	m.mounts = mounts
	m.refCount = 1

	return m.mounts, nil
}

func (m *mountable) Release() error {
	m.mu.Lock()
	defer m.mu.Unlock()

	if m.refCount > 1 {
		m.refCount--
		return nil
	}

	m.refCount = 0
	if m.release == nil {
		return nil
	}

	m.mounts = nil
	defer func() {
		m.release = nil
	}()
	return m.release()
}

func (m *mountable) IdentityMapping() *idtools.IdentityMapping {
	return m.idmap
}
<commit_msg>builder-next: update mount signature<commit_after>package snapshot

import (
	"context"
	"path/filepath"
	"strconv"
	"strings"
	"sync"

	"github.com/containerd/containerd/mount"
	"github.com/containerd/containerd/snapshots"
	"github.com/docker/docker/daemon/graphdriver"
	"github.com/docker/docker/layer"
	"github.com/docker/docker/pkg/idtools"
	"github.com/moby/buildkit/identity"
	"github.com/moby/buildkit/snapshot"
	digest "github.com/opencontainers/go-digest"
	"github.com/pkg/errors"
	bolt "go.etcd.io/bbolt"
)

var keyParent = []byte("parent")
var keyCommitted = []byte("committed")
var keyChainID = []byte("chainid")
var keySize = []byte("size")

// Opt defines options for creating the snapshotter
type Opt struct {
	GraphDriver     graphdriver.Driver
	LayerStore      layer.Store
	Root            string
	IdentityMapping *idtools.IdentityMapping
}

type graphIDRegistrar interface {
	RegisterByGraphID(string, layer.ChainID, layer.DiffID, string, int64) (layer.Layer, error)
	Release(layer.Layer) ([]layer.Metadata, error)
	checksumCalculator
}

type checksumCalculator interface {
	ChecksumForGraphID(id, parent, oldTarDataPath, newTarDataPath string) (diffID layer.DiffID, size int64, err error)
}

type snapshotter struct {
	opt Opt

	refs map[string]layer.Layer
	db   *bolt.DB
	mu   sync.Mutex
	reg  graphIDRegistrar
}

var _ snapshot.SnapshotterBase = &snapshotter{}

// NewSnapshotter creates a new snapshotter
func NewSnapshotter(opt Opt) (snapshot.SnapshotterBase, error) {
	dbPath := filepath.Join(opt.Root, "snapshots.db")
	db, err := bolt.Open(dbPath, 0600, nil)
	if err != nil {
		return nil, errors.Wrapf(err, "failed to open database file %s", dbPath)
	}

	reg, ok := opt.LayerStore.(graphIDRegistrar)
	if !ok {
		return nil, errors.Errorf("layerstore doesn't support graphID registration")
	}

	s := &snapshotter{
		opt:  opt,
		db:   db,
		refs: map[string]layer.Layer{},
		reg:  reg,
	}
	return s, nil
}

func (s *snapshotter) Name() string {
	return "default"
}

func (s *snapshotter) IdentityMapping() *idtools.IdentityMapping {
	return s.opt.IdentityMapping
}

func (s *snapshotter) Prepare(ctx context.Context, key, parent string, opts ...snapshots.Opt) error {
	origParent := parent
	if parent != "" {
		if l, err := s.getLayer(parent, false); err != nil {
			return err
		} else if l != nil {
			parent, err = getGraphID(l)
			if err != nil {
				return err
			}
		} else {
			parent, _ = s.getGraphDriverID(parent)
		}
	}
	if err := s.opt.GraphDriver.Create(key, parent, nil); err != nil {
		return err
	}
	return s.db.Update(func(tx *bolt.Tx) error {
		b, err := tx.CreateBucketIfNotExists([]byte(key))
		if err != nil {
			return err
		}
		return b.Put(keyParent, []byte(origParent))
	})
}

func (s *snapshotter) chainID(key string) (layer.ChainID, bool) {
	if strings.HasPrefix(key, "sha256:") {
		dgst, err := digest.Parse(key)
		if err != nil {
			return "", false
		}
		return layer.ChainID(dgst), true
	}
	return "", false
}

func (s *snapshotter) GetLayer(key string) (layer.Layer, error) {
	return s.getLayer(key, true)
}

func (s *snapshotter) getLayer(key string, withCommitted bool) (layer.Layer, error) {
	s.mu.Lock()
	l, ok := s.refs[key]
	if !ok {
		id, ok := s.chainID(key)
		if !ok {
			if !withCommitted {
				s.mu.Unlock()
				return nil, nil
			}
			if err := s.db.View(func(tx *bolt.Tx) error {
				b := tx.Bucket([]byte(key))
				if b == nil {
					return nil
				}
				v := b.Get(keyChainID)
				if v != nil {
					id = layer.ChainID(v)
				}
				return nil
			}); err != nil {
				s.mu.Unlock()
				return nil, err
			}
			if id == "" {
				s.mu.Unlock()
				return nil, nil
			}
		}
		var err error
		l, err = s.opt.LayerStore.Get(id)
		if err != nil {
			s.mu.Unlock()
			return nil, err
		}
		s.refs[key] = l
		if err := s.db.Update(func(tx *bolt.Tx) error {
			_, err := tx.CreateBucketIfNotExists([]byte(key))
			return err
		}); err != nil {
			s.mu.Unlock()
			return nil, err
		}
	}
	s.mu.Unlock()

	return l, nil
}

func (s *snapshotter) getGraphDriverID(key string) (string, bool) {
	var gdID string
	if err := s.db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(key))
		if b == nil {
			return errors.Errorf("not found") // TODO: typed
		}
		v := b.Get(keyCommitted)
		if v != nil {
			gdID = string(v)
		}
		return nil
	}); err != nil || gdID == "" {
		return key, false
	}
	return gdID, true
}

func (s *snapshotter) Stat(ctx context.Context, key string) (snapshots.Info, error) {
	inf := snapshots.Info{
		Kind: snapshots.KindActive,
	}

	l, err := s.getLayer(key, false)
	if err != nil {
		return snapshots.Info{}, err
	}
	if l != nil {
		if p := l.Parent(); p != nil {
			inf.Parent = p.ChainID().String()
		}
		inf.Kind = snapshots.KindCommitted
		inf.Name = key
		return inf, nil
	}

	l, err = s.getLayer(key, true)
	if err != nil {
		return snapshots.Info{}, err
	}

	id, committed := s.getGraphDriverID(key)
	if committed {
		inf.Kind = snapshots.KindCommitted
	}

	if err := s.db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(id))
		if b == nil && l == nil {
			return errors.Errorf("snapshot %s not found", id) // TODO: typed
		}
		inf.Name = key
		if b != nil {
			v := b.Get(keyParent)
			if v != nil {
				inf.Parent = string(v)
				return nil
			}
		}
		if l != nil {
			if p := l.Parent(); p != nil {
				inf.Parent = p.ChainID().String()
			}
			inf.Kind = snapshots.KindCommitted
		}
		return nil
	}); err != nil {
		return snapshots.Info{}, err
	}
	return inf, nil
}

func (s *snapshotter) Mounts(ctx context.Context, key string) (snapshot.Mountable, error) {
	l, err := s.getLayer(key, true)
	if err != nil {
		return nil, err
	}
	if l != nil {
		id := identity.NewID()
		var rwlayer layer.RWLayer
		return &mountable{
			idmap: s.opt.IdentityMapping,
			acquire: func() ([]mount.Mount, func() error, error) {
				rwlayer, err = s.opt.LayerStore.CreateRWLayer(id, l.ChainID(), nil)
				if err != nil {
					return nil, nil, err
				}
				rootfs, err := rwlayer.Mount("")
				if err != nil {
					return nil, nil, err
				}
				return []mount.Mount{{
						Source:  rootfs.Path(),
						Type:    "bind",
						Options: []string{"rbind"},
					}}, func() error {
						_, err := s.opt.LayerStore.ReleaseRWLayer(rwlayer)
						return err
					}, nil
			},
		}, nil
	}

	id, _ := s.getGraphDriverID(key)

	return &mountable{
		idmap: s.opt.IdentityMapping,
		acquire: func() ([]mount.Mount, func() error, error) {
			rootfs, err := s.opt.GraphDriver.Get(id, "")
			if err != nil {
				return nil, nil, err
			}
			return []mount.Mount{{
					Source:  rootfs.Path(),
					Type:    "bind",
					Options: []string{"rbind"},
				}}, func() error {
					return s.opt.GraphDriver.Put(id)
				}, nil
		},
	}, nil
}

func (s *snapshotter) Remove(ctx context.Context, key string) error {
	l, err := s.getLayer(key, true)
	if err != nil {
		return err
	}

	id, _ := s.getGraphDriverID(key)

	var found bool
	if err := s.db.Update(func(tx *bolt.Tx) error {
		found = tx.Bucket([]byte(key)) != nil
		if found {
			tx.DeleteBucket([]byte(key))
			if id != key {
				tx.DeleteBucket([]byte(id))
			}
		}
		return nil
	}); err != nil {
		return err
	}

	if l != nil {
		s.mu.Lock()
		delete(s.refs, key)
		s.mu.Unlock()
		_, err := s.opt.LayerStore.Release(l)
		return err
	}

	if !found { // this happens when removing views
		return nil
	}

	return s.opt.GraphDriver.Remove(id)
}

func (s *snapshotter) Commit(ctx context.Context, name, key string, opts ...snapshots.Opt) error {
	return s.db.Update(func(tx *bolt.Tx) error {
		b, err := tx.CreateBucketIfNotExists([]byte(name))
		if err != nil {
			return err
		}
		return b.Put(keyCommitted, []byte(key))
	})
}

func (s *snapshotter) View(ctx context.Context, key, parent string, opts ...snapshots.Opt) (snapshot.Mountable, error) {
	return s.Mounts(ctx, parent)
}

func (s *snapshotter) Walk(ctx context.Context, fn func(context.Context, snapshots.Info) error) error {
	return errors.Errorf("not-implemented")
}

func (s *snapshotter) Update(ctx context.Context, info snapshots.Info, fieldpaths ...string) (snapshots.Info, error) {
	// not implemented
	return s.Stat(ctx, info.Name)
}

func (s *snapshotter) Usage(ctx context.Context, key string) (us snapshots.Usage, retErr error) {
	usage := snapshots.Usage{}
	if l, err := s.getLayer(key, true); err != nil {
		return usage, err
	} else if l != nil {
		s, err := l.DiffSize()
		if err != nil {
			return usage, err
		}
		usage.Size = s
		return usage, nil
	}

	size := int64(-1)
	if err := s.db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(key))
		if b == nil {
			return nil
		}
		v := b.Get(keySize)
		if v != nil {
			s, err := strconv.Atoi(string(v))
			if err != nil {
				return err
			}
			size = int64(s)
		}
		return nil
	}); err != nil {
		return usage, err
	}

	if size != -1 {
		usage.Size = size
		return usage, nil
	}

	id, _ := s.getGraphDriverID(key)

	info, err := s.Stat(ctx, key)
	if err != nil {
		return usage, err
	}
	var parent string
	if info.Parent != "" {
		if l, err := s.getLayer(info.Parent, false); err != nil {
			return usage, err
		} else if l != nil {
			parent, err = getGraphID(l)
			if err != nil {
				return usage, err
			}
		} else {
			parent, _ = s.getGraphDriverID(info.Parent)
		}
	}

	diffSize, err := s.opt.GraphDriver.DiffSize(id, parent)
	if err != nil {
		return usage, err
	}

	if err := s.db.Update(func(tx *bolt.Tx) error {
		b, err := tx.CreateBucketIfNotExists([]byte(key))
		if err != nil {
			return err
		}
		return b.Put(keySize, []byte(strconv.Itoa(int(diffSize))))
	}); err != nil {
		return usage, err
	}
	usage.Size = diffSize
	return usage, nil
}

func (s *snapshotter) Close() error {
	return s.db.Close()
}

type mountable struct {
	mu       sync.Mutex
	mounts   []mount.Mount
	acquire  func() ([]mount.Mount, func() error, error)
	release  func() error
	refCount int
	idmap    *idtools.IdentityMapping
}

func (m *mountable) Mount() ([]mount.Mount, func() error, error) {
	m.mu.Lock()
	defer m.mu.Unlock()

	if m.mounts != nil {
		m.refCount++
		return m.mounts, m.releaseMount, nil
	}

	mounts, release, err := m.acquire()
	if err != nil {
		return nil, nil, err
	}
	m.mounts = mounts
	m.release = release
	m.refCount = 1

	return m.mounts, m.releaseMount, nil
}

func (m *mountable) releaseMount() error {
	m.mu.Lock()
	defer m.mu.Unlock()

	if m.refCount > 1 {
		m.refCount--
		return nil
	}

	m.refCount = 0
	if m.release == nil {
		return nil
	}

	m.mounts = nil
	defer func() {
		m.release = nil
	}()
	return m.release()
}

func (m *mountable) IdentityMapping() *idtools.IdentityMapping {
	return m.idmap
}
<|endoftext|>


<commit_before>// UVa 133 - The Dole Queue

package main

import (
	"fmt"
	"os"
	"strings"
)

func main() {
	in, _ := os.Open("133.in")
	defer in.Close()
	out, _ := os.Create("133.out")
	defer out.Close()

	var n, k, m int
	for {
		if fmt.Fscanf(in, "%d%d%d", &n, &k, &m); n == 0 {
			break
		}
		dole := make([]int, n)
		for i := range dole {
			dole[i] = i + 1
		}
		pk, pm := 0, n-1
		outNum := 0
		var outs []string
		for {
			for i := 1; i < k; i++ {
				for {
					if pk++; pk == n {
						pk = 0
					}
					if dole[pk] != -1 {
						break
					}
				}
			}
			for i := 1; i < m; i++ {
				for {
					if pm--; pm == -1 {
						pm = n - 1
					}
					if dole[pm] != -1 {
						break
					}
				}
			}
			if pk == pm {
				outNum++
				outs = append(outs, fmt.Sprintf("% 3d", dole[pk]))
			} else {
				outNum += 2
				outs = append(outs, fmt.Sprintf("% 3d% 3d", dole[pk], dole[pm]))
			}
			if outNum == len(dole) {
				break
			}
			dole[pk], dole[pm] = -1, -1
			for {
				if pk++; pk == n {
					pk = 0
				}
				if dole[pk] != -1 {
					break
				}
			}
			for {
				if pm--; pm == -1 {
					pm = n - 1
				}
				if dole[pm] != -1 {
					break
				}
			}
		}
		fmt.Fprintln(out, strings.Join(outs, ","))
	}
}
<commit_msg>code clean up<commit_after>// UVa 133 - The Dole Queue

package main

import (
	"fmt"
	"os"
	"strings"
)

func solve(n, k, m int, out *os.File) {
	dole := make([]int, n)
	for i := range dole {
		dole[i] = i + 1
	}
	pk, pm := 0, n-1
	outNum := 0
	var outs []string
	for {
		for i := 1; i < k; i++ {
			for {
				if pk++; pk == n {
					pk = 0
				}
				if dole[pk] != -1 {
					break
				}
			}
		}
		for i := 1; i < m; i++ {
			for {
				if pm--; pm == -1 {
					pm = n - 1
				}
				if dole[pm] != -1 {
					break
				}
			}
		}
		if pk == pm {
			outNum++
			outs = append(outs, fmt.Sprintf("% 3d", dole[pk]))
		} else {
			outNum += 2
			outs = append(outs, fmt.Sprintf("% 3d% 3d", dole[pk], dole[pm]))
		}
		if outNum == len(dole) {
			break
		}
		dole[pk], dole[pm] = -1, -1
		for {
			if pk++; pk == n {
				pk = 0
			}
			if dole[pk] != -1 {
				break
			}
		}
		for {
			if pm--; pm == -1 {
				pm = n - 1
			}
			if dole[pm] != -1 {
				break
			}
		}
	}
	fmt.Fprintln(out, strings.Join(outs, ","))
}

func main() {
	in, _ := os.Open("133.in")
	defer in.Close()
	out, _ := os.Create("133.out")
	defer out.Close()

	var n, k, m int
	for {
		if fmt.Fscanf(in, "%d%d%d", &n, &k, &m); n == 0 {
			break
		}
		solve(n, k, m, out)
	}
}
<|endoftext|>