<commit_before>package opentracing

import (
	"net/http"
	"net/url"
	"strings"
)

const (
	// OpenTracingContextIDHTTPHeaderPrefix precedes the opentracing-related
	// ContextID HTTP headers.
	OpenTracingContextIDHTTPHeaderPrefix = "Open-Tracing-Context-Id-"
	// OpenTracingTagsHTTPHeaderPrefix precedes the opentracing-related
	// trace-tags HTTP headers.
	OpenTracingTagsHTTPHeaderPrefix = "Open-Tracing-Trace-Tags-"
)

// AddTraceContextToHeader marshals TraceContext `ctx` to `h` as a series of
// HTTP headers. Values are URL-escaped.
func AddTraceContextToHeader(
	ctx TraceContext,
	h http.Header,
	marshaler TraceContextMarshaler,
) {
	contextIDMap, tagsMap := marshaler.MarshalTraceContextStringMap(ctx)
	for headerSuffix, val := range contextIDMap {
		h.Add(OpenTracingContextIDHTTPHeaderPrefix+headerSuffix, url.QueryEscape(val))
	}
	for headerSuffix, val := range tagsMap {
		h.Add(OpenTracingTagsHTTPHeaderPrefix+headerSuffix, url.QueryEscape(val))
	}
}

// TraceContextFromHeader unmarshals a TraceContext from `h`, expecting that
// header values are URL-escpaed.
func TraceContextFromHeader(
	h http.Header,
	unmarshaler TraceContextUnmarshaler,
) (TraceContext, error) {
	contextIDMap := make(map[string]string)
	tagsMap := make(map[string]string)
	for key, val := range h {
		if strings.HasPrefix(key, OpenTracingContextIDHTTPHeaderPrefix) {
			// We don't know what to do with anything beyond slice item v[0]:
			unescaped, err := url.QueryUnescape(val[0])
			if err != nil {
				return nil, err
			}
			contextIDMap[strings.TrimPrefix(key, OpenTracingContextIDHTTPHeaderPrefix)] = unescaped
		} else if strings.HasPrefix(key, OpenTracingTagsHTTPHeaderPrefix) {
			// We don't know what to do with anything beyond slice item v[0]:
			unescaped, err := url.QueryUnescape(val[0])
			if err != nil {
				return nil, err
			}
			tagsMap[strings.TrimPrefix(key, OpenTracingTagsHTTPHeaderPrefix)] = unescaped
		}

	}
	return unmarshaler.UnmarshalTraceContextStringMap(contextIDMap, tagsMap)
}
<commit_msg>opentracing: make HTTP header prefix constants more readable<commit_after>package opentracing

import (
	"net/http"
	"net/url"
	"strings"
)

const (
	// ContextIDHTTPHeaderPrefix precedes the opentracing-related ContextID HTTP
	// headers.
	ContextIDHTTPHeaderPrefix = "Open-Tracing-Context-Id-"

	// TagsHTTPHeaderPrefix precedes the opentracing-related trace-tags HTTP
	// headers.
	TagsHTTPHeaderPrefix = "Open-Tracing-Trace-Tags-"
)

// AddTraceContextToHeader marshals TraceContext `ctx` to `h` as a series of
// HTTP headers. Values are URL-escaped.
func AddTraceContextToHeader(
	ctx TraceContext,
	h http.Header,
	marshaler TraceContextMarshaler,
) {
	contextIDMap, tagsMap := marshaler.MarshalTraceContextStringMap(ctx)
	for headerSuffix, val := range contextIDMap {
		h.Add(ContextIDHTTPHeaderPrefix+headerSuffix, url.QueryEscape(val))
	}
	for headerSuffix, val := range tagsMap {
		h.Add(TagsHTTPHeaderPrefix+headerSuffix, url.QueryEscape(val))
	}
}

// TraceContextFromHeader unmarshals a TraceContext from `h`, expecting that
// header values are URL-escpaed.
func TraceContextFromHeader(
	h http.Header,
	unmarshaler TraceContextUnmarshaler,
) (TraceContext, error) {
	contextIDMap := make(map[string]string)
	tagsMap := make(map[string]string)
	for key, val := range h {
		if strings.HasPrefix(key, ContextIDHTTPHeaderPrefix) {
			// We don't know what to do with anything beyond slice item v[0]:
			unescaped, err := url.QueryUnescape(val[0])
			if err != nil {
				return nil, err
			}
			contextIDMap[strings.TrimPrefix(key, ContextIDHTTPHeaderPrefix)] = unescaped
		} else if strings.HasPrefix(key, TagsHTTPHeaderPrefix) {
			// We don't know what to do with anything beyond slice item v[0]:
			unescaped, err := url.QueryUnescape(val[0])
			if err != nil {
				return nil, err
			}
			tagsMap[strings.TrimPrefix(key, TagsHTTPHeaderPrefix)] = unescaped
		}

	}
	return unmarshaler.UnmarshalTraceContextStringMap(contextIDMap, tagsMap)
}
<|endoftext|>


<commit_before>package otto

import (
	"encoding/json"
	"math"
	"reflect"
	"testing"
)

func TestValue(t *testing.T) {
	tt(t, func() {
		value := UndefinedValue()
		is(value.IsUndefined(), true)
		is(value, UndefinedValue())
		is(value, "undefined")

		is(toValue(false), false)
		is(toValue(1), 1)
		is(toValue(1).float64(), float64(1))
	})
}

func TestObject(t *testing.T) {
	tt(t, func() {
		is(emptyValue.isEmpty(), true)
		//is(newObject().Value(), "[object]")
		//is(newBooleanObject(false).Value(), "false")
		//is(newFunctionObject(nil).Value(), "[function]")
		//is(newNumberObject(1).Value(), "1")
		//is(newStringObject("Hello, World.").Value(), "Hello, World.")
	})
}

type intAlias int

func TestToValue(t *testing.T) {
	tt(t, func() {
		_, tester := test()
		vm := tester.vm

		value, _ := vm.ToValue(nil)
		is(value, "undefined")

		value, _ = vm.ToValue((*byte)(nil))
		is(value, "undefined")

		value, _ = vm.ToValue(intAlias(5))
		is(value, 5)

		{
			tmp := new(int)

			value, _ = vm.ToValue(&tmp)
			is(value, 0)

			*tmp = 1

			value, _ = vm.ToValue(&tmp)
			is(value, 1)

			tmp = nil

			value, _ = vm.ToValue(&tmp)
			is(value, "undefined")
		}

		{
			tmp0 := new(int)
			tmp1 := &tmp0
			tmp2 := &tmp1

			value, _ = vm.ToValue(&tmp2)
			is(value, 0)

			*tmp0 = 1

			value, _ = vm.ToValue(&tmp2)
			is(value, 1)

			tmp0 = nil

			value, _ = vm.ToValue(&tmp2)
			is(value, "undefined")
		}
	})
}

func TestToBoolean(t *testing.T) {
	tt(t, func() {
		is := func(left interface{}, right bool) {
			is(toValue(left).bool(), right)
		}

		is("", false)
		is("xyzzy", true)
		is(1, true)
		is(0, false)
		//is(toValue(newObject()), true)
		is(UndefinedValue(), false)
		is(NullValue(), false)
	})
}

func TestToFloat(t *testing.T) {
	tt(t, func() {
		{
			is := func(left interface{}, right float64) {
				is(toValue(left).float64(), right)
			}
			is("", 0)
			is("xyzzy", math.NaN())
			is("2", 2)
			is(1, 1)
			is(0, 0)
			is(NullValue(), 0)
			//is(newObjectValue(), math.NaN())
		}
		is(math.IsNaN(UndefinedValue().float64()), true)
	})
}

func TestToString(t *testing.T) {
	tt(t, func() {
		is("undefined", UndefinedValue().string())
		is("null", NullValue().string())
		is("true", toValue(true).string())
		is("false", toValue(false).string())

		is(UndefinedValue(), "undefined")
		is(NullValue(), "null")
		is(toValue(true), true)
		is(toValue(false), false)
	})
}

func Test_toInt32(t *testing.T) {
	tt(t, func() {
		test := []interface{}{
			0, int32(0),
			1, int32(1),
			-2147483649.0, int32(2147483647),
			-4294967297.0, int32(-1),
			-4294967296.0, int32(0),
			-4294967295.0, int32(1),
			math.Inf(+1), int32(0),
			math.Inf(-1), int32(0),
		}
		for index := 0; index < len(test)/2; index++ {
			// FIXME terst, Make strict again?
			is(
				toInt32(toValue(test[index*2])),
				test[index*2+1].(int32),
			)
		}
	})
}

func Test_toUint32(t *testing.T) {
	tt(t, func() {
		test := []interface{}{
			0, uint32(0),
			1, uint32(1),
			-2147483649.0, uint32(2147483647),
			-4294967297.0, uint32(4294967295),
			-4294967296.0, uint32(0),
			-4294967295.0, uint32(1),
			math.Inf(+1), uint32(0),
			math.Inf(-1), uint32(0),
		}
		for index := 0; index < len(test)/2; index++ {
			// FIXME terst, Make strict again?
			is(
				toUint32(toValue(test[index*2])),
				test[index*2+1].(uint32),
			)
		}
	})
}

func Test_toUint16(t *testing.T) {
	tt(t, func() {
		test := []interface{}{
			0, uint16(0),
			1, uint16(1),
			-2147483649.0, uint16(65535),
			-4294967297.0, uint16(65535),
			-4294967296.0, uint16(0),
			-4294967295.0, uint16(1),
			math.Inf(+1), uint16(0),
			math.Inf(-1), uint16(0),
		}
		for index := 0; index < len(test)/2; index++ {
			// FIXME terst, Make strict again?
			is(
				toUint16(toValue(test[index*2])),
				test[index*2+1].(uint16),
			)
		}
	})
}

func Test_sameValue(t *testing.T) {
	tt(t, func() {
		is(sameValue(positiveZeroValue(), negativeZeroValue()), false)
		is(sameValue(positiveZeroValue(), toValue(0)), true)
		is(sameValue(NaNValue(), NaNValue()), true)
		is(sameValue(NaNValue(), toValue("Nothing happens.")), false)
	})
}

func TestExport(t *testing.T) {
	tt(t, func() {
		test, vm := test()

		is(test(`null;`).export(), nil)
		is(test(`undefined;`).export(), nil)
		is(test(`true;`).export(), true)
		is(test(`false;`).export(), false)
		is(test(`0;`).export(), 0)
		is(test(`3.1459`).export(), 3.1459)
		is(test(`"Nothing happens";`).export(), "Nothing happens")
		is(test(`String.fromCharCode(97,98,99,100,101,102)`).export(), "abcdef")
		{
			value := test(`({ abc: 1, def: true, ghi: undefined, jkl: null });`).export().(map[string]interface{})
			is(value["abc"], 1)
			is(value["def"], true)
			is(value["ghi"], nil)
			is(value["jkl"], nil)
		}
		{
			value := test(`[ "abc", 1, "def", true, undefined, null ];`).export().([]interface{})
			is(value[0], "abc")
			is(value[1], 1)
			is(value[2], "def")
			is(value[3], true)
			is(value[4], nil)
			is(value[5], nil)
			is(value[5], interface{}(nil))
		}
		{
			value := test(`[ undefined, null ];`).export().([]interface{})
			is(value[0], nil)
			is(value[1], nil)
			is(value[1], interface{}(nil))
		}

		roundtrip := []interface{}{
			true,
			false,
			0,
			3.1459,
			[]interface{}{true, false, 0, 3.1459, "abc"},
			map[string]interface{}{
				"Boolean": true,
				"Number":  3.1459,
				"String":  "abc",
				"Array":   []interface{}{false, 0, "", nil},
				"Object": map[string]interface{}{
					"Boolean": false,
					"Number":  0,
					"String":  "def",
				},
			},
		}

		for _, value := range roundtrip {
			input, err := json.Marshal(value)
			is(err, nil)

			output, err := json.Marshal(test("(" + string(input) + ");").export())
			is(err, nil)

			is(string(input), string(output))
		}

		{
			abc := struct {
				def int
				ghi interface{}
				xyz float32
			}{}
			abc.def = 3
			abc.xyz = 3.1459
			vm.Set("abc", abc)
			is(test(`abc;`).export(), abc)
		}
	})
}

func Test_toReflectValue(t *testing.T) {
	tt(t, func() {
		value := toValue(0.0)
		tmp, err := value.toReflectValue(reflect.Float32)
		is(tmp.Float(), 0.0)
		is(err, nil)
	})
}
<commit_msg>Revert, ref pull request #2<commit_after>package otto

import (
	"encoding/json"
	"math"
	"reflect"
	"testing"
)

func TestValue(t *testing.T) {
	tt(t, func() {
		value := UndefinedValue()
		is(value.IsUndefined(), true)
		is(value, UndefinedValue())
		is(value, "undefined")

		is(toValue(false), false)
		is(toValue(1), 1)
		is(toValue(1).float64(), float64(1))
	})
}

func TestObject(t *testing.T) {
	tt(t, func() {
		is(emptyValue.isEmpty(), true)
		//is(newObject().Value(), "[object]")
		//is(newBooleanObject(false).Value(), "false")
		//is(newFunctionObject(nil).Value(), "[function]")
		//is(newNumberObject(1).Value(), "1")
		//is(newStringObject("Hello, World.").Value(), "Hello, World.")
	})
}

type intAlias int

func TestToValue(t *testing.T) {
	tt(t, func() {
		_, tester := test()
		vm := tester.vm

		value, _ := vm.ToValue(nil)
		is(value, "undefined")

		value, _ = vm.ToValue((*byte)(nil))
		is(value, "undefined")

		value, _ = vm.ToValue(intAlias(5))
		is(value, 5)

		{
			tmp := new(int)

			value, _ = vm.ToValue(&tmp)
			is(value, 0)

			*tmp = 1

			value, _ = vm.ToValue(&tmp)
			is(value, 1)

			tmp = nil

			value, _ = vm.ToValue(&tmp)
			is(value, "undefined")
		}

		{
			tmp0 := new(int)
			tmp1 := &tmp0
			tmp2 := &tmp1

			value, _ = vm.ToValue(&tmp2)
			is(value, 0)

			*tmp0 = 1

			value, _ = vm.ToValue(&tmp2)
			is(value, 1)

			tmp0 = nil

			value, _ = vm.ToValue(&tmp2)
			is(value, "undefined")
		}
	})
}

