<commit_before>/*
Copyright 2017 The Kubernetes Authors.

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 conformance implements conformance test kubetest code.
package conformance

import (
	"context"
	"fmt"
	"log"
	"os"
	"os/exec"
	"time"

	"k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/tools/clientcmd"
	"k8s.io/test-infra/kubetest/e2e"
	"k8s.io/test-infra/kubetest/process"
)

// Tester runs conformance tests against a given cluster.
type Tester struct {
	kubecfg   string
	ginkgo    string
	e2etest   string
	reportdir string
	testArgs  *string
	control   *process.Control
}

// BuildTester returns an object that knows how to test the cluster it deployed.
func (d *Deployer) BuildTester(o *e2e.BuildTesterOptions) (e2e.Tester, error) {
	reportdir, err := os.Getwd()
	if err != nil {
		return nil, err
	}

	if o.FocusRegex == "" {
		o.FocusRegex = "\".*\""
	}
	if o.SkipRegex == "" {
		o.SkipRegex = "\".*(Feature)|(NFS)|(StatefulSet).*\""
	}

	t := e2e.NewGinkgoTester(o)

	t.Seed = 1436380640
	t.GinkgoParallel = 10
	t.Kubeconfig = d.kubecfg
	t.FlakeAttempts = 2
	t.NumNodes = 4
	t.SystemdServices = []string{"docker", "kubelet"}
	t.ReportDir = reportdir

	return t, nil
}

// Deployer returns a deployer stub that expects a cluster to already exist.
type Deployer struct {
	kubecfg   string
	apiserver *kubernetes.Clientset
}

// Deployer implements e2e.TestBuilder, overriding testing
var _ e2e.TestBuilder = &Deployer{}

// NewDeployer returns a new Deployer.
func NewDeployer(kubecfg string) (*Deployer, error) {
	// The easiest thing to do is just load the altereted kubecfg from the file we wrote.
	config, err := clientcmd.BuildConfigFromFlags("", kubecfg)
	if err != nil {
		return nil, err
	}
	apiserver, err := kubernetes.NewForConfig(config)
	if err != nil {
		return nil, err
	}

	return &Deployer{
		kubecfg:   kubecfg,
		apiserver: apiserver,
	}, nil
}

// Up synchronously starts a cluster, or times out.
func (d *Deployer) Up() error {
	return fmt.Errorf("cannot up a conformance cluster")
}

// IsUp returns nil if the apiserver is running, or the error received while checking.
func (d *Deployer) IsUp() error {
	_, err := d.isAPIServerUp()
	return err
}

func (d *Deployer) isAPIServerUp() (*v1.ComponentStatusList, error) {
	if d.apiserver == nil {
		return nil, fmt.Errorf("no apiserver client available")
	}
	//TODO(Q-Lee): check that relevant components have started. May consider checking addons.
	return d.apiserver.CoreV1().ComponentStatuses().List(context.TODO(), metav1.ListOptions{})
}

// DumpClusterLogs is a no-op.
func (d *Deployer) DumpClusterLogs(localPath, gcsPath string) error {
	return nil
}

// TestSetup is a no-op.
func (d *Deployer) TestSetup() error {
	return nil
}

// Down stops and removes the cluster container.
func (d *Deployer) Down() error {
	return fmt.Errorf("cannot down a conformance cluster")
}

// GetClusterCreated returns the start time of the cluster container. If the container doesn't exist, has no start time, or has a malformed start time, then an error is returned.
func (d *Deployer) GetClusterCreated(gcpProject string) (time.Time, error) {
	return time.Time{}, fmt.Errorf("cannot get cluster create time for conformance cluster")
}

func (d *Deployer) KubectlCommand() (*exec.Cmd, error) {
	log.Print("Noop - Conformance KubectlCommand()")
	return nil, nil
}
<commit_msg>Set default flake attempt to 1 (not 2)<commit_after>/*
Copyright 2017 The Kubernetes Authors.

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 conformance implements conformance test kubetest code.
package conformance

import (
	"context"
	"fmt"
	"log"
	"os"
	"os/exec"
	"time"

	"k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/tools/clientcmd"
	"k8s.io/test-infra/kubetest/e2e"
	"k8s.io/test-infra/kubetest/process"
)

// Tester runs conformance tests against a given cluster.
type Tester struct {
	kubecfg   string
	ginkgo    string
	e2etest   string
	reportdir string
	testArgs  *string
	control   *process.Control
}

// BuildTester returns an object that knows how to test the cluster it deployed.
func (d *Deployer) BuildTester(o *e2e.BuildTesterOptions) (e2e.Tester, error) {
	reportdir, err := os.Getwd()
	if err != nil {
		return nil, err
	}

	if o.FocusRegex == "" {
		o.FocusRegex = "\".*\""
	}
	if o.SkipRegex == "" {
		o.SkipRegex = "\".*(Feature)|(NFS)|(StatefulSet).*\""
	}

	t := e2e.NewGinkgoTester(o)

	t.Seed = 1436380640
	t.GinkgoParallel = 10
	t.Kubeconfig = d.kubecfg
	t.FlakeAttempts = 1
	t.NumNodes = 4
	t.SystemdServices = []string{"docker", "kubelet"}
	t.ReportDir = reportdir

	return t, nil
}

// Deployer returns a deployer stub that expects a cluster to already exist.
type Deployer struct {
	kubecfg   string
	apiserver *kubernetes.Clientset
}

// Deployer implements e2e.TestBuilder, overriding testing
var _ e2e.TestBuilder = &Deployer{}

// NewDeployer returns a new Deployer.
func NewDeployer(kubecfg string) (*Deployer, error) {
	// The easiest thing to do is just load the altereted kubecfg from the file we wrote.
	config, err := clientcmd.BuildConfigFromFlags("", kubecfg)
	if err != nil {
		return nil, err
	}
	apiserver, err := kubernetes.NewForConfig(config)
	if err != nil {
		return nil, err
	}

	return &Deployer{
		kubecfg:   kubecfg,
		apiserver: apiserver,
	}, nil
}

// Up synchronously starts a cluster, or times out.
func (d *Deployer) Up() error {
	return fmt.Errorf("cannot up a conformance cluster")
}

// IsUp returns nil if the apiserver is running, or the error received while checking.
func (d *Deployer) IsUp() error {
	_, err := d.isAPIServerUp()
	return err
}

func (d *Deployer) isAPIServerUp() (*v1.ComponentStatusList, error) {
	if d.apiserver == nil {
		return nil, fmt.Errorf("no apiserver client available")
	}
	//TODO(Q-Lee): check that relevant components have started. May consider checking addons.
	return d.apiserver.CoreV1().ComponentStatuses().List(context.TODO(), metav1.ListOptions{})
}

// DumpClusterLogs is a no-op.
func (d *Deployer) DumpClusterLogs(localPath, gcsPath string) error {
	return nil
}

// TestSetup is a no-op.
func (d *Deployer) TestSetup() error {
	return nil
}

// Down stops and removes the cluster container.
func (d *Deployer) Down() error {
	return fmt.Errorf("cannot down a conformance cluster")
}

// GetClusterCreated returns the start time of the cluster container. If the container doesn't exist, has no start time, or has a malformed start time, then an error is returned.
func (d *Deployer) GetClusterCreated(gcpProject string) (time.Time, error) {
	return time.Time{}, fmt.Errorf("cannot get cluster create time for conformance cluster")
}

func (d *Deployer) KubectlCommand() (*exec.Cmd, error) {
	log.Print("Noop - Conformance KubectlCommand()")
	return nil, nil
}
<|endoftext|>


<commit_before>// +build linux
// +build cgo

package drivers

import (
	"fmt"
	"os"
	"unsafe"

	"github.com/pkg/errors"
)

/*
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#define _FILE_OFFSET_BITS 64
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <linux/loop.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "../../include/macro.h"
#include "../../include/memory_utils.h"

#define LXD_MAXPATH 4096
#define LXD_NUMSTRLEN64 21
#define LXD_MAX_LOOP_PATHLEN (2 * sizeof("loop/")) + LXD_NUMSTRLEN64 + sizeof("backing_file") + 1

// If a loop file is already associated with a loop device, find it.
// This looks at "/sys/block" to avoid having to parse all of "/dev". Also, this
// allows to retrieve the full name of the backing file even if
// strlen(backing file) > LO_NAME_SIZE.
static int find_associated_loop_device(const char *loop_file,
				       char *loop_dev_name)
{
	__do_closedir DIR *dir = NULL;
	char looppath[LXD_MAX_LOOP_PATHLEN];
	char buf[LXD_MAXPATH];
	struct dirent *dp;

	dir = opendir("/sys/block");
	if (!dir)
		return -1;

	while ((dp = readdir(dir))) {
		__do_close_prot_errno int loop_path_fd = -EBADF;
		int ret;
		size_t totlen;
		struct stat fstatbuf;
		int dfd = -1;

		if (!dp)
			break;

		if (strncmp(dp->d_name, "loop", 4))
			continue;

		dfd = dirfd(dir);
		if (dfd < 0)
			continue;

		ret = snprintf(looppath, sizeof(looppath), "%s/loop/backing_file", dp->d_name);
		if (ret < 0 || (size_t)ret >= sizeof(looppath))
			continue;

		ret = fstatat(dfd, looppath, &fstatbuf, 0);
		if (ret < 0)
			continue;

		loop_path_fd = openat(dfd, looppath, O_RDONLY | O_CLOEXEC, 0);
		if (ret < 0)
			continue;

		// Clear buffer.
		memset(buf, 0, sizeof(buf));
		ret = read(loop_path_fd, buf, sizeof(buf));
		if (ret < 0)
			continue;

		totlen = strlen(buf);

		// Trim newlines.
		while ((totlen > 0) && (buf[totlen - 1] == '\n'))
			buf[--totlen] = '\0';

		if (strcmp(buf, loop_file))
			continue;

		// Create path to loop device.
		ret = snprintf(loop_dev_name, LO_NAME_SIZE, "/dev/%s",
			       dp->d_name);
		if (ret < 0 || ret >= LO_NAME_SIZE)
			continue;

		// Open fd to loop device.
		return open(loop_dev_name, O_RDWR);
	}

	return -1;
}

static int get_unused_loop_dev_legacy(char *loop_name)
{
	__do_closedir DIR *dir = NULL;
	struct dirent *dp;
	struct loop_info64 lo64;

	dir = opendir("/dev");
	if (!dir)
		return -1;

	while ((dp = readdir(dir))) {
		__do_close_prot_errno int dfd = -EBADF, fd = -EBADF;
		int ret;

		if (!dp)
			break;

		if (strncmp(dp->d_name, "loop", 4) != 0)
			continue;

		dfd = dirfd(dir);
		if (dfd < 0)
			continue;

		fd = openat(dfd, dp->d_name, O_RDWR);
		if (fd < 0)
			continue;

		ret = ioctl(fd, LOOP_GET_STATUS64, &lo64);
		if (ret < 0)
			if (ioctl(fd, LOOP_GET_STATUS64, &lo64) == 0 || errno != ENXIO)
				continue;

		ret = snprintf(loop_name, LO_NAME_SIZE, "/dev/%s", dp->d_name);
		if (ret < 0 || ret >= LO_NAME_SIZE)
			continue;

		return move_fd(fd);
	}

	return -1;
}

static int get_unused_loop_dev(char *name_loop)
{
	__do_close_prot_errno int fd_ctl = -1;
	int loop_nr, ret;

	fd_ctl = open("/dev/loop-control", O_RDWR | O_CLOEXEC);
	if (fd_ctl < 0)
		return -ENODEV;

	loop_nr = ioctl(fd_ctl, LOOP_CTL_GET_FREE);
	if (loop_nr < 0)
		return -1;

	ret = snprintf(name_loop, LO_NAME_SIZE, "/dev/loop%d", loop_nr);
	if (ret < 0 || ret >= LO_NAME_SIZE)
		return -1;

	return open(name_loop, O_RDWR | O_CLOEXEC);
}

static int prepare_loop_dev(const char *source, char *loop_dev, int flags)
{
	__do_close_prot_errno int fd_img = -1, fd_loop = -1;
	int ret;
	struct loop_info64 lo64;

	fd_loop = get_unused_loop_dev(loop_dev);
	if (fd_loop < 0) {
		if (fd_loop == -ENODEV)
			fd_loop = get_unused_loop_dev_legacy(loop_dev);
		else
			return -1;
	}

	fd_img = open(source, O_RDWR | O_CLOEXEC);
	if (fd_img < 0)
		return -1;

	ret = ioctl(fd_loop, LOOP_SET_FD, fd_img);
	if (ret < 0)
		return -1;

	memset(&lo64, 0, sizeof(lo64));
	lo64.lo_flags = flags;

	ret = ioctl(fd_loop, LOOP_SET_STATUS64, &lo64);
	if (ret < 0)
		return -1;

	return move_fd(fd_loop);
}

static inline int prepare_loop_dev_retry(const char *source, char *loop_dev, int flags)
{
	int ret;
	unsigned int idx = 0;

	do {
		ret = prepare_loop_dev(source, loop_dev, flags);
		idx++;
	} while (ret < 0 && errno == EBUSY && idx < 30);

	return ret;
}

// Note that this does not guarantee to clear the loop device in time so that
// find_associated_loop_device() will not report that there still is a
// configured device (udev and so on...). So don't call
// find_associated_loop_device() after having called
// set_autoclear_loop_device().
int set_autoclear_loop_device(int fd_loop)
{
	struct loop_info64 lo64;

	memset(&lo64, 0, sizeof(lo64));
	lo64.lo_flags = LO_FLAGS_AUTOCLEAR;
	errno = 0;
	return ioctl(fd_loop, LOOP_SET_STATUS64, &lo64);
}

// Unset the LO_FLAGS_AUTOCLEAR flag on the given loop device file descriptor.
int unset_autoclear_loop_device(int fd_loop)
{
	int ret;
	struct loop_info64 lo64;

	errno = 0;
	ret = ioctl(fd_loop, LOOP_GET_STATUS64, &lo64);
	if (ret < 0)
		return -1;

	if ((lo64.lo_flags & LO_FLAGS_AUTOCLEAR) == 0)
		return 0;

	lo64.lo_flags &= ~LO_FLAGS_AUTOCLEAR;
	errno = 0;
	return ioctl(fd_loop, LOOP_SET_STATUS64, &lo64);
}
*/
import "C"

