<commit_before>/*
Copyright 2017 Mirantis

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 libvirttools

import (
	"crypto/sha1"
	"fmt"
	"io"
	"net/url"
	"os"
	"strings"

	"github.com/Mirantis/virtlet/pkg/utils"
	libvirt "github.com/libvirt/libvirt-go"
)

const (
	defaultDownloadProtocol = "https"
)

func stripTagFromImageName(imageName string) string {
	return strings.Split(imageName, ":")[0]
}

func ImageNameToVolumeName(imageName string) (string, error) {
	u, err := url.Parse(stripTagFromImageName(imageName))
	if err != nil {
		return "", err
	}

	h := sha1.New()
	io.WriteString(h, u.Path)

	segments := strings.Split(u.Path, "/")

	volumeName := fmt.Sprintf("%x_%s", h.Sum(nil), segments[len(segments)-1])

	return volumeName, nil
}

type ImageTool struct {
	protocol string
	tool     *StorageTool
}

func NewImageTool(conn *libvirt.Connect, poolName, protocol string) (*ImageTool, error) {
	storageTool, err := NewStorageTool(conn, poolName, "")
	if err != nil {
		return nil, err
	}
	if protocol == "" {
		protocol = defaultDownloadProtocol
	}
	return &ImageTool{tool: storageTool, protocol: protocol}, nil
}

func (i *ImageTool) ListLibvirtVolumesAsVolumeInfos() ([]*VolumeInfo, error) {
	return i.tool.ListVolumes()
}

func (i *ImageTool) ImageAsVolumeInfo(volumeName string) (*VolumeInfo, error) {
	vol, err := i.tool.LookupVolume(volumeName)
	if err != nil {<commit_msg>images: Use whole url instead of path for hash<commit_after>/*
Copyright 2017 Mirantis

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 libvirttools

import (
	"crypto/sha1"
	"fmt"
	"io"
	"net/url"
	"os"
	"strings"

	"github.com/Mirantis/virtlet/pkg/utils"
	libvirt "github.com/libvirt/libvirt-go"
)

const (
	defaultDownloadProtocol = "https"
)

func stripTagFromImageName(imageName string) string {
	return strings.Split(imageName, ":")[0]
}

func ImageNameToVolumeName(imageName string) (string, error) {
	u, err := url.Parse(stripTagFromImageName(imageName))
	if err != nil {
		return "", err
	}

	h := sha1.New()
	io.WriteString(h, u.String())

	segments := strings.Split(u.Path, "/")

	volumeName := fmt.Sprintf("%x_%s", h.Sum(nil), segments[len(segments)-1])

	return volumeName, nil
}

type ImageTool struct {
	protocol string
	tool     *StorageTool
}

func NewImageTool(conn *libvirt.Connect, poolName, protocol string) (*ImageTool, error) {
	storageTool, err := NewStorageTool(conn, poolName, "")
	if err != nil {
		return nil, err
	}
	if protocol == "" {
		protocol = defaultDownloadProtocol
	}
	return &ImageTool{tool: storageTool, protocol: protocol}, nil
}

func (i *ImageTool) ListLibvirtVolumesAsVolumeInfos() ([]*VolumeInfo, error) {
	return i.tool.ListVolumes()
}

func (i *ImageTool) ImageAsVolumeInfo(volumeName string) (*VolumeInfo, error) {
	vol, err := i.tool.LookupVolume(volumeName)
	if err != nil {
{'fraction_non_alphanumeric': 0.11697913916028518, 'fraction_numerical': 0.006337470293108001, 'mean_word_length': 4.748103186646434, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3188042', 'n_tokens_mistral': 1246, 'n_tokens_neox': 1203, 'n_words': 380}