func TestToBoolean(t *testing.T) {
	tt(t, func() {
		is := func(left interface{}, right bool) {
			is(toValue(left).bool(), right)
		}

		is("", false)
		is("xyzzy", true)
		is(1, true)
		is(0, false)
		//is(toValue(newObject()), true)
		is(UndefinedValue(), false)
		is(NullValue(), false)
	})
}

func TestToFloat(t *testing.T) {
	tt(t, func() {
		{
			is := func(left interface{}, right float64) {
				is(toValue(left).float64(), right)
			}
			is("", 0)
			is("xyzzy", math.NaN())
			is("2", 2)
			is(1, 1)
			is(0, 0)
			is(NullValue(), 0)
			//is(newObjectValue(), math.NaN())
		}
		is(math.IsNaN(UndefinedValue().float64()), true)
	})
}

func TestToString(t *testing.T) {
	tt(t, func() {
		is("undefined", UndefinedValue().string())
		is("null", NullValue().string())
		is("true", toValue(true).string())
		is("false", toValue(false).string())

		is(UndefinedValue(), "undefined")
		is(NullValue(), "null")
		is(toValue(true), true)
		is(toValue(false), false)
	})
}

func Test_toInt32(t *testing.T) {
	tt(t, func() {
		test := []interface{}{
			0, int32(0),
			1, int32(1),
			-2147483649.0, int32(2147483647),
			-4294967297.0, int32(-1),
			-4294967296.0, int32(0),
			-4294967295.0, int32(1),
			math.Inf(+1), int32(0),
			math.Inf(-1), int32(0),
		}
		for index := 0; index < len(test)/2; index++ {
			// FIXME terst, Make strict again?
			is(
				toInt32(toValue(test[index*2])),
				test[index*2+1].(int32),
			)
		}
	})
}

func Test_toUint32(t *testing.T) {
	tt(t, func() {
		test := []interface{}{
			0, uint32(0),
			1, uint32(1),
			-2147483649.0, uint32(2147483647),
			-4294967297.0, uint32(4294967295),
			-4294967296.0, uint32(0),
			-4294967295.0, uint32(1),
			math.Inf(+1), uint32(0),
			math.Inf(-1), uint32(0),
		}
		for index := 0; index < len(test)/2; index++ {
			// FIXME terst, Make strict again?
			is(
				toUint32(toValue(test[index*2])),
				test[index*2+1].(uint32),
			)
		}
	})
}

func Test_toUint16(t *testing.T) {
	tt(t, func() {
		test := []interface{}{
			0, uint16(0),
			1, uint16(1),
			-2147483649.0, uint16(65535),
			-4294967297.0, uint16(65535),
			-4294967296.0, uint16(0),
			-4294967295.0, uint16(1),
			math.Inf(+1), uint16(0),
			math.Inf(-1), uint16(0),
		}
		for index := 0; index < len(test)/2; index++ {
			// FIXME terst, Make strict again?
			is(
				toUint16(toValue(test[index*2])),
				test[index*2+1].(uint16),
			)
		}
	})
}

func Test_sameValue(t *testing.T) {
	tt(t, func() {
		is(sameValue(positiveZeroValue(), negativeZeroValue()), false)
		is(sameValue(positiveZeroValue(), toValue(0)), true)
		is(sameValue(NaNValue(), NaNValue()), true)
		is(sameValue(NaNValue(), toValue("Nothing happens.")), false)
	})
}

func TestExport(t *testing.T) {
	tt(t, func() {
		test, vm := test()

		is(test(`null;`).export(), nil)
		is(test(`undefined;`).export(), nil)
		is(test(`true;`).export(), true)
		is(test(`false;`).export(), false)
		is(test(`0;`).export(), 0)
		is(test(`3.1459`).export(), 3.1459)
		is(test(`"Nothing happens";`).export(), "Nothing happens")
		is(test(`String.fromCharCode(97,98,99,100,101,102)`).export(), "abcdef")
		{
			value := test(`({ abc: 1, def: true, ghi: undefined, jkl: null });`).export().(map[string]interface{})
			is(value["abc"], 1)
			is(value["def"], true)
			_, exists := value["ghi"]
			is(exists, false)
			is(value["jkl"], nil)
		}
		{
			value := test(`[ "abc", 1, "def", true, undefined, null ];`).export().([]interface{})
			is(value[0], "abc")
			is(value[1], 1)
			is(value[2], "def")
			is(value[3], true)
			is(value[4], nil)
			is(value[5], nil)
			is(value[5], interface{}(nil))
		}
		{
			value := test(`[ undefined, null ];`).export().([]interface{})
			is(value[0], nil)
			is(value[1], nil)
			is(value[1], interface{}(nil))
		}

		roundtrip := []interface{}{
			true,
			false,
			0,
			3.1459,
			[]interface{}{true, false, 0, 3.1459, "abc"},
			map[string]interface{}{
				"Boolean": true,
				"Number":  3.1459,
				"String":  "abc",
				"Array":   []interface{}{false, 0, "", nil},
				"Object": map[string]interface{}{
					"Boolean": false,
					"Number":  0,
					"String":  "def",
				},
			},
		}

		for _, value := range roundtrip {
			input, err := json.Marshal(value)
			is(err, nil)

			output, err := json.Marshal(test("(" + string(input) + ");").export())
			is(err, nil)

			is(string(input), string(output))
		}

		{
			abc := struct {
				def int
				ghi interface{}
				xyz float32
			}{}
			abc.def = 3
			abc.xyz = 3.1459
			vm.Set("abc", abc)
			is(test(`abc;`).export(), abc)
		}
	})
}

func Test_toReflectValue(t *testing.T) {
	tt(t, func() {
		value := toValue(0.0)
		tmp, err := value.toReflectValue(reflect.Float32)
		is(tmp.Float(), 0.0)
		is(err, nil)
	})
}
<|endoftext|>


<commit_before>package common

import (
	"net"
	"fmt"
	"errors"
	"encoding/binary"
)

type Session struct {
	addr uint32
	table map[uint32]*net.UDPAddr
	next_addr uint8
}

func NewSessions(addr net.IP) *Session {
	return &Session {
		binary.BigEndian.Uint32(addr.To4()),
		make(map[uint32]*net.UDPAddr),
		2,
	}
}

func (n *Session) NewClient(tuple *net.UDPAddr) uint32 {
	result := n.addr & 0xffffff00 | uint32(n.next_addr)
	n.next_addr++

	n.table[result] = tuple
	return result
}

func (n *Session) Lookup(addr uint32) (*net.UDPAddr, error) {
	tuple, ok := n.table[addr]
	if !ok {
		var buf net.IP
		buf.To4()
		binary.BigEndian.PutUint32(buf, addr)
		msg := fmt.Sprintf("No session found for internal IP address: %d", buf.String())
		return nil, errors.New(msg)
	}
	return tuple, nil
}<commit_msg>bugfix<commit_after>package common

import (
	"net"
	"fmt"
	"errors"
	"encoding/binary"
)

type Session struct {
	addr uint32
	table map[uint32]*net.UDPAddr
	next_addr uint8
}

func NewSessions(addr net.IP) *Session {
	return &Session {
		binary.BigEndian.Uint32(addr.To4()),
		make(map[uint32]*net.UDPAddr),
		2,
	}
}

func (n *Session) NewClient(tuple *net.UDPAddr) uint32 {
	result := n.addr & 0xffffff00 | uint32(n.next_addr)
	n.next_addr++

	n.table[result] = tuple
	return result
}

func (n *Session) Lookup(addr uint32) (*net.UDPAddr, error) {
	tuple, ok := n.table[addr]
	if !ok {
		buf := make([]byte, 4)
		binary.BigEndian.PutUint32(buf, addr)
		var ip net.IP
		ip = buf
		msg := fmt.Sprintf("No session found for internal IP address: %d", ip.String())
		return nil, errors.New(msg)
	}
	return tuple, nil
}<|endoftext|>


<commit_before>package compiler

import "golang.org/x/sys/unix"

type shift struct {
	position int
}

func (c *compilerContext) isLongJump(jumpSize int) bool {
	return jumpSize > c.maxJumpSize
}

func hasLongJump(index int, jts, jfs map[int]int) bool {
	// Using the unshifted index to look up positions in jts and jfs is
	// only safe if we're iterating backwards. Otherwise we would have to
	// fix up the positions in the maps as well and that would be fugly.

	if _, ok := jts[index]; ok {
		return true
	}
	if _, ok := jfs[index]; ok {
		return true
	}
	return false
}

func fixupWithShifts(pos, add int, shifts []shift) int {
	to := pos + add + 1
	currentAdd := add
	for _, s := range shifts {
		if s.position > pos && s.position <= to {
			currentAdd++
			to++
		}
	}
	return currentAdd
}

func (c *compilerContext) fixupJumps() {
	maxIndexWithLongJump := -1
	jtLongJumps := make(map[int]int)
	jfLongJumps := make(map[int]int)

	for l, at := range c.labels.allLabels() {
		for _, pos := range c.jts.allJumpsTo(l) {
			jumpSize := (at - pos) - 1
			if c.isLongJump(jumpSize) {
				if maxIndexWithLongJump < pos {
					maxIndexWithLongJump = pos
				}
				jtLongJumps[pos] = jumpSize
			} else {
				c.result[pos].Jt = uint8(jumpSize)
			}
		}

		for _, pos := range c.jfs.allJumpsTo(l) {
			jumpSize := (at - pos) - 1
			if c.isLongJump(jumpSize) {
				if maxIndexWithLongJump < pos {
					maxIndexWithLongJump = pos
				}
				jfLongJumps[pos] = jumpSize
			} else {
				c.result[pos].Jf = uint8(jumpSize)
			}
		}

		for _, pos := range c.uconds.allJumpsTo(l) {
			c.result[pos].K = uint32((at - pos) - 1)
		}
	}

	// This has no impact on our tests
	//if maxIndexWithLongJump == -1 {
	//	return
	//}

	shifts := []shift{}

	currentIndex := maxIndexWithLongJump
	for currentIndex > -1 {
		current := c.result[currentIndex]

		if isConditionalJump(current) && hasLongJump(currentIndex, jtLongJumps, jfLongJumps) {
			hadJt := c.handleJTLongJumpFor(currentIndex, jtLongJumps, jfLongJumps, &shifts)
			c.handleJFLongJumpFor(currentIndex, jfLongJumps, hadJt, &shifts)
		} else {
			if isUnconditionalJump(current) {
				c.result[currentIndex].K = uint32(fixupWithShifts(currentIndex, int(c.result[currentIndex].K), shifts))
			} else {
				hadJt := c.shiftJt(currentIndex, &shifts)
				c.shiftJf(hadJt, currentIndex, &shifts)
			}
		}
		currentIndex--
	}
}

func (c *compilerContext) handleJTLongJumpFor(currentIndex int, jtLongJumps map[int]int, jfLongJumps map[int]int, shifts *[]shift) bool {
	hadJt := false
	if jmpLen, ok := jtLongJumps[currentIndex]; ok {
		jmpLen = fixupWithShifts(currentIndex, jmpLen, *shifts)
		hadJt = true

		newJf := int(c.result[currentIndex].Jf) + 1
		if c.isLongJump(newJf) {
			// Simple case, we can just add it to the long jumps for JF:
			jfLongJumps[currentIndex] = newJf
		} else {
			c.result[currentIndex].Jf = uint8(newJf)
		}

		shifts = c.insertJumps(currentIndex, jmpLen, 0, shifts)
	}
	return hadJt
}

func (c *compilerContext) handleJFLongJumpFor(currentIndex int, jfLongJumps map[int]int, hadJt bool, shifts *[]shift) {
	if jmpLen, ok := jfLongJumps[currentIndex]; ok {
		jmpLen = fixupWithShifts(currentIndex, jmpLen, *shifts)
		var incr int
		shifts, incr, jmpLen = c.increment(hadJt, jmpLen, currentIndex, shifts)
		shifts = c.insertJumps(currentIndex, jmpLen, incr, shifts)
	}
}

func (c *compilerContext) increment(hadJt bool, jmpLen, currentIndex int, shifts *[]shift) (*[]shift, int, int) {
	incr := 0
	if hadJt {
		c.result[currentIndex+1].K++
		incr++
		jmpLen--
	} else {
		newJt := int(c.result[currentIndex].Jt) + 1
		if c.isLongJump(newJt) {
			// incr in this case doesn't seem to do much, all tests pass when it is changed to 0
			shifts = c.insertJumps(currentIndex, newJt, incr, shifts)
			incr++
		} else {
			c.result[currentIndex].Jt = uint8(newJt)
		}
	}
	return shifts, incr, jmpLen
}

func (c *compilerContext) shiftJf(hadJt bool, currentIndex int, shifts *[]shift) {
	newJf := fixupWithShifts(currentIndex, int(c.result[currentIndex].Jf), *shifts)
	if c.isLongJump(newJf) {
		var incr int
		shifts, incr, _ = c.increment(hadJt, 0, currentIndex, shifts)
		shifts = c.insertJumps(currentIndex, newJf, incr, shifts)
	} else {
		c.result[currentIndex].Jf = uint8(newJf)
	}
}

func (c *compilerContext) shiftJt(currentIndex int, shifts *[]shift) bool {
	hadJt := false
	newJt := fixupWithShifts(currentIndex, int(c.result[currentIndex].Jt), *shifts)
	if c.isLongJump(newJt) {
		hadJt = true

		// Jf doesn't need to be modified here, because it will be fixed up with the shifts. Hopefully correctly...
		shifts = c.insertJumps(currentIndex, newJt, 0, shifts)
	} else {
		c.result[currentIndex].Jt = uint8(newJt)
	}
	return hadJt
}

func (c *compilerContext) insertJumps(currentIndex, pos, incr int, shifts *[]shift) *[]shift {
	c.insertUnconditionalJump(currentIndex+1+incr, pos)
	c.result[currentIndex].Jf = uint8(incr)
	*shifts = append(*shifts, shift{currentIndex + 1 + incr})
	return shifts
}