// LoFlagsAutoclear determines whether the loop device will autodestruct on last
// close.
const LoFlagsAutoclear int = C.LO_FLAGS_AUTOCLEAR

// PrepareLoopDev detects and sets up a loop device for source. It returns an
// open file descriptor to the free loop device and the path of the free loop
// device. It's the callers responsibility to close the open file descriptor.
func PrepareLoopDev(source string, flags int) (*os.File, error) {
	cLoopDev := C.malloc(C.size_t(C.LO_NAME_SIZE))
	if cLoopDev == nil {
		return nil, fmt.Errorf("Failed to allocate memory in C")
	}
	defer C.free(cLoopDev)

	cSource := C.CString(source)
	defer C.free(unsafe.Pointer(cSource))
	loopFd, _ := C.find_associated_loop_device(cSource, (*C.char)(cLoopDev))
	if loopFd >= 0 {
		return os.NewFile(uintptr(loopFd), C.GoString((*C.char)(cLoopDev))), nil
	}

	loopFd, err := C.prepare_loop_dev_retry(cSource, (*C.char)(cLoopDev), C.int(flags))
	if loopFd < 0 {
		if err != nil {
			return nil, errors.Wrapf(err, "Failed to prepare loop device for %q", source)
		}

		return nil, fmt.Errorf("Failed to prepare loop device for %q", source)
	}

	return os.NewFile(uintptr(loopFd), C.GoString((*C.char)(cLoopDev))), nil
}

// SetAutoclearOnLoopDev enables autodestruction of the provided loopback device.
func SetAutoclearOnLoopDev(loopFd int) error {
	ret, err := C.set_autoclear_loop_device(C.int(loopFd))
	if ret < 0 {
		if err != nil {
			return err
		}
		return fmt.Errorf("Failed to set LO_FLAGS_AUTOCLEAR")
	}

	return nil
}

// UnsetAutoclearOnLoopDev disables autodestruction of the provided loopback device.
func UnsetAutoclearOnLoopDev(loopFd int) error {
	ret, err := C.unset_autoclear_loop_device(C.int(loopFd))
	if ret < 0 {
		if err != nil {
			return err
		}
		return fmt.Errorf("Failed to unset LO_FLAGS_AUTOCLEAR")
	}

	return nil
}
<commit_msg>lxd/storage/drivers: Add releaseLoopDev<commit_after>// +build linux
// +build cgo

package drivers

import (
	"fmt"
	"os"
	"unsafe"

	"github.com/pkg/errors"
)

/*
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#define _FILE_OFFSET_BITS 64
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <linux/loop.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "../../include/macro.h"
#include "../../include/memory_utils.h"

#define LXD_MAXPATH 4096
#define LXD_NUMSTRLEN64 21
#define LXD_MAX_LOOP_PATHLEN (2 * sizeof("loop/")) + LXD_NUMSTRLEN64 + sizeof("backing_file") + 1

// If a loop file is already associated with a loop device, find it.
// This looks at "/sys/block" to avoid having to parse all of "/dev". Also, this
// allows to retrieve the full name of the backing file even if
// strlen(backing file) > LO_NAME_SIZE.
static int find_associated_loop_device(const char *loop_file,
				       char *loop_dev_name)
{
	__do_closedir DIR *dir = NULL;
	char looppath[LXD_MAX_LOOP_PATHLEN];
	char buf[LXD_MAXPATH];
	struct dirent *dp;

	dir = opendir("/sys/block");
	if (!dir)
		return -1;

	while ((dp = readdir(dir))) {
		__do_close_prot_errno int loop_path_fd = -EBADF;
		int ret;
		size_t totlen;
		struct stat fstatbuf;
		int dfd = -1;

		if (!dp)
			break;

		if (strncmp(dp->d_name, "loop", 4))
			continue;

		dfd = dirfd(dir);
		if (dfd < 0)
			continue;

		ret = snprintf(looppath, sizeof(looppath), "%s/loop/backing_file", dp->d_name);
		if (ret < 0 || (size_t)ret >= sizeof(looppath))
			continue;

		ret = fstatat(dfd, looppath, &fstatbuf, 0);
		if (ret < 0)
			continue;

		loop_path_fd = openat(dfd, looppath, O_RDONLY | O_CLOEXEC, 0);
		if (ret < 0)
			continue;

		// Clear buffer.
		memset(buf, 0, sizeof(buf));
		ret = read(loop_path_fd, buf, sizeof(buf));
		if (ret < 0)
			continue;

		totlen = strlen(buf);

		// Trim newlines.
		while ((totlen > 0) && (buf[totlen - 1] == '\n'))
			buf[--totlen] = '\0';

		if (strcmp(buf, loop_file))
			continue;

		// Create path to loop device.
		ret = snprintf(loop_dev_name, LO_NAME_SIZE, "/dev/%s",
			       dp->d_name);
		if (ret < 0 || ret >= LO_NAME_SIZE)
			continue;

		// Open fd to loop device.
		return open(loop_dev_name, O_RDWR);
	}

	return -1;
}

static int get_unused_loop_dev_legacy(char *loop_name)
{
	__do_closedir DIR *dir = NULL;
	struct dirent *dp;
	struct loop_info64 lo64;

	dir = opendir("/dev");
	if (!dir)
		return -1;

	while ((dp = readdir(dir))) {
		__do_close_prot_errno int dfd = -EBADF, fd = -EBADF;
		int ret;

		if (!dp)
			break;

		if (strncmp(dp->d_name, "loop", 4) != 0)
			continue;

		dfd = dirfd(dir);
		if (dfd < 0)
			continue;

		fd = openat(dfd, dp->d_name, O_RDWR);
		if (fd < 0)
			continue;

		ret = ioctl(fd, LOOP_GET_STATUS64, &lo64);
		if (ret < 0)
			if (ioctl(fd, LOOP_GET_STATUS64, &lo64) == 0 || errno != ENXIO)
				continue;

		ret = snprintf(loop_name, LO_NAME_SIZE, "/dev/%s", dp->d_name);
		if (ret < 0 || ret >= LO_NAME_SIZE)
			continue;

		return move_fd(fd);
	}

	return -1;
}

static int get_unused_loop_dev(char *name_loop)
{
	__do_close_prot_errno int fd_ctl = -1;
	int loop_nr, ret;

	fd_ctl = open("/dev/loop-control", O_RDWR | O_CLOEXEC);
	if (fd_ctl < 0)
		return -ENODEV;

	loop_nr = ioctl(fd_ctl, LOOP_CTL_GET_FREE);
	if (loop_nr < 0)
		return -1;

	ret = snprintf(name_loop, LO_NAME_SIZE, "/dev/loop%d", loop_nr);
	if (ret < 0 || ret >= LO_NAME_SIZE)
		return -1;

	return open(name_loop, O_RDWR | O_CLOEXEC);
}

static int prepare_loop_dev(const char *source, char *loop_dev, int flags)
{
	__do_close_prot_errno int fd_img = -1, fd_loop = -1;
	int ret;
	struct loop_info64 lo64;

	fd_loop = get_unused_loop_dev(loop_dev);
	if (fd_loop < 0) {
		if (fd_loop == -ENODEV)
			fd_loop = get_unused_loop_dev_legacy(loop_dev);
		else
			return -1;
	}

	fd_img = open(source, O_RDWR | O_CLOEXEC);
	if (fd_img < 0)
		return -1;

	ret = ioctl(fd_loop, LOOP_SET_FD, fd_img);
	if (ret < 0)
		return -1;

	memset(&lo64, 0, sizeof(lo64));
	lo64.lo_flags = flags;

	ret = ioctl(fd_loop, LOOP_SET_STATUS64, &lo64);
	if (ret < 0)
		return -1;

	return move_fd(fd_loop);
}

static inline int prepare_loop_dev_retry(const char *source, char *loop_dev, int flags)
{
	int ret;
	unsigned int idx = 0;

	do {
		ret = prepare_loop_dev(source, loop_dev, flags);
		idx++;
	} while (ret < 0 && errno == EBUSY && idx < 30);

	return ret;
}

// Note that this does not guarantee to clear the loop device in time so that
// find_associated_loop_device() will not report that there still is a
// configured device (udev and so on...). So don't call
// find_associated_loop_device() after having called
// set_autoclear_loop_device().
int set_autoclear_loop_device(int fd_loop)
{
	struct loop_info64 lo64;

	memset(&lo64, 0, sizeof(lo64));
	lo64.lo_flags = LO_FLAGS_AUTOCLEAR;
	errno = 0;
	return ioctl(fd_loop, LOOP_SET_STATUS64, &lo64);
}

// Directly release the loop device
int free_loop_device(int fd_loop)
{
	return ioctl(fd_loop, LOOP_CLR_FD);
}

// Unset the LO_FLAGS_AUTOCLEAR flag on the given loop device file descriptor.
int unset_autoclear_loop_device(int fd_loop)
{
	int ret;
	struct loop_info64 lo64;

	errno = 0;
	ret = ioctl(fd_loop, LOOP_GET_STATUS64, &lo64);
	if (ret < 0)
		return -1;

	if ((lo64.lo_flags & LO_FLAGS_AUTOCLEAR) == 0)
		return 0;

	lo64.lo_flags &= ~LO_FLAGS_AUTOCLEAR;
	errno = 0;
	return ioctl(fd_loop, LOOP_SET_STATUS64, &lo64);
}
*/
import "C"

// LoFlagsAutoclear determines whether the loop device will autodestruct on last
// close.
const LoFlagsAutoclear int = C.LO_FLAGS_AUTOCLEAR

// PrepareLoopDev detects and sets up a loop device for source. It returns an
// open file descriptor to the free loop device and the path of the free loop
// device. It's the callers responsibility to close the open file descriptor.
func PrepareLoopDev(source string, flags int) (*os.File, error) {
	cLoopDev := C.malloc(C.size_t(C.LO_NAME_SIZE))
	if cLoopDev == nil {
		return nil, fmt.Errorf("Failed to allocate memory in C")
	}
	defer C.free(cLoopDev)

	cSource := C.CString(source)
	defer C.free(unsafe.Pointer(cSource))
	loopFd, _ := C.find_associated_loop_device(cSource, (*C.char)(cLoopDev))
	if loopFd >= 0 {
		return os.NewFile(uintptr(loopFd), C.GoString((*C.char)(cLoopDev))), nil
	}

	loopFd, err := C.prepare_loop_dev_retry(cSource, (*C.char)(cLoopDev), C.int(flags))
	if loopFd < 0 {
		if err != nil {
			return nil, errors.Wrapf(err, "Failed to prepare loop device for %q", source)
		}

		return nil, fmt.Errorf("Failed to prepare loop device for %q", source)
	}

	return os.NewFile(uintptr(loopFd), C.GoString((*C.char)(cLoopDev))), nil
}

// releaseLoopDev releases the loop dev assigned to the provided file.
func releaseLoopDev(source string) error {
	cLoopDev := C.malloc(C.size_t(C.LO_NAME_SIZE))
	if cLoopDev == nil {
		return fmt.Errorf("Failed to allocate memory in C")
	}
	defer C.free(cLoopDev)

	cSource := C.CString(source)
	loopFd, err := C.find_associated_loop_device(cSource, (*C.char)(cLoopDev))
	if err != nil {
		return err
	}

	// Prepare a Go file and defer close on the loop device.
	fd := os.NewFile(uintptr(loopFd), C.GoString((*C.char)(cLoopDev)))
	defer fd.Close()

	if loopFd >= 0 {
		_, err := C.free_loop_device(C.int(loopFd))
		if err != nil {
			return err
		}
	}

	return nil
}

