<commit_before>package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"strconv"

	"github.com/gorilla/mux"

        "database/sql"
        _ "github.com/go-sql-driver/mysql"
)

// error response contains everything we need to use http.Error
type handlerError struct {
	Error   error
	Message string
	Code    int
}

// ticket model
type ticket struct {
	FirstName string `json:"firstname"`
	LastName string `json:"lastname"`
	SourceCity  string `json:"arrivecity"`
	DepartCity  string `json:"departcity"`
	FlightID  string `json:"flightid"`
	FlightDate string `json:"flightdate"`
	Id        int    `json:"id"`
	BackgroundArrive string `json:"backgroundarrive"`
	BackgroundDepart string `json:"backgrounddepart"`
}

// list of all of the tickets
var tickets = make([]ticket, 0)
var selected_ticket_id = 0;

// a custom type that we can use for handling errors and formatting responses
type handler func(w http.ResponseWriter, r *http.Request) (interface{}, *handlerError)

// attach the standard ServeHTTP method to our handler so the http library can call it
func (fn handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// here we could do some prep work before calling the handler if we wanted to

	// call the actual handler
	response, err := fn(w, r)

	// check for errors
	if err != nil {
		log.Printf("ERROR: %v\n", err.Error)
		http.Error(w, fmt.Sprintf(`{"error":"%s"}`, err.Message), err.Code)
		return
	}
	if response == nil {
		log.Printf("ERROR: response from method is nil\n")
		http.Error(w, "Internal server error. Check the logs.", http.StatusInternalServerError)
		return
	}

	// turn the response into JSON
	bytes, e := json.Marshal(response)
	if e != nil {
		http.Error(w, "Error marshalling JSON", http.StatusInternalServerError)
		return
	}

	// send the response and log
	w.Header().Set("Content-Type", "application/json")
	w.Write(bytes)
	log.Printf("%s %s %s %d", r.RemoteAddr, r.Method, r.URL, 200)
}

func listFlights( w http.ResponseWriter, r *http.Request ) ( interface{}, *handlerError) {
	con, err := sql.Open("mysql", "ars:ARSePassW0rd@/ARSdb")
	if err != nil {
	  log.Fatal( err )
	}
	defer con.Close()

	rows, err := con.Query("select * from flights")
	if err != nil {
	  log.Fatal( err )
	}

	return rows, nil
}

func listTickets(w http.ResponseWriter, r *http.Request) (interface{}, *handlerError) {
	return tickets, nil
}

func getTicketID(w http.ResponseWriter, r *http.Request) (interface{}, *handlerError) {
	return selected_ticket_id, nil;
}

func getTickets(w http.ResponseWriter, r *http.Request) (interface{}, *handlerError) {
	// mux.Vars grabs variables from the path
	param := mux.Vars(r)["id"]
	id, e := strconv.Atoi(param)
	if e != nil {
		return nil, &handlerError{e, "Id should be an integer", http.StatusBadRequest}
	}
	b, index := getTicketById(id)

	if index < 0 {
		return nil, &handlerError{nil, "Could not find ticket " + param, http.StatusNotFound}
	}

	return b, nil
}

func parseTicketRequest(r *http.Request) (ticket, *handlerError) {
	// the ticket payload is in the request body
	data, e := ioutil.ReadAll(r.Body)
	if e != nil {
		return ticket{}, &handlerError{e, "Could not read request", http.StatusBadRequest}
	}

	// turn the request body (JSON) into a ticket object
	var payload ticket
	e = json.Unmarshal(data, &payload)
	if e != nil {
		return ticket{}, &handlerError{e, "Could not parse JSON", http.StatusBadRequest}
	}

	return payload, nil
}

func addTicket(w http.ResponseWriter, r *http.Request) (interface{}, *handlerError) {
	payload, e := parseTicketRequest(r)
	if e != nil {
		return nil, e
	}

	// it's our job to assign IDs, ignore what (if anything) the client sent
	payload.Id = getNextId()
	tickets = append(tickets, payload)

	// we return the ticket we just made so the client can see the ID if they want
	return payload, nil
}

func updateTicket(w http.ResponseWriter, r *http.Request) (interface{}, *handlerError) {
	payload, e := parseTicketRequest(r)
	if e != nil {
		return nil, e
	}

	_, index := getTicketById(payload.Id)
	tickets[index] = payload
	return make(map[string]string), nil
}

func removeTicket(w http.ResponseWriter, r *http.Request) (interface{}, *handlerError) {
	param := mux.Vars(r)["id"]
	id, e := strconv.Atoi(param)
	if e != nil {
		return nil, &handlerError{e, "Id should be an integer", http.StatusBadRequest}
	}
	// this is jsut to check to see if the ticket exists
	_, index := getTicketById(id)

	if index < 0 {
		return nil, &handlerError{nil, "Could not find entry " + param, http.StatusNotFound}
	}

	// remove a ticket from the list
	tickets = append(tickets[:index], tickets[index+1:]...)
	return make(map[string]string), nil
}

// searches the tickets for the ticket with `id` and returns the ticket and it's index, or -1 for 404
func getTicketById(id int) (ticket, int) {
	for i, b := range tickets {
		if b.Id == id {
			return b, i
		}
	}
	return ticket{}, -1
}

var id = 0

// increments id and returns the value
func getNextId() int {
	id += 1
	return id
}

func main() {
	// command line flags
	port := flag.Int("port", 80, "port to serve on")
	dir := flag.String("directory", "web/", "directory of web files")
	flag.Parse()

	// handle all requests by serving a file of the same name
	fs := http.Dir(*dir)
	fileHandler := http.FileServer(fs)

	// setup routes
	router := mux.NewRouter()
	router.Handle("/", http.RedirectHandler("/static/", 302))
	router.Handle("/flights", handler(listFlights)).Methods("GET")
	router.Handle("/tickets", handler(listTickets)).Methods("GET")
	router.Handle("/tickets", handler(addTicket)).Methods("POST")
	router.Handle("/selected_ticket_id", handler(getTicketID)).Methods("GET")
	router.Handle("/tickets/{id}", handler(getTickets)).Methods("GET")
	router.Handle("/tickets/{id}", handler(updateTicket)).Methods("POST")
	router.Handle("/tickets/{id}", handler(removeTicket)).Methods("DELETE")
	router.PathPrefix("/static/").Handler(http.StripPrefix("/static", fileHandler))
	http.Handle("/", router)

	// bootstrap some data
	tickets = append(tickets, ticket{"Nathan", "Acosta", "Albuquerque, NM", "Dallas, TX", "NMA4601", "12/4/2015", getNextId(), "./images/alb_flight_img.png", "./images/dal_flight_img.png"})
	tickets = append(tickets, ticket{"Nathan", "Acosta", "Dallas, TX", "Albuquerque, NM", "NMA4603", "12/5/2015", getNextId(), "./images/dal_flight_img.png", "./images/alb_flight_img.png"})
	tickets = append(tickets, ticket{"Nathan", "Acosta", "Albuquerque, NM", "Dallas, TX", "NMA4602", "12/8/2015", getNextId(), "./images/alb_flight_img.png", "./images/dal_flight_img.png"})

	log.Printf("Running on port %d\n", *port)

	addr := fmt.Sprintf(":%d", *port)
	// this call blocks -- the progam runs here forever
	err := http.ListenAndServe(addr, nil)
	fmt.Println(err.Error())
}
<commit_msg>End-to-end db query to json<commit_after>package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"strconv"
	"time"

	"github.com/gorilla/mux"

	"database/sql"
	_ "github.com/go-sql-driver/mysql"
	"gopkg.in/gorp.v1"
)

var connectStr = "ars:ARSePassW0rd@/ARSdb?parseTime=true"
var dbType = "mysql"

func checkErr(err error, msg string) bool {

	if err != nil {
		log.Fatalln(msg, err)
	}

	return err != nil
}

// error response contains everything we need to use http.Error
type handlerError struct {
	Error   error
	Message string
	Code    int
}

// ticket model
type ticket struct {
	FirstName        string `json:"firstname"`
	LastName         string `json:"lastname"`
	SourceCity       string `json:"arrivecity"`
	DepartCity       string `json:"departcity"`
	FlightID         string `json:"flightid"`
	FlightDate       string `json:"flightdate"`
	Id               int    `json:"id"`
	BackgroundArrive string `json:"backgroundarrive"`
	BackgroundDepart string `json:"backgrounddepart"`
}

// list of all of the tickets
var tickets = make([]ticket, 0)
var selected_ticket_id = 0

type Airport struct {
	Id        int    `json:"id"`
	ShortName string `json:"shortname" db:"short_name"`
	LongName  string `json:"longname" db:"long_name"`
}

type Flight struct {
	Id            int       `json:"id" db:"id"`
	IdStr         string    `json:"idstr" db:"id_str"`
	DepartAirport string    `json:"departairport" db:"depart_airport"`
	DepartTime    time.Time `json:"departtime" db:"depart_time"`
	ArriveAirport string    `json:"arriveairport" db:"arrive_airport"`
	ArriveTime    time.Time `json:"arrivetime" db:"arrive_time"`
}

var dbmap *gorp.DbMap

func initDB() error {
	db, err := sql.Open(dbType, connectStr)

	if checkErr(err, "Database connection failed, sql.Open") {
		return err
	}

	if dbmap == nil {
		dbmap = &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
	}

	return nil
}

// a custom type that we can use for handling errors and formatting responses
type handler func(w http.ResponseWriter, r *http.Request) (interface{}, *handlerError)

// attach the standard ServeHTTP method to our handler so the http library can call it
func (fn handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// here we could do some prep work before calling the handler if we wanted to

	// call the actual handler
	response, err := fn(w, r)

	// check for errors
	if err != nil {
		log.Printf("ERROR: %v\n", err.Error)
		http.Error(w, fmt.Sprintf(`{"error":"%s"}`, err.Message), err.Code)
		return
	}
	if response == nil {
		log.Printf("ERROR: response from method is nil\n")
		http.Error(w, "Internal server error. Check the logs.", http.StatusInternalServerError)
		return
	}

	// turn the response into JSON
	bytes, e := json.Marshal(response)
	if e != nil {
		http.Error(w, "Error marshalling JSON", http.StatusInternalServerError)
		return
	}

	// send the response and log
	w.Header().Set("Content-Type", "application/json")
	w.Write(bytes)
	log.Printf("%s %s %s %d", r.RemoteAddr, r.Method, r.URL, 200)
}

func listFlights(w http.ResponseWriter, r *http.Request) (interface{}, *handlerError) {
	var flights []Flight
	_, err := dbmap.Select(&flights, "select f.id, f.id_str, da.short_name as depart_airport, f.depart_time, aa.short_name as arrive_airport, f.arrive_time from flights f, airports da, airports aa where f.depart_airport=da.id and f.arrive_airport=aa.id")
	if checkErr(err, "select from flights") {
		return nil, nil
	}

	return flights, nil
}

func listAirports(w http.ResponseWriter, r *http.Request) (interface{}, *handlerError) {
	var airports []Airport
	_, err := dbmap.Select(&airports, "select id, short_name, long_name from airports")
	if checkErr(err, "select from airports") {
		return nil, nil
	}

	return airports, nil
}

func listTickets(w http.ResponseWriter, r *http.Request) (interface{}, *handlerError) {
	return tickets, nil
}

func getTicketID(w http.ResponseWriter, r *http.Request) (interface{}, *handlerError) {
	return selected_ticket_id, nil
}

func getTickets(w http.ResponseWriter, r *http.Request) (interface{}, *handlerError) {
	// mux.Vars grabs variables from the path
	param := mux.Vars(r)["id"]
	id, e := strconv.Atoi(param)
	if e != nil {
		return nil, &handlerError{e, "Id should be an integer", http.StatusBadRequest}
	}
	b, index := getTicketById(id)

	if index < 0 {
		return nil, &handlerError{nil, "Could not find ticket " + param, http.StatusNotFound}
	}

	return b, nil
}

func parseTicketRequest(r *http.Request) (ticket, *handlerError) {
	// the ticket payload is in the request body
	data, e := ioutil.ReadAll(r.Body)
	if e != nil {
		return ticket{}, &handlerError{e, "Could not read request", http.StatusBadRequest}
	}

	// turn the request body (JSON) into a ticket object
	var payload ticket
	e = json.Unmarshal(data, &payload)
	if e != nil {
		return ticket{}, &handlerError{e, "Could not parse JSON", http.StatusBadRequest}
	}

	return payload, nil
}

func addTicket(w http.ResponseWriter, r *http.Request) (interface{}, *handlerError) {
	payload, e := parseTicketRequest(r)
	if e != nil {
		return nil, e
	}

	// it's our job to assign IDs, ignore what (if anything) the client sent
	payload.Id = getNextId()
	tickets = append(tickets, payload)

	// we return the ticket we just made so the client can see the ID if they want
	return payload, nil
}

func updateTicket(w http.ResponseWriter, r *http.Request) (interface{}, *handlerError) {
	payload, e := parseTicketRequest(r)
	if e != nil {
		return nil, e
	}

	_, index := getTicketById(payload.Id)
	tickets[index] = payload
	return make(map[string]string), nil
}

func removeTicket(w http.ResponseWriter, r *http.Request) (interface{}, *handlerError) {
	param := mux.Vars(r)["id"]
	id, e := strconv.Atoi(param)
	if e != nil {
		return nil, &handlerError{e, "Id should be an integer", http.StatusBadRequest}
	}
	// this is jsut to check to see if the ticket exists
	_, index := getTicketById(id)

	if index < 0 {
		return nil, &handlerError{nil, "Could not find entry " + param, http.StatusNotFound}
	}

	// remove a ticket from the list
	tickets = append(tickets[:index], tickets[index+1:]...)
	return make(map[string]string), nil
}

// searches the tickets for the ticket with `id` and returns the ticket and it's index, or -1 for 404
func getTicketById(id int) (ticket, int) {
	for i, b := range tickets {
		if b.Id == id {
			return b, i
		}
	}
	return ticket{}, -1
}

var id = 0

// increments id and returns the value
func getNextId() int {
	id += 1
	return id
}