func (c *compilerContext) hasPreviousUnconditionalJump(from int) bool {
	return c.uconds.hasJumpFrom(from)
}

func insertSockFilter(sfs []unix.SockFilter, ix int, x unix.SockFilter) []unix.SockFilter {
	return append(
		append(
			append([]unix.SockFilter{}, sfs[:ix]...), x), sfs[ix:]...)
}

func (c *compilerContext) insertUnconditionalJump(from, k int) {
	x := unix.SockFilter{Code: OP_JMP_K, K: uint32(k)}
	c.result = insertSockFilter(c.result, from, x)
}

func (c *compilerContext) shiftJumps(from int, hasPrev bool) {
	incr := 1
	if hasPrev {
		incr = 2
	}
	c.shiftJumpsBy(from, incr)
}

func (c *compilerContext) shiftJumpsBy(from, incr int) {
	c.jts.shift(from, incr)
	c.jfs.shift(from, incr)
	c.uconds.shift(from, incr)
	c.labels.shiftLabels(from, incr)
}

func (c *compilerContext) fixUpPreviousRule(from int, positiveJump bool) {
	if positiveJump {
		c.result[from].Jt = 0
		c.result[from].Jf = 1
	} else {
		c.result[from].Jt = 1
		c.result[from].Jf = 0
	}
}
<commit_msg>Put back commented out code<commit_after>package compiler

import "golang.org/x/sys/unix"

type shift struct {
	position int
}

func (c *compilerContext) isLongJump(jumpSize int) bool {
	return jumpSize > c.maxJumpSize
}

func hasLongJump(index int, jts, jfs map[int]int) bool {
	// Using the unshifted index to look up positions in jts and jfs is
	// only safe if we're iterating backwards. Otherwise we would have to
	// fix up the positions in the maps as well and that would be fugly.

	if _, ok := jts[index]; ok {
		return true
	}
	if _, ok := jfs[index]; ok {
		return true
	}
	return false
}

func fixupWithShifts(pos, add int, shifts []shift) int {
	to := pos + add + 1
	currentAdd := add
	for _, s := range shifts {
		if s.position > pos && s.position <= to {
			currentAdd++
			to++
		}
	}
	return currentAdd
}

func (c *compilerContext) fixupJumps() {
	maxIndexWithLongJump := -1
	jtLongJumps := make(map[int]int)
	jfLongJumps := make(map[int]int)

	for l, at := range c.labels.allLabels() {
		for _, pos := range c.jts.allJumpsTo(l) {
			jumpSize := (at - pos) - 1
			if c.isLongJump(jumpSize) {
				if maxIndexWithLongJump < pos {
					maxIndexWithLongJump = pos
				}
				jtLongJumps[pos] = jumpSize
			} else {
				c.result[pos].Jt = uint8(jumpSize)
			}
		}

		for _, pos := range c.jfs.allJumpsTo(l) {
			jumpSize := (at - pos) - 1
			if c.isLongJump(jumpSize) {
				if maxIndexWithLongJump < pos {
					maxIndexWithLongJump = pos
				}
				jfLongJumps[pos] = jumpSize
			} else {
				c.result[pos].Jf = uint8(jumpSize)
			}
		}

		for _, pos := range c.uconds.allJumpsTo(l) {
			c.result[pos].K = uint32((at - pos) - 1)
		}
	}

	// This is an optimization. Please don't comment away.
	if maxIndexWithLongJump == -1 {
		return
	}

	shifts := []shift{}

	currentIndex := maxIndexWithLongJump
	for currentIndex > -1 {
		current := c.result[currentIndex]

		if isConditionalJump(current) && hasLongJump(currentIndex, jtLongJumps, jfLongJumps) {
			hadJt := c.handleJTLongJumpFor(currentIndex, jtLongJumps, jfLongJumps, &shifts)
			c.handleJFLongJumpFor(currentIndex, jfLongJumps, hadJt, &shifts)
		} else {
			if isUnconditionalJump(current) {
				c.result[currentIndex].K = uint32(fixupWithShifts(currentIndex, int(c.result[currentIndex].K), shifts))
			} else {
				hadJt := c.shiftJt(currentIndex, &shifts)
				c.shiftJf(hadJt, currentIndex, &shifts)
			}
		}
		currentIndex--
	}
}

func (c *compilerContext) handleJTLongJumpFor(currentIndex int, jtLongJumps map[int]int, jfLongJumps map[int]int, shifts *[]shift) bool {
	hadJt := false
	if jmpLen, ok := jtLongJumps[currentIndex]; ok {
		jmpLen = fixupWithShifts(currentIndex, jmpLen, *shifts)
		hadJt = true

		newJf := int(c.result[currentIndex].Jf) + 1
		if c.isLongJump(newJf) {
			// Simple case, we can just add it to the long jumps for JF:
			jfLongJumps[currentIndex] = newJf
		} else {
			c.result[currentIndex].Jf = uint8(newJf)
		}

		shifts = c.insertJumps(currentIndex, jmpLen, 0, shifts)
	}
	return hadJt
}

func (c *compilerContext) handleJFLongJumpFor(currentIndex int, jfLongJumps map[int]int, hadJt bool, shifts *[]shift) {
	if jmpLen, ok := jfLongJumps[currentIndex]; ok {
		jmpLen = fixupWithShifts(currentIndex, jmpLen, *shifts)
		var incr int
		shifts, incr, jmpLen = c.increment(hadJt, jmpLen, currentIndex, shifts)
		shifts = c.insertJumps(currentIndex, jmpLen, incr, shifts)
	}
}

func (c *compilerContext) increment(hadJt bool, jmpLen, currentIndex int, shifts *[]shift) (*[]shift, int, int) {
	incr := 0
	if hadJt {
		c.result[currentIndex+1].K++
		incr++
		jmpLen--
	} else {
		newJt := int(c.result[currentIndex].Jt) + 1
		if c.isLongJump(newJt) {
			// incr in this case doesn't seem to do much, all tests pass when it is changed to 0
			shifts = c.insertJumps(currentIndex, newJt, incr, shifts)
			incr++
		} else {
			c.result[currentIndex].Jt = uint8(newJt)
		}
	}
	return shifts, incr, jmpLen
}

func (c *compilerContext) shiftJf(hadJt bool, currentIndex int, shifts *[]shift) {
	newJf := fixupWithShifts(currentIndex, int(c.result[currentIndex].Jf), *shifts)
	if c.isLongJump(newJf) {
		var incr int
		shifts, incr, _ = c.increment(hadJt, 0, currentIndex, shifts)
		shifts = c.insertJumps(currentIndex, newJf, incr, shifts)
	} else {
		c.result[currentIndex].Jf = uint8(newJf)
	}
}

func (c *compilerContext) shiftJt(currentIndex int, shifts *[]shift) bool {
	hadJt := false
	newJt := fixupWithShifts(currentIndex, int(c.result[currentIndex].Jt), *shifts)
	if c.isLongJump(newJt) {
		hadJt = true

		// Jf doesn't need to be modified here, because it will be fixed up with the shifts. Hopefully correctly...
		shifts = c.insertJumps(currentIndex, newJt, 0, shifts)
	} else {
		c.result[currentIndex].Jt = uint8(newJt)
	}
	return hadJt
}

func (c *compilerContext) insertJumps(currentIndex, pos, incr int, shifts *[]shift) *[]shift {
	c.insertUnconditionalJump(currentIndex+1+incr, pos)
	c.result[currentIndex].Jf = uint8(incr)
	*shifts = append(*shifts, shift{currentIndex + 1 + incr})
	return shifts
}

func (c *compilerContext) hasPreviousUnconditionalJump(from int) bool {
	return c.uconds.hasJumpFrom(from)
}

func insertSockFilter(sfs []unix.SockFilter, ix int, x unix.SockFilter) []unix.SockFilter {
	return append(
		append(
			append([]unix.SockFilter{}, sfs[:ix]...), x), sfs[ix:]...)
}

func (c *compilerContext) insertUnconditionalJump(from, k int) {
	x := unix.SockFilter{Code: OP_JMP_K, K: uint32(k)}
	c.result = insertSockFilter(c.result, from, x)
}

func (c *compilerContext) shiftJumps(from int, hasPrev bool) {
	incr := 1
	if hasPrev {
		incr = 2
	}
	c.shiftJumpsBy(from, incr)
}

func (c *compilerContext) shiftJumpsBy(from, incr int) {
	c.jts.shift(from, incr)
	c.jfs.shift(from, incr)
	c.uconds.shift(from, incr)
	c.labels.shiftLabels(from, incr)
}

func (c *compilerContext) fixUpPreviousRule(from int, positiveJump bool) {
	if positiveJump {
		c.result[from].Jt = 0
		c.result[from].Jf = 1
	} else {
		c.result[from].Jt = 1
		c.result[from].Jf = 0
	}
}
<|endoftext|>


<commit_before>package topgun_test

import (
	"fmt"
	"time"

	sq "github.com/Masterminds/squirrel"
	. "github.com/concourse/concourse/topgun/common"
	_ "github.com/lib/pq"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Garbage-collecting volumes", func() {
	BeforeEach(func() {
		Deploy("deployments/concourse.yml")
	})

	Describe("A volume that belonged to a container that is now gone", func() {
		It("is removed from the database and worker [#129726011]", func() {
			By("running a task with container having a rootfs, input, and output volume")
			Fly.Run("execute", "-c", "tasks/input-output.yml", "-i", "some-input=./tasks")

			By("collecting the build containers")
			rows, err := Psql.Select("id, handle").From("containers").Where(sq.Eq{"build_id": 1, "meta_step_name": "one-off"}).RunWith(DbConn).Query()
			Expect(err).ToNot(HaveOccurred())

			buildContainers := map[int]string{}
			containerIDs := []int{}
			for rows.Next() {
				var id int
				var handle string
				err := rows.Scan(&id, &handle)
				Expect(err).ToNot(HaveOccurred())

				buildContainers[id] = handle
				containerIDs = append(containerIDs, id)
			}

			Expect(containerIDs).To(HaveLen(1), "should only be testing one-off task container")

			By("collecting the container volumes")
			rows, err = Psql.Select("id, handle").From("volumes").Where(sq.Eq{"container_id": containerIDs}).RunWith(DbConn).Query()
			Expect(err).ToNot(HaveOccurred())

			containerVolumes := map[int]string{}
			volumeIDs := []int{}
			for rows.Next() {
				var id int
				var handle string
				err := rows.Scan(&id, &handle)
				Expect(err).ToNot(HaveOccurred())

				containerVolumes[id] = handle
				volumeIDs = append(volumeIDs, id)
			}

			By(fmt.Sprintf("eventually expiring the build containers: %v", containerIDs))
			Eventually(func() int {
				var volNum int
				err := Psql.Select("COUNT(id)").From("volumes").Where(sq.Eq{"id": volumeIDs}).RunWith(DbConn).QueryRow().Scan(&volNum)
				Expect(err).ToNot(HaveOccurred())

				var containerNum int
				err = Psql.Select("COUNT(id)").From("containers").Where(sq.Eq{"id": containerIDs}).RunWith(DbConn).QueryRow().Scan(&containerNum)
				Expect(err).ToNot(HaveOccurred())

				if containerNum == len(containerIDs) {
					By(fmt.Sprintf("not expiring volumes so long as their containers are there (%d remaining)", containerNum))
					Expect(volNum).To(Equal(len(volumeIDs)))
				}

				return containerNum
			}, 10*time.Minute, time.Second).Should(BeZero())

			By("having removed the containers from the worker")
			containers, err := WorkerGardenClient.Containers(nil)
			Expect(err).ToNot(HaveOccurred())

			existingHandles := []string{}
			for _, c := range containers {
				existingHandles = append(existingHandles, c.Handle())
			}

			for _, handle := range buildContainers {
				Expect(existingHandles).ToNot(ContainElement(handle))
			}

			By(fmt.Sprintf("eventually expiring the container volumes: %v", volumeIDs))
			Eventually(func() int {
				var num int
				err := Psql.Select("COUNT(id)").From("volumes").Where(sq.Eq{"id": volumeIDs}).RunWith(DbConn).QueryRow().Scan(&num)
				Expect(err).ToNot(HaveOccurred())
				return num
			}, 10*time.Minute, time.Second).Should(BeZero())

			By("having removed the volumes from the worker")
			volumes, err := WorkerBaggageclaimClient.ListVolumes(Logger, nil)
			Expect(err).ToNot(HaveOccurred())

			existingHandles = []string{}
			for _, v := range volumes {
				existingHandles = append(existingHandles, v.Handle())
			}

			for _, handle := range containerVolumes {
				Expect(existingHandles).ToNot(ContainElement(handle))
			}
		})
	})

	Describe("A volume that belonged to a resource cache that is no longer in use", func() {
		It("is removed from the database and worker [#129726933]", func() {
			By("setting pipeline that creates resource cache")
			Fly.Run("set-pipeline", "-n", "-c", "pipelines/get-task-changing-resource.yml", "-p", "volume-gc-test")

			By("unpausing the pipeline")
			Fly.Run("unpause-pipeline", "-p", "volume-gc-test")

			By("triggering job")
			Fly.Run("trigger-job", "-w", "-j", "volume-gc-test/simple-job")

			By("getting resource cache")
			var resourceCacheID int
			err := Psql.Select("rch.id").
				From("resource_caches rch").
				LeftJoin("resource_configs rcf ON rch.resource_config_id = rcf.id").
				LeftJoin("base_resource_types brt ON rcf.base_resource_type_id = brt.id").
				Where("brt.name = 'time'").
				RunWith(DbConn).
				QueryRow().Scan(&resourceCacheID)
			Expect(err).ToNot(HaveOccurred())

			By("getting volume for resource cache")
			var volumeID int
			var volumeHandle string
			err = Psql.Select("v.id, v.handle").
				From("volumes v").
				LeftJoin("worker_resource_caches wrc ON wrc.id = v.worker_resource_cache_id").
				Where(sq.Eq{"wrc.resource_cache_id": resourceCacheID}).
				RunWith(DbConn).
				QueryRow().
				Scan(&volumeID, &volumeHandle)
			Expect(err).ToNot(HaveOccurred())

			By("creating a new version of resource")
			Fly.Run("check-resource", "-r", "volume-gc-test/tick-tock")

			By(fmt.Sprintf("eventually expiring the resource cache: %d", resourceCacheID))
			Eventually(func() int {
				var resourceCacheNum int
				err := Psql.Select("COUNT(id)").From("resource_caches").Where(sq.Eq{"id": resourceCacheID}).RunWith(DbConn).QueryRow().Scan(&resourceCacheNum)
				Expect(err).ToNot(HaveOccurred())

				var volumeNum int
				err = Psql.Select("COUNT(id)").From("volumes").Where(sq.Eq{"id": volumeID}).RunWith(DbConn).QueryRow().Scan(&volumeNum)
				Expect(err).ToNot(HaveOccurred())

				if resourceCacheNum == 1 {
					By(fmt.Sprintf("not expiring volume so long as its resource cache is there"))
					Expect(volumeNum).To(Equal(1))
				}

				return resourceCacheNum
			}, 10*time.Minute, time.Second).Should(BeZero())

			By(fmt.Sprintf("eventually expiring the resource cache volumes: %d", resourceCacheID))
			Eventually(func() int {
				var volumeNum int
				err := Psql.Select("COUNT(id)").From("volumes").Where(sq.Eq{"id": volumeID}).RunWith(DbConn).QueryRow().Scan(&volumeNum)
				Expect(err).ToNot(HaveOccurred())
				return volumeNum
			}, 10*time.Minute, time.Second).Should(BeZero())

			By("having removed the volumes from the worker")
			volumes, err := WorkerBaggageclaimClient.ListVolumes(Logger, nil)
			Expect(err).ToNot(HaveOccurred())

			existingHandles := []string{}
			for _, v := range volumes {
				existingHandles = append(existingHandles, v.Handle())
			}

			Expect(existingHandles).ToNot(ContainElement(volumeHandle))
		})
	})
})
<commit_msg>fix compilation error<commit_after>package topgun_test