// SetAutoclearOnLoopDev enables autodestruction of the provided loopback device.
func SetAutoclearOnLoopDev(loopFd int) error {
	ret, err := C.set_autoclear_loop_device(C.int(loopFd))
	if ret < 0 {
		if err != nil {
			return err
		}
		return fmt.Errorf("Failed to set LO_FLAGS_AUTOCLEAR")
	}

	return nil
}

// UnsetAutoclearOnLoopDev disables autodestruction of the provided loopback device.
func UnsetAutoclearOnLoopDev(loopFd int) error {
	ret, err := C.unset_autoclear_loop_device(C.int(loopFd))
	if ret < 0 {
		if err != nil {
			return err
		}
		return fmt.Errorf("Failed to unset LO_FLAGS_AUTOCLEAR")
	}

	return nil
}
<|endoftext|>


<commit_before>// Copyright 2020 The LUCI Authors.
//
// 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.

// +build !windows

package invoke

import (
	"os/exec"
	"syscall"

	"go.chromium.org/luci/common/errors"
)

func setSysProcAttr(_ *exec.Cmd) {}

func (s *Subprocess) terminate() error {
	if err := s.cmd.Process.Signal(syscall.SIGTERM); err != nil {
		return errors.Annotate(err, "send SIGTERM").Err()
	}
	return nil
}
<commit_msg>[bbagent] Make and signal process group on *nix.<commit_after>// Copyright 2020 The LUCI Authors.
//
// 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.

// +build !windows

package invoke

import (
	"os/exec"
	"syscall"

	"go.chromium.org/luci/common/errors"
)

func setSysProcAttr(cmd *exec.Cmd) {
	cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
}

func (s *Subprocess) terminate() error {
	if err := syscall.Kill(-s.cmd.Process.Pid, syscall.SIGTERM); err != nil {
		return errors.Annotate(err, "send SIGTERM").Err()
	}
	return nil
}
<|endoftext|>


<commit_before>package drivers

import (
	"io"

	"github.com/lxc/lxd/lxd/migration"
	"github.com/lxc/lxd/lxd/operations"
	"github.com/lxc/lxd/lxd/state"
	"github.com/lxc/lxd/shared/api"
	"github.com/lxc/lxd/shared/logger"
)

// driver is the extended internal interface.
type driver interface {
	Driver

	init(state *state.State, name string, config map[string]string, logger logger.Logger, volIDFunc func(volType VolumeType, volName string) (int64, error), commonRulesFunc func() map[string]func(string) error) error
	load() error
}

// Driver represents a low-level storage driver.
type Driver interface {
	// Internal.
	Config() map[string]string
	Info() Info
	HasVolume(volType VolumeType, volName string) bool

	// Pool.
	Create() error
	Delete(op *operations.Operation) error
	Mount() (bool, error)
	Unmount() (bool, error)
	GetResources() (*api.ResourcesStoragePool, error)

	// Volumes.
	ValidateVolume(vol Volume, removeUnknownKeys bool) error
	CreateVolume(vol Volume, filler func(mountPath, rootBlockPath string) error, op *operations.Operation) error
	CreateVolumeFromCopy(vol Volume, srcVol Volume, copySnapshots bool, op *operations.Operation) error
	RefreshVolume(vol Volume, srcVol Volume, srcSnapshots []Volume, op *operations.Operation) error
	DeleteVolume(volType VolumeType, volName string, op *operations.Operation) error
	RenameVolume(volType VolumeType, volName string, newName string, op *operations.Operation) error
	UpdateVolume(vol Volume, changedConfig map[string]string) error
	GetVolumeUsage(volType VolumeType, volName string) (int64, error)
	SetVolumeQuota(volType VolumeType, volName, size string, op *operations.Operation) error
	GetVolumeDiskPath(volType VolumeType, volName string) (string, error)

	// MountVolume mounts a storage volume, returns true if we caused a new mount, false if
	// already mounted.
	MountVolume(volType VolumeType, volName string, op *operations.Operation) (bool, error)

	// MountVolumeSnapshot mounts a storage volume snapshot as readonly, returns true if we
	// caused a new mount, false if already mounted.
	MountVolumeSnapshot(volType VolumeType, volName, snapshotName string, op *operations.Operation) (bool, error)

	// UnmountVolume unmounts a storage volume, returns true if unmounted, false if was not
	// mounted.
	UnmountVolume(volType VolumeType, volName string, op *operations.Operation) (bool, error)

	// UnmountVolume unmounts a storage volume snapshot, returns true if unmounted, false if was
	// not mounted.
	UnmountVolumeSnapshot(VolumeType VolumeType, volName, snapshotName string, op *operations.Operation) (bool, error)

	CreateVolumeSnapshot(volType VolumeType, volName string, newSnapshotName string, op *operations.Operation) error
	DeleteVolumeSnapshot(volType VolumeType, volName string, snapshotName string, op *operations.Operation) error
	RenameVolumeSnapshot(volType VolumeType, volName string, snapshotName string, newSnapshotName string, op *operations.Operation) error
	VolumeSnapshots(volType VolumeType, volName string, op *operations.Operation) ([]string, error)
	RestoreVolume(vol Volume, snapshotName string, op *operations.Operation) error

	// Migration.
	MigrationTypes(contentType ContentType) []migration.Type
	MigrateVolume(vol Volume, conn io.ReadWriteCloser, volSrcArgs migration.VolumeSourceArgs, op *operations.Operation) error
	CreateVolumeFromMigration(vol Volume, conn io.ReadWriteCloser, volTargetArgs migration.VolumeTargetArgs, op *operations.Operation) error

	// Backup.
	BackupVolume(vol Volume, targetPath string, optimized bool, snapshots bool, op *operations.Operation) error
	RestoreBackupVolume(vol Volume, snapshots []string, srcData io.ReadSeeker, op *operations.Operation) (func(vol Volume) error, func(), error)
}
<commit_msg>lxd/storage/drivers/interface: Updates CreateVolumeFromMigration and CreateVolume to use VolumeFiller<commit_after>package drivers

import (
	"io"

	"github.com/lxc/lxd/lxd/migration"
	"github.com/lxc/lxd/lxd/operations"
	"github.com/lxc/lxd/lxd/state"
	"github.com/lxc/lxd/shared/api"
	"github.com/lxc/lxd/shared/logger"
)

// driver is the extended internal interface.
type driver interface {
	Driver

	init(state *state.State, name string, config map[string]string, logger logger.Logger, volIDFunc func(volType VolumeType, volName string) (int64, error), commonRulesFunc func() map[string]func(string) error) error
	load() error
}

// Driver represents a low-level storage driver.
type Driver interface {
	// Internal.
	Config() map[string]string
	Info() Info
	HasVolume(volType VolumeType, volName string) bool

	// Pool.
	Create() error
	Delete(op *operations.Operation) error
	Mount() (bool, error)
	Unmount() (bool, error)
	GetResources() (*api.ResourcesStoragePool, error)

	// Volumes.
	ValidateVolume(vol Volume, removeUnknownKeys bool) error
	CreateVolume(vol Volume, filler *VolumeFiller, op *operations.Operation) error
	CreateVolumeFromCopy(vol Volume, srcVol Volume, copySnapshots bool, op *operations.Operation) error
	RefreshVolume(vol Volume, srcVol Volume, srcSnapshots []Volume, op *operations.Operation) error
	DeleteVolume(volType VolumeType, volName string, op *operations.Operation) error
	RenameVolume(volType VolumeType, volName string, newName string, op *operations.Operation) error
	UpdateVolume(vol Volume, changedConfig map[string]string) error
	GetVolumeUsage(volType VolumeType, volName string) (int64, error)
	SetVolumeQuota(volType VolumeType, volName, size string, op *operations.Operation) error
	GetVolumeDiskPath(volType VolumeType, volName string) (string, error)

	// MountVolume mounts a storage volume, returns true if we caused a new mount, false if
	// already mounted.
	MountVolume(volType VolumeType, volName string, op *operations.Operation) (bool, error)

	// MountVolumeSnapshot mounts a storage volume snapshot as readonly, returns true if we
	// caused a new mount, false if already mounted.
	MountVolumeSnapshot(volType VolumeType, volName, snapshotName string, op *operations.Operation) (bool, error)

	// UnmountVolume unmounts a storage volume, returns true if unmounted, false if was not
	// mounted.
	UnmountVolume(volType VolumeType, volName string, op *operations.Operation) (bool, error)

	// UnmountVolume unmounts a storage volume snapshot, returns true if unmounted, false if was
	// not mounted.
	UnmountVolumeSnapshot(VolumeType VolumeType, volName, snapshotName string, op *operations.Operation) (bool, error)

	CreateVolumeSnapshot(volType VolumeType, volName string, newSnapshotName string, op *operations.Operation) error
	DeleteVolumeSnapshot(volType VolumeType, volName string, snapshotName string, op *operations.Operation) error
	RenameVolumeSnapshot(volType VolumeType, volName string, snapshotName string, newSnapshotName string, op *operations.Operation) error
	VolumeSnapshots(volType VolumeType, volName string, op *operations.Operation) ([]string, error)
	RestoreVolume(vol Volume, snapshotName string, op *operations.Operation) error

	// Migration.
	MigrationTypes(contentType ContentType) []migration.Type
	MigrateVolume(vol Volume, conn io.ReadWriteCloser, volSrcArgs migration.VolumeSourceArgs, op *operations.Operation) error
	CreateVolumeFromMigration(vol Volume, conn io.ReadWriteCloser, volTargetArgs migration.VolumeTargetArgs, preFiller *VolumeFiller, op *operations.Operation) error

	// Backup.
	BackupVolume(vol Volume, targetPath string, optimized bool, snapshots bool, op *operations.Operation) error
	RestoreBackupVolume(vol Volume, snapshots []string, srcData io.ReadSeeker, op *operations.Operation) (func(vol Volume) error, func(), error)
}
<|endoftext|>


<commit_before>package posix

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"
)

func TestCommits(t *testing.T) {
	remote := "/tmp/remote/greeting"

	base, err := ioutil.TempDir("", "test")
	if err != nil {
		t.Error(err)
		return
	}
	defer os.Remove(base)

	for i, test := range tests {
		local := filepath.Join(base, fmt.Sprint(i))
		err = os.MkdirAll(local, 0777)
		if err != nil {
			t.Error(err)
			return
		}

		bin, err := filepath.Abs("clone-commit")
		if err != nil {
			t.Error(err)
			return
		}

		cmd := exec.Command(bin)
		cmd.Dir = local
		cmd.Env = []string{
			fmt.Sprintf("DRONE_COMMIT_BRANCH=%s", test.branch),
			fmt.Sprintf("DRONE_COMMIT_SHA=%s", test.commit),
			fmt.Sprintf("DRONE_WORKSPACE=%s", local),
			fmt.Sprintf("DRONE_REMOTE_URL=%s", remote),
		}

		out, err := cmd.CombinedOutput()
		if err != nil {
			t.Error(err)
			t.Log(string(out))
			return
		}

		commit, err := getCommit(local)
		if err != nil {
			t.Error(err)
			return
		}

		branch, err := getBranch(local)
		if err != nil {
			t.Error(err)
			return
		}

		if want, got := test.commit, commit; got != want {
			t.Errorf("Want commit %s, got %s", want, got)
		}

		if want, got := test.branch, branch; got != want {
			t.Errorf("Want branch %s, got %s", want, got)
		}

		file := filepath.Join(local, test.file)
		out, err = ioutil.ReadFile(file)
		if err != nil {
			t.Error(err)
			return
		}

		if want, got := test.text, string(out); want != got {
			t.Errorf("Want file content %q, got %q", want, got)
		}
	}
}

func TestTags(t *testing.T) {
	remote := "/tmp/remote/greeting"

	base, err := ioutil.TempDir("", "test")
	if err != nil {
		t.Error(err)
		return
	}
	defer os.Remove(base)

	for i, test := range tests {
		local := filepath.Join(base, fmt.Sprint(i))
		err = os.MkdirAll(local, 0777)
		if err != nil {
			t.Error(err)
			return
		}

		bin, err := filepath.Abs("clone-tag")
		if err != nil {
			t.Error(err)
			return
		}

		cmd := exec.Command(bin)
		cmd.Dir = local
		cmd.Env = []string{
			fmt.Sprintf("DRONE_TAG=%s", test.tag),
			fmt.Sprintf("DRONE_COMMIT_SHA=%s", test.commit),
			fmt.Sprintf("DRONE_WORKSPACE=%s", local),
			fmt.Sprintf("DRONE_REMOTE_URL=%s", remote),
		}

		out, err := cmd.CombinedOutput()
		if err != nil {
			t.Error(err)
			t.Log(string(out))
			return
		}

		commit, err := getCommit(local)
		if err != nil {
			t.Error(err)
			return
		}

		if want, got := test.commit, commit; got != want {
			t.Errorf("Want commit %s, got %s", want, got)
		}

		file := filepath.Join(local, test.file)
		out, err = ioutil.ReadFile(file)
		if err != nil {
			t.Error(err)
			return
		}

		if want, got := test.text, string(out); want != got {
			t.Errorf("Want file content %q, got %q", want, got)
		}
	}
}