func main() {
	// command line flags
	port := flag.Int("port", 80, "port to serve on")
	dir := flag.String("directory", "web/", "directory of web files")
	flag.Parse()

	// handle all requests by serving a file of the same name
	fs := http.Dir(*dir)
	fileHandler := http.FileServer(fs)

	// Initialize table mappings
	err := initDB()
	if err != nil {
		return
	}

	// setup routes
	router := mux.NewRouter()
	router.Handle("/", http.RedirectHandler("/static/", 302))
	router.Handle("/flights", handler(listFlights)).Methods("GET")
	router.Handle("/airports", handler(listAirports)).Methods("GET")
	router.Handle("/tickets", handler(listTickets)).Methods("GET")
	router.Handle("/tickets", handler(addTicket)).Methods("POST")
	router.Handle("/selected_ticket_id", handler(getTicketID)).Methods("GET")
	router.Handle("/tickets/{id}", handler(getTickets)).Methods("GET")
	router.Handle("/tickets/{id}", handler(updateTicket)).Methods("POST")
	router.Handle("/tickets/{id}", handler(removeTicket)).Methods("DELETE")
	router.PathPrefix("/static/").Handler(http.StripPrefix("/static", fileHandler))
	http.Handle("/", router)

	// bootstrap some data
	tickets = append(tickets, ticket{"Nathan", "Acosta", "Albuquerque, NM", "Dallas, TX", "NMA4601", "12/4/2015", getNextId(), "./images/alb_flight_img.png", "./images/dal_flight_img.png"})
	tickets = append(tickets, ticket{"Nathan", "Acosta", "Dallas, TX", "Albuquerque, NM", "NMA4603", "12/5/2015", getNextId(), "./images/dal_flight_img.png", "./images/alb_flight_img.png"})
	tickets = append(tickets, ticket{"Nathan", "Acosta", "Albuquerque, NM", "Dallas, TX", "NMA4602", "12/8/2015", getNextId(), "./images/alb_flight_img.png", "./images/dal_flight_img.png"})

	log.Printf("Running on port %d\n", *port)

	addr := fmt.Sprintf(":%d", *port)
	// this call blocks -- the progam runs here forever
	err = http.ListenAndServe(addr, nil)
	fmt.Println(err.Error())
}
<|endoftext|>


<commit_before>// Copyright 2017 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.

// PerUserKeyRoll creates a new per-user-key for the active user.
// This can be the first per-user-key for the user.
package engine

import (
	"fmt"

	"github.com/keybase/client/go/libkb"
	"github.com/keybase/client/go/protocol/keybase1"
)

// PerUserKeyRoll is an engine.
type PerUserKeyRoll struct {
	libkb.Contextified
	args      *PerUserKeyRollArgs
	DidNewKey bool
}

type PerUserKeyRollArgs struct {
	LoginContext libkb.LoginContext // optional
	Me           *libkb.User        // optional
}

// NewPerUserKeyRoll creates a PerUserKeyRoll engine.
func NewPerUserKeyRoll(g *libkb.GlobalContext, args *PerUserKeyRollArgs) *PerUserKeyRoll {
	return &PerUserKeyRoll{
		args:         args,
		Contextified: libkb.NewContextified(g),
	}
}

// Name is the unique engine name.
func (e *PerUserKeyRoll) Name() string {
	return "PerUserKeyRoll"
}

// GetPrereqs returns the engine prereqs.
func (e *PerUserKeyRoll) Prereqs() Prereqs {
	return Prereqs{
		Session: true,
	}
}

// RequiredUIs returns the required UIs.
func (e *PerUserKeyRoll) RequiredUIs() []libkb.UIKind {
	return []libkb.UIKind{}
}

// SubConsumers returns the other UI consumers for this engine.
func (e *PerUserKeyRoll) SubConsumers() []libkb.UIConsumer {
	return []libkb.UIConsumer{}
}

// Run starts the engine.
func (e *PerUserKeyRoll) Run(ctx *Context) (err error) {
	defer e.G().CTrace(ctx.GetNetContext(), "PerUserKeyRoll", func() error { return err })()
	return e.inner(ctx)
}

func (e *PerUserKeyRoll) inner(ctx *Context) error {
	var err error

	uid := e.G().GetMyUID()
	if uid.IsNil() {
		return libkb.NoUIDError{}
	}

	me := e.args.Me
	if me == nil {
		e.G().Log.CDebugf(ctx.GetNetContext(), "PerUserKeyRoll load self")

		loadArg := libkb.NewLoadUserArgBase(e.G()).
			WithNetContext(ctx.GetNetContext()).
			WithUID(uid).
			WithSelf(true).
			WithPublicKeyOptional()
		loadArg.LoginContext = e.args.LoginContext
		me, err = libkb.LoadUser(*loadArg)
		if err != nil {
			return err
		}
	}
	meUPAK := me.ExportToUserPlusAllKeys()

	sigKey, err := e.G().ActiveDevice.SigningKey()
	if err != nil {
		return fmt.Errorf("signing key not found: (%v)", err)
	}
	encKey, err := e.G().ActiveDevice.EncryptionKey()
	if err != nil {
		return fmt.Errorf("encryption key not found: (%v)", err)
	}

	pukring, err := e.G().GetPerUserKeyring()
	if err != nil {
		return err
	}
	err = pukring.Sync(ctx.GetNetContext())
	if err != nil {
		return err
	}

	// Generation of the new key
	gen := pukring.CurrentGeneration() + keybase1.PerUserKeyGeneration(1)

	pukSeed, err := libkb.GeneratePerUserKeySeed()
	if err != nil {
		return err
	}

	pukReceivers, err := e.getPukReceivers(ctx, &meUPAK)
	if err != nil {
		return err
	}
	if len(pukReceivers) == 0 {
		return fmt.Errorf("no receivers")
	}

	// Create boxes of the new per-user-key
	pukBoxes, err := pukring.PrepareBoxesForDevices(ctx.GetNetContext(),
		pukSeed, gen, pukReceivers, encKey)
	if err != nil {
		return err
	}

	e.G().Log.CDebugf(ctx.GetNetContext(), "PerUserKeyRoll make sigs")
	sig, err := libkb.PerUserKeyProofReverseSigned(me, pukSeed, gen, sigKey)
	if err != nil {
		return err
	}
	// Seqno when the per-user-key will be signed in.
	pukSeqno := me.GetSigChainLastKnownSeqno()

	var sigsList []libkb.JSONPayload
	sigsList = append(sigsList, sig)

	payload := make(libkb.JSONPayload)
	payload["sigs"] = sigsList

	libkb.AddPerUserKeyServerArg(payload, gen, pukBoxes, nil)

	e.G().Log.CDebugf(ctx.GetNetContext(), "PerUserKeyRoll post")
	_, err = e.G().API.PostJSON(libkb.APIArg{
		Endpoint:    "key/multi",
		SessionType: libkb.APISessionTypeREQUIRED,
		JSONPayload: payload,
	})
	if err != nil {
		return err
	}
	e.DidNewKey = true

	// Add the per-user-key locally
	err = pukring.AddKey(ctx.GetNetContext(), gen, pukSeqno, pukSeed)
	if err != nil {
		return err
	}

	e.G().UserChanged(uid)
	return nil
}

// Get the receivers of the new per-user-key boxes.
// Includes all the user's device subkeys.
func (e *PerUserKeyRoll) getPukReceivers(ctx *Context, meUPAK *keybase1.UserPlusAllKeys) (res []libkb.NaclDHKeyPair, err error) {
	for _, dk := range meUPAK.Base.DeviceKeys {
		if dk.IsSibkey == false && !dk.IsRevoked {
			receiver, err := libkb.ImportNaclDHKeyPairFromHex(dk.KID.String())
			if err != nil {
				return res, err
			}
			res = append(res, receiver)
		}
	}
	return res, nil
}
<commit_msg>fix roll prev<commit_after>// Copyright 2017 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.

// PerUserKeyRoll creates a new per-user-key for the active user.
// This can be the first per-user-key for the user.
package engine

import (
	"fmt"

	"github.com/keybase/client/go/libkb"
	"github.com/keybase/client/go/protocol/keybase1"
)

// PerUserKeyRoll is an engine.
type PerUserKeyRoll struct {
	libkb.Contextified
	args      *PerUserKeyRollArgs
	DidNewKey bool
}

type PerUserKeyRollArgs struct {
	LoginContext libkb.LoginContext // optional
	Me           *libkb.User        // optional
}

// NewPerUserKeyRoll creates a PerUserKeyRoll engine.
func NewPerUserKeyRoll(g *libkb.GlobalContext, args *PerUserKeyRollArgs) *PerUserKeyRoll {
	return &PerUserKeyRoll{
		args:         args,
		Contextified: libkb.NewContextified(g),
	}
}

// Name is the unique engine name.
func (e *PerUserKeyRoll) Name() string {
	return "PerUserKeyRoll"
}

// GetPrereqs returns the engine prereqs.
func (e *PerUserKeyRoll) Prereqs() Prereqs {
	return Prereqs{
		Session: true,
	}
}

// RequiredUIs returns the required UIs.
func (e *PerUserKeyRoll) RequiredUIs() []libkb.UIKind {
	return []libkb.UIKind{}
}

// SubConsumers returns the other UI consumers for this engine.
func (e *PerUserKeyRoll) SubConsumers() []libkb.UIConsumer {
	return []libkb.UIConsumer{}
}

// Run starts the engine.
func (e *PerUserKeyRoll) Run(ctx *Context) (err error) {
	defer e.G().CTrace(ctx.GetNetContext(), "PerUserKeyRoll", func() error { return err })()
	return e.inner(ctx)
}

func (e *PerUserKeyRoll) inner(ctx *Context) error {
	var err error

	uid := e.G().GetMyUID()
	if uid.IsNil() {
		return libkb.NoUIDError{}
	}

	me := e.args.Me
	if me == nil {
		e.G().Log.CDebugf(ctx.GetNetContext(), "PerUserKeyRoll load self")

		loadArg := libkb.NewLoadUserArgBase(e.G()).
			WithNetContext(ctx.GetNetContext()).
			WithUID(uid).
			WithSelf(true).
			WithPublicKeyOptional()
		loadArg.LoginContext = e.args.LoginContext
		me, err = libkb.LoadUser(*loadArg)
		if err != nil {
			return err
		}
	}
	meUPAK := me.ExportToUserPlusAllKeys()

	sigKey, err := e.G().ActiveDevice.SigningKey()
	if err != nil {
		return fmt.Errorf("signing key not found: (%v)", err)
	}
	encKey, err := e.G().ActiveDevice.EncryptionKey()
	if err != nil {
		return fmt.Errorf("encryption key not found: (%v)", err)
	}

	pukring, err := e.G().GetPerUserKeyring()
	if err != nil {
		return err
	}
	err = pukring.Sync(ctx.GetNetContext())
	if err != nil {
		return err
	}

	// Generation of the new key
	gen := pukring.CurrentGeneration() + keybase1.PerUserKeyGeneration(1)
	e.G().Log.CDebugf(ctx.GetNetContext(), "PerUserKeyRoll creating gen: %v", gen)

	pukSeed, err := libkb.GeneratePerUserKeySeed()
	if err != nil {
		return err
	}

	var pukPrev *libkb.PerUserKeyPrev
	if gen > 1 {
		pukPrevInner, err := pukring.PreparePrev(ctx.GetNetContext(), pukSeed, gen)
		if err != nil {
			return err
		}
		pukPrev = &pukPrevInner
	}

	pukReceivers, err := e.getPukReceivers(ctx, &meUPAK)
	if err != nil {
		return err
	}
	if len(pukReceivers) == 0 {
		return fmt.Errorf("no receivers")
	}

	// Create boxes of the new per-user-key
	pukBoxes, err := pukring.PrepareBoxesForDevices(ctx.GetNetContext(),
		pukSeed, gen, pukReceivers, encKey)
	if err != nil {
		return err
	}

	e.G().Log.CDebugf(ctx.GetNetContext(), "PerUserKeyRoll make sigs")
	sig, err := libkb.PerUserKeyProofReverseSigned(me, pukSeed, gen, sigKey)
	if err != nil {
		return err
	}
	// Seqno when the per-user-key will be signed in.
	pukSeqno := me.GetSigChainLastKnownSeqno()

	var sigsList []libkb.JSONPayload
	sigsList = append(sigsList, sig)

	payload := make(libkb.JSONPayload)
	payload["sigs"] = sigsList

	e.G().Log.CDebugf(ctx.NetContext, "PerUserKeyRoll pukBoxes:%v pukPrev:%v for generation %v",
		len(pukBoxes), pukPrev != nil, gen)
	libkb.AddPerUserKeyServerArg(payload, gen, pukBoxes, pukPrev)

	e.G().Log.CDebugf(ctx.GetNetContext(), "PerUserKeyRoll post")
	_, err = e.G().API.PostJSON(libkb.APIArg{
		Endpoint:    "key/multi",
		SessionType: libkb.APISessionTypeREQUIRED,
		JSONPayload: payload,
	})
	if err != nil {
		return err
	}
	e.DidNewKey = true

	// Add the per-user-key locally
	err = pukring.AddKey(ctx.GetNetContext(), gen, pukSeqno, pukSeed)
	if err != nil {
		return err
	}

	e.G().UserChanged(uid)
	return nil
}

// Get the receivers of the new per-user-key boxes.
// Includes all the user's device subkeys.
func (e *PerUserKeyRoll) getPukReceivers(ctx *Context, meUPAK *keybase1.UserPlusAllKeys) (res []libkb.NaclDHKeyPair, err error) {
	for _, dk := range meUPAK.Base.DeviceKeys {
		if dk.IsSibkey == false && !dk.IsRevoked {
			receiver, err := libkb.ImportNaclDHKeyPairFromHex(dk.KID.String())
			if err != nil {
				return res, err
			}
			res = append(res, receiver)
		}
	}
	return res, nil
}
<|endoftext|>


<commit_before>package via

import (
	"compress/gzip"
	"errors"
	"fmt"
	"github.com/str1ngs/gurl"
	"github.com/str1ngs/util/console"
	"github.com/str1ngs/util/file"
	"github.com/str1ngs/util/json"
	"log"
	"net/http"
	"net/url"
	"os"
	"os/exec"
	"path"
	"path/filepath"
	"regexp"
	"sort"
)

var (
	client  = new(http.Client)
	verbose = false
	elog    = log.New(os.Stderr, "", log.Lshortfile)
	lfmt    = "%-20.20s %v\n"
	debug   = false
	expand  = os.ExpandEnv
	update  = false
	deps    = false
)

func Root(s string) {
	config.Root = s
}

func Verbose(b bool) {
	verbose = b
}

func Deps(b bool) {
	deps = b
}

func Update(b bool) {
	update = b
}

func Debug(b bool) {
	debug = b
}

func DownloadSrc(plan *Plan) (err error) {
	if file.Exists(plan.SourcePath()) && !update {
		return nil
	}
	fmt.Printf(lfmt, "download", plan.NameVersion())
	u, err := url.Parse(plan.Url)
	if err != nil {
		return err
	}
	switch u.Scheme {
	case "ftp":
		wget(cache.Sources(), plan.Url)
	case "http", "https":
		return gurl.Download(cache.Sources(), plan.Url)
	default:
		panic("unsupported")
	}
	return nil
}

// Stages the downloaded source via's cache directory
// the stage only happens once unless BuilInStage is used
func Stage(plan *Plan) (err error) {
	if plan.Url == "" || file.Exists(plan.GetStageDir()) {
		// nothing to stage
		return nil
	}
	u, err := url.Parse(plan.Url)
	if err != nil {
		elog.Println(err)
		return err
	}
	if u.Scheme == "git" {
		fmt.Println(cache.Stages())
		fmt.Println(plan.SourcePath())
		cmd := exec.Command("git", "clone", plan.SourcePath(), plan.SourceFile())
		cmd.Dir = cache.Stages()
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
		cmd.Run()
		goto ret
	}
	switch path.Ext(plan.SourceFile()) {
	case ".zip":
		unzip(cache.Stages(), plan.SourcePath())
	default:
		GNUUntar(cache.Stages(), plan.SourcePath())
	}
ret:
	fmt.Printf(lfmt, "patch", plan.NameVersion())
	if err := doCommands(join(cache.Stages(), plan.stageDir()), plan.Patch); err != nil {
		return err
	}
	return
}