import (
	"context"
	"fmt"
	"time"

	sq "github.com/Masterminds/squirrel"
	. "github.com/concourse/concourse/topgun/common"
	_ "github.com/lib/pq"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Garbage-collecting volumes", func() {
	BeforeEach(func() {
		Deploy("deployments/concourse.yml")
	})

	Describe("A volume that belonged to a container that is now gone", func() {
		It("is removed from the database and worker [#129726011]", func() {
			By("running a task with container having a rootfs, input, and output volume")
			Fly.Run("execute", "-c", "tasks/input-output.yml", "-i", "some-input=./tasks")

			By("collecting the build containers")
			rows, err := Psql.Select("id, handle").From("containers").Where(sq.Eq{"build_id": 1, "meta_step_name": "one-off"}).RunWith(DbConn).Query()
			Expect(err).ToNot(HaveOccurred())

			buildContainers := map[int]string{}
			containerIDs := []int{}
			for rows.Next() {
				var id int
				var handle string
				err := rows.Scan(&id, &handle)
				Expect(err).ToNot(HaveOccurred())

				buildContainers[id] = handle
				containerIDs = append(containerIDs, id)
			}

			Expect(containerIDs).To(HaveLen(1), "should only be testing one-off task container")

			By("collecting the container volumes")
			rows, err = Psql.Select("id, handle").From("volumes").Where(sq.Eq{"container_id": containerIDs}).RunWith(DbConn).Query()
			Expect(err).ToNot(HaveOccurred())

			containerVolumes := map[int]string{}
			volumeIDs := []int{}
			for rows.Next() {
				var id int
				var handle string
				err := rows.Scan(&id, &handle)
				Expect(err).ToNot(HaveOccurred())

				containerVolumes[id] = handle
				volumeIDs = append(volumeIDs, id)
			}

			By(fmt.Sprintf("eventually expiring the build containers: %v", containerIDs))
			Eventually(func() int {
				var volNum int
				err := Psql.Select("COUNT(id)").From("volumes").Where(sq.Eq{"id": volumeIDs}).RunWith(DbConn).QueryRow().Scan(&volNum)
				Expect(err).ToNot(HaveOccurred())

				var containerNum int
				err = Psql.Select("COUNT(id)").From("containers").Where(sq.Eq{"id": containerIDs}).RunWith(DbConn).QueryRow().Scan(&containerNum)
				Expect(err).ToNot(HaveOccurred())

				if containerNum == len(containerIDs) {
					By(fmt.Sprintf("not expiring volumes so long as their containers are there (%d remaining)", containerNum))
					Expect(volNum).To(Equal(len(volumeIDs)))
				}

				return containerNum
			}, 10*time.Minute, time.Second).Should(BeZero())

			By("having removed the containers from the worker")
			containers, err := WorkerGardenClient.Containers(nil)
			Expect(err).ToNot(HaveOccurred())

			existingHandles := []string{}
			for _, c := range containers {
				existingHandles = append(existingHandles, c.Handle())
			}

			for _, handle := range buildContainers {
				Expect(existingHandles).ToNot(ContainElement(handle))
			}

			By(fmt.Sprintf("eventually expiring the container volumes: %v", volumeIDs))
			Eventually(func() int {
				var num int
				err := Psql.Select("COUNT(id)").From("volumes").Where(sq.Eq{"id": volumeIDs}).RunWith(DbConn).QueryRow().Scan(&num)
				Expect(err).ToNot(HaveOccurred())
				return num
			}, 10*time.Minute, time.Second).Should(BeZero())

			By("having removed the volumes from the worker")
			volumes, err := WorkerBaggageclaimClient.ListVolumes(context.Background(), nil)
			Expect(err).ToNot(HaveOccurred())

			existingHandles = []string{}
			for _, v := range volumes {
				existingHandles = append(existingHandles, v.Handle())
			}

			for _, handle := range containerVolumes {
				Expect(existingHandles).ToNot(ContainElement(handle))
			}
		})
	})

	Describe("A volume that belonged to a resource cache that is no longer in use", func() {
		It("is removed from the database and worker [#129726933]", func() {
			By("setting pipeline that creates resource cache")
			Fly.Run("set-pipeline", "-n", "-c", "pipelines/get-task-changing-resource.yml", "-p", "volume-gc-test")

			By("unpausing the pipeline")
			Fly.Run("unpause-pipeline", "-p", "volume-gc-test")

			By("triggering job")
			Fly.Run("trigger-job", "-w", "-j", "volume-gc-test/simple-job")

			By("getting resource cache")
			var resourceCacheID int
			err := Psql.Select("rch.id").
				From("resource_caches rch").
				LeftJoin("resource_configs rcf ON rch.resource_config_id = rcf.id").
				LeftJoin("base_resource_types brt ON rcf.base_resource_type_id = brt.id").
				Where("brt.name = 'time'").
				RunWith(DbConn).
				QueryRow().Scan(&resourceCacheID)
			Expect(err).ToNot(HaveOccurred())

			By("getting volume for resource cache")
			var volumeID int
			var volumeHandle string
			err = Psql.Select("v.id, v.handle").
				From("volumes v").
				LeftJoin("worker_resource_caches wrc ON wrc.id = v.worker_resource_cache_id").
				Where(sq.Eq{"wrc.resource_cache_id": resourceCacheID}).
				RunWith(DbConn).
				QueryRow().
				Scan(&volumeID, &volumeHandle)
			Expect(err).ToNot(HaveOccurred())

			By("creating a new version of resource")
			Fly.Run("check-resource", "-r", "volume-gc-test/tick-tock")

			By(fmt.Sprintf("eventually expiring the resource cache: %d", resourceCacheID))
			Eventually(func() int {
				var resourceCacheNum int
				err := Psql.Select("COUNT(id)").From("resource_caches").Where(sq.Eq{"id": resourceCacheID}).RunWith(DbConn).QueryRow().Scan(&resourceCacheNum)
				Expect(err).ToNot(HaveOccurred())

				var volumeNum int
				err = Psql.Select("COUNT(id)").From("volumes").Where(sq.Eq{"id": volumeID}).RunWith(DbConn).QueryRow().Scan(&volumeNum)
				Expect(err).ToNot(HaveOccurred())

				if resourceCacheNum == 1 {
					By(fmt.Sprintf("not expiring volume so long as its resource cache is there"))
					Expect(volumeNum).To(Equal(1))
				}

				return resourceCacheNum
			}, 10*time.Minute, time.Second).Should(BeZero())

			By(fmt.Sprintf("eventually expiring the resource cache volumes: %d", resourceCacheID))
			Eventually(func() int {
				var volumeNum int
				err := Psql.Select("COUNT(id)").From("volumes").Where(sq.Eq{"id": volumeID}).RunWith(DbConn).QueryRow().Scan(&volumeNum)
				Expect(err).ToNot(HaveOccurred())
				return volumeNum
			}, 10*time.Minute, time.Second).Should(BeZero())

			By("having removed the volumes from the worker")
			volumes, err := WorkerBaggageclaimClient.ListVolumes(context.Background(), nil)
			Expect(err).ToNot(HaveOccurred())

			existingHandles := []string{}
			for _, v := range volumes {
				existingHandles = append(existingHandles, v.Handle())
			}

			Expect(existingHandles).ToNot(ContainElement(volumeHandle))
		})
	})
})
<|endoftext|>


<commit_before>package helpers

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
	"os/user"

	"github.com/Azure/go-autorest/autorest/azure"
)

const (
	credentialsPath = "/.azure/credentials.json"
)

// ToJSON returns the passed item as a pretty-printed JSON string. If any JSON error occurs,
// it returns the empty string.
func ToJSON(v interface{}) string {
	j, _ := json.MarshalIndent(v, "", "  ")
	return string(j)
}

// NewServicePrincipalTokenFromCredentials creates a new ServicePrincipalToken using values of the
// passed credentials map.
func NewServicePrincipalTokenFromCredentials(c map[string]string, scope string) (*azure.ServicePrincipalToken, error) {
	return azure.NewServicePrincipalToken(c["clientID"], c["clientSecret"], c["tenantID"], scope)
}

// LoadCredentials reads credentials from a ~/.azure/credentials.json file. See the accompanying
// credentials_sample.json file for an example.
//
// Note: Storing crendentials in a local file must be secured and not shared. It is used here
// simply to reduce code in the examples.
func LoadCredentials() (map[string]string, error) {
	u, err := user.Current()
	if err != nil {
		return nil, fmt.Errorf("ERROR: Unable to determine current user")
	}

	n := u.HomeDir + credentialsPath
	f, err := os.Open(n)
	if err != nil {
		return nil, fmt.Errorf("ERROR: Unable to locate or open Azure credentials at %s (%v)", n, err)
	}

	b, err := ioutil.ReadAll(f)
	if err != nil {
		return nil, fmt.Errorf("ERROR: Unable to read %s (%v)", n, err)
	}

	c := map[string]interface{}{}
	err = json.Unmarshal(b, &c)
	if err != nil {
		return nil, fmt.Errorf("ERROR: %s contained invalid JSON (%s)", n, err)
	}

	return ensureValueStrings(c), nil
}

func ensureValueStrings(mapOfInterface map[string]interface{}) map[string]string {
	mapOfStrings := make(map[string]string)
	for key, value := range mapOfInterface {
		mapOfStrings[key] = ensureValueString(value)
	}
	return mapOfStrings
}

func ensureValueString(value interface{}) string {
	if value == nil {
		return ""
	}
	switch v := value.(type) {
	case string:
		return v
	default:
		return fmt.Sprintf("%v", v)
	}
}
<commit_msg>example/helpers/helpers.go<commit_after>package helpers

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
	"os/user"

	"github.com/Azure/go-autorest/autorest/azure"
)

const (
	credentialsPath = "/.azure/credentials.json"
)

// ToJSON returns the passed item as a pretty-printed JSON string. If any JSON error occurs,
// it returns the empty string.
func ToJSON(v interface{}) string {
	j, _ := json.MarshalIndent(v, "", "  ")
	return string(j)
}

// NewServicePrincipalTokenFromCredentials creates a new ServicePrincipalToken using values of the
// passed credentials map.
func NewServicePrincipalTokenFromCredentials(c map[string]string, scope string) (*azure.ServicePrincipalToken, error) {
	oauthConfig, err := azure.PublicCloud.OAuthConfigForTenant(c["tenantID"])
	if err != nil {
 		panic(err)
	}
	return azure.NewServicePrincipalToken(*oauthConfig, c["clientID"], c["clientSecret"], scope)
}

// LoadCredentials reads credentials from a ~/.azure/credentials.json file. See the accompanying
// credentials_sample.json file for an example.
//
// Note: Storing crendentials in a local file must be secured and not shared. It is used here
// simply to reduce code in the examples.
func LoadCredentials() (map[string]string, error) {
	u, err := user.Current()
	if err != nil {
		return nil, fmt.Errorf("ERROR: Unable to determine current user")
	}

	n := u.HomeDir + credentialsPath
	f, err := os.Open(n)
	if err != nil {
		return nil, fmt.Errorf("ERROR: Unable to locate or open Azure credentials at %s (%v)", n, err)
	}

	b, err := ioutil.ReadAll(f)
	if err != nil {
		return nil, fmt.Errorf("ERROR: Unable to read %s (%v)", n, err)
	}

	c := map[string]interface{}{}
	err = json.Unmarshal(b, &c)
	if err != nil {
		return nil, fmt.Errorf("ERROR: %s contained invalid JSON (%s)", n, err)
	}

	return ensureValueStrings(c), nil
}

func ensureValueStrings(mapOfInterface map[string]interface{}) map[string]string {
	mapOfStrings := make(map[string]string)
	for key, value := range mapOfInterface {
		mapOfStrings[key] = ensureValueString(value)
	}
	return mapOfStrings
}

func ensureValueString(value interface{}) string {
	if value == nil {
		return ""
	}
	switch v := value.(type) {
	case string:
		return v
	default:
		return fmt.Sprintf("%v", v)
	}
}
<|endoftext|>


<commit_before><commit_msg>signals<commit_after><|endoftext|>


<commit_before><commit_msg>cmd/kubepkg: Update copyright<commit_after><|endoftext|>


<commit_before>package opts

import (
	"fmt"

	"github.com/docker/docker/pkg/blkiodev"
)

// WeightdeviceOpt defines a map of WeightDevices
type WeightdeviceOpt struct {
	values    []*blkiodev.WeightDevice
	validator ValidatorWeightFctType
}