func TestPullRequest(t *testing.T) {
	remote := "https://github.com/octocat/Spoon-Knife.git"

	local, err := ioutil.TempDir("", "test")
	if err != nil {
		t.Error(err)
		return
	}
	defer os.Remove(local)

	bin, err := filepath.Abs("clone-pull-request")
	if err != nil {
		t.Error(err)
		return
	}

	cmd := exec.Command(bin)
	cmd.Dir = local
	cmd.Env = []string{
		fmt.Sprintf("DRONE_COMMIT_REF=%s", "refs/pull/14596/head"),
		fmt.Sprintf("DRONE_COMMIT_BRANCH=%s", "main"),
		fmt.Sprintf("DRONE_COMMIT_SHA=%s", "26923a8f37933ccc23943de0d4ebd53908268582"),
		fmt.Sprintf("DRONE_WORKSPACE=%s", local),
		fmt.Sprintf("DRONE_REMOTE_URL=%s", remote),
	}

	out, err := cmd.CombinedOutput()
	if err != nil {
		t.Error(err)
		t.Log(string(out))
		return
	}

	commit, err := getCommit(local)
	if err != nil {
		t.Error(err)
		return
	}

	branch, err := getBranch(local)
	if err != nil {
		t.Error(err)
		return
	}

	if want, got := "26923a8f37933ccc23943de0d4ebd53908268582", commit; got != want {
		t.Errorf("Want commit %s, got %s", want, got)
	}

	if want, got := "main", branch; got != want {
		t.Errorf("Want branch %s, got %s", want, got)
	}

	file := filepath.Join(local, "directory/file.txt")
	out, err = ioutil.ReadFile(file)
	if err != nil {
		t.Error(err)
		return
	}
}

func getBranch(path string) (string, error) {
	cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
	cmd.Dir = path
	out, err := cmd.CombinedOutput()
	return strings.TrimSpace(string(out)), err
}

func getCommit(path string) (string, error) {
	cmd := exec.Command("git", "rev-parse", "HEAD")
	cmd.Dir = path
	out, err := cmd.CombinedOutput()
	return strings.TrimSpace(string(out)), err
}

var tests = []struct {
	branch string
	commit string
	tag    string
	file   string
	text   string
}{
	{
		commit: "9cd29dca0a98f76df94d66493ee54788a18190a0",
		branch: "main",
		tag:    "v1.0.0",
		file:   "hello.txt",
		text:   "hi world\n",
	},
	{
		commit: "bbdf5d4028a6066431f59fcd8d83afff610a55ae",
		branch: "main",
		tag:    "v1.1.0",
		file:   "hello.txt",
		text:   "hello world\n",
	},
	{
		commit: "553af1ca53c9ad54b096d7ff1416f6c4d1e5049f",
		branch: "fr",
		tag:    "v2.0.0",
		file:   "hello.txt",
		text:   "salut monde\n",
	},
	{
		commit: "94b4a1710d1581b8b00c5f7b077026eae3c07646",
		branch: "fr",
		tag:    "v2.1.0",
		file:   "hello.txt",
		text:   "bonjour monde\n",
	},
}
<commit_msg>Fix UT<commit_after>package posix

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"
)

func TestCommits(t *testing.T) {
	remote := "/tmp/remote/greeting"

	base, err := ioutil.TempDir("", "test")
	if err != nil {
		t.Error(err)
		return
	}
	defer os.Remove(base)

	for i, test := range tests {
		local := filepath.Join(base, fmt.Sprint(i))
		err = os.MkdirAll(local, 0777)
		if err != nil {
			t.Error(err)
			return
		}

		bin, err := filepath.Abs("clone-commit")
		if err != nil {
			t.Error(err)
			return
		}

		cmd := exec.Command(bin)
		cmd.Dir = local
		cmd.Env = []string{
			fmt.Sprintf("DRONE_COMMIT_BRANCH=%s", test.branch),
			fmt.Sprintf("DRONE_COMMIT_SHA=%s", test.commit),
			fmt.Sprintf("DRONE_WORKSPACE=%s", local),
			fmt.Sprintf("DRONE_REMOTE_URL=%s", remote),
		}

		out, err := cmd.CombinedOutput()
		if err != nil {
			t.Error(err)
			t.Log(string(out))
			return
		}

		commit, err := getCommit(local)
		if err != nil {
			t.Error(err)
			return
		}

		branch, err := getBranch(local)
		if err != nil {
			t.Error(err)
			return
		}

		if want, got := test.commit, commit; got != want {
			t.Errorf("Want commit %s, got %s", want, got)
		}

		if want, got := test.branch, branch; got != want {
			t.Errorf("Want branch %s, got %s", want, got)
		}

		file := filepath.Join(local, test.file)
		out, err = ioutil.ReadFile(file)
		if err != nil {
			t.Error(err)
			return
		}

		if want, got := test.text, string(out); want != got {
			t.Errorf("Want file content %q, got %q", want, got)
		}
	}
}

func TestTags(t *testing.T) {
	remote := "/tmp/remote/greeting"

	base, err := ioutil.TempDir("", "test")
	if err != nil {
		t.Error(err)
		return
	}
	defer os.Remove(base)

	for i, test := range tests {
		local := filepath.Join(base, fmt.Sprint(i))
		err = os.MkdirAll(local, 0777)
		if err != nil {
			t.Error(err)
			return
		}

		bin, err := filepath.Abs("clone-tag")
		if err != nil {
			t.Error(err)
			return
		}

		cmd := exec.Command(bin)
		cmd.Dir = local
		cmd.Env = []string{
			fmt.Sprintf("DRONE_TAG=%s", test.tag),
			fmt.Sprintf("DRONE_COMMIT_SHA=%s", test.commit),
			fmt.Sprintf("DRONE_WORKSPACE=%s", local),
			fmt.Sprintf("DRONE_REMOTE_URL=%s", remote),
		}

		out, err := cmd.CombinedOutput()
		if err != nil {
			t.Error(err)
			t.Log(string(out))
			return
		}

		commit, err := getCommit(local)
		if err != nil {
			t.Error(err)
			return
		}

		if want, got := test.commit, commit; got != want {
			t.Errorf("Want commit %s, got %s", want, got)
		}

		file := filepath.Join(local, test.file)
		out, err = ioutil.ReadFile(file)
		if err != nil {
			t.Error(err)
			return
		}

		if want, got := test.text, string(out); want != got {
			t.Errorf("Want file content %q, got %q", want, got)
		}
	}
}

func TestPullRequest(t *testing.T) {
	remote := "https://github.com/octocat/Spoon-Knife.git"

	local, err := ioutil.TempDir("", "test")
	if err != nil {
		t.Error(err)
		return
	}
	defer os.Remove(local)

	bin, err := filepath.Abs("clone-pull-request")
	if err != nil {
		t.Error(err)
		return
	}

	cmd := exec.Command(bin)
	cmd.Dir = local
	cmd.Env = []string{
		fmt.Sprintf("DRONE_COMMIT_REF=%s", "refs/pull/14596/head"),
		fmt.Sprintf("DRONE_COMMIT_BRANCH=%s", "main"),
		fmt.Sprintf("DRONE_COMMIT_SHA=%s", "26923a8f37933ccc23943de0d4ebd53908268582"),
		fmt.Sprintf("DRONE_WORKSPACE=%s", local),
		fmt.Sprintf("DRONE_REMOTE_URL=%s", remote),
	}

	out, err := cmd.CombinedOutput()
	if err != nil {
		t.Error(err)
		t.Log(string(out))
		return
	}

	commit, err := getCommit(local)
	if err != nil {
		t.Error(err)
		return
	}

	branch, err := getBranch(local)
	if err != nil {
		t.Error(err)
		return
	}

	if want, got := "26923a8f37933ccc23943de0d4ebd53908268582", commit; got != want {
		t.Errorf("Want commit %s, got %s", want, got)
	}

	if want, got := "main", branch; got != want {
		t.Errorf("Want branch %s, got %s", want, got)
	}

	file := filepath.Join(local, "directory/file.txt")
	out, err = ioutil.ReadFile(file)
	if err != nil {
		t.Error(err)
		return
	}
}

func getBranch(path string) (string, error) {
	cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
	cmd.Dir = path
	out, err := cmd.CombinedOutput()
	return strings.TrimSpace(string(out)), err
}

func getCommit(path string) (string, error) {
	cmd := exec.Command("git", "rev-parse", "HEAD")
	cmd.Dir = path
	out, err := cmd.CombinedOutput()
	return strings.TrimSpace(string(out)), err
}

var tests = []struct {
	branch string
	commit string
	tag    string
	file   string
	text   string
}{
	{
		commit: "9cd29dca0a98f76df94d66493ee54788a18190a0",
		branch: "master",
		tag:    "v1.0.0",
		file:   "hello.txt",
		text:   "hi world\n",
	},
	{
		commit: "bbdf5d4028a6066431f59fcd8d83afff610a55ae",
		branch: "master",
		tag:    "v1.1.0",
		file:   "hello.txt",
		text:   "hello world\n",
	},
	{
		commit: "553af1ca53c9ad54b096d7ff1416f6c4d1e5049f",
		branch: "fr",
		tag:    "v2.0.0",
		file:   "hello.txt",
		text:   "salut monde\n",
	},
	{
		commit: "94b4a1710d1581b8b00c5f7b077026eae3c07646",
		branch: "fr",
		tag:    "v2.1.0",
		file:   "hello.txt",
		text:   "bonjour monde\n",
	},
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	tc "github.com/thijzert/go-termcolours"
	hex "github.com/thijzert/sslprobe/hexdump"
	"github.com/thijzert/sslprobe/ssltvd"
	"os"
)

var (
	port = flag.Int("port", 443, "Connect to this port")
)

func init() {
	flag.Parse()
}

func main() {
	if flag.NArg() == 0 {
		fmt.Fprintf(os.Stderr, "Usage: %s {HOST} [{OPTIONS}]\n", os.Args[0])
		flag.PrintDefaults()
		os.Exit(1)
	}

	host := flag.Arg(0)
	fmt.Printf("Server:   %s\n", tc.Bblue(fmt.Sprintf("%s:%d", host, *port)))

	c, err := ssltvd.Dial("tcp", fmt.Sprintf("%s:%d", host, *port), &ssltvd.Config{
		InsecureSkipVerify: true,
	})
	if err != nil {
		panic(err)
	}

	_, err = c.Write([]byte("hi!\n"))
	if err != nil {
		panic(err)
	}

	pl, err := c.Heartbeat(4, []byte("tree"))
	if err != nil {
		panic(err)
	}
	hex.Dump(pl)

	pl, err = c.Heartbeat(1000, []byte("hat"))
	if err != nil {
		panic(err)
	}
	hex.Dump(pl)
}
<commit_msg>Fix misremembered XKCD reference<commit_after>package main

import (
	"flag"
	"fmt"
	tc "github.com/thijzert/go-termcolours"
	hex "github.com/thijzert/sslprobe/hexdump"
	"github.com/thijzert/sslprobe/ssltvd"
	"os"
)

var (
	port = flag.Int("port", 443, "Connect to this port")
)

func init() {
	flag.Parse()
}

func main() {
	if flag.NArg() == 0 {
		fmt.Fprintf(os.Stderr, "Usage: %s {HOST} [{OPTIONS}]\n", os.Args[0])
		flag.PrintDefaults()
		os.Exit(1)
	}

	host := flag.Arg(0)
	fmt.Printf("Server:   %s\n", tc.Bblue(fmt.Sprintf("%s:%d", host, *port)))

	c, err := ssltvd.Dial("tcp", fmt.Sprintf("%s:%d", host, *port), &ssltvd.Config{
		InsecureSkipVerify: true,
	})
	if err != nil {
		panic(err)
	}

	_, err = c.Write([]byte("hi!\n"))
	if err != nil {
		panic(err)
	}

	pl, err := c.Heartbeat(6, []byte("potato"))
	if err != nil {
		panic(err)
	}
	hex.Dump(pl)

	pl, err = c.Heartbeat(4, []byte("bird"))
	if err != nil {
		panic(err)
	}
	hex.Dump(pl)

	pl, err = c.Heartbeat(1000, []byte("hat"))
	if err != nil {
		panic(err)
	}
	hex.Dump(pl)
}
<|endoftext|>


<commit_before>// Copyright (c) 2016 Tigera, Inc. All rights reserved.

// 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 model

import (
	"fmt"

	"regexp"

	"reflect"

	log "github.com/Sirupsen/logrus"
	"github.com/projectcalico/libcalico-go/lib/errors"
)

var (
	matchWorkloadEndpointStatus = regexp.MustCompile("^/?calico/felix/v1/host/([^/]+)/workload/([^/]+)/([^/]+)/endpoint/([^/]+)$")
)