// Calls each shell command in the plans Build field.
func Build(plan *Plan) (err error) {
	var (
		build = plan.Build
	)
	if file.Exists(plan.PackagePath()) {
		fmt.Printf("FIXME: (short flags)  package %s exists building anyways.\n", plan.PackagePath())
	}
	flags := config.Flags
	if plan.Flags != nil {
		flags = append(flags, plan.Flags...)
	}
	if !file.Exists(plan.BuildDir()) {
		os.MkdirAll(plan.BuildDir(), 0755)
	}
	// Parent plan's Build is run first this plans is added at the end.
	if plan.Inherit != "" {
		parent, _ := NewPlan(plan.Inherit)
		build = append(parent.Build, plan.Build...)
		flags = append(flags, parent.Flags...)
	}
	os.Setenv("SRCDIR", plan.GetStageDir())
	os.Setenv("Flags", expand(flags.String()))
	err = doCommands(plan.BuildDir(), build)
	if err != nil {
		es := fmt.Sprintf("%s in %s", err.Error(), plan.BuildDir())
		return errors.New(es)
	}
	return nil
}

func doCommands(dir string, cmds []string) (err error) {
	for i, j := range cmds {
		j := expand(j)
		if debug {
			elog.Println(i, j)
		}
		cmd := exec.Command("sh", "-c", j)
		cmd.Dir = dir
		cmd.Stdin = os.Stdin
		if verbose {
			cmd.Stdout = os.Stdout
		}
		cmd.Stderr = os.Stderr
		err = cmd.Run()
		if err != nil {
			elog.Printf("%s: %s\n", j, err)
			return err
		}
	}
	return nil
}

func Package(bdir string, plan *Plan) (err error) {
	var (
		pack = plan.Package
	)
	pdir := join(cache.Packages(), plan.NameVersion())
	if bdir == "" {
		bdir = join(cache.Builds(), plan.NameVersion())
	}
	if plan.BuildInStage {
		bdir = join(cache.Stages(), plan.stageDir())
	}
	if file.Exists(pdir) {
		err := os.RemoveAll(pdir)
		if err != nil {
			return err
		}
	}
	err = os.Mkdir(pdir, 0755)
	if err != nil {
		elog.Println(err)
		return err
	}
	os.Setenv("PKGDIR", pdir)
	if plan.Inherit != "" {
		parent, _ := NewPlan(plan.Inherit)
		pack = parent.Package
		pack = append(pack, plan.Package...)
	}
	err = doCommands(bdir, pack)
	if err != nil {
		return err
	}
	for _, j := range plan.SubPackages {
		sub, err := NewPlan(j)
		if err != nil {
			return err
		}
		if err = Package(bdir, sub); err != nil {
			return err
		}
	}
	return CreatePackage(plan)
	/*
		err = CreatePackage(plan)
		if err != nil {
			return err
		}
		return Sign(plan)
	*/
}

func CreatePackage(plan *Plan) (err error) {
	pfile := plan.PackagePath()
	os.MkdirAll(path.Dir(pfile), 0755)
	fd, err := os.Create(pfile)
	if err != nil {
		elog.Println(err)
		return err
	}
	defer fd.Close()
	gz := gzip.NewWriter(fd)
	defer gz.Close()
	return Tarball(gz, plan)
}

func Install(name string) (err error) {
	plan, err := NewPlan(name)
	if err != nil {
		elog.Println(name, err)
		return
	}
	fmt.Printf(lfmt, "installing", plan.Name)
	if IsInstalled(name) {
		fmt.Printf("FIXME: (short flags) package %s installed upgrading anyways.\n", plan.NameVersion())
		err := Remove(name)
		if err != nil {
			return err
		}
	}
	for _, d := range plan.Depends {
		if IsInstalled(d) {
			continue
		}
		err := Install(d)
		if err != nil {
			return err
		}
	}
	db := path.Join(config.DB.Installed(), plan.Name)
	if file.Exists(db) {
		return fmt.Errorf("%s is already installed", name)
	}
	pfile := plan.PackagePath()
	if !file.Exists(pfile) {
		//return errors.New(fmt.Sprintf("%s does not exist", pfile))
		err := gurl.Download(config.Repo+"/master", config.Binary+"/"+plan.PackageFile())
		if err != nil {
			log.Fatal(err)
		}
		//fatal(gurl.Download(config.Repo, config.Binary+"/"+plan.PackageFile()+".sig"))
	}
	/*
		err = CheckSig(pfile)
		if err != nil {
			return
		}
	*/
	man, err := ReadPackManifest(pfile)
	if err != nil {
		return err
	}
	errs := conflicts(man)
	if len(errs) > 0 {
		for _, e := range errs {
			elog.Println(e)
		}
		//return errs[0]
	}
	fd, err := os.Open(pfile)
	if err != nil {
		return
	}
	defer fd.Close()
	gz, err := gzip.NewReader(fd)
	if err != nil {
		return
	}
	defer gz.Close()
	err = Untar(config.Root, gz)
	if err != nil {
		return err
	}
	err = os.MkdirAll(db, 0755)
	if err != nil {
		elog.Println(err)
		return err
	}
	err = json.Write(man, join(db, "manifest.json"))
	if err != nil {
		return err
	}
	return PostInstall(plan)
}

func PostInstall(plan *Plan) (err error) {
	return doCommands("/", append(plan.PostInstall, config.PostInstall...))
}

func Remove(name string) (err error) {
	if !IsInstalled(name) {
		err = fmt.Errorf("%s is not installed.", name)
		elog.Println(err)
		return err
	}

	man, err := ReadManifest(name)
	if err != nil {
		return err
	}
	for _, f := range man.Files {
		fpath := join(config.Root, f)
		err = os.Remove(fpath)
		if err != nil {
			elog.Println(f, err)
		}
	}

	return os.RemoveAll(join(config.DB.Installed(), name))
}

func BuildDeps(plan *Plan) (err error) {
	deps := append(plan.Depends, plan.ManDepends...)
	for _, d := range deps {
		if IsInstalled(d) {
			continue
		}
		fmt.Println("building", d, "for", plan.NameVersion())
		p, _ := NewPlan(d)
		if file.Exists(p.PackagePath()) {
			return Install(p.Name)
		}
		err := BuildDeps(p)
		if err != nil {
			elog.Println(err)
			return err
		}
	}
	err = BuildSteps(plan)
	if err != nil {
		return err
	}
	return Install(plan.Name)
}

// Run all of the functions required to build a package
func BuildSteps(plan *Plan) (err error) {
	if file.Exists(plan.PackageFile()) {
		return fmt.Errorf("package %s exists", plan.PackageFile())
	}
	if err := DownloadSrc(plan); err != nil {
		elog.Println(err)
		return err
	}
	fmt.Printf(lfmt, "stage", plan.NameVersion())
	if err := Stage(plan); err != nil {
		elog.Println(err)
		return err
	}
	fmt.Printf(lfmt, "build", plan.NameVersion())
	if err := Build(plan); err != nil {
		elog.Println(err)
		return err
	}
	fmt.Printf(lfmt, "package", plan.NameVersion())
	if err := Package("", plan); err != nil {
		elog.Println(err)
		return err
	}
	return nil
}

var (
	rexName   = regexp.MustCompile("[A-Za-z]+")
	rexTruple = regexp.MustCompile("[0-9]+.[0-9]+.[0-9]+")
	rexDouble = regexp.MustCompile("[0-9]+.[0-9]+")
)

// Creates a new plan from a given Url
func Create(url, group string) (err error) {
	var (
		xfile   = path.Base(url)
		name    = rexName.FindString(xfile)
		truple  = rexTruple.FindString(xfile)
		double  = rexDouble.FindString(xfile)
		version string
	)
	switch {
	case truple != "":
		version = truple
	case double != "":
		version = double
	default:
		return errors.New("regex fail for " + xfile)
	}
	plan := &Plan{Name: name, Version: version, Url: url, Group: group}
	plan.Inherit = "gnu"
	if file.Exists(plan.Path()) {
		return errors.New(fmt.Sprintf("%s already exists", plan.Path()))
	}
	return plan.Save()
}

func IsInstalled(name string) bool {
	return file.Exists(join(config.DB.Installed(), name))
}

func Lint() (err error) {
	e, err := PlanFiles()
	if err != nil {
		return err
	}
	for _, j := range e {
		plan, err := ReadPath(j)
		if err != nil {
			err = fmt.Errorf("%s %s", j, err)
			elog.Println(err)
			return err
		}
		// If Group is empty, we can set it
		if plan.Group == "" {
			plan.Group = baseDir(j)
		}
		if verbose {
			console.Println("lint", plan.NameVersion(), plan.Package)
		}
		sort.Strings(plan.SubPackages)
		sort.Strings(plan.Flags)
		sort.Strings(plan.Remove)
		sort.Strings(plan.Depends)
		err = plan.Save()
		if err != nil {
			elog.Println(err)
			return err
		}
	}
	console.Flush()
	return nil
}

func fatal(err error) {
	if err != nil {
		log.Fatal(err)
	}
}
func Clean(name string) error {
	plan, err := NewPlan(name)
	if err != nil {
		return err
	}
	fmt.Printf(lfmt, "clean", plan.NameVersion())
	dir := join(cache.Builds(), plan.NameVersion())
	if err = os.RemoveAll(dir); err != nil {
		return err
	}

	dir = join(cache.Stages(), plan.stageDir())
	return os.RemoveAll(dir)
}

func PlanFiles() ([]string, error) {
	return filepath.Glob(join(config.Plans, "*", "*.json"))
}

func conflicts(man *Plan) (errs []error) {
	for _, f := range man.Files {
		fpath := join(config.Root, f)
		if file.Exists(fpath) {
			errs = append(errs, fmt.Errorf("%s already exists.", f))
		}
	}
	return errs
}

func GetConfig() *Config {
	return config
}
<commit_msg>use bash when running commands<commit_after>package via

import (
	"compress/gzip"
	"errors"
	"fmt"
	"github.com/str1ngs/gurl"
	"github.com/str1ngs/util/console"
	"github.com/str1ngs/util/file"
	"github.com/str1ngs/util/json"
	"log"
	"net/http"
	"net/url"
	"os"
	"os/exec"
	"path"
	"path/filepath"
	"regexp"
	"sort"
)

var (
	client  = new(http.Client)
	verbose = false
	elog    = log.New(os.Stderr, "", log.Lshortfile)
	lfmt    = "%-20.20s %v\n"
	debug   = false
	expand  = os.ExpandEnv
	update  = false
	deps    = false
)

func Root(s string) {
	config.Root = s
}

func Verbose(b bool) {
	verbose = b
}

func Deps(b bool) {
	deps = b
}

func Update(b bool) {
	update = b
}

func Debug(b bool) {
	debug = b
}

func DownloadSrc(plan *Plan) (err error) {
	if file.Exists(plan.SourcePath()) && !update {
		return nil
	}
	fmt.Printf(lfmt, "download", plan.NameVersion())
	u, err := url.Parse(plan.Url)
	if err != nil {
		return err
	}
	switch u.Scheme {
	case "ftp":
		wget(cache.Sources(), plan.Url)
	case "http", "https":
		return gurl.Download(cache.Sources(), plan.Url)
	default:
		panic("unsupported")
	}
	return nil
}

// Stages the downloaded source via's cache directory
// the stage only happens once unless BuilInStage is used
func Stage(plan *Plan) (err error) {
	if plan.Url == "" || file.Exists(plan.GetStageDir()) {
		// nothing to stage
		return nil
	}
	u, err := url.Parse(plan.Url)
	if err != nil {
		elog.Println(err)
		return err
	}
	if u.Scheme == "git" {
		fmt.Println(cache.Stages())
		fmt.Println(plan.SourcePath())
		cmd := exec.Command("git", "clone", plan.SourcePath(), plan.SourceFile())
		cmd.Dir = cache.Stages()
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
		cmd.Run()
		goto ret
	}
	switch path.Ext(plan.SourceFile()) {
	case ".zip":
		unzip(cache.Stages(), plan.SourcePath())
	default:
		GNUUntar(cache.Stages(), plan.SourcePath())
	}
ret:
	fmt.Printf(lfmt, "patch", plan.NameVersion())
	if err := doCommands(join(cache.Stages(), plan.stageDir()), plan.Patch); err != nil {
		return err
	}
	return
}

// Calls each shell command in the plans Build field.
func Build(plan *Plan) (err error) {
	var (
		build = plan.Build
	)
	if file.Exists(plan.PackagePath()) {
		fmt.Printf("FIXME: (short flags)  package %s exists building anyways.\n", plan.PackagePath())
	}
	flags := config.Flags
	if plan.Flags != nil {
		flags = append(flags, plan.Flags...)
	}
	if !file.Exists(plan.BuildDir()) {
		os.MkdirAll(plan.BuildDir(), 0755)
	}
	// Parent plan's Build is run first this plans is added at the end.
	if plan.Inherit != "" {
		parent, _ := NewPlan(plan.Inherit)
		build = append(parent.Build, plan.Build...)
		flags = append(flags, parent.Flags...)
	}
	os.Setenv("SRCDIR", plan.GetStageDir())
	os.Setenv("Flags", expand(flags.String()))
	err = doCommands(plan.BuildDir(), build)
	if err != nil {
		es := fmt.Sprintf("%s in %s", err.Error(), plan.BuildDir())
		return errors.New(es)
	}
	return nil
}

func doCommands(dir string, cmds []string) (err error) {
	for i, j := range cmds {
		j := expand(j)
		if debug {
			elog.Println(i, j)
		}
		cmd := exec.Command("bash", "-c", j)
		cmd.Dir = dir
		cmd.Stdin = os.Stdin
		if verbose {
			cmd.Stdout = os.Stdout
		}
		cmd.Stderr = os.Stderr
		err = cmd.Run()
		if err != nil {
			elog.Printf("%s: %s\n", j, err)
			return err
		}
	}
	return nil
}

func Package(bdir string, plan *Plan) (err error) {
	var (
		pack = plan.Package
	)
	pdir := join(cache.Packages(), plan.NameVersion())
	if bdir == "" {
		bdir = join(cache.Builds(), plan.NameVersion())
	}
	if plan.BuildInStage {
		bdir = join(cache.Stages(), plan.stageDir())
	}
	if file.Exists(pdir) {
		err := os.RemoveAll(pdir)
		if err != nil {
			return err
		}
	}
	err = os.Mkdir(pdir, 0755)
	if err != nil {
		elog.Println(err)
		return err
	}
	os.Setenv("PKGDIR", pdir)
	if plan.Inherit != "" {
		parent, _ := NewPlan(plan.Inherit)
		pack = parent.Package
		pack = append(pack, plan.Package...)
	}
	err = doCommands(bdir, pack)
	if err != nil {
		return err
	}
	for _, j := range plan.SubPackages {
		sub, err := NewPlan(j)
		if err != nil {
			return err
		}
		if err = Package(bdir, sub); err != nil {
			return err
		}
	}
	return CreatePackage(plan)
	/*
		err = CreatePackage(plan)
		if err != nil {
			return err
		}
		return Sign(plan)
	*/
}