// NewWeightdeviceOpt creates a new WeightdeviceOpt
func NewWeightdeviceOpt(validator ValidatorWeightFctType) WeightdeviceOpt {
	values := []*blkiodev.WeightDevice{}
	return WeightdeviceOpt{
		values:    values,
		validator: validator,
	}
}

// Set validates a WeightDevice and sets its name as a key in WeightdeviceOpt
func (opt *WeightdeviceOpt) Set(val string) error {
	var value *blkiodev.WeightDevice
	if opt.validator != nil {
		v, err := opt.validator(val)
		if err != nil {
			return err
		}
		value = v
	}
	(opt.values) = append((opt.values), value)
	return nil
}

// String returns Ulimit values as a string.
func (opt *WeightdeviceOpt) String() string {
	var out []string
	for _, v := range opt.values {
		out = append(out, v.String())
	}

	return fmt.Sprintf("%v", out)
}

// GetList returns a slice of pointers to WeightDevices.
func (opt *WeightdeviceOpt) GetList() []*blkiodev.WeightDevice {
	var weightdevice []*blkiodev.WeightDevice
	for _, v := range opt.values {
		weightdevice = append(weightdevice, v)
	}

	return weightdevice
}
<commit_msg>opts/weightdevice: fix typo<commit_after>package opts

import (
	"fmt"

	"github.com/docker/docker/pkg/blkiodev"
)

// WeightdeviceOpt defines a map of WeightDevices
type WeightdeviceOpt struct {
	values    []*blkiodev.WeightDevice
	validator ValidatorWeightFctType
}

// NewWeightdeviceOpt creates a new WeightdeviceOpt
func NewWeightdeviceOpt(validator ValidatorWeightFctType) WeightdeviceOpt {
	values := []*blkiodev.WeightDevice{}
	return WeightdeviceOpt{
		values:    values,
		validator: validator,
	}
}

// Set validates a WeightDevice and sets its name as a key in WeightdeviceOpt
func (opt *WeightdeviceOpt) Set(val string) error {
	var value *blkiodev.WeightDevice
	if opt.validator != nil {
		v, err := opt.validator(val)
		if err != nil {
			return err
		}
		value = v
	}
	(opt.values) = append((opt.values), value)
	return nil
}

// String returns WeightdeviceOpt values as a string.
func (opt *WeightdeviceOpt) String() string {
	var out []string
	for _, v := range opt.values {
		out = append(out, v.String())
	}

	return fmt.Sprintf("%v", out)
}

// GetList returns a slice of pointers to WeightDevices.
func (opt *WeightdeviceOpt) GetList() []*blkiodev.WeightDevice {
	var weightdevice []*blkiodev.WeightDevice
	for _, v := range opt.values {
		weightdevice = append(weightdevice, v)
	}

	return weightdevice
}
<|endoftext|>


<commit_before>package command

import (
	"fmt"
	"strings"
)

type AllocStatusCommand struct {
	Meta
}

func (c *AllocStatusCommand) Help() string {
	helpText := `
Usage: nomad alloc-stauts [options] <allocation>

  Display information about existing allocations. This command can
  be used to inspect the current status of all allocation,
  including its running status, metadata, and verbose failure
  messages reported by internal subsystems.

General Options:

  ` + generalOptionsUsage()
	return strings.TrimSpace(helpText)
}

func (c *AllocStatusCommand) Synopsis() string {
	return "Display allocation status information and metadata"
}

func (c *AllocStatusCommand) Run(args []string) int {
	flags := c.Meta.FlagSet("alloc-status", FlagSetClient)
	flags.Usage = func() { c.Ui.Output(c.Help()) }
	if err := flags.Parse(args); err != nil {
		return 1
	}

	// Check that we got exactly one allocation ID
	args = flags.Args()
	if len(args) != 1 {
		c.Ui.Error(c.Help())
		return 1
	}
	allocID := args[0]

	// Get the HTTP client
	client, err := c.Meta.Client()
	if err != nil {
		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
		return 1
	}

	// Query the allocation info
	alloc, _, err := client.Allocations().Info(allocID, nil)
	if err != nil {
		c.Ui.Error(fmt.Sprintf("Error querying allocation: %s", err))
		return 1
	}

	// Format the allocation data
	basic := []string{
		fmt.Sprintf("ID|%s", alloc.ID),
		fmt.Sprintf("EvalID|%s", alloc.EvalID),
		fmt.Sprintf("Name|%s", alloc.Name),
		fmt.Sprintf("NodeID|%s", alloc.NodeID),
		fmt.Sprintf("JobID|%s", alloc.JobID),
		fmt.Sprintf("ClientStatus|%s", alloc.ClientStatus),
		fmt.Sprintf("ClientDescription|%s", alloc.ClientDescription),
		fmt.Sprintf("NodesEvaluated|%d", alloc.Metrics.NodesEvaluated),
		fmt.Sprintf("NodesFiltered|%d", alloc.Metrics.NodesFiltered),
		fmt.Sprintf("NodesExhausted|%d", alloc.Metrics.NodesExhausted),
		fmt.Sprintf("AllocationTime|%s", alloc.Metrics.AllocationTime),
		fmt.Sprintf("CoalescedFailures|%d", alloc.Metrics.CoalescedFailures),
	}
	c.Ui.Output(formatKV(basic))

	// Format the detailed status
	c.Ui.Output("\n==> Status")
	dumpAllocStatus(c.Ui, alloc)

	return 0
}
<commit_msg>Changed typo in documentation<commit_after>package command

import (
	"fmt"
	"strings"
)

type AllocStatusCommand struct {
	Meta
}

func (c *AllocStatusCommand) Help() string {
	helpText := `
Usage: nomad alloc-status [options] <allocation>

  Display information about existing allocations. This command can
  be used to inspect the current status of all allocation,
  including its running status, metadata, and verbose failure
  messages reported by internal subsystems.

General Options:

  ` + generalOptionsUsage()
	return strings.TrimSpace(helpText)
}

func (c *AllocStatusCommand) Synopsis() string {
	return "Display allocation status information and metadata"
}

func (c *AllocStatusCommand) Run(args []string) int {
	flags := c.Meta.FlagSet("alloc-status", FlagSetClient)
	flags.Usage = func() { c.Ui.Output(c.Help()) }
	if err := flags.Parse(args); err != nil {
		return 1
	}

	// Check that we got exactly one allocation ID
	args = flags.Args()
	if len(args) != 1 {
		c.Ui.Error(c.Help())
		return 1
	}
	allocID := args[0]

	// Get the HTTP client
	client, err := c.Meta.Client()
	if err != nil {
		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
		return 1
	}

	// Query the allocation info
	alloc, _, err := client.Allocations().Info(allocID, nil)
	if err != nil {
		c.Ui.Error(fmt.Sprintf("Error querying allocation: %s", err))
		return 1
	}

	// Format the allocation data
	basic := []string{
		fmt.Sprintf("ID|%s", alloc.ID),
		fmt.Sprintf("EvalID|%s", alloc.EvalID),
		fmt.Sprintf("Name|%s", alloc.Name),
		fmt.Sprintf("NodeID|%s", alloc.NodeID),
		fmt.Sprintf("JobID|%s", alloc.JobID),
		fmt.Sprintf("ClientStatus|%s", alloc.ClientStatus),
		fmt.Sprintf("ClientDescription|%s", alloc.ClientDescription),
		fmt.Sprintf("NodesEvaluated|%d", alloc.Metrics.NodesEvaluated),
		fmt.Sprintf("NodesFiltered|%d", alloc.Metrics.NodesFiltered),
		fmt.Sprintf("NodesExhausted|%d", alloc.Metrics.NodesExhausted),
		fmt.Sprintf("AllocationTime|%s", alloc.Metrics.AllocationTime),
		fmt.Sprintf("CoalescedFailures|%d", alloc.Metrics.CoalescedFailures),
	}
	c.Ui.Output(formatKV(basic))

	// Format the detailed status
	c.Ui.Output("\n==> Status")
	dumpAllocStatus(c.Ui, alloc)

	return 0
}
<|endoftext|>


<commit_before>package command

import (
	"os"
	"strings"
	"testing"

	"github.com/hashicorp/terraform/addrs"
	"github.com/hashicorp/terraform/states"
	"github.com/hashicorp/terraform/terraform"
	"github.com/mitchellh/cli"
)

func TestUntaint(t *testing.T) {
	state := states.BuildState(func(s *states.SyncState) {
		s.SetResourceInstanceCurrent(
			addrs.Resource{
				Mode: addrs.ManagedResourceMode,
				Type: "test_instance",
				Name: "foo",
			}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
			&states.ResourceInstanceObjectSrc{
				AttrsJSON: []byte(`{"id":"bar"}`),
				Status:    states.ObjectTainted,
			},
			addrs.ProviderConfig{Type: "test"}.Absolute(addrs.RootModuleInstance),
		)
	})
	statePath := testStateFile(t, state)

	ui := new(cli.MockUi)
	c := &UntaintCommand{
		Meta: Meta{
			Ui: ui,
		},
	}

	args := []string{
		"-state", statePath,
		"test_instance.foo",
	}
	if code := c.Run(args); code != 0 {
		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
	}

	expected := strings.TrimSpace(`
test_instance.foo:
  ID = bar
	`)
	testStateOutput(t, statePath, expected)
}

func TestUntaint_lockedState(t *testing.T) {
	state := states.BuildState(func(s *states.SyncState) {
		s.SetResourceInstanceCurrent(
			addrs.Resource{
				Mode: addrs.ManagedResourceMode,
				Type: "test_instance",
				Name: "foo",
			}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
			&states.ResourceInstanceObjectSrc{
				AttrsJSON: []byte(`{"id":"bar"}`),
				Status:    states.ObjectTainted,
			},
			addrs.ProviderConfig{Type: "test"}.Absolute(addrs.RootModuleInstance),
		)
	})
	statePath := testStateFile(t, state)
	unlock, err := testLockState("./testdata", statePath)
	if err != nil {
		t.Fatal(err)
	}
	defer unlock()

	ui := new(cli.MockUi)
	c := &UntaintCommand{
		Meta: Meta{
			Ui: ui,
		},
	}

	args := []string{
		"-state", statePath,
		"test_instance.foo",
	}
	if code := c.Run(args); code == 0 {
		t.Fatal("expected error")
	}

	output := ui.ErrorWriter.String()
	if !strings.Contains(output, "lock") {
		t.Fatal("command output does not look like a lock error:", output)
	}
}

func TestUntaint_backup(t *testing.T) {
	// Get a temp cwd
	tmp, cwd := testCwd(t)
	defer testFixCwd(t, tmp, cwd)

	// Write the temp state
	state := &terraform.State{
		Modules: []*terraform.ModuleState{
			&terraform.ModuleState{
				Path: []string{"root"},
				Resources: map[string]*terraform.ResourceState{
					"test_instance.foo": &terraform.ResourceState{
						Type: "test_instance",
						Primary: &terraform.InstanceState{
							ID:      "bar",
							Tainted: true,
						},
					},
				},
			},
		},
	}
	path := testStateFileDefault(t, state)

	ui := new(cli.MockUi)
	c := &UntaintCommand{
		Meta: Meta{
			Ui: ui,
		},
	}

	args := []string{
		"test_instance.foo",
	}
	if code := c.Run(args); code != 0 {
		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
	}

	// Backup is still tainted
	testStateOutput(t, path+".backup", strings.TrimSpace(`
test_instance.foo: (tainted)
  ID = bar
  provider = provider.test
	`))

	// State is untainted
	testStateOutput(t, path, strings.TrimSpace(`
test_instance.foo:
  ID = bar
  provider = provider.test
	`))
}

func TestUntaint_backupDisable(t *testing.T) {
	// Get a temp cwd
	tmp, cwd := testCwd(t)
	defer testFixCwd(t, tmp, cwd)

	// Write the temp state
	state := &terraform.State{
		Modules: []*terraform.ModuleState{
			&terraform.ModuleState{
				Path: []string{"root"},
				Resources: map[string]*terraform.ResourceState{
					"test_instance.foo": &terraform.ResourceState{
						Type: "test_instance",
						Primary: &terraform.InstanceState{
							ID:      "bar",
							Tainted: true,
						},
					},
				},
			},
		},
	}
	path := testStateFileDefault(t, state)

	ui := new(cli.MockUi)
	c := &UntaintCommand{
		Meta: Meta{
			Ui: ui,
		},
	}

	args := []string{
		"-backup", "-",
		"test_instance.foo",
	}
	if code := c.Run(args); code != 0 {
		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
	}

	if _, err := os.Stat(path + ".backup"); err == nil {
		t.Fatal("backup path should not exist")
	}

	testStateOutput(t, path, strings.TrimSpace(`
test_instance.foo:
  ID = bar
  provider = provider.test
	`))
}

func TestUntaint_badState(t *testing.T) {
	ui := new(cli.MockUi)
	c := &UntaintCommand{
		Meta: Meta{
			Ui: ui,
		},
	}

	args := []string{
		"-state", "i-should-not-exist-ever",
		"foo",
	}
	if code := c.Run(args); code != 1 {
		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
	}
}

func TestUntaint_defaultState(t *testing.T) {
	// Get a temp cwd
	tmp, cwd := testCwd(t)
	defer testFixCwd(t, tmp, cwd)

	// Write the temp state
	state := &terraform.State{
		Modules: []*terraform.ModuleState{
			&terraform.ModuleState{
				Path: []string{"root"},
				Resources: map[string]*terraform.ResourceState{
					"test_instance.foo": &terraform.ResourceState{
						Type: "test_instance",
						Primary: &terraform.InstanceState{
							ID:      "bar",
							Tainted: true,
						},
					},
				},
			},
		},
	}
	path := testStateFileDefault(t, state)

	ui := new(cli.MockUi)
	c := &UntaintCommand{
		Meta: Meta{
			Ui: ui,
		},
	}

	args := []string{
		"test_instance.foo",
	}
	if code := c.Run(args); code != 0 {
		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
	}

	testStateOutput(t, path, strings.TrimSpace(`
test_instance.foo:
  ID = bar
  provider = provider.test
	`))
}