type WorkloadEndpointStatusKey struct {
	Hostname       string `json:"-"`
	OrchestratorID string `json:"-"`
	WorkloadID     string `json:"-"`
	EndpointID     string `json:"-"`
}

func (key WorkloadEndpointStatusKey) defaultPath() (string, error) {
	if key.Hostname == "" {
		return "", errors.ErrorInsufficientIdentifiers{Name: "hostname"}
	}
	if key.OrchestratorID == "" {
		return "", errors.ErrorInsufficientIdentifiers{Name: "orchestrator"}
	}
	if key.WorkloadID == "" {
		return "", errors.ErrorInsufficientIdentifiers{Name: "workload"}
	}
	if key.EndpointID == "" {
		return "", errors.ErrorInsufficientIdentifiers{Name: "endpoint"}
	}
	return fmt.Sprintf("/calico/felix/v1/host/%s/workload/%s/%s/endpoint/%s",
		key.Hostname, key.OrchestratorID, key.WorkloadID, key.EndpointID), nil
}

func (key WorkloadEndpointStatusKey) defaultDeletePath() (string, error) {
	return key.defaultPath()
}

func (key WorkloadEndpointStatusKey) defaultDeleteParentPaths() ([]string, error) {
	if key.Hostname == "" {
		return nil, errors.ErrorInsufficientIdentifiers{Name: "hostname"}
	}
	if key.OrchestratorID == "" {
		return nil, errors.ErrorInsufficientIdentifiers{Name: "orchestrator"}
	}
	if key.WorkloadID == "" {
		return nil, errors.ErrorInsufficientIdentifiers{Name: "workload"}
	}
	workload := fmt.Sprintf("/calico/felix/v1/host/%s/workload/%s/%s",
		key.Hostname, key.OrchestratorID, key.WorkloadID)
	endpoints := workload + "/endpoint"
	return []string{endpoints, workload}, nil
}

func (key WorkloadEndpointStatusKey) valueType() reflect.Type {
	return reflect.TypeOf(WorkloadEndpoint{})
}

func (key WorkloadEndpointStatusKey) String() string {
	return fmt.Sprintf("WorkloadEndpointStatus(hostname=%s, orchestrator=%s, workload=%s, name=%s)",
		key.Hostname, key.OrchestratorID, key.WorkloadID, key.EndpointID)
}

type WorkloadEndpointStatusListOptions struct {
	Hostname       string
	OrchestratorID string
	WorkloadID     string
	EndpointID     string
}

func (options WorkloadEndpointStatusListOptions) defaultPathRoot() string {
	k := "/calico/felix/v1/host"
	if options.Hostname == "" {
		return k
	}
	k = k + fmt.Sprintf("/%s/workload", options.Hostname)
	if options.OrchestratorID == "" {
		return k
	}
	k = k + fmt.Sprintf("/%s", options.OrchestratorID)
	if options.WorkloadID == "" {
		return k
	}
	k = k + fmt.Sprintf("/%s/endpoint", options.WorkloadID)
	if options.EndpointID == "" {
		return k
	}
	k = k + fmt.Sprintf("/%s", options.EndpointID)
	return k
}

func (options WorkloadEndpointStatusListOptions) KeyFromDefaultPath(ekey string) Key {
	log.Infof("Get WorkloadEndpoint key from %s", ekey)
	r := matchWorkloadEndpointStatus.FindAllStringSubmatch(ekey, -1)
	if len(r) != 1 {
		log.Infof("Didn't match regex")
		return nil
	}
	hostname := r[0][1]
	orch := r[0][2]
	workload := r[0][3]
	endpointID := r[0][4]
	if options.Hostname != "" && hostname != options.Hostname {
		log.Infof("Didn't match hostname %s != %s", options.Hostname, hostname)
		return nil
	}
	if options.OrchestratorID != "" && orch != options.OrchestratorID {
		log.Infof("Didn't match orchestrator %s != %s", options.OrchestratorID, orch)
		return nil
	}
	if options.WorkloadID != "" && workload != options.WorkloadID {
		log.Infof("Didn't match workload %s != %s", options.WorkloadID, workload)
		return nil
	}
	if options.EndpointID != "" && endpointID != options.EndpointID {
		log.Infof("Didn't match endpoint ID %s != %s", options.EndpointID, endpointID)
		return nil
	}
	return WorkloadEndpointStatusKey{Hostname: hostname, EndpointID: endpointID}
}

type WorkloadEndpointStatus struct {
	Status string `json:"status"`
}
<commit_msg>Fix missing fields on returned WorkloadEndpointStatusKey struct.<commit_after>// Copyright (c) 2016 Tigera, Inc. All rights reserved.

// 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 model

import (
	"fmt"

	"regexp"

	"reflect"

	log "github.com/Sirupsen/logrus"
	"github.com/projectcalico/libcalico-go/lib/errors"
)

var (
	matchWorkloadEndpointStatus = regexp.MustCompile("^/?calico/felix/v1/host/([^/]+)/workload/([^/]+)/([^/]+)/endpoint/([^/]+)$")
)

type WorkloadEndpointStatusKey struct {
	Hostname       string `json:"-"`
	OrchestratorID string `json:"-"`
	WorkloadID     string `json:"-"`
	EndpointID     string `json:"-"`
}

func (key WorkloadEndpointStatusKey) defaultPath() (string, error) {
	if key.Hostname == "" {
		return "", errors.ErrorInsufficientIdentifiers{Name: "hostname"}
	}
	if key.OrchestratorID == "" {
		return "", errors.ErrorInsufficientIdentifiers{Name: "orchestrator"}
	}
	if key.WorkloadID == "" {
		return "", errors.ErrorInsufficientIdentifiers{Name: "workload"}
	}
	if key.EndpointID == "" {
		return "", errors.ErrorInsufficientIdentifiers{Name: "endpoint"}
	}
	return fmt.Sprintf("/calico/felix/v1/host/%s/workload/%s/%s/endpoint/%s",
		key.Hostname, key.OrchestratorID, key.WorkloadID, key.EndpointID), nil
}

func (key WorkloadEndpointStatusKey) defaultDeletePath() (string, error) {
	return key.defaultPath()
}

func (key WorkloadEndpointStatusKey) defaultDeleteParentPaths() ([]string, error) {
	if key.Hostname == "" {
		return nil, errors.ErrorInsufficientIdentifiers{Name: "hostname"}
	}
	if key.OrchestratorID == "" {
		return nil, errors.ErrorInsufficientIdentifiers{Name: "orchestrator"}
	}
	if key.WorkloadID == "" {
		return nil, errors.ErrorInsufficientIdentifiers{Name: "workload"}
	}
	workload := fmt.Sprintf("/calico/felix/v1/host/%s/workload/%s/%s",
		key.Hostname, key.OrchestratorID, key.WorkloadID)
	endpoints := workload + "/endpoint"
	return []string{endpoints, workload}, nil
}

func (key WorkloadEndpointStatusKey) valueType() reflect.Type {
	return reflect.TypeOf(WorkloadEndpoint{})
}

func (key WorkloadEndpointStatusKey) String() string {
	return fmt.Sprintf("WorkloadEndpointStatus(hostname=%s, orchestrator=%s, workload=%s, name=%s)",
		key.Hostname, key.OrchestratorID, key.WorkloadID, key.EndpointID)
}

type WorkloadEndpointStatusListOptions struct {
	Hostname       string
	OrchestratorID string
	WorkloadID     string
	EndpointID     string
}

func (options WorkloadEndpointStatusListOptions) defaultPathRoot() string {
	k := "/calico/felix/v1/host"
	if options.Hostname == "" {
		return k
	}
	k = k + fmt.Sprintf("/%s/workload", options.Hostname)
	if options.OrchestratorID == "" {
		return k
	}
	k = k + fmt.Sprintf("/%s", options.OrchestratorID)
	if options.WorkloadID == "" {
		return k
	}
	k = k + fmt.Sprintf("/%s/endpoint", options.WorkloadID)
	if options.EndpointID == "" {
		return k
	}
	k = k + fmt.Sprintf("/%s", options.EndpointID)
	return k
}

func (options WorkloadEndpointStatusListOptions) KeyFromDefaultPath(ekey string) Key {
	log.Infof("Get WorkloadEndpoint key from %s", ekey)
	r := matchWorkloadEndpointStatus.FindAllStringSubmatch(ekey, -1)
	if len(r) != 1 {
		log.Infof("Didn't match regex")
		return nil
	}
	hostname := r[0][1]
	orchID := r[0][2]
	workloadID := r[0][3]
	endpointID := r[0][4]
	if options.Hostname != "" && hostname != options.Hostname {
		log.Infof("Didn't match hostname %s != %s", options.Hostname, hostname)
		return nil
	}
	if options.OrchestratorID != "" && orchID != options.OrchestratorID {
		log.Infof("Didn't match orchestrator %s != %s", options.OrchestratorID, orchID)
		return nil
	}
	if options.WorkloadID != "" && workloadID != options.WorkloadID {
		log.Infof("Didn't match workload %s != %s", options.WorkloadID, workloadID)
		return nil
	}
	if options.EndpointID != "" && endpointID != options.EndpointID {
		log.Infof("Didn't match endpoint ID %s != %s", options.EndpointID, endpointID)
		return nil
	}
	return WorkloadEndpointStatusKey{
		Hostname:       hostname,
		OrchestratorID: orchID,
		WorkloadID:     workloadID,
		EndpointID:     endpointID,
	}
}

type WorkloadEndpointStatus struct {
	Status string `json:"status"`
}
<|endoftext|>


<commit_before>package main

import (
	"bytes"
	"fmt"
	"html/template"
	"net/http"
	"net/url"
	"time"

	"appengine"
	"appengine/datastore"
	"appengine/delay"
	"appengine/mail"
	"appengine/urlfetch"

	"github.com/gorilla/mux"
	"github.com/gorilla/sessions"
	"github.com/nlopes/slack"
)

var router *mux.Router
var slackOAuthConfig OAuthConfig
var sessionStore *sessions.CookieStore
var sessionConfig SessionConfig
var styles map[string]template.CSS
var templates map[string]*Template

func init() {
	styles = loadStyles()
	templates = loadTemplates()
	sessionStore, sessionConfig = initSession()
	slackOAuthConfig = initSlackOAuthConfig()

	router = mux.NewRouter()
	router.Handle("/", AppHandler(indexHandler)).Name("index")

	router.Handle("/session/sign-in", AppHandler(signInHandler)).Name("sign-in").Methods("POST")
	router.Handle("/session/sign-out", AppHandler(signOutHandler)).Name("sign-out").Methods("POST")
	router.Handle("/slack/callback", AppHandler(slackOAuthCallbackHandler)).Name("slack-callback")

	router.Handle("/archive/send", SignedInAppHandler(sendArchiveHandler)).Name("send-archive").Methods("POST")
	router.Handle("/archive/cron", AppHandler(archiveCronHandler))
	router.Handle("/archive/conversation/send", SignedInAppHandler(sendConversationArchiveHandler)).Name("send-conversation-archive").Methods("POST")
	router.Handle("/archive/conversation/{type}/{ref}", SignedInAppHandler(conversationArchiveHandler)).Name("conversation-archive")

	http.Handle("/", router)
}

func indexHandler(w http.ResponseWriter, r *http.Request) *AppError {
	session, _ := sessionStore.Get(r, sessionConfig.CookieName)
	userId, ok := session.Values[sessionConfig.UserIdKey].(string)
	if !ok {
		data := map[string]interface{}{
			"ContinueUrl": r.FormValue("continue_url"),
		}
		return templates["index-signed-out"].Render(w, data)
	}
	c := appengine.NewContext(r)
	account, err := getAccount(c, userId)
	if account == nil {
		// Can't look up the account, session cookie must be invalid, clear it.
		session.Options.MaxAge = -1
		session.Save(r, w)
		return RedirectToRoute("index")
	}
	if err != nil {
		return InternalError(err, "Could not look up account")
	}

	slackClient := slack.New(account.ApiToken)

	user, err := slackClient.GetUserInfo(account.SlackUserId)
	if err != nil {
		return SlackFetchError(err, "user")
	}
	team, err := slackClient.GetTeamInfo()
	if err != nil {
		return SlackFetchError(err, "team")
	}
	conversations, err := getConversations(slackClient, account)
	if err != nil {
		return SlackFetchError(err, "conversations")
	}

	emailAddress, err := account.GetDigestEmailAddress(slackClient)
	if err != nil {
		return SlackFetchError(err, "emails")
	}

	var settingsSummary = map[string]interface{}{
		"EmailAddress": emailAddress,
	}
	var data = map[string]interface{}{
		"User":            user,
		"Team":            team,
		"Conversations":   conversations,
		"SettingsSummary": settingsSummary,
		"DetectTimezone":  !account.HasTimezoneSet,
	}
	return templates["index"].Render(w, data, &AppSignedInState{
		Account:        account,
		SlackClient:    slackClient,
		session:        session,
		responseWriter: w,
		request:        r,
	})
}