func CreatePackage(plan *Plan) (err error) {
	pfile := plan.PackagePath()
	os.MkdirAll(path.Dir(pfile), 0755)
	fd, err := os.Create(pfile)
	if err != nil {
		elog.Println(err)
		return err
	}
	defer fd.Close()
	gz := gzip.NewWriter(fd)
	defer gz.Close()
	return Tarball(gz, plan)
}

func Install(name string) (err error) {
	plan, err := NewPlan(name)
	if err != nil {
		elog.Println(name, err)
		return
	}
	fmt.Printf(lfmt, "installing", plan.Name)
	if IsInstalled(name) {
		fmt.Printf("FIXME: (short flags) package %s installed upgrading anyways.\n", plan.NameVersion())
		err := Remove(name)
		if err != nil {
			return err
		}
	}
	for _, d := range plan.Depends {
		if IsInstalled(d) {
			continue
		}
		err := Install(d)
		if err != nil {
			return err
		}
	}
	db := path.Join(config.DB.Installed(), plan.Name)
	if file.Exists(db) {
		return fmt.Errorf("%s is already installed", name)
	}
	pfile := plan.PackagePath()
	if !file.Exists(pfile) {
		//return errors.New(fmt.Sprintf("%s does not exist", pfile))
		err := gurl.Download(config.Repo+"/master", config.Binary+"/"+plan.PackageFile())
		if err != nil {
			log.Fatal(err)
		}
		//fatal(gurl.Download(config.Repo, config.Binary+"/"+plan.PackageFile()+".sig"))
	}
	/*
		err = CheckSig(pfile)
		if err != nil {
			return
		}
	*/
	man, err := ReadPackManifest(pfile)
	if err != nil {
		return err
	}
	errs := conflicts(man)
	if len(errs) > 0 {
		for _, e := range errs {
			elog.Println(e)
		}
		//return errs[0]
	}
	fd, err := os.Open(pfile)
	if err != nil {
		return
	}
	defer fd.Close()
	gz, err := gzip.NewReader(fd)
	if err != nil {
		return
	}
	defer gz.Close()
	err = Untar(config.Root, gz)
	if err != nil {
		return err
	}
	err = os.MkdirAll(db, 0755)
	if err != nil {
		elog.Println(err)
		return err
	}
	err = json.Write(man, join(db, "manifest.json"))
	if err != nil {
		return err
	}
	return PostInstall(plan)
}

func PostInstall(plan *Plan) (err error) {
	return doCommands("/", append(plan.PostInstall, config.PostInstall...))
}

func Remove(name string) (err error) {
	if !IsInstalled(name) {
		err = fmt.Errorf("%s is not installed.", name)
		elog.Println(err)
		return err
	}

	man, err := ReadManifest(name)
	if err != nil {
		return err
	}
	for _, f := range man.Files {
		fpath := join(config.Root, f)
		err = os.Remove(fpath)
		if err != nil {
			elog.Println(f, err)
		}
	}

	return os.RemoveAll(join(config.DB.Installed(), name))
}

func BuildDeps(plan *Plan) (err error) {
	deps := append(plan.Depends, plan.ManDepends...)
	for _, d := range deps {
		if IsInstalled(d) {
			continue
		}
		fmt.Println("building", d, "for", plan.NameVersion())
		p, _ := NewPlan(d)
		if file.Exists(p.PackagePath()) {
			return Install(p.Name)
		}
		err := BuildDeps(p)
		if err != nil {
			elog.Println(err)
			return err
		}
	}
	err = BuildSteps(plan)
	if err != nil {
		return err
	}
	return Install(plan.Name)
}

// Run all of the functions required to build a package
func BuildSteps(plan *Plan) (err error) {
	if file.Exists(plan.PackageFile()) {
		return fmt.Errorf("package %s exists", plan.PackageFile())
	}
	if err := DownloadSrc(plan); err != nil {
		elog.Println(err)
		return err
	}
	fmt.Printf(lfmt, "stage", plan.NameVersion())
	if err := Stage(plan); err != nil {
		elog.Println(err)
		return err
	}
	fmt.Printf(lfmt, "build", plan.NameVersion())
	if err := Build(plan); err != nil {
		elog.Println(err)
		return err
	}
	fmt.Printf(lfmt, "package", plan.NameVersion())
	if err := Package("", plan); err != nil {
		elog.Println(err)
		return err
	}
	return nil
}

var (
	rexName   = regexp.MustCompile("[A-Za-z]+")
	rexTruple = regexp.MustCompile("[0-9]+.[0-9]+.[0-9]+")
	rexDouble = regexp.MustCompile("[0-9]+.[0-9]+")
)

// Creates a new plan from a given Url
func Create(url, group string) (err error) {
	var (
		xfile   = path.Base(url)
		name    = rexName.FindString(xfile)
		truple  = rexTruple.FindString(xfile)
		double  = rexDouble.FindString(xfile)
		version string
	)
	switch {
	case truple != "":
		version = truple
	case double != "":
		version = double
	default:
		return errors.New("regex fail for " + xfile)
	}
	plan := &Plan{Name: name, Version: version, Url: url, Group: group}
	plan.Inherit = "gnu"
	if file.Exists(plan.Path()) {
		return errors.New(fmt.Sprintf("%s already exists", plan.Path()))
	}
	return plan.Save()
}

func IsInstalled(name string) bool {
	return file.Exists(join(config.DB.Installed(), name))
}

func Lint() (err error) {
	e, err := PlanFiles()
	if err != nil {
		return err
	}
	for _, j := range e {
		plan, err := ReadPath(j)
		if err != nil {
			err = fmt.Errorf("%s %s", j, err)
			elog.Println(err)
			return err
		}
		// If Group is empty, we can set it
		if plan.Group == "" {
			plan.Group = baseDir(j)
		}
		if verbose {
			console.Println("lint", plan.NameVersion(), plan.Package)
		}
		sort.Strings(plan.SubPackages)
		sort.Strings(plan.Flags)
		sort.Strings(plan.Remove)
		sort.Strings(plan.Depends)
		err = plan.Save()
		if err != nil {
			elog.Println(err)
			return err
		}
	}
	console.Flush()
	return nil
}

func fatal(err error) {
	if err != nil {
		log.Fatal(err)
	}
}
func Clean(name string) error {
	plan, err := NewPlan(name)
	if err != nil {
		return err
	}
	fmt.Printf(lfmt, "clean", plan.NameVersion())
	dir := join(cache.Builds(), plan.NameVersion())
	if err = os.RemoveAll(dir); err != nil {
		return err
	}

	dir = join(cache.Stages(), plan.stageDir())
	return os.RemoveAll(dir)
}

func PlanFiles() ([]string, error) {
	return filepath.Glob(join(config.Plans, "*", "*.json"))
}

func conflicts(man *Plan) (errs []error) {
	for _, f := range man.Files {
		fpath := join(config.Root, f)
		if file.Exists(fpath) {
			errs = append(errs, fmt.Errorf("%s already exists.", f))
		}
	}
	return errs
}

func GetConfig() *Config {
	return config
}
<|endoftext|>


<commit_before>package netlinkAudit

const (
	MAX_AUDIT_MESSAGE_LENGTH = 8960
	AUDIT_GET                = 1000
	AUDIT_SET                = 1001 /* Set status (enable/disable/auditd) */
	AUDIT_LIST               = 1002
	AUDIT_LIST_RULES         = 1013
	AUDIT_ADD_RULE           = 1011 /* Add syscall filtering rule */
	AUDIT_FIRST_USER_MSG     = 1100 /* Userspace messages mostly uninteresting to kernel */
	AUDIT_MAX_FIELDS         = 64
	AUDIT_BITMASK_SIZE       = 64
	AUDIT_GET_FEATURE        = 1019
	//Rule Flags
	AUDIT_FILTER_USER  = 0x00 /* Apply rule to user-generated messages */
	AUDIT_FILTER_TASK  = 0x01 /* Apply rule at task creation (not syscall) */
	AUDIT_FILTER_ENTRY = 0x02 /* Apply rule at syscall entry */
	AUDIT_FILTER_WATCH = 0x03 /* Apply rule to file system watches */
	AUDIT_FILTER_EXIT  = 0x04 /* Apply rule at syscall exit */
	AUDIT_FILTER_TYPE  = 0x05 /* Apply rule at audit_log_start */
	/* These are used in filter control */
	AUDIT_FILTER_MASK  = 0x07 /* Mask to get actual filter */
	AUDIT_FILTER_UNSET = 0x80 /* This value means filter is unset */

	/* Rule actions */
	AUDIT_NEVER    = 0 /* Do not build context if rule matches */
	AUDIT_POSSIBLE = 1 /* Build context if rule matches  */
	AUDIT_ALWAYS   = 2 /* Generate audit record if rule matches */
	AUDIT_DEL_RULE = 1012

	/*Audit Message Types */
	AUDIT_SYSCALL       = 1300 /* Syscall event */
	AUDIT_PATH          = 1302 /* Filename path information */
	AUDIT_IPC           = 1303 /* IPC record */
	AUDIT_SOCKETCALL    = 1304 /* sys_socketcall arguments */
	AUDIT_CONFIG_CHANGE = 1305 /* Audit system configuration change */
	AUDIT_SOCKADDR      = 1306 /* sockaddr copied as syscall arg */
	AUDIT_CWD           = 1307 /* Current working directory */
	AUDIT_EXECVE        = 1309 /* execve arguments */
	AUDIT_EOE           = 1320 /* End of multi-record event */

	/* Rule fields */
	/* These are useful when checking the
	 * task structure at task creation time
	 * (AUDIT_PER_TASK).  */
	AUDIT_PID                   = 0
	AUDIT_UID                   = 1
	AUDIT_EUID                  = 2
	AUDIT_SUID                  = 3
	AUDIT_FSUID                 = 4
	AUDIT_GID                   = 5
	AUDIT_EGID                  = 6
	AUDIT_SGID                  = 7
	AUDIT_FSGID                 = 8
	AUDIT_LOGINUID              = 9
	AUDIT_OBJ_GID               = 110
	AUDIT_OBJ_UID               = 109
	AUDIT_EXIT                  = 103
	AUDIT_PERS                  = 10
	AUDIT_FILTER_EXCLUDE        = 0x05
	AUDIT_ARCH                  = 11
	PATH_MAX                    = 4096
	AUDIT_MSGTYPE               = 12
	AUDIT_MAX_KEY_LEN           = 256
	AUDIT_PERM                  = 106
	AUDIT_FILTERKEY             = 210
	AUDIT_SUBJ_USER             = 13 /* security label user */
	AUDIT_SUBJ_ROLE             = 14 /* security label role */
	AUDIT_SUBJ_TYPE             = 15 /* security label type */
	AUDIT_SUBJ_SEN              = 16 /* security label sensitivity label */
	AUDIT_SUBJ_CLR              = 17 /* security label clearance label */
	AUDIT_PPID                  = 18
	AUDIT_OBJ_USER              = 19
	AUDIT_OBJ_ROLE              = 20
	AUDIT_OBJ_TYPE              = 21
	AUDIT_WATCH                 = 105
	AUDIT_DIR                   = 107
	AUDIT_OBJ_LEV_LOW           = 22
	AUDIT_OBJ_LEV_HIGH          = 23
	AUDIT_LOGINUID_SET          = 24
	AUDIT_DEVMAJOR              = 100
	AUDIT_INODE                 = 102
	AUDIT_SUCCESS               = 104
	AUDIT_PERM_EXEC             = 1
	AUDIT_PERM_WRITE            = 2
	AUDIT_PERM_READ             = 4
	AUDIT_PERM_ATTR             = 8
	AUDIT_FILETYPE              = 108
	AUDIT_ARG0                  = 200
	AUDIT_ARG1                  = (AUDIT_ARG0 + 1)
	AUDIT_ARG2                  = (AUDIT_ARG0 + 2)
	AUDIT_ARG3                  = (AUDIT_ARG0 + 3)
	AUDIT_BIT_MASK              = 0x08000000
	AUDIT_LESS_THAN             = 0x10000000
	AUDIT_GREATER_THAN          = 0x20000000
	AUDIT_NOT_EQUAL             = 0x30000000
	AUDIT_EQUAL                 = 0x40000000
	AUDIT_BIT_TEST              = (AUDIT_BIT_MASK | AUDIT_EQUAL)
	AUDIT_LESS_THAN_OR_EQUAL    = (AUDIT_LESS_THAN | AUDIT_EQUAL)
	AUDIT_GREATER_THAN_OR_EQUAL = (AUDIT_GREATER_THAN | AUDIT_EQUAL)
	AUDIT_OPERATORS             = (AUDIT_EQUAL | AUDIT_NOT_EQUAL | AUDIT_BIT_MASK)
	/* Status symbols */
	/* Mask values */
	AUDIT_STATUS_ENABLED       = 0x0001
	AUDIT_STATUS_FAILURE       = 0x0002
	AUDIT_STATUS_PID           = 0x0004
	AUDIT_STATUS_RATE_LIMIT    = 0x0008
	AUDIT_STATUS_BACKLOG_LIMIT = 0x0010
	/* Failure-to-log actions */
	AUDIT_FAIL_SILENT = 0
	AUDIT_FAIL_PRINTK = 1
	AUDIT_FAIL_PANIC  = 2

	/* distinguish syscall tables */
	__AUDIT_ARCH_64BIT  = 0x80000000
	__AUDIT_ARCH_LE     = 0x40000000
	AUDIT_ARCH_ALPHA    = (EM_ALPHA | __AUDIT_ARCH_64BIT | __AUDIT_ARCH_LE)
	AUDIT_ARCH_ARM      = (EM_ARM | __AUDIT_ARCH_LE)
	AUDIT_ARCH_ARMEB    = (EM_ARM)
	AUDIT_ARCH_CRIS     = (EM_CRIS | __AUDIT_ARCH_LE)
	AUDIT_ARCH_FRV      = (EM_FRV)
	AUDIT_ARCH_I386     = (EM_386 | __AUDIT_ARCH_LE)
	AUDIT_ARCH_IA64     = (EM_IA_64 | __AUDIT_ARCH_64BIT | __AUDIT_ARCH_LE)
	AUDIT_ARCH_M32R     = (EM_M32R)
	AUDIT_ARCH_M68K     = (EM_68K)
	AUDIT_ARCH_MIPS     = (EM_MIPS)
	AUDIT_ARCH_MIPSEL   = (EM_MIPS | __AUDIT_ARCH_LE)
	AUDIT_ARCH_MIPS64   = (EM_MIPS | __AUDIT_ARCH_64BIT)
	AUDIT_ARCH_MIPSEL64 = (EM_MIPS | __AUDIT_ARCH_64BIT | __AUDIT_ARCH_LE)
	//	AUDIT_ARCH_OPENRISC = (EM_OPENRISC)
	//	AUDIT_ARCH_PARISC   = (EM_PARISC)
	//	AUDIT_ARCH_PARISC64 = (EM_PARISC | __AUDIT_ARCH_64BIT)
	AUDIT_ARCH_PPC     = (EM_PPC)
	AUDIT_ARCH_PPC64   = (EM_PPC64 | __AUDIT_ARCH_64BIT)
	AUDIT_ARCH_S390    = (EM_S390)
	AUDIT_ARCH_S390X   = (EM_S390 | __AUDIT_ARCH_64BIT)
	AUDIT_ARCH_SH      = (EM_SH)
	AUDIT_ARCH_SHEL    = (EM_SH | __AUDIT_ARCH_LE)
	AUDIT_ARCH_SH64    = (EM_SH | __AUDIT_ARCH_64BIT)
	AUDIT_ARCH_SHEL64  = (EM_SH | __AUDIT_ARCH_64BIT | __AUDIT_ARCH_LE)
	AUDIT_ARCH_SPARC   = (EM_SPARC)
	AUDIT_ARCH_SPARC64 = (EM_SPARCV9 | __AUDIT_ARCH_64BIT)
	AUDIT_ARCH_X86_64  = (EM_X86_64 | __AUDIT_ARCH_64BIT | __AUDIT_ARCH_LE)
	///Temporary Solution need to add linux/elf-em.h
	EM_NONE  = 0
	EM_M32   = 1
	EM_SPARC = 2
	EM_386   = 3
	EM_68K   = 4
	EM_88K   = 5
	EM_486   = 6 /* Perhaps disused */
	EM_860   = 7
	EM_MIPS  = 8 /* MIPS R3000 (officially, big-endian only) */
	/* Next two are historical and binaries and
	   modules of these types will be rejected by
	   Linux.  */
	EM_MIPS_RS3_LE = 10 /* MIPS R3000 little-endian */
	EM_MIPS_RS4_BE = 10 /* MIPS R4000 big-endian */

	EM_PARISC      = 15     /* HPPA */
	EM_SPARC32PLUS = 18     /* Sun's "v8plus" */
	EM_PPC         = 20     /* PowerPC */
	EM_PPC64       = 21     /* PowerPC64 */
	EM_SPU         = 23     /* Cell BE SPU */
	EM_ARM         = 40     /* ARM 32 bit */
	EM_SH          = 42     /* SuperH */
	EM_SPARCV9     = 43     /* SPARC v9 64-bit */
	EM_IA_64       = 50     /* HP/Intel IA-64 */
	EM_X86_64      = 62     /* AMD x86-64 */
	EM_S390        = 22     /* IBM S/390 */
	EM_CRIS        = 76     /* Axis Communications 32-bit embedded processor */
	EM_V850        = 87     /* NEC v850 */
	EM_M32R        = 88     /* Renesas M32R */
	EM_MN10300     = 89     /* Panasonic/MEI MN10300, AM33 */
	EM_BLACKFIN    = 106    /* ADI Blackfin Processor */
	EM_TI_C6000    = 140    /* TI C6X DSPs */
	EM_AARCH64     = 183    /* ARM 64 bit */
	EM_FRV         = 0x5441 /* Fujitsu FR-V */
	EM_AVR32       = 0x18ad /* Atmel AVR32 */

	/*
	 * This is an interim value that we will use until the committee comes
	 * up with a final number.
	 */
	EM_ALPHA = 0x9026

	/* Bogus old v850 magic number, used by old tools. */
	EM_CYGNUS_V850 = 0x9080
	/* Bogus old m32r magic number, used by old tools. */
	EM_CYGNUS_M32R = 0x9041
	/* This is the old interim value for S/390 architecture */
	EM_S390_OLD = 0xA390
	/* Also Panasonic/MEI MN10300, AM33 */
	EM_CYGNUS_MN10300 = 0xbeef
	//AUDIT_ARCH determination purpose
	_UTSNAME_LENGTH          = 65
	_UTSNAME_DOMAIN_LENGTH   = _UTSNAME_LENGTH
	_UTSNAME_NODENAME_LENGTH = _UTSNAME_DOMAIN_LENGTH
)
<commit_msg>Correct Value of MAX_AUDIT_MESSAGE_LENGTH #5<commit_after>package netlinkAudit