func TestUntaint_missing(t *testing.T) {
	state := states.BuildState(func(s *states.SyncState) {
		s.SetResourceInstanceCurrent(
			addrs.Resource{
				Mode: addrs.ManagedResourceMode,
				Type: "test_instance",
				Name: "foo",
			}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
			&states.ResourceInstanceObjectSrc{
				AttrsJSON: []byte(`{"id":"bar"}`),
				Status:    states.ObjectTainted,
			},
			addrs.ProviderConfig{Type: "test"}.Absolute(addrs.RootModuleInstance),
		)
	})
	statePath := testStateFile(t, state)

	ui := new(cli.MockUi)
	c := &UntaintCommand{
		Meta: Meta{
			Ui: ui,
		},
	}

	args := []string{
		"-state", statePath,
		"test_instance.bar",
	}
	if code := c.Run(args); code == 0 {
		t.Fatalf("bad: %d\n\n%s", code, ui.OutputWriter.String())
	}
}

func TestUntaint_missingAllow(t *testing.T) {
	state := states.BuildState(func(s *states.SyncState) {
		s.SetResourceInstanceCurrent(
			addrs.Resource{
				Mode: addrs.ManagedResourceMode,
				Type: "test_instance",
				Name: "foo",
			}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
			&states.ResourceInstanceObjectSrc{
				AttrsJSON: []byte(`{"id":"bar"}`),
				Status:    states.ObjectTainted,
			},
			addrs.ProviderConfig{Type: "test"}.Absolute(addrs.RootModuleInstance),
		)
	})
	statePath := testStateFile(t, state)

	ui := new(cli.MockUi)
	c := &UntaintCommand{
		Meta: Meta{
			Ui: ui,
		},
	}

	args := []string{
		"-allow-missing",
		"-state", statePath,
		"test_instance.bar",
	}
	if code := c.Run(args); code != 0 {
		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
	}
}

func TestUntaint_stateOut(t *testing.T) {
	// Get a temp cwd
	tmp, cwd := testCwd(t)
	defer testFixCwd(t, tmp, cwd)

	// Write the temp state
	state := &terraform.State{
		Modules: []*terraform.ModuleState{
			&terraform.ModuleState{
				Path: []string{"root"},
				Resources: map[string]*terraform.ResourceState{
					"test_instance.foo": &terraform.ResourceState{
						Type: "test_instance",
						Primary: &terraform.InstanceState{
							ID:      "bar",
							Tainted: true,
						},
					},
				},
			},
		},
	}
	path := testStateFileDefault(t, state)

	ui := new(cli.MockUi)
	c := &UntaintCommand{
		Meta: Meta{
			Ui: ui,
		},
	}

	args := []string{
		"-state-out", "foo",
		"test_instance.foo",
	}
	if code := c.Run(args); code != 0 {
		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
	}

	testStateOutput(t, path, strings.TrimSpace(`
test_instance.foo: (tainted)
  ID = bar
  provider = provider.test
	`))
	testStateOutput(t, "foo", strings.TrimSpace(`
test_instance.foo:
  ID = bar
  provider = provider.test
	`))
}

func TestUntaint_module(t *testing.T) {
	state := states.BuildState(func(s *states.SyncState) {
		s.SetResourceInstanceCurrent(
			addrs.Resource{
				Mode: addrs.ManagedResourceMode,
				Type: "test_instance",
				Name: "foo",
			}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
			&states.ResourceInstanceObjectSrc{
				AttrsJSON: []byte(`{"id":"bar"}`),
				Status:    states.ObjectTainted,
			},
			addrs.ProviderConfig{Type: "test"}.Absolute(addrs.RootModuleInstance),
		)
		s.SetResourceInstanceCurrent(
			addrs.Resource{
				Mode: addrs.ManagedResourceMode,
				Type: "test_instance",
				Name: "blah",
			}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance.Child("child", addrs.NoKey)),
			&states.ResourceInstanceObjectSrc{
				AttrsJSON: []byte(`{"id":"bar"}`),
				Status:    states.ObjectTainted,
			},
			addrs.ProviderConfig{Type: "test"}.Absolute(addrs.RootModuleInstance),
		)
	})
	statePath := testStateFile(t, state)

	ui := new(cli.MockUi)
	c := &UntaintCommand{
		Meta: Meta{
			Ui: ui,
		},
	}

	args := []string{
		"-state", statePath,
		"module.child.test_instance.blah",
	}
	if code := c.Run(args); code != 0 {
		t.Fatalf("command exited with status code %d; want 0\n\n%s", code, ui.ErrorWriter.String())
	}

	testStateOutput(t, statePath, strings.TrimSpace(`
test_instance.foo: (tainted)
  ID = bar
  provider = provider.test

module.child:
  test_instance.blah:
    ID = bar
    provider = provider.test
	`))
}
<commit_msg>command: Fix TestUntaint<commit_after>package command

import (
	"os"
	"strings"
	"testing"

	"github.com/hashicorp/terraform/addrs"
	"github.com/hashicorp/terraform/states"
	"github.com/hashicorp/terraform/terraform"
	"github.com/mitchellh/cli"
)

func TestUntaint(t *testing.T) {
	state := states.BuildState(func(s *states.SyncState) {
		s.SetResourceInstanceCurrent(
			addrs.Resource{
				Mode: addrs.ManagedResourceMode,
				Type: "test_instance",
				Name: "foo",
			}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
			&states.ResourceInstanceObjectSrc{
				AttrsJSON: []byte(`{"id":"bar"}`),
				Status:    states.ObjectTainted,
			},
			addrs.ProviderConfig{Type: "test"}.Absolute(addrs.RootModuleInstance),
		)
	})
	statePath := testStateFile(t, state)

	ui := new(cli.MockUi)
	c := &UntaintCommand{
		Meta: Meta{
			Ui: ui,
		},
	}

	args := []string{
		"-state", statePath,
		"test_instance.foo",
	}
	if code := c.Run(args); code != 0 {
		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
	}

	expected := strings.TrimSpace(`
test_instance.foo:
  ID = bar
  provider = provider.test
	`)
	testStateOutput(t, statePath, expected)
}

func TestUntaint_lockedState(t *testing.T) {
	state := states.BuildState(func(s *states.SyncState) {
		s.SetResourceInstanceCurrent(
			addrs.Resource{
				Mode: addrs.ManagedResourceMode,
				Type: "test_instance",
				Name: "foo",
			}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
			&states.ResourceInstanceObjectSrc{
				AttrsJSON: []byte(`{"id":"bar"}`),
				Status:    states.ObjectTainted,
			},
			addrs.ProviderConfig{Type: "test"}.Absolute(addrs.RootModuleInstance),
		)
	})
	statePath := testStateFile(t, state)
	unlock, err := testLockState("./testdata", statePath)
	if err != nil {
		t.Fatal(err)
	}
	defer unlock()

	ui := new(cli.MockUi)
	c := &UntaintCommand{
		Meta: Meta{
			Ui: ui,
		},
	}

	args := []string{
		"-state", statePath,
		"test_instance.foo",
	}
	if code := c.Run(args); code == 0 {
		t.Fatal("expected error")
	}

	output := ui.ErrorWriter.String()
	if !strings.Contains(output, "lock") {
		t.Fatal("command output does not look like a lock error:", output)
	}
}

func TestUntaint_backup(t *testing.T) {
	// Get a temp cwd
	tmp, cwd := testCwd(t)
	defer testFixCwd(t, tmp, cwd)

	// Write the temp state
	state := &terraform.State{
		Modules: []*terraform.ModuleState{
			&terraform.ModuleState{
				Path: []string{"root"},
				Resources: map[string]*terraform.ResourceState{
					"test_instance.foo": &terraform.ResourceState{
						Type: "test_instance",
						Primary: &terraform.InstanceState{
							ID:      "bar",
							Tainted: true,
						},
					},
				},
			},
		},
	}
	path := testStateFileDefault(t, state)

	ui := new(cli.MockUi)
	c := &UntaintCommand{
		Meta: Meta{
			Ui: ui,
		},
	}

	args := []string{
		"test_instance.foo",
	}
	if code := c.Run(args); code != 0 {
		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
	}

	// Backup is still tainted
	testStateOutput(t, path+".backup", strings.TrimSpace(`
test_instance.foo: (tainted)
  ID = bar
  provider = provider.test
	`))

	// State is untainted
	testStateOutput(t, path, strings.TrimSpace(`
test_instance.foo:
  ID = bar
  provider = provider.test
	`))
}

func TestUntaint_backupDisable(t *testing.T) {
	// Get a temp cwd
	tmp, cwd := testCwd(t)
	defer testFixCwd(t, tmp, cwd)

	// Write the temp state
	state := &terraform.State{
		Modules: []*terraform.ModuleState{
			&terraform.ModuleState{
				Path: []string{"root"},
				Resources: map[string]*terraform.ResourceState{
					"test_instance.foo": &terraform.ResourceState{
						Type: "test_instance",
						Primary: &terraform.InstanceState{
							ID:      "bar",
							Tainted: true,
						},
					},
				},
			},
		},
	}
	path := testStateFileDefault(t, state)

	ui := new(cli.MockUi)
	c := &UntaintCommand{
		Meta: Meta{
			Ui: ui,
		},
	}

	args := []string{
		"-backup", "-",
		"test_instance.foo",
	}
	if code := c.Run(args); code != 0 {
		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
	}

	if _, err := os.Stat(path + ".backup"); err == nil {
		t.Fatal("backup path should not exist")
	}

	testStateOutput(t, path, strings.TrimSpace(`
test_instance.foo:
  ID = bar
  provider = provider.test
	`))
}

func TestUntaint_badState(t *testing.T) {
	ui := new(cli.MockUi)
	c := &UntaintCommand{
		Meta: Meta{
			Ui: ui,
		},
	}

	args := []string{
		"-state", "i-should-not-exist-ever",
		"foo",
	}
	if code := c.Run(args); code != 1 {
		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
	}
}

func TestUntaint_defaultState(t *testing.T) {
	// Get a temp cwd
	tmp, cwd := testCwd(t)
	defer testFixCwd(t, tmp, cwd)

	// Write the temp state
	state := &terraform.State{
		Modules: []*terraform.ModuleState{
			&terraform.ModuleState{
				Path: []string{"root"},
				Resources: map[string]*terraform.ResourceState{
					"test_instance.foo": &terraform.ResourceState{
						Type: "test_instance",
						Primary: &terraform.InstanceState{
							ID:      "bar",
							Tainted: true,
						},
					},
				},
			},
		},
	}
	path := testStateFileDefault(t, state)

	ui := new(cli.MockUi)
	c := &UntaintCommand{
		Meta: Meta{
			Ui: ui,
		},
	}

	args := []string{
		"test_instance.foo",
	}
	if code := c.Run(args); code != 0 {
		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
	}

	testStateOutput(t, path, strings.TrimSpace(`
test_instance.foo:
  ID = bar
  provider = provider.test
	`))
}

func TestUntaint_missing(t *testing.T) {
	state := states.BuildState(func(s *states.SyncState) {
		s.SetResourceInstanceCurrent(
			addrs.Resource{
				Mode: addrs.ManagedResourceMode,
				Type: "test_instance",
				Name: "foo",
			}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
			&states.ResourceInstanceObjectSrc{
				AttrsJSON: []byte(`{"id":"bar"}`),
				Status:    states.ObjectTainted,
			},
			addrs.ProviderConfig{Type: "test"}.Absolute(addrs.RootModuleInstance),
		)
	})
	statePath := testStateFile(t, state)

	ui := new(cli.MockUi)
	c := &UntaintCommand{
		Meta: Meta{
			Ui: ui,
		},
	}

	args := []string{
		"-state", statePath,
		"test_instance.bar",
	}
	if code := c.Run(args); code == 0 {
		t.Fatalf("bad: %d\n\n%s", code, ui.OutputWriter.String())
	}
}

func TestUntaint_missingAllow(t *testing.T) {
	state := states.BuildState(func(s *states.SyncState) {
		s.SetResourceInstanceCurrent(
			addrs.Resource{
				Mode: addrs.ManagedResourceMode,
				Type: "test_instance",
				Name: "foo",
			}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
			&states.ResourceInstanceObjectSrc{
				AttrsJSON: []byte(`{"id":"bar"}`),
				Status:    states.ObjectTainted,
			},
			addrs.ProviderConfig{Type: "test"}.Absolute(addrs.RootModuleInstance),
		)
	})
	statePath := testStateFile(t, state)

	ui := new(cli.MockUi)
	c := &UntaintCommand{
		Meta: Meta{
			Ui: ui,
		},
	}

	args := []string{
		"-allow-missing",
		"-state", statePath,
		"test_instance.bar",
	}
	if code := c.Run(args); code != 0 {
		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
	}
}

func TestUntaint_stateOut(t *testing.T) {
	// Get a temp cwd
	tmp, cwd := testCwd(t)
	defer testFixCwd(t, tmp, cwd)

	// Write the temp state
	state := &terraform.State{
		Modules: []*terraform.ModuleState{
			&terraform.ModuleState{
				Path: []string{"root"},
				Resources: map[string]*terraform.ResourceState{
					"test_instance.foo": &terraform.ResourceState{
						Type: "test_instance",
						Primary: &terraform.InstanceState{
							ID:      "bar",
							Tainted: true,
						},
					},
				},
			},
		},
	}
	path := testStateFileDefault(t, state)

	ui := new(cli.MockUi)
	c := &UntaintCommand{
		Meta: Meta{
			Ui: ui,
		},
	}

	args := []string{
		"-state-out", "foo",
		"test_instance.foo",
	}
	if code := c.Run(args); code != 0 {
		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
	}

	testStateOutput(t, path, strings.TrimSpace(`
test_instance.foo: (tainted)
  ID = bar
  provider = provider.test
	`))
	testStateOutput(t, "foo", strings.TrimSpace(`
test_instance.foo:
  ID = bar
  provider = provider.test
	`))
}