func signInHandler(w http.ResponseWriter, r *http.Request) *AppError {
	authCodeUrl, _ := url.Parse("https://slack.com/oauth/authorize")
	authCodeUrlQuery := authCodeUrl.Query()
	authCodeUrlQuery.Set("client_id", slackOAuthConfig.ClientId)
	authCodeUrlQuery.Set("scope",
		// Basic user info
		"users:read "+
			// Team info
			"team:read "+
			// Channel archive
			"channels:read channels:history "+
			// Private channel archive
			"groups:read groups:history "+
			// Direct message archive
			"im:read im:history "+
			// Multi-party direct mesage archive
			"mpim:read mpim:history")
	redirectUrlString, _ := AbsoluteRouteUrl("slack-callback")
	redirectUrl, _ := url.Parse(redirectUrlString)
	if continueUrl := r.FormValue("continue_url"); continueUrl != "" {
		redirectUrlQuery := redirectUrl.Query()
		redirectUrlQuery.Set("continue_url", continueUrl)
		redirectUrl.RawQuery = redirectUrlQuery.Encode()
	}
	authCodeUrlQuery.Set("redirect_uri", redirectUrl.String())
	authCodeUrl.RawQuery = authCodeUrlQuery.Encode()
	return RedirectToUrl(authCodeUrl.String())
}

func signOutHandler(w http.ResponseWriter, r *http.Request) *AppError {
	session, _ := sessionStore.Get(r, sessionConfig.CookieName)
	session.Options.MaxAge = -1
	session.Save(r, w)
	return RedirectToRoute("index")
}

func slackOAuthCallbackHandler(w http.ResponseWriter, r *http.Request) *AppError {
	code := r.FormValue("code")
	redirectUrl := AbsolutePathUrl(r.URL.Path)
	token, _, err := slack.GetOAuthToken(
		slackOAuthConfig.ClientId, slackOAuthConfig.ClientSecret, code,
		redirectUrl, false)
	if err != nil {
		return InternalError(err, "Could not exchange OAuth code")
	}

	slackClient := slack.New(token)
	authTest, err := slackClient.AuthTest()
	if err != nil {
		return SlackFetchError(err, "user")
	}

	if authTest.Team != "Partyslack" {
		return templates["team-not-on-whitelist"].Render(w, map[string]interface{}{})
	}

	c := appengine.NewContext(r)
	account, err := getAccount(c, authTest.UserID)
	if err != nil && err != datastore.ErrNoSuchEntity {
		return InternalError(err, "Could not look up user")
	}
	if account == nil {
		account = &Account{
			SlackUserId:   authTest.UserID,
			SlackTeamName: authTest.Team,
			SlackTeamUrl:  authTest.URL,
		}
	}
	account.ApiToken = token
	// Persist the default email address now, both to avoid additional lookups
	// later and to have a way to contact the user if they ever revoke their
	// OAuth token.
	emailAddress, err := account.GetDigestEmailAddress(slackClient)
	if err == nil && len(emailAddress) > 0 {
		account.DigestEmailAddress = emailAddress
	}
	err = account.Put(c)
	if err != nil {
		return InternalError(err, "Could not save user")
	}

	session, _ := sessionStore.Get(r, sessionConfig.CookieName)
	session.Values[sessionConfig.UserIdKey] = account.SlackUserId
	session.Save(r, w)
	continueUrl := r.FormValue("continue_url")
	if continueUrl != "" {
		continueUrlParsed, err := url.Parse(continueUrl)
		if err != nil || continueUrlParsed.Host != r.URL.Host {
			continueUrl = ""
		}
	}
	if continueUrl == "" {
		indexUrl, _ := router.Get("index").URL()
		continueUrl = indexUrl.String()
	}
	return RedirectToUrl(continueUrl)
}

func conversationArchiveHandler(w http.ResponseWriter, r *http.Request, state *AppSignedInState) *AppError {
	vars := mux.Vars(r)
	conversationType := vars["type"]
	ref := vars["ref"]
	conversation, err := getConversationFromRef(conversationType, ref, state.SlackClient)
	if err != nil {
		return SlackFetchError(err, "conversation")
	}

	archive, err := newConversationArchive(conversation, state.SlackClient, state.Account)
	if err != nil {
		return SlackFetchError(err, "archive")
	}

	var data = map[string]interface{}{
		"Conversation":        conversation,
		"ConversationType":    conversationType,
		"ConversationRef":     ref,
		"ConversationArchive": archive,
	}
	return templates["conversation-archive-page"].Render(w, data, state)
}

func sendArchiveHandler(w http.ResponseWriter, r *http.Request, state *AppSignedInState) *AppError {
	c := appengine.NewContext(r)
	sentCount, err := sendArchive(state.Account, c)
	if err != nil {
		return InternalError(err, "Could not send archive")
	}
	if sentCount > 0 {
		if sentCount == 1 {
			state.AddFlash("Emailed 1 archive!")
		} else {
			state.AddFlash(fmt.Sprintf("Emailed %d archives!", sentCount))
		}
	} else {
		state.AddFlash("No archives were sent, they were either all empty or disabled.")
	}
	return RedirectToRoute("index")
}

func archiveCronHandler(w http.ResponseWriter, r *http.Request) *AppError {
	c := appengine.NewContext(r)
	accounts, err := getAllAccounts(c)
	if err != nil {
		return InternalError(err, "Could not look up accounts")
	}
	for _, account := range accounts {
		c.Infof("Enqueing task for %s...", account.SlackUserId)
		sendArchiveFunc.Call(c, account.SlackUserId)
	}
	fmt.Fprint(w, "Done")
	return nil
}

var sendArchiveFunc = delay.Func(
	"sendArchive",
	func(c appengine.Context, slackUserId string) error {
		c.Infof("Sending digest for %s...", slackUserId)
		account, err := getAccount(c, slackUserId)
		if err != nil {
			c.Errorf("  Error looking up account: %s", err.Error())
			return err
		}
		// The Slack API uses the default HTTP transport, so we need to override
		// it to get it to work on App Engine. This is normally done for all
		// handlers, but since we're in a delay function that code has not run.
		appengineTransport := &urlfetch.Transport{Context: c}
		appengineTransport.Deadline = time.Second * 60
		http.DefaultTransport = &CachingTransport{
			Transport: appengineTransport,
			Context:   c,
		}
		sentCount, err := sendArchive(account, c)
		if err != nil {
			c.Errorf("  Error: %s", err.Error())
			if !appengine.IsDevAppServer() {
				sendArchiveErrorMail(err, c, slackUserId)
			}
		} else if sentCount > 0 {
			c.Infof(fmt.Sprintf("  Sent %d archives!", sentCount))
		} else {
			c.Infof("  Not sent, archive was empty")
		}
		return err
	})

func sendArchive(account *Account, c appengine.Context) (int, error) {
	slackClient := slack.New(account.ApiToken)
	conversations, err := getConversations(slackClient, account)
	if err != nil {
		return 0, err
	}
	sentCount := 0
	for _, conversation := range conversations.AllConversations {
		sent, err := sendConversationArchive(conversation, account, c)
		if err != nil {
			return sentCount, nil
		}
		if sent {
			sentCount++
		}
	}
	return sentCount, nil
}

func sendArchiveErrorMail(e error, c appengine.Context, slackUserId string) {
	errorMessage := &mail.Message{
		Sender:  "Slack Archive Admin <admin@slack-archive.appspotmail.com>",
		To:      []string{"mihai.parparita@gmail.com"},
		Subject: fmt.Sprintf("Slack Archive Send Error for %s", slackUserId),
		Body:    fmt.Sprintf("Error: %s", e),
	}
	err := mail.Send(c, errorMessage)
	if err != nil {
		c.Errorf("Error %s sending error email.", err.Error())
	}
}

func sendConversationArchiveHandler(w http.ResponseWriter, r *http.Request, state *AppSignedInState) *AppError {
	conversationType := r.FormValue("conversation_type")
	ref := r.FormValue("conversation_ref")
	conversation, err := getConversationFromRef(conversationType, ref, state.SlackClient)
	if err != nil {
		return SlackFetchError(err, "conversation")
	}
	c := appengine.NewContext(r)
	sent, err := sendConversationArchive(conversation, state.Account, c)
	if err != nil {
		return InternalError(err, "Could not send conversation archive")
	}
	if sent {
		state.AddFlash("Emailed archive!")
	} else {
		state.AddFlash("No archive was sent, it was empty or disabled.")
	}
	return RedirectToRoute("conversation-archive", "type", conversationType, "ref", ref)
}

func sendConversationArchive(conversation Conversation, account *Account, c appengine.Context) (bool, error) {
	slackClient := slack.New(account.ApiToken)
	emailAddress, err := account.GetDigestEmailAddress(slackClient)
	if err != nil {
		return false, err
	}
	if emailAddress == "disabled" {
		return false, nil
	}
	archive, err := newConversationArchive(conversation, slackClient, account)
	if err != nil {
		return false, err
	}
	if archive.Empty() {
		return false, nil
	}
	var data = map[string]interface{}{
		"ConversationArchive": archive,
	}
	var archiveHtml bytes.Buffer
	if err := templates["conversation-archive-email"].Execute(&archiveHtml, data); err != nil {
		return false, err
	}
	team, err := slackClient.GetTeamInfo()
	if err != nil {
		return false, err
	}
	sender := fmt.Sprintf(
		"%s Slack Archive <archive@slack-archive.appspotmail.com>", team.Name)
	archiveMessage := &mail.Message{
		Sender:   sender,
		To:       []string{emailAddress},
		Subject:  fmt.Sprintf("%s Archive", conversation.Name()),
		HTMLBody: archiveHtml.String(),
	}
	err = mail.Send(c, archiveMessage)
	return true, err
}
<commit_msg>Correctly report conversation send errors.<commit_after>package main

import (
	"bytes"
	"fmt"
	"html/template"
	"net/http"
	"net/url"
	"time"

	"appengine"
	"appengine/datastore"
	"appengine/delay"
	"appengine/mail"
	"appengine/urlfetch"

	"github.com/gorilla/mux"
	"github.com/gorilla/sessions"
	"github.com/nlopes/slack"
)

var router *mux.Router
var slackOAuthConfig OAuthConfig
var sessionStore *sessions.CookieStore
var sessionConfig SessionConfig
var styles map[string]template.CSS
var templates map[string]*Template

func init() {
	styles = loadStyles()
	templates = loadTemplates()
	sessionStore, sessionConfig = initSession()
	slackOAuthConfig = initSlackOAuthConfig()

	router = mux.NewRouter()
	router.Handle("/", AppHandler(indexHandler)).Name("index")

	router.Handle("/session/sign-in", AppHandler(signInHandler)).Name("sign-in").Methods("POST")
	router.Handle("/session/sign-out", AppHandler(signOutHandler)).Name("sign-out").Methods("POST")
	router.Handle("/slack/callback", AppHandler(slackOAuthCallbackHandler)).Name("slack-callback")

	router.Handle("/archive/send", SignedInAppHandler(sendArchiveHandler)).Name("send-archive").Methods("POST")
	router.Handle("/archive/cron", AppHandler(archiveCronHandler))
	router.Handle("/archive/conversation/send", SignedInAppHandler(sendConversationArchiveHandler)).Name("send-conversation-archive").Methods("POST")
	router.Handle("/archive/conversation/{type}/{ref}", SignedInAppHandler(conversationArchiveHandler)).Name("conversation-archive")

	http.Handle("/", router)
}

func indexHandler(w http.ResponseWriter, r *http.Request) *AppError {
	session, _ := sessionStore.Get(r, sessionConfig.CookieName)
	userId, ok := session.Values[sessionConfig.UserIdKey].(string)
	if !ok {
		data := map[string]interface{}{
			"ContinueUrl": r.FormValue("continue_url"),
		}
		return templates["index-signed-out"].Render(w, data)
	}
	c := appengine.NewContext(r)
	account, err := getAccount(c, userId)
	if account == nil {
		// Can't look up the account, session cookie must be invalid, clear it.
		session.Options.MaxAge = -1
		session.Save(r, w)
		return RedirectToRoute("index")
	}
	if err != nil {
		return InternalError(err, "Could not look up account")
	}

	slackClient := slack.New(account.ApiToken)

	user, err := slackClient.GetUserInfo(account.SlackUserId)
	if err != nil {
		return SlackFetchError(err, "user")
	}
	team, err := slackClient.GetTeamInfo()
	if err != nil {
		return SlackFetchError(err, "team")
	}
	conversations, err := getConversations(slackClient, account)
	if err != nil {
		return SlackFetchError(err, "conversations")
	}

	emailAddress, err := account.GetDigestEmailAddress(slackClient)
	if err != nil {
		return SlackFetchError(err, "emails")
	}

	var settingsSummary = map[string]interface{}{
		"EmailAddress": emailAddress,
	}
	var data = map[string]interface{}{
		"User":            user,
		"Team":            team,
		"Conversations":   conversations,
		"SettingsSummary": settingsSummary,
		"DetectTimezone":  !account.HasTimezoneSet,
	}
	return templates["index"].Render(w, data, &AppSignedInState{
		Account:        account,
		SlackClient:    slackClient,
		session:        session,
		responseWriter: w,
		request:        r,
	})
}