const (
	MAX_AUDIT_MESSAGE_LENGTH = 8970
	AUDIT_GET                = 1000
	AUDIT_SET                = 1001 /* Set status (enable/disable/auditd) */
	AUDIT_LIST               = 1002
	AUDIT_LIST_RULES         = 1013
	AUDIT_ADD_RULE           = 1011 /* Add syscall filtering rule */
	AUDIT_FIRST_USER_MSG     = 1100 /* Userspace messages mostly uninteresting to kernel */
	AUDIT_MAX_FIELDS         = 64
	AUDIT_BITMASK_SIZE       = 64
	AUDIT_GET_FEATURE        = 1019
	//Rule Flags
	AUDIT_FILTER_USER  = 0x00 /* Apply rule to user-generated messages */
	AUDIT_FILTER_TASK  = 0x01 /* Apply rule at task creation (not syscall) */
	AUDIT_FILTER_ENTRY = 0x02 /* Apply rule at syscall entry */
	AUDIT_FILTER_WATCH = 0x03 /* Apply rule to file system watches */
	AUDIT_FILTER_EXIT  = 0x04 /* Apply rule at syscall exit */
	AUDIT_FILTER_TYPE  = 0x05 /* Apply rule at audit_log_start */
	/* These are used in filter control */
	AUDIT_FILTER_MASK  = 0x07 /* Mask to get actual filter */
	AUDIT_FILTER_UNSET = 0x80 /* This value means filter is unset */

	/* Rule actions */
	AUDIT_NEVER    = 0 /* Do not build context if rule matches */
	AUDIT_POSSIBLE = 1 /* Build context if rule matches  */
	AUDIT_ALWAYS   = 2 /* Generate audit record if rule matches */
	AUDIT_DEL_RULE = 1012

	/*Audit Message Types */
	AUDIT_SYSCALL       = 1300 /* Syscall event */
	AUDIT_PATH          = 1302 /* Filename path information */
	AUDIT_IPC           = 1303 /* IPC record */
	AUDIT_SOCKETCALL    = 1304 /* sys_socketcall arguments */
	AUDIT_CONFIG_CHANGE = 1305 /* Audit system configuration change */
	AUDIT_SOCKADDR      = 1306 /* sockaddr copied as syscall arg */
	AUDIT_CWD           = 1307 /* Current working directory */
	AUDIT_EXECVE        = 1309 /* execve arguments */
	AUDIT_EOE           = 1320 /* End of multi-record event */

	/* Rule fields */
	/* These are useful when checking the
	 * task structure at task creation time
	 * (AUDIT_PER_TASK).  */
	AUDIT_PID                   = 0
	AUDIT_UID                   = 1
	AUDIT_EUID                  = 2
	AUDIT_SUID                  = 3
	AUDIT_FSUID                 = 4
	AUDIT_GID                   = 5
	AUDIT_EGID                  = 6
	AUDIT_SGID                  = 7
	AUDIT_FSGID                 = 8
	AUDIT_LOGINUID              = 9
	AUDIT_OBJ_GID               = 110
	AUDIT_OBJ_UID               = 109
	AUDIT_EXIT                  = 103
	AUDIT_PERS                  = 10
	AUDIT_FILTER_EXCLUDE        = 0x05
	AUDIT_ARCH                  = 11
	PATH_MAX                    = 4096
	AUDIT_MSGTYPE               = 12
	AUDIT_MAX_KEY_LEN           = 256
	AUDIT_PERM                  = 106
	AUDIT_FILTERKEY             = 210
	AUDIT_SUBJ_USER             = 13 /* security label user */
	AUDIT_SUBJ_ROLE             = 14 /* security label role */
	AUDIT_SUBJ_TYPE             = 15 /* security label type */
	AUDIT_SUBJ_SEN              = 16 /* security label sensitivity label */
	AUDIT_SUBJ_CLR              = 17 /* security label clearance label */
	AUDIT_PPID                  = 18
	AUDIT_OBJ_USER              = 19
	AUDIT_OBJ_ROLE              = 20
	AUDIT_OBJ_TYPE              = 21
	AUDIT_WATCH                 = 105
	AUDIT_DIR                   = 107
	AUDIT_OBJ_LEV_LOW           = 22
	AUDIT_OBJ_LEV_HIGH          = 23
	AUDIT_LOGINUID_SET          = 24
	AUDIT_DEVMAJOR              = 100
	AUDIT_INODE                 = 102
	AUDIT_SUCCESS               = 104
	AUDIT_PERM_EXEC             = 1
	AUDIT_PERM_WRITE            = 2
	AUDIT_PERM_READ             = 4
	AUDIT_PERM_ATTR             = 8
	AUDIT_FILETYPE              = 108
	AUDIT_ARG0                  = 200
	AUDIT_ARG1                  = (AUDIT_ARG0 + 1)
	AUDIT_ARG2                  = (AUDIT_ARG0 + 2)
	AUDIT_ARG3                  = (AUDIT_ARG0 + 3)
	AUDIT_BIT_MASK              = 0x08000000
	AUDIT_LESS_THAN             = 0x10000000
	AUDIT_GREATER_THAN          = 0x20000000
	AUDIT_NOT_EQUAL             = 0x30000000
	AUDIT_EQUAL                 = 0x40000000
	AUDIT_BIT_TEST              = (AUDIT_BIT_MASK | AUDIT_EQUAL)
	AUDIT_LESS_THAN_OR_EQUAL    = (AUDIT_LESS_THAN | AUDIT_EQUAL)
	AUDIT_GREATER_THAN_OR_EQUAL = (AUDIT_GREATER_THAN | AUDIT_EQUAL)
	AUDIT_OPERATORS             = (AUDIT_EQUAL | AUDIT_NOT_EQUAL | AUDIT_BIT_MASK)
	/* Status symbols */
	/* Mask values */
	AUDIT_STATUS_ENABLED       = 0x0001
	AUDIT_STATUS_FAILURE       = 0x0002
	AUDIT_STATUS_PID           = 0x0004
	AUDIT_STATUS_RATE_LIMIT    = 0x0008
	AUDIT_STATUS_BACKLOG_LIMIT = 0x0010
	/* Failure-to-log actions */
	AUDIT_FAIL_SILENT = 0
	AUDIT_FAIL_PRINTK = 1
	AUDIT_FAIL_PANIC  = 2

	/* distinguish syscall tables */
	__AUDIT_ARCH_64BIT  = 0x80000000
	__AUDIT_ARCH_LE     = 0x40000000
	AUDIT_ARCH_ALPHA    = (EM_ALPHA | __AUDIT_ARCH_64BIT | __AUDIT_ARCH_LE)
	AUDIT_ARCH_ARM      = (EM_ARM | __AUDIT_ARCH_LE)
	AUDIT_ARCH_ARMEB    = (EM_ARM)
	AUDIT_ARCH_CRIS     = (EM_CRIS | __AUDIT_ARCH_LE)
	AUDIT_ARCH_FRV      = (EM_FRV)
	AUDIT_ARCH_I386     = (EM_386 | __AUDIT_ARCH_LE)
	AUDIT_ARCH_IA64     = (EM_IA_64 | __AUDIT_ARCH_64BIT | __AUDIT_ARCH_LE)
	AUDIT_ARCH_M32R     = (EM_M32R)
	AUDIT_ARCH_M68K     = (EM_68K)
	AUDIT_ARCH_MIPS     = (EM_MIPS)
	AUDIT_ARCH_MIPSEL   = (EM_MIPS | __AUDIT_ARCH_LE)
	AUDIT_ARCH_MIPS64   = (EM_MIPS | __AUDIT_ARCH_64BIT)
	AUDIT_ARCH_MIPSEL64 = (EM_MIPS | __AUDIT_ARCH_64BIT | __AUDIT_ARCH_LE)
	//	AUDIT_ARCH_OPENRISC = (EM_OPENRISC)
	//	AUDIT_ARCH_PARISC   = (EM_PARISC)
	//	AUDIT_ARCH_PARISC64 = (EM_PARISC | __AUDIT_ARCH_64BIT)
	AUDIT_ARCH_PPC     = (EM_PPC)
	AUDIT_ARCH_PPC64   = (EM_PPC64 | __AUDIT_ARCH_64BIT)
	AUDIT_ARCH_S390    = (EM_S390)
	AUDIT_ARCH_S390X   = (EM_S390 | __AUDIT_ARCH_64BIT)
	AUDIT_ARCH_SH      = (EM_SH)
	AUDIT_ARCH_SHEL    = (EM_SH | __AUDIT_ARCH_LE)
	AUDIT_ARCH_SH64    = (EM_SH | __AUDIT_ARCH_64BIT)
	AUDIT_ARCH_SHEL64  = (EM_SH | __AUDIT_ARCH_64BIT | __AUDIT_ARCH_LE)
	AUDIT_ARCH_SPARC   = (EM_SPARC)
	AUDIT_ARCH_SPARC64 = (EM_SPARCV9 | __AUDIT_ARCH_64BIT)
	AUDIT_ARCH_X86_64  = (EM_X86_64 | __AUDIT_ARCH_64BIT | __AUDIT_ARCH_LE)
	///Temporary Solution need to add linux/elf-em.h
	EM_NONE  = 0
	EM_M32   = 1
	EM_SPARC = 2
	EM_386   = 3
	EM_68K   = 4
	EM_88K   = 5
	EM_486   = 6 /* Perhaps disused */
	EM_860   = 7
	EM_MIPS  = 8 /* MIPS R3000 (officially, big-endian only) */
	/* Next two are historical and binaries and
	   modules of these types will be rejected by
	   Linux.  */
	EM_MIPS_RS3_LE = 10 /* MIPS R3000 little-endian */
	EM_MIPS_RS4_BE = 10 /* MIPS R4000 big-endian */

	EM_PARISC      = 15     /* HPPA */
	EM_SPARC32PLUS = 18     /* Sun's "v8plus" */
	EM_PPC         = 20     /* PowerPC */
	EM_PPC64       = 21     /* PowerPC64 */
	EM_SPU         = 23     /* Cell BE SPU */
	EM_ARM         = 40     /* ARM 32 bit */
	EM_SH          = 42     /* SuperH */
	EM_SPARCV9     = 43     /* SPARC v9 64-bit */
	EM_IA_64       = 50     /* HP/Intel IA-64 */
	EM_X86_64      = 62     /* AMD x86-64 */
	EM_S390        = 22     /* IBM S/390 */
	EM_CRIS        = 76     /* Axis Communications 32-bit embedded processor */
	EM_V850        = 87     /* NEC v850 */
	EM_M32R        = 88     /* Renesas M32R */
	EM_MN10300     = 89     /* Panasonic/MEI MN10300, AM33 */
	EM_BLACKFIN    = 106    /* ADI Blackfin Processor */
	EM_TI_C6000    = 140    /* TI C6X DSPs */
	EM_AARCH64     = 183    /* ARM 64 bit */
	EM_FRV         = 0x5441 /* Fujitsu FR-V */
	EM_AVR32       = 0x18ad /* Atmel AVR32 */

	/*
	 * This is an interim value that we will use until the committee comes
	 * up with a final number.
	 */
	EM_ALPHA = 0x9026

	/* Bogus old v850 magic number, used by old tools. */
	EM_CYGNUS_V850 = 0x9080
	/* Bogus old m32r magic number, used by old tools. */
	EM_CYGNUS_M32R = 0x9041
	/* This is the old interim value for S/390 architecture */
	EM_S390_OLD = 0xA390
	/* Also Panasonic/MEI MN10300, AM33 */
	EM_CYGNUS_MN10300 = 0xbeef
	//AUDIT_ARCH determination purpose
	_UTSNAME_LENGTH          = 65
	_UTSNAME_DOMAIN_LENGTH   = _UTSNAME_LENGTH
	_UTSNAME_NODENAME_LENGTH = _UTSNAME_DOMAIN_LENGTH
)
<|endoftext|>