func TestUntaint_module(t *testing.T) {
	state := states.BuildState(func(s *states.SyncState) {
		s.SetResourceInstanceCurrent(
			addrs.Resource{
				Mode: addrs.ManagedResourceMode,
				Type: "test_instance",
				Name: "foo",
			}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
			&states.ResourceInstanceObjectSrc{
				AttrsJSON: []byte(`{"id":"bar"}`),
				Status:    states.ObjectTainted,
			},
			addrs.ProviderConfig{Type: "test"}.Absolute(addrs.RootModuleInstance),
		)
		s.SetResourceInstanceCurrent(
			addrs.Resource{
				Mode: addrs.ManagedResourceMode,
				Type: "test_instance",
				Name: "blah",
			}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance.Child("child", addrs.NoKey)),
			&states.ResourceInstanceObjectSrc{
				AttrsJSON: []byte(`{"id":"bar"}`),
				Status:    states.ObjectTainted,
			},
			addrs.ProviderConfig{Type: "test"}.Absolute(addrs.RootModuleInstance),
		)
	})
	statePath := testStateFile(t, state)

	ui := new(cli.MockUi)
	c := &UntaintCommand{
		Meta: Meta{
			Ui: ui,
		},
	}

	args := []string{
		"-state", statePath,
		"module.child.test_instance.blah",
	}
	if code := c.Run(args); code != 0 {
		t.Fatalf("command exited with status code %d; want 0\n\n%s", code, ui.ErrorWriter.String())
	}

	testStateOutput(t, statePath, strings.TrimSpace(`
test_instance.foo: (tainted)
  ID = bar
  provider = provider.test

module.child:
  test_instance.blah:
    ID = bar
    provider = provider.test
	`))
}
<|endoftext|>


<commit_before>package http

import (
	"fmt"
	"net"
	"net/http"
	"strings"

	"github.com/sparrowdb/db"
	"github.com/sparrowdb/monitor"
	"github.com/sparrowdb/slog"
	"github.com/sparrowdb/spql"
)

// HTTPServer holds HTTP server configuration and routes
type HTTPServer struct {
	Config        *db.SparrowConfig
	mux           *http.ServeMux
	dbManager     *db.DBManager
	routers       map[string]*controllerInfo
	queryExecutor *spql.QueryExecutor
	listener      net.Listener
}

type controllerInfo struct {
	route      string
	httpMethod string
	method     func(request *RequestData)
}

func (httpServer *HTTPServer) add(c *controllerInfo) {
	parts := strings.Split(c.route[1:], "/")
	httpServer.routers[parts[0]] = c
}

func (httpServer *HTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	parts := strings.Split(r.URL.Path[1:], "/")

	if parts[0] == "favicon.ico" {
		return
	}

	monitor.IncHTTPRequests()

	if controller, ok := httpServer.routers[parts[0]]; ok {
		parts := strings.Split(r.URL.Path[1:], "/")
		w.Header().Set("Access-Control-Allow-Origin", "*")
		controller.method(&RequestData{responseWriter: w, request: r, params: parts[1:]})
	}
}

// Start starts HTTP server listener
func (httpServer *HTTPServer) Start() {
	var err error
	httpServer.listener, err = net.Listen("tcp", fmt.Sprintf("%s:%s", httpServer.Config.HTTPHost, httpServer.Config.HTTPPort))
	if err != nil {
		slog.Fatalf(err.Error())
	}

	slog.Infof("Starting HTTP Server %s:%s", httpServer.Config.HTTPHost, httpServer.Config.HTTPPort)

	handler := NewServeHandler(httpServer.dbManager, httpServer.queryExecutor)

	httpServer.add(&controllerInfo{route: "/user", httpMethod: "POST", method: handler.user})

	r, w, q := httpServer.Config.GetMode()
	if r == true {
		httpServer.add(&controllerInfo{route: "/g", httpMethod: "GET", method: handler.get})
	}
	if w == true {
		httpServer.add(&controllerInfo{route: "/upload", httpMethod: "POST", method: handler.upload})
	}
	if q == true {
		httpServer.add(&controllerInfo{route: "/query", httpMethod: "POST", method: handler.serveQuery})
	}

	httpServer.mux.Handle("/", httpServer)

	http.Serve(httpServer.listener, httpServer.mux)
}

// Stop stops HTTP server listener
func (httpServer *HTTPServer) Stop() {
	slog.Infof("Stopping HTTP Server")
	httpServer.listener.Close()
}

// NewHTTPServer returns new HTTPServer
func NewHTTPServer(config *db.SparrowConfig, dbm *db.DBManager) HTTPServer {
	return HTTPServer{
		Config:        config,
		dbManager:     dbm,
		queryExecutor: spql.NewQueryExecutor(dbm),
		mux:           http.NewServeMux(),
		routers:       make(map[string]*controllerInfo),
	}
}
<commit_msg>server name response header<commit_after>package http

import (
	"fmt"
	"net"
	"net/http"
	"strings"

	"github.com/sparrowdb/db"
	"github.com/sparrowdb/monitor"
	"github.com/sparrowdb/slog"
	"github.com/sparrowdb/spql"
)

// HTTPServer holds HTTP server configuration and routes
type HTTPServer struct {
	Config        *db.SparrowConfig
	mux           *http.ServeMux
	dbManager     *db.DBManager
	routers       map[string]*controllerInfo
	queryExecutor *spql.QueryExecutor
	listener      net.Listener
}

type controllerInfo struct {
	route      string
	httpMethod string
	method     func(request *RequestData)
}

func (httpServer *HTTPServer) add(c *controllerInfo) {
	parts := strings.Split(c.route[1:], "/")
	httpServer.routers[parts[0]] = c
}

func (httpServer *HTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	parts := strings.Split(r.URL.Path[1:], "/")

	if parts[0] == "favicon.ico" {
		return
	}

	monitor.IncHTTPRequests()

	if controller, ok := httpServer.routers[parts[0]]; ok {
		parts := strings.Split(r.URL.Path[1:], "/")
		w.Header().Set("Access-Control-Allow-Origin", "*")
		w.Header().Set("Server", "SparrowDb")
		controller.method(&RequestData{responseWriter: w, request: r, params: parts[1:]})
	}
}

// Start starts HTTP server listener
func (httpServer *HTTPServer) Start() {
	var err error
	httpServer.listener, err = net.Listen("tcp", fmt.Sprintf("%s:%s", httpServer.Config.HTTPHost, httpServer.Config.HTTPPort))
	if err != nil {
		slog.Fatalf(err.Error())
	}

	slog.Infof("Starting HTTP Server %s:%s", httpServer.Config.HTTPHost, httpServer.Config.HTTPPort)

	handler := NewServeHandler(httpServer.dbManager, httpServer.queryExecutor)

	httpServer.add(&controllerInfo{route: "/user", httpMethod: "POST", method: handler.user})

	r, w, q := httpServer.Config.GetMode()
	if r == true {
		httpServer.add(&controllerInfo{route: "/g", httpMethod: "GET", method: handler.get})
	}
	if w == true {
		httpServer.add(&controllerInfo{route: "/upload", httpMethod: "POST", method: handler.upload})
	}
	if q == true {
		httpServer.add(&controllerInfo{route: "/query", httpMethod: "POST", method: handler.serveQuery})
	}

	httpServer.mux.Handle("/", httpServer)

	http.Serve(httpServer.listener, httpServer.mux)
}

// Stop stops HTTP server listener
func (httpServer *HTTPServer) Stop() {
	slog.Infof("Stopping HTTP Server")
	httpServer.listener.Close()
}

// NewHTTPServer returns new HTTPServer
func NewHTTPServer(config *db.SparrowConfig, dbm *db.DBManager) HTTPServer {
	return HTTPServer{
		Config:        config,
		dbManager:     dbm,
		queryExecutor: spql.NewQueryExecutor(dbm),
		mux:           http.NewServeMux(),
		routers:       make(map[string]*controllerInfo),
	}
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"html/template"
	"log"
	"net/http"
	"os"
	"path/filepath"

	"github.com/gorilla/mux"
)

type HTTPStaticServer struct {
	Root string
	m    *mux.Router
}

func NewHTTPStaticServer(root string) *HTTPStaticServer {
	if root == "" {
		root = "."
	}
	m := mux.NewRouter()
	s := &HTTPStaticServer{
		Root: root,
		m:    m,
	}
	m.HandleFunc("/-/res/{path:.*}", s.hAssets)
	m.HandleFunc("/-/raw/{path:.*}", s.hFileOrDirectory)
	m.HandleFunc("/-/json/{path:.*}", s.hJSONList)
	m.HandleFunc("/{path:.*}", s.hIndex)
	return s
}

func (s *HTTPStaticServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	s.m.ServeHTTP(w, r)
}

func (s *HTTPStaticServer) hIndex(w http.ResponseWriter, r *http.Request) {
	indexPath := filepath.Join("./res", "index.tmpl.html")
	t := template.New("index").Delims("[[", "]]")
	tmpl := template.Must(t.ParseFiles(indexPath))
	tmpl.ExecuteTemplate(w, "index.tmpl.html", nil)
}

func (s *HTTPStaticServer) hAssets(w http.ResponseWriter, r *http.Request) {
	path := mux.Vars(r)["path"]
	// w.Header().Set("Content-Type", "text/plain") // -_-! not working in chrome
	http.ServeFile(w, r, filepath.Join("./res", path))
}

func (s *HTTPStaticServer) hFileOrDirectory(w http.ResponseWriter, r *http.Request) {
	path := mux.Vars(r)["path"]
	log.Println("Path:", s.Root, path)
	http.ServeFile(w, r, filepath.Join(s.Root, path))
}

type ListResponse struct {
	Name string `json:"name"`
	Path string `json:"path"`
	Type string `json:"type"`
	Size string `json:"size"`
}

func (s *HTTPStaticServer) hJSONList(w http.ResponseWriter, r *http.Request) {
	path := mux.Vars(r)["path"]
	lrs := make([]ListResponse, 0)
	fd, err := os.Open(filepath.Join(s.Root, path))
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}
	defer fd.Close()

	files, err := fd.Readdir(-1)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}
	for _, file := range files {
		lr := ListResponse{
			Name: file.Name(),
			Path: filepath.Join(path, file.Name()), // lstrip "/"
		}
		if file.IsDir() {
			lr.Type = "dir"
			lr.Size = "-"
		} else {
			lr.Type = "file"
			lr.Size = formatSize(file)
		}
		lrs = append(lrs, lr)
	}

	data, _ := json.Marshal(lrs)
	w.Header().Set("Content-Type", "application/json")
	w.Write(data)
}
<commit_msg>use normal as serve path<commit_after>package main

import (
	"encoding/json"
	"html/template"
	"log"
	"net/http"
	"os"
	"path/filepath"

	"github.com/gorilla/mux"
)

type HTTPStaticServer struct {
	Root string
	m    *mux.Router
}

func NewHTTPStaticServer(root string) *HTTPStaticServer {
	if root == "" {
		root = "."
	}
	m := mux.NewRouter()
	s := &HTTPStaticServer{
		Root: root,
		m:    m,
	}
	m.HandleFunc("/-/res/{path:.*}", s.hAssets)
	m.HandleFunc("/-/raw/{path:.*}", s.hFileOrDirectory)
	m.HandleFunc("/-/json/{path:.*}", s.hJSONList)
	m.HandleFunc("/{path:.*}", s.hIndex)
	return s
}

func (s *HTTPStaticServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	s.m.ServeHTTP(w, r)
}

func (s *HTTPStaticServer) hIndex(w http.ResponseWriter, r *http.Request) {
	path := mux.Vars(r)["path"]
	relPath := filepath.Join(s.Root, path)
	finfo, err := os.Stat(relPath)
	if err == nil && finfo.IsDir() {
		indexPath := filepath.Join("./res", "index.tmpl.html")
		t := template.New("index").Delims("[[", "]]")
		tmpl := template.Must(t.ParseFiles(indexPath))
		tmpl.ExecuteTemplate(w, "index.tmpl.html", nil)
	} else {
		http.ServeFile(w, r, relPath)
	}
}

func (s *HTTPStaticServer) hAssets(w http.ResponseWriter, r *http.Request) {
	path := mux.Vars(r)["path"]
	// w.Header().Set("Content-Type", "text/plain") // -_-! not working in chrome
	http.ServeFile(w, r, filepath.Join("./res", path))
}

func (s *HTTPStaticServer) hFileOrDirectory(w http.ResponseWriter, r *http.Request) {
	path := mux.Vars(r)["path"]
	log.Println("Path:", s.Root, path)
	http.ServeFile(w, r, filepath.Join(s.Root, path))
}

type ListResponse struct {
	Name string `json:"name"`
	Path string `json:"path"`
	Type string `json:"type"`
	Size string `json:"size"`
}

func (s *HTTPStaticServer) hJSONList(w http.ResponseWriter, r *http.Request) {
	path := mux.Vars(r)["path"]
	lrs := make([]ListResponse, 0)
	fd, err := os.Open(filepath.Join(s.Root, path))
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}
	defer fd.Close()

	files, err := fd.Readdir(-1)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}
	for _, file := range files {
		lr := ListResponse{
			Name: file.Name(),
			Path: filepath.Join(path, file.Name()), // lstrip "/"
		}
		if file.IsDir() {
			lr.Type = "dir"
			lr.Size = "-"
		} else {
			lr.Type = "file"
			lr.Size = formatSize(file)
		}
		lrs = append(lrs, lr)
	}

	data, _ := json.Marshal(lrs)
	w.Header().Set("Content-Type", "application/json")
	w.Write(data)
}
<|endoftext|>


<commit_before>package ctxcopy

import (
	"errors"
	"io"

	"golang.org/x/net/context"
)

const (
	// Zero buffer length error string.
	ZeroBufferLenError string = "Buffer length is 0."
	// Copy canceled string.
	CanceledStr string = "Copy operation is canceled."
)

// copyWithCancelation() reads bytes to buffer from source and write to destination from buffer with cancelation signal.
//
//   Params:
//     dst: Destination.
//     src: Source.
//     buf: Buffer(length should >= 0).
//     cancel: cancelation flag. It'll stop copy while the cancelation signal is set to true.
func copyWithCancelation(dst io.Writer, src io.Reader, buf []byte, cancel *bool) (err error) {
	if len(buf) == 0 {
		return errors.New(ZeroBufferLenError)
	}

	for {
		if cancel != nil && (*cancel) {
			return errors.New(CanceledStr)
		}

		n, err := src.Read(buf)
		if err != nil && err != io.EOF {
			return err
		} else {
			if n == 0 {
				break
			}
		}

		if _, err := dst.Write(buf[:n]); err != nil {
			return err
		}
	}
	return nil
}