func signInHandler(w http.ResponseWriter, r *http.Request) *AppError {
	authCodeUrl, _ := url.Parse("https://slack.com/oauth/authorize")
	authCodeUrlQuery := authCodeUrl.Query()
	authCodeUrlQuery.Set("client_id", slackOAuthConfig.ClientId)
	authCodeUrlQuery.Set("scope",
		// Basic user info
		"users:read "+
			// Team info
			"team:read "+
			// Channel archive
			"channels:read channels:history "+
			// Private channel archive
			"groups:read groups:history "+
			// Direct message archive
			"im:read im:history "+
			// Multi-party direct mesage archive
			"mpim:read mpim:history")
	redirectUrlString, _ := AbsoluteRouteUrl("slack-callback")
	redirectUrl, _ := url.Parse(redirectUrlString)
	if continueUrl := r.FormValue("continue_url"); continueUrl != "" {
		redirectUrlQuery := redirectUrl.Query()
		redirectUrlQuery.Set("continue_url", continueUrl)
		redirectUrl.RawQuery = redirectUrlQuery.Encode()
	}
	authCodeUrlQuery.Set("redirect_uri", redirectUrl.String())
	authCodeUrl.RawQuery = authCodeUrlQuery.Encode()
	return RedirectToUrl(authCodeUrl.String())
}

func signOutHandler(w http.ResponseWriter, r *http.Request) *AppError {
	session, _ := sessionStore.Get(r, sessionConfig.CookieName)
	session.Options.MaxAge = -1
	session.Save(r, w)
	return RedirectToRoute("index")
}

func slackOAuthCallbackHandler(w http.ResponseWriter, r *http.Request) *AppError {
	code := r.FormValue("code")
	redirectUrl := AbsolutePathUrl(r.URL.Path)
	token, _, err := slack.GetOAuthToken(
		slackOAuthConfig.ClientId, slackOAuthConfig.ClientSecret, code,
		redirectUrl, false)
	if err != nil {
		return InternalError(err, "Could not exchange OAuth code")
	}

	slackClient := slack.New(token)
	authTest, err := slackClient.AuthTest()
	if err != nil {
		return SlackFetchError(err, "user")
	}

	if authTest.Team != "Partyslack" {
		return templates["team-not-on-whitelist"].Render(w, map[string]interface{}{})
	}

	c := appengine.NewContext(r)
	account, err := getAccount(c, authTest.UserID)
	if err != nil && err != datastore.ErrNoSuchEntity {
		return InternalError(err, "Could not look up user")
	}
	if account == nil {
		account = &Account{
			SlackUserId:   authTest.UserID,
			SlackTeamName: authTest.Team,
			SlackTeamUrl:  authTest.URL,
		}
	}
	account.ApiToken = token
	// Persist the default email address now, both to avoid additional lookups
	// later and to have a way to contact the user if they ever revoke their
	// OAuth token.
	emailAddress, err := account.GetDigestEmailAddress(slackClient)
	if err == nil && len(emailAddress) > 0 {
		account.DigestEmailAddress = emailAddress
	}
	err = account.Put(c)
	if err != nil {
		return InternalError(err, "Could not save user")
	}

	session, _ := sessionStore.Get(r, sessionConfig.CookieName)
	session.Values[sessionConfig.UserIdKey] = account.SlackUserId
	session.Save(r, w)
	continueUrl := r.FormValue("continue_url")
	if continueUrl != "" {
		continueUrlParsed, err := url.Parse(continueUrl)
		if err != nil || continueUrlParsed.Host != r.URL.Host {
			continueUrl = ""
		}
	}
	if continueUrl == "" {
		indexUrl, _ := router.Get("index").URL()
		continueUrl = indexUrl.String()
	}
	return RedirectToUrl(continueUrl)
}

func conversationArchiveHandler(w http.ResponseWriter, r *http.Request, state *AppSignedInState) *AppError {
	vars := mux.Vars(r)
	conversationType := vars["type"]
	ref := vars["ref"]
	conversation, err := getConversationFromRef(conversationType, ref, state.SlackClient)
	if err != nil {
		return SlackFetchError(err, "conversation")
	}

	archive, err := newConversationArchive(conversation, state.SlackClient, state.Account)
	if err != nil {
		return SlackFetchError(err, "archive")
	}

	var data = map[string]interface{}{
		"Conversation":        conversation,
		"ConversationType":    conversationType,
		"ConversationRef":     ref,
		"ConversationArchive": archive,
	}
	return templates["conversation-archive-page"].Render(w, data, state)
}

func sendArchiveHandler(w http.ResponseWriter, r *http.Request, state *AppSignedInState) *AppError {
	c := appengine.NewContext(r)
	sentCount, err := sendArchive(state.Account, c)
	if err != nil {
		return InternalError(err, "Could not send archive")
	}
	if sentCount > 0 {
		if sentCount == 1 {
			state.AddFlash("Emailed 1 archive!")
		} else {
			state.AddFlash(fmt.Sprintf("Emailed %d archives!", sentCount))
		}
	} else {
		state.AddFlash("No archives were sent, they were either all empty or disabled.")
	}
	return RedirectToRoute("index")
}

func archiveCronHandler(w http.ResponseWriter, r *http.Request) *AppError {
	c := appengine.NewContext(r)
	accounts, err := getAllAccounts(c)
	if err != nil {
		return InternalError(err, "Could not look up accounts")
	}
	for _, account := range accounts {
		c.Infof("Enqueing task for %s...", account.SlackUserId)
		sendArchiveFunc.Call(c, account.SlackUserId)
	}
	fmt.Fprint(w, "Done")
	return nil
}

var sendArchiveFunc = delay.Func(
	"sendArchive",
	func(c appengine.Context, slackUserId string) error {
		c.Infof("Sending digest for %s...", slackUserId)
		account, err := getAccount(c, slackUserId)
		if err != nil {
			c.Errorf("  Error looking up account: %s", err.Error())
			return err
		}
		// The Slack API uses the default HTTP transport, so we need to override
		// it to get it to work on App Engine. This is normally done for all
		// handlers, but since we're in a delay function that code has not run.
		appengineTransport := &urlfetch.Transport{Context: c}
		appengineTransport.Deadline = time.Second * 60
		http.DefaultTransport = &CachingTransport{
			Transport: appengineTransport,
			Context:   c,
		}
		sentCount, err := sendArchive(account, c)
		if err != nil {
			c.Errorf("  Error: %s", err.Error())
			if !appengine.IsDevAppServer() {
				sendArchiveErrorMail(err, c, slackUserId)
			}
		} else if sentCount > 0 {
			c.Infof(fmt.Sprintf("  Sent %d archives!", sentCount))
		} else {
			c.Infof("  Not sent, archive was empty")
		}
		return err
	})

func sendArchive(account *Account, c appengine.Context) (int, error) {
	slackClient := slack.New(account.ApiToken)
	conversations, err := getConversations(slackClient, account)
	if err != nil {
		return 0, err
	}
	sentCount := 0
	for _, conversation := range conversations.AllConversations {
		sent, err := sendConversationArchive(conversation, account, c)
		if err != nil {
			return sentCount, err
		}
		if sent {
			sentCount++
		}
	}
	return sentCount, nil
}

func sendArchiveErrorMail(e error, c appengine.Context, slackUserId string) {
	errorMessage := &mail.Message{
		Sender:  "Slack Archive Admin <admin@slack-archive.appspotmail.com>",
		To:      []string{"mihai.parparita@gmail.com"},
		Subject: fmt.Sprintf("Slack Archive Send Error for %s", slackUserId),
		Body:    fmt.Sprintf("Error: %s", e),
	}
	err := mail.Send(c, errorMessage)
	if err != nil {
		c.Errorf("Error %s sending error email.", err.Error())
	}
}

func sendConversationArchiveHandler(w http.ResponseWriter, r *http.Request, state *AppSignedInState) *AppError {
	conversationType := r.FormValue("conversation_type")
	ref := r.FormValue("conversation_ref")
	conversation, err := getConversationFromRef(conversationType, ref, state.SlackClient)
	if err != nil {
		return SlackFetchError(err, "conversation")
	}
	c := appengine.NewContext(r)
	sent, err := sendConversationArchive(conversation, state.Account, c)
	if err != nil {
		return InternalError(err, "Could not send conversation archive")
	}
	if sent {
		state.AddFlash("Emailed archive!")
	} else {
		state.AddFlash("No archive was sent, it was empty or disabled.")
	}
	return RedirectToRoute("conversation-archive", "type", conversationType, "ref", ref)
}

func sendConversationArchive(conversation Conversation, account *Account, c appengine.Context) (bool, error) {
	slackClient := slack.New(account.ApiToken)
	emailAddress, err := account.GetDigestEmailAddress(slackClient)
	if err != nil {
		return false, err
	}
	if emailAddress == "disabled" {
		return false, nil
	}
	archive, err := newConversationArchive(conversation, slackClient, account)
	if err != nil {
		return false, err
	}
	if archive.Empty() {
		return false, nil
	}
	var data = map[string]interface{}{
		"ConversationArchive": archive,
	}
	var archiveHtml bytes.Buffer
	if err := templates["conversation-archive-email"].Execute(&archiveHtml, data); err != nil {
		return false, err
	}
	team, err := slackClient.GetTeamInfo()
	if err != nil {
		return false, err
	}
	sender := fmt.Sprintf(
		"%s Slack Archive <archive@slack-archive.appspotmail.com>", team.Name)
	archiveMessage := &mail.Message{
		Sender:   sender,
		To:       []string{emailAddress},
		Subject:  fmt.Sprintf("%s Archive", conversation.Name()),
		HTMLBody: archiveHtml.String(),
	}
	err = mail.Send(c, archiveMessage)
	return true, err
}
<|endoftext|>


<commit_before>package completion

import (
	"context"
	"os"
	"path/filepath"
	"strings"

	"github.com/mitchellh/go-ps"
)

func completionSet(ctx context.Context, ua UncCompletion, params []string) ([]Element, error) {
	result := []Element{}
	base := strings.ToUpper(params[len(params)-1])
	for _, env1 := range os.Environ() {
		if strings.HasPrefix(strings.ToUpper(env1), base) {
			result = append(result, Element1(env1))
		}
	}
	return result, nil
}

func completionDir(ctx context.Context, ua UncCompletion, params []string) ([]Element, error) {
	return listUpDirs(ctx, ua, params[len(params)-1])
}

func completionCd(ctx context.Context, ua UncCompletion, params []string) ([]Element, error) {

	list, err := completionDir(ctx, ua, params)
	source := params[len(params)-1]
	if len(source) < 1 || source[0] == '.' || strings.ContainsAny(source, "/\\:") {
		return list, err
	}
	cdpath := os.Getenv("CDPATH")
	if cdpath == "" {
		return list, err
	}
	base := strings.ToUpper(source)
	for _, cdpath1 := range filepath.SplitList(cdpath) {
		if files, err := os.ReadDir(cdpath1); err == nil {
			for _, file1 := range files {
				if file1.IsDir() {
					name := strings.ToUpper(file1.Name())
					if strings.HasPrefix(name, base) {
						list = append(list, Element1(file1.Name()))
					}
				}
			}
		}
	}
	return list, nil
}

func completionEnv(ctx context.Context, ua UncCompletion, param []string) ([]Element, error) {
	eq := -1
	for i := 1; i < len(param); i++ {
		if strings.Contains(param[i], "=") {
			eq = i
		}
	}
	current := len(param) - 1

	if current == eq || current == 1 {
		return completionSet(ctx, ua, param)
	} else if current == eq+1 {
		return listUpCommands(ctx, param[current])
	} else {
		return nil, nil
	}
}

func completionWhich(ctx context.Context, ua UncCompletion, param []string) ([]Element, error) {
	if len(param) == 2 {
		return listUpCommands(ctx, param[len(param)-1])
	}
	return nil, nil
}

func completionProcessName(ctx context.Context, ua UncCompletion, param []string) ([]Element, error) {
	processes, err := ps.Processes()
	if err != nil {
		return nil, err
	}
	uniq := map[string]struct{}{}
	base := strings.ToUpper(param[len(param)-1])
	for _, ps1 := range processes {
		name := ps1.Executable()
		if strings.HasPrefix(strings.ToUpper(name), base) {
			uniq[name] = struct{}{}
		}
	}
	result := make([]Element, 0, len(uniq))
	for name := range uniq {
		result = append(result, Element1(name))
	}
	return result, nil
}

func completionTaskKill(ctx context.Context, ua UncCompletion, param []string) ([]Element, error) {
	if len(param) >= 3 && strings.EqualFold(param[len(param)-2], "/IM") {
		return completionProcessName(ctx, ua, param)
	}
	return nil, nil
}
<commit_msg>Fix: on cd completion with CDPATH, folders on current's case were changed<commit_after>package completion