<commit_before>package main

import (
	"container/list"
	"crypto/md5"
	"encoding/hex"
	"errors"
	"fmt"
	"runtime"
	"strings"
	"time"
)

type Key struct {
	minl      int
	maxl      int
	keyString string
	keyIndex  *list.List
}

//k 查询字符表
func NewKey(k string) *Key {
	return NewMinKey(k, 1)
}

//k 查询字符表 minl最短位数
func NewMinKey(k string, minl int) *Key {
	return NewMinMaxKey(k, minl, 0)
}

//k 查询字符表 maxl最长位数
func NewMaxKey(k string, maxl int) *Key {
	return NewMinMaxKey(k, 1, maxl)
}

//k 查询字符表 minl最长位数 maxl最长位数
func NewMinMaxKey(k string, minl, maxl int) *Key {
	key := &Key{}
	key.minl = minl
	key.maxl = maxl
	key.keyString = k
	key.Init()
	return key
}

//初始化
func (k *Key) Init() {
	k.keyIndex = list.New()
	for i := 0; i < k.minl; i++ {
		k.keyIndex.PushBack(0)
	}
}

//生成匹配key
func (k *Key) Generate() (string, error) {
	if k.maxl > 0 && k.keyIndex.Len() > k.maxl {
		return "", errors.New("Beyond the maximum number of digits!")
	}
	s := ""
	v := k.keyIndex.Front()
	for v != nil {
		s = string(k.keyString[v.Value.(int)]) + s
		v = v.Next()
	}
	k.add(0)
	return string(s), nil
}

func (k *Key) add(index int) {
	v := k.keyIndex.Front()
	for i := 0; i < index; i++ {
		v = v.Next()
	}
	if v == nil {
		k.keyIndex.PushBack(0)
	} else {
		vi := v.Value.(int)
		if vi+1 == len(k.keyString) {
			v.Value = 0
			k.add(index + 1)
		} else {
			v.Value = vi + 1
		}
	}
}

//匹配字符串生成
func generate(keyString string, min, max, ckNum int, keyMsg chan string, msg, errOver chan bool) {
	key := NewMinMaxKey(keyString, min, max)
	for {
		<-msg
		s := ""
		for i := 0; i < ckNum; i++ {
			k, err := key.Generate()
			if err != nil {
				if i == 0 {
					errOver <- true
					return
				} else {
					break
				}
			}
			s = s + ";" + k
		}
		keyMsg <- s
	}
}

//检测
func check(mw string, keyMsg chan string, msg chan bool, over chan string) {
	for {
		msg <- true
		keys := <-keyMsg
		key := strings.Split(keys, ";")
		for _, value := range key {
			h := md5.New()
			h.Write([]byte(value)) // 需要加密的字符串为 123456
			m := hex.EncodeToString(h.Sum(nil))
			fmt.Println("check:", value)
			if m == mw {
				over <- value
				return
			}
		}
	}
}

func main() {
	t1 := time.Now()
	runtime.GOMAXPROCS(runtime.NumCPU())
	keyString := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+-=/." //检测字符表
	mw := "b28b7d691bb595df727a47cbf1240464"                                                       //需要破解的密文 明文：008784
	num := 10                                                                                //开启线程数
	min := 1                                                                                //最短位数
	max := 20                                                                                //最长位数
	ckNum := 20                                                                                //每次生成匹配数
	keyMsg := make(chan string)                                                                    //传递生成检测key
	msg := make(chan bool)                                                                         //传递需要生成检测key
	over := make(chan string)                                                                      //完成
	errOver := make(chan bool)                                                                     //未完成

	go generate(keyString, min, max, ckNum, keyMsg, msg, errOver)

	for i := 0; i < num; i++ {
		go check(mw, keyMsg, msg, over)
	}

	select {
	case v := <-over:
		fmt.Println("find:", v, "time:", time.Now().Sub(t1))
	case <-errOver:
		fmt.Println("Not find! time:", time.Now().Sub(t1))
	}
}
<commit_msg>Modified<commit_after>package main

import (
	"container/list"
	"crypto/md5"
	"encoding/hex"
	"errors"
	"fmt"
	"runtime"
	"strings"
	"time"
)

const (
	number    = "0123456789"
	lowerCase = "abcdefghijklmnopqrstuvwxyz"
	upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	symbol    = "~!@#$%^&*()_+-=/."
)

type Key struct {
	minl      int
	maxl      int
	keyString string
	keyIndex  *list.List
}

//k 查询字符表
func NewKey(k string) *Key {
	return NewMinKey(k, 1)
}

//k 查询字符表 minl最短位数
func NewMinKey(k string, minl int) *Key {
	return NewMinMaxKey(k, minl, 0)
}

//k 查询字符表 maxl最长位数
func NewMaxKey(k string, maxl int) *Key {
	return NewMinMaxKey(k, 1, maxl)
}

//k 查询字符表 minl最长位数 maxl最长位数
func NewMinMaxKey(k string, minl, maxl int) *Key {
	key := &Key{}
	key.minl = minl
	key.maxl = maxl
	key.keyString = k
	key.Init()
	return key
}

//初始化
func (k *Key) Init() {
	k.keyIndex = list.New()
	for i := 0; i < k.minl; i++ {
		k.keyIndex.PushBack(0)
	}
}

//生成匹配key
func (k *Key) Generate() (string, error) {
	if k.maxl > 0 && k.keyIndex.Len() > k.maxl {
		return "", errors.New("Beyond the maximum number of digits!")
	}
	s := ""
	v := k.keyIndex.Front()
	for v != nil {
		s = string(k.keyString[v.Value.(int)]) + s
		v = v.Next()
	}
	k.add(0)
	return string(s), nil
}

func (k *Key) add(index int) {
	v := k.keyIndex.Front()
	for i := 0; i < index; i++ {
		v = v.Next()
	}
	if v == nil {
		k.keyIndex.PushBack(0)
	} else {
		vi := v.Value.(int)
		if vi+1 == len(k.keyString) {
			v.Value = 0
			k.add(index + 1)
		} else {
			v.Value = vi + 1
		}
	}
}

//匹配字符串生成
func generate(keyString string, min, max, ckNum int, keyMsg chan string, msg, errOver chan bool) {
	key := NewMinMaxKey(keyString, min, max)
	for {
		<-msg
		s := ""
		for i := 0; i < ckNum; i++ {
			k, err := key.Generate()
			if err != nil {
				if i == 0 {
					errOver <- true
					return
				} else {
					break
				}
			}
			s = s + ";" + k
		}
		keyMsg <- string(s[1:len(s)])
	}
}

//检测
func check(mw string, keyMsg chan string, msg chan bool, over chan string) {
	for {
		msg <- true
		keys := <-keyMsg
		key := strings.Split(keys, ";")
		for _, value := range key {
			h := md5.New()
			h.Write([]byte(value)) // 需要加密的字符串为 123456
			m := hex.EncodeToString(h.Sum(nil))
			fmt.Println("check:", value)
			if m == mw {
				over <- value
				return
			}
		}
	}
}

func main() {

	t1 := time.Now()
	runtime.GOMAXPROCS(runtime.NumCPU())
	keyString := number + lowerCase          //检测字符表
	mw := "5f74656319f1cd16cd4b36a5b6ef4b02" //需要破解的密文 明文：123654000abc
	num := 10                                //开启线程数
	min := 1                                 //最短位数
	max := 20                                //最长位数
	ckNum := 50                              //每次生成匹配数
	keyMsg := make(chan string)              //传递生成检测key
	msg := make(chan bool)                   //传递需要生成检测key
	over := make(chan string)                //完成
	errOver := make(chan bool)               //未完成

	go generate(keyString, min, max, ckNum, keyMsg, msg, errOver)

	for i := 0; i < num; i++ {
		go check(mw, keyMsg, msg, over)
	}

	select {
	case v := <-over:
		fmt.Println("find:", v, "time:", time.Now().Sub(t1))
	case <-errOver:
		fmt.Println("Not find! time:", time.Now().Sub(t1))
	}
}
<|endoftext|>


<commit_before>package pkgs

import (
	"context"
	"fmt"
	"io/ioutil"
	"net/http"
	"os/user"
	"path/filepath"
	"runtime"

	"github.com/arduino/arduino-create-agent/gen/tools"
	"github.com/codeclysm/extract"
	"github.com/sirupsen/logrus"
	"github.com/xrash/smetrics"
)

type Tools struct {
	Log     *logrus.Logger
	Indexes interface {
		List(context.Context) ([]string, error)
		Get(context.Context, string) (Index, error)
	}
	Folder string
}

func (c *Tools) Available(ctx context.Context) (res tools.ToolCollection, err error) {
	list, err := c.Indexes.List(ctx)
	if err != nil {
		return nil, err
	}

	for _, url := range list {
		index, err := c.Indexes.Get(ctx, url)
		if err != nil {
			return nil, err
		}

		for _, packager := range index.Packages {
			for _, tool := range packager.Tools {
				res = append(res, &tools.Tool{
					Packager: packager.Name,
					Name:     tool.Name,
					Version:  tool.Version,
				})
			}
		}
	}

	return res, nil
}

func (c *Tools) Installed(ctx context.Context) (tools.ToolCollection, error) {
	res := tools.ToolCollection{}

	// Find packagers
	usr, _ := user.Current()
	path := filepath.Join(usr.HomeDir, ".arduino-create")
	packagers, err := ioutil.ReadDir(path)
	if err != nil {
		return nil, err
	}

	for _, packager := range packagers {
		if !packager.IsDir() {
			continue
		}
		// Find tools
		toolss, err := ioutil.ReadDir(filepath.Join(path, packager.Name()))
		if err != nil {
			return nil, err
		}
		for _, tool := range toolss {
			// Find versions
			versions, err := ioutil.ReadDir(filepath.Join(path, packager.Name(), tool.Name()))
			if err != nil {
				return nil, err
			}

			for _, version := range versions {
				res = append(res, &tools.Tool{
					Packager: packager.Name(),
					Name:     tool.Name(),
					Version:  version.Name(),
				})
			}
		}
	}

	return res, nil
}

func (c *Tools) Install(ctx context.Context, payload *tools.ToolPayload) error {
	list, err := c.Indexes.List(ctx)
	if err != nil {
		return err
	}

	for _, url := range list {
		index, err := c.Indexes.Get(ctx, url)
		if err != nil {
			return err
		}

		for _, packager := range index.Packages {
			if packager.Name != payload.Packager {
				continue
			}

			for _, tool := range packager.Tools {
				if tool.Name == payload.Name &&
					tool.Version == payload.Version {
					return c.install(ctx, tool)
				}
			}
		}
	}

	return tools.MakeNotFound(
		fmt.Errorf("tool not found with packager '%s', name '%s', version '%s'",
			payload.Packager, payload.Name, payload.Version))
}

func (c *Tools) install(ctx context.Context, tool Tool) error {
	i := findSystem(tool)

	// Download
	fmt.Println(tool.Systems[i].URL)
	res, err := http.Get(tool.Systems[i].URL)
	if err != nil {
		return err
	}
	defer res.Body.Close()

	err = extract.Archive(ctx, res.Body, c.Folder, nil)
	if err != nil {
		return err
	}

	return nil
}

func (c *Tools) Remove(ctx context.Context, payload *tools.ToolPayload) error {
	return nil
}

func findSystem(tool Tool) int {
	var systems = map[string]string{
		"linuxamd64":   "x86_64-linux-gnu",
		"linux386":     "i686-linux-gnu",
		"darwinamd64":  "apple-darwin",
		"windows386":   "i686-mingw32",
		"windowsamd64": "i686-mingw32",
		"linuxarm":     "arm-linux-gnueabihf",
	}

	var correctSystem int
	maxSimilarity := 0.7

	for i, system := range tool.Systems {
		similarity := smetrics.Jaro(system.Host, systems[runtime.GOOS+runtime.GOARCH])
		if similarity > maxSimilarity {
			correctSystem = i
			maxSimilarity = similarity
		}
	}

	return correctSystem
}
<commit_msg>make tools.install extract in the proper folder<commit_after>package pkgs

import (
	"context"
	"fmt"
	"io/ioutil"
	"net/http"
	"os/user"
	"path/filepath"
	"runtime"
	"strings"

	"github.com/arduino/arduino-create-agent/gen/tools"
	"github.com/codeclysm/extract"
	"github.com/sirupsen/logrus"
	"github.com/xrash/smetrics"
)

type Tools struct {
	Log     *logrus.Logger
	Indexes interface {
		List(context.Context) ([]string, error)
		Get(context.Context, string) (Index, error)
	}
	Folder string
}

func (c *Tools) Available(ctx context.Context) (res tools.ToolCollection, err error) {
	list, err := c.Indexes.List(ctx)
	if err != nil {
		return nil, err
	}

	for _, url := range list {
		index, err := c.Indexes.Get(ctx, url)
		if err != nil {
			return nil, err
		}

		for _, packager := range index.Packages {
			for _, tool := range packager.Tools {
				res = append(res, &tools.Tool{
					Packager: packager.Name,
					Name:     tool.Name,
					Version:  tool.Version,
				})
			}
		}
	}

	return res, nil
}

func (c *Tools) Installed(ctx context.Context) (tools.ToolCollection, error) {
	res := tools.ToolCollection{}

	// Find packagers
	usr, _ := user.Current()
	path := filepath.Join(usr.HomeDir, ".arduino-create")
	packagers, err := ioutil.ReadDir(path)
	if err != nil {
		return nil, err
	}

	for _, packager := range packagers {
		if !packager.IsDir() {
			continue
		}
		// Find tools
		toolss, err := ioutil.ReadDir(filepath.Join(path, packager.Name()))
		if err != nil {
			return nil, err
		}
		for _, tool := range toolss {
			// Find versions
			versions, err := ioutil.ReadDir(filepath.Join(path, packager.Name(), tool.Name()))
			if err != nil {
				return nil, err
			}

			for _, version := range versions {
				res = append(res, &tools.Tool{
					Packager: packager.Name(),
					Name:     tool.Name(),
					Version:  version.Name(),
				})
			}
		}
	}

	return res, nil
}