// Copy() reads bytes to buffer from source and write to destination from buffer with cancelation signal.
//
//   Params:
//     ctx:
//       Google's Context type which carries deadlines, cacelation signals,
//       and other request-scoped values across API boundaries and between processes.
//       See https://godoc.org/golang.org/x/net/context for more.
//     dst: Destination.
//     src: Source.
//     buf: Buffer(length should >= 0).
func Copy(ctx context.Context, dst io.Writer, src io.Reader, buf []byte) (err error) {
	cancel := false
	c := make(chan error)

	go func() {
		c <- copyWithCancelation(dst, src, buf, &cancel)
	}()

	select {
	case err = <-c:
		return err

	case <-ctx.Done():
		// Set cancelation flag to true
		cancel = true
		// Wait for copyWithCancelation() return.
		<-c
		return ctx.Err()
	}
}
<commit_msg>Fix const, method names in comments to remove golint warnings.<commit_after>package ctxcopy

import (
	"errors"
	"io"

	"golang.org/x/net/context"
)

const (
	// ZeroBufferLenError is error message of buffer length.
	ZeroBufferLenError string = "Buffer length is 0."
	// CanceledStr is the message of copy operation canceled.
	CanceledStr string = "Copy operation is canceled."
)

// copyWithCancelation reads bytes to buffer from source and write to destination from buffer with cancelation signal.
//
//   Params:
//     dst: Destination.
//     src: Source.
//     buf: Buffer(length should >= 0).
//     cancel: cancelation flag. It'll stop copy while the cancelation signal is set to true.
func copyWithCancelation(dst io.Writer, src io.Reader, buf []byte, cancel *bool) (err error) {
	if len(buf) == 0 {
		return errors.New(ZeroBufferLenError)
	}

	for {
		if cancel != nil && (*cancel) {
			return errors.New(CanceledStr)
		}

		n, err := src.Read(buf)
		if err != nil && err != io.EOF {
			return err
		} else {
			if n == 0 {
				break
			}
		}

		if _, err := dst.Write(buf[:n]); err != nil {
			return err
		}
	}
	return nil
}

// Copy reads bytes to buffer from source and write to destination from buffer with cancelation signal.
//
//   Params:
//     ctx:
//       Google's Context type which carries deadlines, cacelation signals,
//       and other request-scoped values across API boundaries and between processes.
//       See https://godoc.org/golang.org/x/net/context for more.
//     dst: Destination.
//     src: Source.
//     buf: Buffer(length should >= 0).
func Copy(ctx context.Context, dst io.Writer, src io.Reader, buf []byte) (err error) {
	cancel := false
	c := make(chan error)

	go func() {
		c <- copyWithCancelation(dst, src, buf, &cancel)
	}()

	select {
	case err = <-c:
		return err

	case <-ctx.Done():
		// Set cancelation flag to true
		cancel = true
		// Wait for copyWithCancelation() return.
		<-c
		return ctx.Err()
	}
}
<|endoftext|>


<commit_before>package tartrans

import (
	"io"
	"net/url"

	. "github.com/polydawn/go-errcat"

	"go.polydawn.net/go-timeless-api"
	"go.polydawn.net/go-timeless-api/rio"
	"go.polydawn.net/rio/transmat/mixins/log"
	"go.polydawn.net/rio/warehouse"
	"go.polydawn.net/rio/warehouse/impl/kvfs"
	"go.polydawn.net/rio/warehouse/impl/kvhttp"
)

// The shared bits of warehouseAddr parse and dial code.

// Pick a warehouse.
//  With K/V warehouses, this takes the form of "pick the first one that answers".
func PickReader(
	wareID api.WareID,
	warehouses []api.WarehouseAddr,
	requireMono bool,
	mon rio.Monitor,
) (_ io.ReadCloser, err error) {
	defer RequireErrorHasCategory(&err, rio.ErrorCategory(""))

	var anyWarehouses bool // for clarity in final error messages
	for _, addr := range warehouses {
		// REVIEW ... Do I really have to parse this again?  is this sanely encapsulated?
		u, err := url.Parse(string(addr))
		if err != nil {
			return nil, Errorf(rio.ErrUsage, "failed to parse URI: %s", err)
		}
		var whCtrl warehouse.BlobstoreController
		switch u.Scheme {
		case "ca+file":
			if requireMono {
				return nil, Errorf(rio.ErrUsage, "this fetch operation doesn't support %q scheme (a single-ware warehouse is required, not CA-mode)", u.Scheme)
			}
			fallthrough
		case "file":
			whCtrl, err = kvfs.NewController(addr)
		case "ca+http", "ca+https":
			if requireMono {
				return nil, Errorf(rio.ErrUsage, "this fetch operation doesn't support %q scheme (a single-ware warehouse is required, not CA-mode)", u.Scheme)
			}
			fallthrough
		case "http", "https":
			whCtrl, err = kvhttp.NewController(addr)
		default:
			return nil, Errorf(rio.ErrUsage, "this fetch operation doesn't support %q scheme (valid options are 'file', 'ca+file', 'http', 'ca+http', 'https', or 'ca+https')", u.Scheme)
		}
		switch Category(err) {
		case nil:
			anyWarehouses = true
			// pass
		case rio.ErrWarehouseUnavailable:
			if requireMono {
				return nil, err
			}
			log.WarehouseUnavailable(mon, err, addr, wareID, "read")
			continue // okay!  skip to the next one.
		default:
			return nil, err
		}
		reader, err := whCtrl.OpenReader(wareID)
		switch Category(err) {
		case nil:
			log.WareReaderOpened(mon, addr, wareID)
			return reader, nil // happy path return!
		case rio.ErrWareNotFound:
			log.WareNotFound(mon, err, addr, wareID)
			continue // okay!  skip to the next one.
		default:
			return nil, err
		}
	}
	if !anyWarehouses {
		return nil, Errorf(rio.ErrWarehouseUnavailable, "no warehouses were available!")
	}
	return nil, Errorf(rio.ErrWareNotFound, "none of the available warehouses have ware %q!", wareID)
}

func OpenWriteController(
	warehouseAddr api.WarehouseAddr,
	packType api.PackType,
	mon rio.Monitor,
) (wc warehouse.BlobstoreWriteController, err error) {
	defer RequireErrorHasCategory(&err, rio.ErrorCategory(""))

	// REVIEW ... Do I really have to parse this again?  is this sanely encapsulated?
	u, err := url.Parse(string(warehouseAddr))
	if err != nil {
		return nil, Errorf(rio.ErrUsage, "failed to parse URI: %s", err)
	}
	switch u.Scheme {
	case "":
		wc = warehouse.NullBlobstoreWriteController{}
		return wc, nil
	case "file", "ca+file":
		whCtrl, err := kvfs.NewController(warehouseAddr)
		switch Category(err) {
		case nil:
			// pass
		case rio.ErrWarehouseUnavailable:
			log.WarehouseUnavailable(mon, err, warehouseAddr, api.WareID{packType, "?"}, "write")
			return nil, err
		default:
			return nil, err
		}
		wc, err = whCtrl.OpenWriter()
		switch Category(err) {
		case nil:
			return wc, nil // Yayy!
		case rio.ErrWarehouseUnwritable:
			log.WarehouseUnavailable(mon, err, warehouseAddr, api.WareID{packType, "?"}, "write")
			return nil, err
		default:
			return nil, err
		}
	default:
		return nil, Errorf(rio.ErrUsage, "this save operation doesn't support %q scheme (valid options are 'file' or 'ca+file')", u.Scheme)
	}
}
<commit_msg>transmat/tar: fix silently using dev null target if the "url" is missing a scheme.<commit_after>package tartrans

import (
	"io"
	"net/url"

	. "github.com/polydawn/go-errcat"

	"go.polydawn.net/go-timeless-api"
	"go.polydawn.net/go-timeless-api/rio"
	"go.polydawn.net/rio/transmat/mixins/log"
	"go.polydawn.net/rio/warehouse"
	"go.polydawn.net/rio/warehouse/impl/kvfs"
	"go.polydawn.net/rio/warehouse/impl/kvhttp"
)

// The shared bits of warehouseAddr parse and dial code.

// Pick a warehouse.
//  With K/V warehouses, this takes the form of "pick the first one that answers".
func PickReader(
	wareID api.WareID,
	warehouses []api.WarehouseAddr,
	requireMono bool,
	mon rio.Monitor,
) (_ io.ReadCloser, err error) {
	defer RequireErrorHasCategory(&err, rio.ErrorCategory(""))

	var anyWarehouses bool // for clarity in final error messages
	for _, addr := range warehouses {
		// REVIEW ... Do I really have to parse this again?  is this sanely encapsulated?
		u, err := url.Parse(string(addr))
		if err != nil {
			return nil, Errorf(rio.ErrUsage, "failed to parse URI: %s", err)
		}
		var whCtrl warehouse.BlobstoreController
		switch u.Scheme {
		case "ca+file":
			if requireMono {
				return nil, Errorf(rio.ErrUsage, "this fetch operation doesn't support %q scheme (a single-ware warehouse is required, not CA-mode)", u.Scheme)
			}
			fallthrough
		case "file":
			whCtrl, err = kvfs.NewController(addr)
		case "ca+http", "ca+https":
			if requireMono {
				return nil, Errorf(rio.ErrUsage, "this fetch operation doesn't support %q scheme (a single-ware warehouse is required, not CA-mode)", u.Scheme)
			}
			fallthrough
		case "http", "https":
			whCtrl, err = kvhttp.NewController(addr)
		default:
			return nil, Errorf(rio.ErrUsage, "this fetch operation doesn't support %q scheme (valid options are 'file', 'ca+file', 'http', 'ca+http', 'https', or 'ca+https')", u.Scheme)
		}
		switch Category(err) {
		case nil:
			anyWarehouses = true
			// pass
		case rio.ErrWarehouseUnavailable:
			if requireMono {
				return nil, err
			}
			log.WarehouseUnavailable(mon, err, addr, wareID, "read")
			continue // okay!  skip to the next one.
		default:
			return nil, err
		}
		reader, err := whCtrl.OpenReader(wareID)
		switch Category(err) {
		case nil:
			log.WareReaderOpened(mon, addr, wareID)
			return reader, nil // happy path return!
		case rio.ErrWareNotFound:
			log.WareNotFound(mon, err, addr, wareID)
			continue // okay!  skip to the next one.
		default:
			return nil, err
		}
	}
	if !anyWarehouses {
		return nil, Errorf(rio.ErrWarehouseUnavailable, "no warehouses were available!")
	}
	return nil, Errorf(rio.ErrWareNotFound, "none of the available warehouses have ware %q!", wareID)
}

func OpenWriteController(
	warehouseAddr api.WarehouseAddr,
	packType api.PackType,
	mon rio.Monitor,
) (wc warehouse.BlobstoreWriteController, err error) {
	defer RequireErrorHasCategory(&err, rio.ErrorCategory(""))

	// REVIEW ... Do I really have to parse this again?  is this sanely encapsulated?
	if warehouseAddr == "" {
		wc = warehouse.NullBlobstoreWriteController{}
		return wc, nil
	}
	u, err := url.Parse(string(warehouseAddr))
	if err != nil {
		return nil, Errorf(rio.ErrUsage, "failed to parse URI: %s", err)
	}
	switch u.Scheme {
	case "":
		return nil, Errorf(rio.ErrUsage, "urls must always have a scheme (e.g. start with 'file://', 'ca+file://', or similar)")
	case "file", "ca+file":
		whCtrl, err := kvfs.NewController(warehouseAddr)
		switch Category(err) {
		case nil:
			// pass
		case rio.ErrWarehouseUnavailable:
			log.WarehouseUnavailable(mon, err, warehouseAddr, api.WareID{packType, "?"}, "write")
			return nil, err
		default:
			return nil, err
		}
		wc, err = whCtrl.OpenWriter()
		switch Category(err) {
		case nil:
			return wc, nil // Yayy!
		case rio.ErrWarehouseUnwritable:
			log.WarehouseUnavailable(mon, err, warehouseAddr, api.WareID{packType, "?"}, "write")
			return nil, err
		default:
			return nil, err
		}
	default:
		return nil, Errorf(rio.ErrUsage, "this save operation doesn't support %q scheme (valid options are 'file' or 'ca+file')", u.Scheme)
	}
}
<|endoftext|>


<commit_before>/* https://leetcode.com/problems/h-index/description/
Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.

According to the definition of h-index on Wikipedia:
"A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."

For example, given citations = [3, 0, 6, 1, 5], which means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 citations respectively.
Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, his h-index is 3.

Note: If there are several possible values for h, the maximum one is taken as the h-index.
*/

package leetcode

// 1. sort
import "sort"

func hIndex(citations []int) int {
	sort.Ints(citations)

	max := func(a, b int) int {
		if a > b {
			return a
		}
		return b
	}

	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}

	res := 0
	for i, citation := range citations {
		res = max(res, min(len(citations)-i, citation))
	}
	return res
}
<commit_msg>use hash table<commit_after>/* https://leetcode.com/problems/h-index/description/
Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.

According to the definition of h-index on Wikipedia:
"A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."

For example, given citations = [3, 0, 6, 1, 5], which means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 citations respectively.
Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, his h-index is 3.

Note: If there are several possible values for h, the maximum one is taken as the h-index.
*/

package leetcode

/*
// 1. sort
import "sort"

func hIndex(citations []int) int {
	sort.Ints(citations)

	max := func(a, b int) int {
		if a > b {
			return a
		}
		return b
	}

	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}

	res := 0
	for i, citation := range citations {
		res = max(res, min(len(citations)-i, citation))
	}
	return res
}
*/

func hIndex(citations []int) int {
	count := make([]int, len(citations)+1)
	for _, citation := range citations {
		if citation >= len(citations) {
			count[len(citations)]++ // 当引用数大于等于 n 时，我们均将其数量计入 count[n] 中
		} else {
			count[citation]++
		}
	}
	for i := len(citations); i > 0; i-- {
		if count[i] >= i {
			return i
		}
		count[i-1] += count[i] // 引用数大于 i-1 的数量是i-1及之后的累加
	}
	return 0
}
<|endoftext|>