import (
	"context"
	"os"
	"path/filepath"
	"strings"

	"github.com/mitchellh/go-ps"
)

func completionSet(ctx context.Context, ua UncCompletion, params []string) ([]Element, error) {
	result := []Element{}
	base := strings.ToUpper(params[len(params)-1])
	for _, env1 := range os.Environ() {
		if strings.HasPrefix(strings.ToUpper(env1), base) {
			result = append(result, Element1(env1))
		}
	}
	return result, nil
}

func completionDir(ctx context.Context, ua UncCompletion, params []string) ([]Element, error) {
	return listUpDirs(ctx, ua, params[len(params)-1])
}

func completionCd(ctx context.Context, ua UncCompletion, params []string) ([]Element, error) {

	list, err := completionDir(ctx, ua, params)
	source := params[len(params)-1]
	if len(source) < 1 || source[0] == '.' || strings.ContainsAny(source, "/\\:") {
		return list, err
	}
	cdpath := os.Getenv("CDPATH")
	if cdpath == "" {
		return list, err
	}
	duplicatedCheckTable := make(map[string]struct{})
	for _, element := range list {
		name := element.String()
		if name[len(name)-1] == '/' || name[len(name)-1] == '\\' {
			name = name[:len(name)-1]
		}
		duplicatedCheckTable[strings.ToUpper(name)] = struct{}{}
	}

	base := strings.ToUpper(source)
	for _, cdpath1 := range filepath.SplitList(cdpath) {
		if files, err := os.ReadDir(cdpath1); err == nil {
			for _, file1 := range files {
				if file1.IsDir() {
					name := strings.ToUpper(file1.Name())
					if strings.HasPrefix(name, base) {
						if _, ok := duplicatedCheckTable[name]; !ok {
							list = append(list, Element1(file1.Name()))
						}
					}
				}
			}
		}
	}
	return list, nil
}

func completionEnv(ctx context.Context, ua UncCompletion, param []string) ([]Element, error) {
	eq := -1
	for i := 1; i < len(param); i++ {
		if strings.Contains(param[i], "=") {
			eq = i
		}
	}
	current := len(param) - 1

	if current == eq || current == 1 {
		return completionSet(ctx, ua, param)
	} else if current == eq+1 {
		return listUpCommands(ctx, param[current])
	} else {
		return nil, nil
	}
}

func completionWhich(ctx context.Context, ua UncCompletion, param []string) ([]Element, error) {
	if len(param) == 2 {
		return listUpCommands(ctx, param[len(param)-1])
	}
	return nil, nil
}

func completionProcessName(ctx context.Context, ua UncCompletion, param []string) ([]Element, error) {
	processes, err := ps.Processes()
	if err != nil {
		return nil, err
	}
	uniq := map[string]struct{}{}
	base := strings.ToUpper(param[len(param)-1])
	for _, ps1 := range processes {
		name := ps1.Executable()
		if strings.HasPrefix(strings.ToUpper(name), base) {
			uniq[name] = struct{}{}
		}
	}
	result := make([]Element, 0, len(uniq))
	for name := range uniq {
		result = append(result, Element1(name))
	}
	return result, nil
}

func completionTaskKill(ctx context.Context, ua UncCompletion, param []string) ([]Element, error) {
	if len(param) >= 3 && strings.EqualFold(param[len(param)-2], "/IM") {
		return completionProcessName(ctx, ua, param)
	}
	return nil, nil
}
<|endoftext|>


<commit_before>package concrete

import (
	"github.com/gonum/graph"
	"math"
)

type DenseGraph struct {
	adjacencyMatrix []float64
	numNodes        int
}

func NewDenseGraph(numNodes int, passable bool) graph.Graph {
	dg := &DenseGraph{adjacencyMatrix: make([]float64, numNodes*numNodes), numNodes: numNodes}
	if passable {
		for i := range dg.adjacencyMatrix {
			dg.adjacencyMatrix[i] = 1.0
		}
	} else {
		for i := range dg.adjacencyMatrix {
			dg.adjacencyMatrix[i] = math.Inf(1)
		}
	}

	return dg
}

func (dg *DenseGraph) NodeExists(node graph.Node) bool {
	return node.ID() < dg.numNodes
}

func (dg *DenseGraph) Degree(node graph.Node) int {
	deg := 0
	for i := 0; i < dg.numNodes; i++ {
		if dg.adjacencyMatrix[i*dg.numNodes+node.ID()] != math.Inf(1) {
			deg++
		}

		if dg.adjacencyMatrix[node.ID()*dg.numNodes+i] != math.Inf(1) {
			deg++
		}
	}

	return deg
}

func (dg *DenseGraph) NodeList() []graph.Node {
	nodes := make([]graph.Node, dg.numNodes)
	for i := 0; i < dg.numNodes; i++ {
		nodes[i] = GonumNode(i)
	}

	return nodes
}

func (dg *DenseGraph) EdgeList() []graph.Edge {
	edges := make([]graph.Edge, 0, len(dg.adjacencyMatrix))
	for i := 0; i < dg.numNodes; i++ {
		for j := 0; j < dg.numNodes; j++ {
			if dg.adjacencyMatrix[i*dg.numNodes+j] != math.Inf(1) {
				edges = append(edges, GonumEdge{GonumNode(i), GonumNode(j)})
			}
		}
	}

	return edges
}

func (dg *DenseGraph) Neighbors(node graph.Node) []graph.Node {
	neighbors := make([]graph.Node, 0)
	for i := 0; i < dg.numNodes; i++ {
		if dg.adjacencyMatrix[i*dg.numNodes+node.ID()] != math.Inf(1) ||
			dg.adjacencyMatrix[node.ID()*dg.numNodes+i] != math.Inf(1) {
			neighbors = append(neighbors, GonumNode(i))
		}
	}

	return neighbors
}

func (dg *DenseGraph) IsNeighbor(node, neighbor graph.Node) bool {
	return dg.adjacencyMatrix[neighbor.ID()*dg.numNodes+node.ID()] != math.Inf(1) ||
		dg.adjacencyMatrix[node.ID()*dg.numNodes+neighbor.ID()] != math.Inf(1)
}

func (dg *DenseGraph) Successors(node graph.Node) []graph.Node {
	neighbors := make([]graph.Node, 0)
	for i := 0; i < dg.numNodes; i++ {
		if dg.adjacencyMatrix[node.ID()*dg.numNodes+i] != math.Inf(1) {
			neighbors = append(neighbors, GonumNode(i))
		}
	}

	return neighbors
}

func (dg *DenseGraph) IsSuccessor(node, succ graph.Node) bool {
	return dg.adjacencyMatrix[node.ID()*dg.numNodes+succ.ID()] != math.Inf(1)
}

func (dg *DenseGraph) Predecessors(node graph.Node) []graph.Node {
	neighbors := make([]graph.Node, 0)
	for i := 0; i < dg.numNodes; i++ {
		if dg.adjacencyMatrix[i*dg.numNodes+node.ID()] != math.Inf(1) {
			neighbors = append(neighbors, GonumNode(i))
		}
	}

	return neighbors
}

func (dg *DenseGraph) IsPredecessor(node, pred graph.Node) bool {
	return dg.adjacencyMatrix[pred.ID()*dg.numNodes+node.ID()] != math.Inf(1)
}

// Naturally dense, we don't need to do anything
func (dg *DenseGraph) Crunch() {
}

func (dg *DenseGraph) SetEdgeCost(node, succ graph.Node, cost float64, directed bool) {
	dg.adjacencyMatrix[node.ID()*dg.numNodes+succ.ID()] = cost
	if !directed {
		dg.adjacencyMatrix[succ.ID()*dg.numNodes+node.ID()] = cost
	}
}

<<<<<<< HEAD
// More or less equivalent to SetEdgeCost(node, succ, math.Inf(1), directed)
=======
>>>>>>> Made a basic dense graph
func (dg *DenseGraph) RemoveEdge(node, succ graph.Node, directed bool) {
	dg.adjacencyMatrix[node.ID()*dg.numNodes+succ.ID()] = math.Inf(1)
	if !directed {
		dg.adjacencyMatrix[succ.ID()*dg.numNodes+node.ID()] = math.Inf(1)
	}
}
<commit_msg>Made dense graph have DirectedEdgeList instead of EdgeList<commit_after>package concrete

import (
	"github.com/gonum/graph"
	"math"
)

type DenseGraph struct {
	adjacencyMatrix []float64
	numNodes        int
}

func NewDenseGraph(numNodes int, passable bool) graph.Graph {
	dg := &DenseGraph{adjacencyMatrix: make([]float64, numNodes*numNodes), numNodes: numNodes}
	if passable {
		for i := range dg.adjacencyMatrix {
			dg.adjacencyMatrix[i] = 1.0
		}
	} else {
		for i := range dg.adjacencyMatrix {
			dg.adjacencyMatrix[i] = math.Inf(1)
		}
	}

	return dg
}

func (dg *DenseGraph) NodeExists(node graph.Node) bool {
	return node.ID() < dg.numNodes
}

func (dg *DenseGraph) Degree(node graph.Node) int {
	deg := 0
	for i := 0; i < dg.numNodes; i++ {
		if dg.adjacencyMatrix[i*dg.numNodes+node.ID()] != math.Inf(1) {
			deg++
		}

		if dg.adjacencyMatrix[node.ID()*dg.numNodes+i] != math.Inf(1) {
			deg++
		}
	}

	return deg
}

func (dg *DenseGraph) NodeList() []graph.Node {
	nodes := make([]graph.Node, dg.numNodes)
	for i := 0; i < dg.numNodes; i++ {
		nodes[i] = GonumNode(i)
	}

	return nodes
}

func (dg *DenseGraph) DirectedEdgeList() []graph.Edge {
	edges := make([]graph.Edge, 0, len(dg.adjacencyMatrix))
	for i := 0; i < dg.numNodes; i++ {
		for j := 0; j < dg.numNodes; j++ {
			if dg.adjacencyMatrix[i*dg.numNodes+j] != math.Inf(1) {
				edges = append(edges, GonumEdge{GonumNode(i), GonumNode(j)})
			}
		}
	}

	return edges
}

func (dg *DenseGraph) Neighbors(node graph.Node) []graph.Node {
	neighbors := make([]graph.Node, 0)
	for i := 0; i < dg.numNodes; i++ {
		if dg.adjacencyMatrix[i*dg.numNodes+node.ID()] != math.Inf(1) ||
			dg.adjacencyMatrix[node.ID()*dg.numNodes+i] != math.Inf(1) {
			neighbors = append(neighbors, GonumNode(i))
		}
	}

	return neighbors
}

func (dg *DenseGraph) IsNeighbor(node, neighbor graph.Node) bool {
	return dg.adjacencyMatrix[neighbor.ID()*dg.numNodes+node.ID()] != math.Inf(1) ||
		dg.adjacencyMatrix[node.ID()*dg.numNodes+neighbor.ID()] != math.Inf(1)
}

func (dg *DenseGraph) Successors(node graph.Node) []graph.Node {
	neighbors := make([]graph.Node, 0)
	for i := 0; i < dg.numNodes; i++ {
		if dg.adjacencyMatrix[node.ID()*dg.numNodes+i] != math.Inf(1) {
			neighbors = append(neighbors, GonumNode(i))
		}
	}

	return neighbors
}

func (dg *DenseGraph) IsSuccessor(node, succ graph.Node) bool {
	return dg.adjacencyMatrix[node.ID()*dg.numNodes+succ.ID()] != math.Inf(1)
}

func (dg *DenseGraph) Predecessors(node graph.Node) []graph.Node {
	neighbors := make([]graph.Node, 0)
	for i := 0; i < dg.numNodes; i++ {
		if dg.adjacencyMatrix[i*dg.numNodes+node.ID()] != math.Inf(1) {
			neighbors = append(neighbors, GonumNode(i))
		}
	}

	return neighbors
}

func (dg *DenseGraph) IsPredecessor(node, pred graph.Node) bool {
	return dg.adjacencyMatrix[pred.ID()*dg.numNodes+node.ID()] != math.Inf(1)
}

// Naturally dense, we don't need to do anything
func (dg *DenseGraph) Crunch() {
}

func (dg *DenseGraph) SetEdgeCost(node, succ graph.Node, cost float64, directed bool) {
	dg.adjacencyMatrix[node.ID()*dg.numNodes+succ.ID()] = cost
	if !directed {
		dg.adjacencyMatrix[succ.ID()*dg.numNodes+node.ID()] = cost
	}
}

<<<<<<< HEAD
// More or less equivalent to SetEdgeCost(node, succ, math.Inf(1), directed)
=======
>>>>>>> Made a basic dense graph
func (dg *DenseGraph) RemoveEdge(node, succ graph.Node, directed bool) {
	dg.adjacencyMatrix[node.ID()*dg.numNodes+succ.ID()] = math.Inf(1)
	if !directed {
		dg.adjacencyMatrix[succ.ID()*dg.numNodes+node.ID()] = math.Inf(1)
	}
}
<|endoftext|>