func (c *Tools) Install(ctx context.Context, payload *tools.ToolPayload) error {
	list, err := c.Indexes.List(ctx)
	if err != nil {
		return err
	}

	for _, url := range list {
		index, err := c.Indexes.Get(ctx, url)
		if err != nil {
			return err
		}

		for _, packager := range index.Packages {
			if packager.Name != payload.Packager {
				continue
			}

			for _, tool := range packager.Tools {
				if tool.Name == payload.Name &&
					tool.Version == payload.Version {
					return c.install(ctx, payload.Packager, tool)
				}
			}
		}
	}

	return tools.MakeNotFound(
		fmt.Errorf("tool not found with packager '%s', name '%s', version '%s'",
			payload.Packager, payload.Name, payload.Version))
}

func (c *Tools) install(ctx context.Context, packager string, tool Tool) error {
	i := findSystem(tool)

	// Download
	fmt.Println(tool.Systems[i].URL)
	res, err := http.Get(tool.Systems[i].URL)
	if err != nil {
		return err
	}
	defer res.Body.Close()

	err = extract.Archive(ctx, res.Body, c.Folder, rename(packager, tool.Name, tool.Version))
	if err != nil {
		return err
	}

	return nil
}

func (c *Tools) Remove(ctx context.Context, payload *tools.ToolPayload) error {
	return nil
}

func rename(packager, name, version string) extract.Renamer {
	base := filepath.Join(packager, name, version)
	return func(path string) string {
		parts := strings.Split(path, string(filepath.Separator))
		path = strings.Join(parts[1:], string(filepath.Separator))
		path = filepath.Join(base, path)
		fmt.Println("path", path)
		return path
	}
}

func findSystem(tool Tool) int {
	var systems = map[string]string{
		"linuxamd64":   "x86_64-linux-gnu",
		"linux386":     "i686-linux-gnu",
		"darwinamd64":  "apple-darwin",
		"windows386":   "i686-mingw32",
		"windowsamd64": "i686-mingw32",
		"linuxarm":     "arm-linux-gnueabihf",
	}

	var correctSystem int
	maxSimilarity := 0.7

	for i, system := range tool.Systems {
		similarity := smetrics.Jaro(system.Host, systems[runtime.GOOS+runtime.GOARCH])
		if similarity > maxSimilarity {
			correctSystem = i
			maxSimilarity = similarity
		}
	}

	return correctSystem
}
<|endoftext|>


<commit_before>package main

import "errors"
import "flag"
import "fmt"
import "os"

// Validate the passed-in arguments as directories.
func validateArgs(args []string) error {
    if len(args) < 1 {
        return errors.New("expected 1 or more arguments")
    }
    for _, directory := range args {
        dir, err := os.Open(directory)
        if err != nil {
            return err
        }
        defer dir.Close()
        info, err := dir.Stat()
        if err != nil {
            return err
        } else if !info.IsDir() {
            return errors.New(fmt.Sprintf("%v is not a directory", directory))
        }
    }

    return nil
}

// find-duplicate-files takes 1 or more directories on the command-line,
// recurses into all of them, and prints out what files are duplicates of
// each other.
func main() {
    flag.Parse()
    directories := flag.Args()
    err := validateArgs(directories); if err != nil {
        // Be more correct if flags.out() were publicly available.
        fmt.Fprintln(os.Stderr, err)
        os.Exit(1)
    }
}
<commit_msg>comment tweak<commit_after>package main

import "errors"
import "flag"
import "fmt"
import "os"

// Validate the passed-in arguments are directories.
func validateArgs(args []string) error {
    if len(args) < 1 {
        return errors.New("expected 1 or more arguments")
    }
    for _, directory := range args {
        dir, err := os.Open(directory)
        if err != nil {
            return err
        }
        defer dir.Close()
        info, err := dir.Stat()
        if err != nil {
            return err
        } else if !info.IsDir() {
            return errors.New(fmt.Sprintf("%v is not a directory", directory))
        }
    }

    return nil
}

// find-duplicate-files takes 1 or more directories on the command-line,
// recurses into all of them, and prints out what files are duplicates of
// each other.
func main() {
    flag.Parse()
    directories := flag.Args()
    err := validateArgs(directories); if err != nil {
        // Be more correct if flags.out() were publicly available.
        fmt.Fprintln(os.Stderr, err)
        os.Exit(1)
    }
}
<|endoftext|>


<commit_before>package geometry

import (
	"math"
	"testing"
)

func TestVector2DCore(t *testing.T) {
	v := Vector2D{0, 0}
	v.Add(&Vector2D{2, 1})
	if !v.Equal(&Vector2D{2, 1}) {
		t.Error("Vector2D.Add")
	}
	v.Subtract(&Vector2D{1, 0})
	if !v.Equal(&Vector2D{1, 1}) {
		t.Error("Vector2D.Subtract")
	}
	v.Multiply(&Vector2D{0.5, 0.5})
	if !v.Equal(&Vector2D{0.5, 0.5}) {
		t.Error("Vector2D.Multiply")
	}
	v.Divide(&Vector2D{2, 2})
	if !v.Equal(&Vector2D{0.25, 0.25}) {
		t.Error("Vector2D.Divide")
	}
}

func TestVector2DScale(t *testing.T) {
	v := Vector2D{2, 2}
	v.Scale(0.5)
	if !v.Equal(&Vector2D{1, 1}) {
		t.Error("Vector2D.Scale")
	}
}

func TestVector2DLength(t *testing.T) {
	v := Vector2D{3, 4}
	if v.Length() != 5 {
		t.Error("Vector2D.Length")
	}
	if v.LengthSquared() != 25 {
		t.Error("Vector2D.LengthSquared")
	}
}

func TestScalarProjectionOnto2D(t *testing.T) {
	v1 := &Vector2D{2, 3}
	v2 := &Vector2D{2, 1}
	if v1.ScalarProjection(v2) != 7.0/5.0 {
		t.Error("Vector2D.ScalarProjection")
	}
}

func TestVectorProjectionOnto2D(t *testing.T) {
	v1 := &Vector2D{2, 3}
	v2 := &Vector2D{2, 1}
	v1.ProjectedOnto(v2)
	if !v1.Equal(&Vector2D{2.8, 1.4}) {
		t.Error("Vector2D.ProjectedOnto")
	}
}

func TestVector2DFuzzyEqual(t *testing.T) {
	v1 := &Vector2D{1.0, 1.0}
	v2 := &Vector2D{1.0, 1.0}
	v2.X += 0.0000000000001
	if v1.Equal(v2) {
		t.Error("Vector2D.Equal")
	}
	if !v1.FuzzyEqual(v2) {
		t.Error("Vector2D.FuzzyEqual")
	}
	v2.Y += 0.000000000001
	if v1.Equal(v2) {
		t.Error("Vector2D.Equal")
	}
	if v1.FuzzyEqual(v2) {
		t.Error("Vector2D.FuzzyEqual")
	}
}

func TestVector2DNormalize(t *testing.T) {
	v := Vector2D{15, 20}
	if v.Normalize(); !v.Equal(&Vector2D{15.0 / 25.0, 20.0 / 25.0}) {
		t.Error("Vector2D.Normalize")
	}
	v = Vector2D{0, 0}
	if v.Normalize(); !v.Equal(&Vector2D{0, 0}) {
		t.Error("Vector2D.Normalize")
	}
}

func TestDotProduct2D(t *testing.T) {
	if DotProduct2D(&Vector2D{2, 4}, &Vector2D{1, 5}) != 22 {
		t.Error("DotProduct2D")
	}
}

func TestAngleBetween2D(t *testing.T) {
	v1 := &Vector2D{1, 0}
	v2 := &Vector2D{1, 1}
	if v1.AngleBetween(v2) != math.Pi/4 {
		t.Error("Vector2D.AngleBetween")
	}
	if v2.AngleBetween(v1) != math.Pi/4 {
		t.Error("Vector2D.AngleBetween")
	}
}
<commit_msg>Started adding vector2d timing.<commit_after>package geometry

import (
	"math"
	"testing"
)

func TestVector2DCore(t *testing.T) {
	v := Vector2D{0, 0}
	v.Add(&Vector2D{2, 1})
	if !v.Equal(&Vector2D{2, 1}) {
		t.Error("Vector2D.Add")
	}
	v.Subtract(&Vector2D{1, 0})
	if !v.Equal(&Vector2D{1, 1}) {
		t.Error("Vector2D.Subtract")
	}
	v.Multiply(&Vector2D{0.5, 0.5})
	if !v.Equal(&Vector2D{0.5, 0.5}) {
		t.Error("Vector2D.Multiply")
	}
	v.Divide(&Vector2D{2, 2})
	if !v.Equal(&Vector2D{0.25, 0.25}) {
		t.Error("Vector2D.Divide")
	}
}

func Benchmark_Vector2D_Add(b *testing.B) {
	v1, v2 := &Vector2D{1, 1}, &Vector2D{2, 1}
	for i := 0; i < b.N; i++ {
		v1.Add(v2)
	}
}

func Benchmark_Vector2D_Subtract(b *testing.B) {
	v1, v2 := &Vector2D{1, 1}, &Vector2D{2, 1}
	for i := 0; i < b.N; i++ {
		v1.Subtract(v2)
	}
}

func Benchmark_Vector2D_Multiply(b *testing.B) {
	v1, v2 := &Vector2D{1, 1}, &Vector2D{2, 1}
	for i := 0; i < b.N; i++ {
		v1.Multiply(v2)
	}
}

func Benchmark_Vector2D_Divide(b *testing.B) {
	v1, v2 := &Vector2D{1, 1}, &Vector2D{2, 1}
	for i := 0; i < b.N; i++ {
		v1.Divide(v2)
	}
}

func TestVector2DScale(t *testing.T) {
	v := Vector2D{2, 2}
	v.Scale(0.5)
	if !v.Equal(&Vector2D{1, 1}) {
		t.Error("Vector2D.Scale")
	}
}

func Benchmark_Vector2D_Scale(b *testing.B) {
	v := &Vector2D{1, 1}
	for i := 0; i < b.N; i++ {
		v.Scale(1.5)
	}
}

func TestVector2DLength(t *testing.T) {
	v := Vector2D{3, 4}
	if v.Length() != 5 {
		t.Error("Vector2D.Length")
	}
	if v.LengthSquared() != 25 {
		t.Error("Vector2D.LengthSquared")
	}
}

func TestScalarProjectionOnto2D(t *testing.T) {
	v1 := &Vector2D{2, 3}
	v2 := &Vector2D{2, 1}
	if v1.ScalarProjection(v2) != 7.0/5.0 {
		t.Error("Vector2D.ScalarProjection")
	}
}

func TestVectorProjectionOnto2D(t *testing.T) {
	v1 := &Vector2D{2, 3}
	v2 := &Vector2D{2, 1}
	v1.ProjectedOnto(v2)
	if !v1.Equal(&Vector2D{2.8, 1.4}) {
		t.Error("Vector2D.ProjectedOnto")
	}
}

func TestVector2DFuzzyEqual(t *testing.T) {
	v1 := &Vector2D{1.0, 1.0}
	v2 := &Vector2D{1.0, 1.0}
	v2.X += 0.0000000000001
	if v1.Equal(v2) {
		t.Error("Vector2D.Equal")
	}
	if !v1.FuzzyEqual(v2) {
		t.Error("Vector2D.FuzzyEqual")
	}
	v2.Y += 0.000000000001
	if v1.Equal(v2) {
		t.Error("Vector2D.Equal")
	}
	if v1.FuzzyEqual(v2) {
		t.Error("Vector2D.FuzzyEqual")
	}
}

func TestVector2DNormalize(t *testing.T) {
	v := Vector2D{15, 20}
	if v.Normalize(); !v.Equal(&Vector2D{15.0 / 25.0, 20.0 / 25.0}) {
		t.Error("Vector2D.Normalize")
	}
	v = Vector2D{0, 0}
	if v.Normalize(); !v.Equal(&Vector2D{0, 0}) {
		t.Error("Vector2D.Normalize")
	}
}

func TestDotProduct2D(t *testing.T) {
	if DotProduct2D(&Vector2D{2, 4}, &Vector2D{1, 5}) != 22 {
		t.Error("DotProduct2D")
	}
}

func TestAngleBetween2D(t *testing.T) {
	v1 := &Vector2D{1, 0}
	v2 := &Vector2D{1, 1}
	if v1.AngleBetween(v2) != math.Pi/4 {
		t.Error("Vector2D.AngleBetween")
	}
	if v2.AngleBetween(v1) != math.Pi/4 {
		t.Error("Vector2D.AngleBetween")
	}
}
<|endoftext|>


<commit_before>//: ----------------------------------------------------------------------------
//: Copyright (C) 2017 Verizon.  All Rights Reserved.
//: All Rights Reserved
//:
//: file:    options.go
//: details: TODO
//: author:  Mehrdad Arshad Rad
//: date:    02/01/2017
//:
//: Licensed under the Apache License, Version 2.0 (the "License");
//: you may not use this file except in compliance with the License.
//: You may obtain a copy of the License at
//:
//:     http://www.apache.org/licenses/LICENSE-2.0
//:
//: Unless required by applicable law or agreed to in writing, software
//: distributed under the License is distributed on an "AS IS" BASIS,
//: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//: See the License for the specific language governing permissions and
//: limitations under the License.
//: ----------------------------------------------------------------------------
package main

import (
	"flag"
	"log"
	"os"
)

type Options struct {
	// global options
	Verbose bool
	Logger  *log.Logger

	// stats options
	StatsEnabled  bool
	StatsHTTPPort string
	StatsHTTPAddr string

	// sFlow options
	SFlowEnabled bool
	SFlowPort    int
	SFlowUDPSize int
	SFlowWorkers int

	// IPFIX options
	IPFIXEnabled           bool
	IPFIXPort              int
	IPFIXUDPSize           int
	IPFIXWorkers           int
	IPFIXMirror            string
	IPFIXTemplateCacheFile string

	// producer
	MQName       string
	MQConfigFile string
}

func NewOptions() *Options {
	return &Options{
		Verbose: true,
		Logger:  log.New(os.Stderr, "[vflow] ", log.Ldate|log.Ltime),

		StatsEnabled:  true,
		StatsHTTPPort: "8080",
		StatsHTTPAddr: "",

		SFlowEnabled: true,
		SFlowPort:    6343,
		SFlowUDPSize: 1500,
		SFlowWorkers: 10,

		IPFIXEnabled:           true,
		IPFIXPort:              4739,
		IPFIXUDPSize:           1500,
		IPFIXWorkers:           10,
		IPFIXMirror:            "139.49.193.73:4172",
		IPFIXTemplateCacheFile: "/tmp/vflow.templates",

		MQName:       "kafka",
		MQConfigFile: "/usr/local/vflow/etc/kafka.conf",
	}
}

func GetOptions() *Options {
	opts := NewOptions()
	vFlowFlagSet(opts)

	return opts
}

func vFlowFlagSet(opts *Options) {

	var config string

	flag.StringVar(&config, "config", "", "path to config file")

	if config != "" {
		vFlowLoadCfg(config, opts)
	}

	// global options
	flag.BoolVar(&opts.Verbose, "verbose", opts.Verbose, "enable verbose logging")

	// stats options
	flag.BoolVar(&opts.StatsEnabled, "stats-enabled", opts.StatsEnabled, "enable stats listener")
	flag.StringVar(&opts.StatsHTTPPort, "stats-http-port", opts.StatsHTTPPort, "stats port listener")
	flag.StringVar(&opts.StatsHTTPAddr, "stats-http-addr", opts.StatsHTTPAddr, "stats bind address listener")

	// sflow options
	flag.BoolVar(&opts.SFlowEnabled, "sflow-enabled", opts.SFlowEnabled, "enable sflow listener")
	flag.IntVar(&opts.SFlowPort, "sflow-port", opts.SFlowPort, "sflow port number")
	flag.IntVar(&opts.SFlowUDPSize, "sflow-max-udp-size", opts.SFlowUDPSize, "sflow maximum UDP size")
	flag.IntVar(&opts.SFlowWorkers, "sflow-workers", opts.SFlowWorkers, "sflow workers / concurrency number")

	// ipfix options
	flag.BoolVar(&opts.IPFIXEnabled, "ipfix-enabled", opts.IPFIXEnabled, "enable IPFIX listener")
	flag.IntVar(&opts.IPFIXPort, "ipfix-port", opts.IPFIXPort, "IPFIX port number")
	flag.IntVar(&opts.IPFIXUDPSize, "ipfix-max-udp-size", opts.IPFIXUDPSize, "IPFIX maximum UDP size")
	flag.IntVar(&opts.IPFIXWorkers, "ipfix-workers", opts.IPFIXWorkers, "IPFIX workers / concurrency number")
	flag.StringVar(&opts.IPFIXTemplateCacheFile, "ipfix-tpl-cache-file", opts.IPFIXTemplateCacheFile, "IPFIX template cache file")

	// producer
	flag.StringVar(&opts.MQName, "mqueue", opts.MQName, "producer message queue name")
	flag.StringVar(&opts.MQConfigFile, "mqueue-conf", opts.MQConfigFile, "producer message queue configuration file")

	flag.Parse()
}

func vFlowLoadCfg(file string, opts *Options) {
	// TODO
}
<commit_msg>load main configuration<commit_after>//: ----------------------------------------------------------------------------
//: Copyright (C) 2017 Verizon.  All Rights Reserved.
//: All Rights Reserved
//:
//: file:    options.go
//: details: TODO
//: author:  Mehrdad Arshad Rad
//: date:    02/01/2017
//:
//: Licensed under the Apache License, Version 2.0 (the "License");
//: you may not use this file except in compliance with the License.
//: You may obtain a copy of the License at
//:
//:     http://www.apache.org/licenses/LICENSE-2.0
//:
//: Unless required by applicable law or agreed to in writing, software
//: distributed under the License is distributed on an "AS IS" BASIS,
//: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//: See the License for the specific language governing permissions and
//: limitations under the License.
//: ----------------------------------------------------------------------------
package main

import (
	"encoding/json"
	"flag"
	"io/ioutil"
	"log"
	"os"
)

type Options struct {
	// global options
	Verbose bool
	Logger  *log.Logger

	// stats options
	StatsEnabled  bool
	StatsHTTPPort string
	StatsHTTPAddr string

	// sFlow options
	SFlowEnabled bool
	SFlowPort    int
	SFlowUDPSize int
	SFlowWorkers int

	// IPFIX options
	IPFIXEnabled           bool
	IPFIXPort              int
	IPFIXUDPSize           int
	IPFIXWorkers           int
	IPFIXMirror            string
	IPFIXTemplateCacheFile string

	// producer
	MQName       string
	MQConfigFile string
}

func NewOptions() *Options {
	return &Options{
		Verbose: true,
		Logger:  log.New(os.Stderr, "[vflow] ", log.Ldate|log.Ltime),

		StatsEnabled:  true,
		StatsHTTPPort: "8080",
		StatsHTTPAddr: "",

		SFlowEnabled: true,
		SFlowPort:    6343,
		SFlowUDPSize: 1500,
		SFlowWorkers: 10,

		IPFIXEnabled:           true,
		IPFIXPort:              4739,
		IPFIXUDPSize:           1500,
		IPFIXWorkers:           10,
		IPFIXMirror:            "139.49.193.73:4172",
		IPFIXTemplateCacheFile: "/tmp/vflow.templates",

		MQName:       "kafka",
		MQConfigFile: "/usr/local/vflow/etc/kafka.conf",
	}
}

func GetOptions() *Options {
	opts := NewOptions()
	vFlowFlagSet(opts)

	return opts
}

func vFlowFlagSet(opts *Options) {

	var config string

	flag.StringVar(&config, "config", "/usr/local/vflow/etc/vflow.conf", "path to config file")

	vFlowLoadCfg(config, opts)

	// global options
	flag.BoolVar(&opts.Verbose, "verbose", opts.Verbose, "enable verbose logging")

	// stats options
	flag.BoolVar(&opts.StatsEnabled, "stats-enabled", opts.StatsEnabled, "enable stats listener")
	flag.StringVar(&opts.StatsHTTPPort, "stats-http-port", opts.StatsHTTPPort, "stats port listener")
	flag.StringVar(&opts.StatsHTTPAddr, "stats-http-addr", opts.StatsHTTPAddr, "stats bind address listener")

	// sflow options
	flag.BoolVar(&opts.SFlowEnabled, "sflow-enabled", opts.SFlowEnabled, "enable sflow listener")
	flag.IntVar(&opts.SFlowPort, "sflow-port", opts.SFlowPort, "sflow port number")
	flag.IntVar(&opts.SFlowUDPSize, "sflow-max-udp-size", opts.SFlowUDPSize, "sflow maximum UDP size")
	flag.IntVar(&opts.SFlowWorkers, "sflow-workers", opts.SFlowWorkers, "sflow workers / concurrency number")

	// ipfix options
	flag.BoolVar(&opts.IPFIXEnabled, "ipfix-enabled", opts.IPFIXEnabled, "enable IPFIX listener")
	flag.IntVar(&opts.IPFIXPort, "ipfix-port", opts.IPFIXPort, "IPFIX port number")
	flag.IntVar(&opts.IPFIXUDPSize, "ipfix-max-udp-size", opts.IPFIXUDPSize, "IPFIX maximum UDP size")
	flag.IntVar(&opts.IPFIXWorkers, "ipfix-workers", opts.IPFIXWorkers, "IPFIX workers / concurrency number")
	flag.StringVar(&opts.IPFIXTemplateCacheFile, "ipfix-tpl-cache-file", opts.IPFIXTemplateCacheFile, "IPFIX template cache file")

	// producer
	flag.StringVar(&opts.MQName, "mqueue", opts.MQName, "producer message queue name")
	flag.StringVar(&opts.MQConfigFile, "mqueue-conf", opts.MQConfigFile, "producer message queue configuration file")

	flag.Parse()
}

func vFlowLoadCfg(f string, opts *Options) {
	b, err := ioutil.ReadFile(f)
	if err != nil {
		opts.Logger.Println(err)
		return
	}
	err = json.Unmarshal(b, opts)
	if err != nil {
		opts.Logger.Println(err)
	}
}
<|endoftext|>


<commit_before>package voting

import (
	"log"
	"strings"
)

const (
	unresolved = "N/A"
)

// StatItem holds counter name and current read.
type StatItem struct {
	Name  string
	Value int
}

// Stats holds collections of StatItems.
type Stats struct {
	Candidates []StatItem
	Countries  []StatItem
}

// Voting is a service that holds all business logic required to run voting.
type Voting struct {
	messenger Messenger
	enquirer  Enquirer
	scoreKpr  ScoreKeeper
	event     string
}

// Messenger is used to send text messages.
type Messenger interface {
	RequestSMS(sender, msisdn, text string)
}

// Enquirer is used to resolve Country by MSISDN.
type Enquirer interface {
	Lookup(msisdn string) (string, error)
}

// ScoreKeeper persists score and stats, returns results.
type ScoreKeeper interface {
	AddPoint(participant string) error
	AddCountry(name string) error
	GetAllCandidates() ([]string, error)
	GetAllCountries() ([]string, error)
	Get(key string) (int, error)
}

// New constructs Voting service instance initialized with all dependencies.
func New(m Messenger, en Enquirer, sk ScoreKeeper, ev string) *Voting {
	return &Voting{
		messenger: m,
		enquirer:  en,
		scoreKpr:  sk,
		event:     ev,
	}
}

// RegisterVote increments votes counter for participant and also keeps track of number of votes for each country.
func (s *Voting) RegisterVote(msisdn, cand string) error {
	log.Printf("Got new message: %q from MSISDN: %q", cand, msisdn)
	var (
		country string
		err     error
	)

	cand = strings.TrimSpace(cand)
	if cand == "" {
		log.Println("Voter sent blank SMS, score not changed.")
		s.messenger.RequestSMS(s.event, msisdn, "Please specify candidate's name to actually vote.")
		return nil
	}

	err = s.scoreKpr.AddPoint(cand)
	if err != nil {
		log.Println("Point was not added to participant's score, error:", err)
		return err
	}

	country, err = s.enquirer.Lookup(msisdn)
	if err != nil {
		log.Printf("Country lookup failed for MSISDN: %q, error: %q", msisdn, err)
		country = unresolved
	}

	err = s.scoreKpr.AddCountry(country)
	if err != nil {
		log.Printf("Failed to assure contry with code: %q is present in countries set. Error: %q", country, err)
	}

	// Here just recording stats, so if failed - no big deal, candidates's vote is there already.
	if err = s.scoreKpr.AddPoint(country); err != nil {
		log.Println("Country counter was not incremented, error:", err)
	}

	s.messenger.RequestSMS(s.event, msisdn, "Thanks for your vote!")

	return nil
}

// GetStats returns voting statistics for each participant and distribution by countries.
func (s *Voting) GetStats() (Stats, error) {
	candidates, err := s.scoreKpr.GetAllCandidates()
	if err != nil {
		log.Println("Failed to retrieve set of all candidates, error:", err)
		return Stats{}, err
	}

	countries, err := s.scoreKpr.GetAllCountries()
	if err != nil {
		log.Println("Failed to retrieve set of all countries, error:", err)
		return Stats{}, err
	}

	return Stats{
		Candidates: s.populateStatItems(candidates),
		Countries:  s.populateStatItems(countries),
	}, nil
}

func (s *Voting) populateStatItems(keys []string) []StatItem {
	results := make([]StatItem, 0, len(keys))

	for _, k := range keys {
		v, err := s.scoreKpr.Get(k)
		if err != nil {
			log.Printf("Failed to get score for key: %q, error: %q", k, err)
			// handle -1 as temporary unresolvable on client,
			// most likely we will get proper value during next update.
			v = -1
		}
		results = append(results, StatItem{Name: k, Value: v})
	}

	return results
}
<commit_msg>adding doc<commit_after>package voting

import (
	"log"
	"strings"
)

const (
	unresolved = "N/A"
)

// StatItem holds counter name and current read.
type StatItem struct {
	Name  string
	Value int
}

// Stats holds collections of StatItems.
type Stats struct {
	Candidates []StatItem
	Countries  []StatItem
}

// Voting is a service that holds all business logic required to run voting.
type Voting struct {
	messenger Messenger
	enquirer  Enquirer
	scoreKpr  ScoreKeeper
	event     string
}

// Messenger is used to send text messages.
type Messenger interface {
	RequestSMS(sender, msisdn, text string)
}

// Enquirer is used to resolve Country by MSISDN.
type Enquirer interface {
	Lookup(msisdn string) (string, error)
}

// ScoreKeeper persists score and stats, returns results.
type ScoreKeeper interface {
	AddPoint(participant string) error
	AddCountry(name string) error
	GetAllCandidates() ([]string, error)
	GetAllCountries() ([]string, error)
	Get(key string) (int, error)
}

// New constructs Voting service instance initialized with all dependencies.
func New(m Messenger, en Enquirer, sk ScoreKeeper, ev string) *Voting {
	return &Voting{
		messenger: m,
		enquirer:  en,
		scoreKpr:  sk,
		event:     ev,
	}
}

// RegisterVote increments votes counter for participant and also keeps track of number of votes for each country.
func (s *Voting) RegisterVote(msisdn, cand string) error {
	log.Printf("Got new message: %q from MSISDN: %q", cand, msisdn)
	var (
		country string
		err     error
	)

	cand = strings.TrimSpace(cand)
	if cand == "" {
		log.Println("Voter sent blank SMS, score not changed.")
		s.messenger.RequestSMS(s.event, msisdn, "Please specify candidate's name to actually vote.")
		return nil
	}

	err = s.scoreKpr.AddPoint(cand)
	if err != nil {
		log.Println("Point was not added to participant's score, error:", err)
		return err
	}

	country, err = s.enquirer.Lookup(msisdn)
	if err != nil {
		log.Printf("Country lookup failed for MSISDN: %q, error: %q", msisdn, err)
		country = unresolved
	}

	err = s.scoreKpr.AddCountry(country)
	if err != nil {
		log.Printf("Failed to assure contry with code: %q is present in countries set. Error: %q", country, err)
	}

	// Here just recording stats, so if failed - no big deal, candidates's vote is there already.
	if err = s.scoreKpr.AddPoint(country); err != nil {
		log.Println("Country counter was not incremented, error:", err)
	}

	s.messenger.RequestSMS(s.event, msisdn, "Thanks for your vote!")

	return nil
}

// GetStats returns voting statistics for each participant and distribution by countries.
func (s *Voting) GetStats() (Stats, error) {
	candidates, err := s.scoreKpr.GetAllCandidates()
	if err != nil {
		log.Println("Failed to retrieve set of all candidates, error:", err)
		return Stats{}, err
	}

	countries, err := s.scoreKpr.GetAllCountries()
	if err != nil {
		log.Println("Failed to retrieve set of all countries, error:", err)
		return Stats{}, err
	}

	return Stats{
		Candidates: s.populateStatItems(candidates),
		Countries:  s.populateStatItems(countries),
	}, nil
}

// populateStatItems checks counter read for every key and then returns slice with all resolved values.
// If there was an error reading single counter we use -1 as temporary value.
// We assume that this will not happen during next update. And we still able to show other values.
func (s *Voting) populateStatItems(keys []string) []StatItem {
	results := make([]StatItem, 0, len(keys))

	for _, k := range keys {
		v, err := s.scoreKpr.Get(k)
		if err != nil {
			log.Printf("Failed to get score for key: %q, error: %q", k, err)
			// handle -1 as temporary unresolvable on client,
			// most likely we will get proper value during next update.
			v = -1
		}
		results = append(results, StatItem{Name: k, Value: v})
	}

	return results
}
<|endoftext|>