<commit_before>package libvirt

import (
	"crypto/md5"
	"encoding/hex"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"strings"

	"github.com/gregjones/httpcache"
	"github.com/gregjones/httpcache/diskcache"
	"github.com/sirupsen/logrus"
	"golang.org/x/sys/unix"
)

// UseCachedImage leaves non-file:// image URIs unalterered.
// Other URIs are retrieved with a local cache at
// $XDG_CACHE_HOME/openshift-install/libvirt [1].  This allows you to
// use the same remote image URI multiple times without needing to
// worry about redundant downloads, although you will want to
// periodically blow away your cache.
//
// [1]: https://standards.freedesktop.org/basedir-spec/basedir-spec-0.7.html
func (libvirt *Libvirt) UseCachedImage() (err error) {
	if strings.HasPrefix(libvirt.Image, "file://") {
		return nil
	}

	logrus.Infof("Fetching OS image: %s", filepath.Base(libvirt.Image))

	// FIXME: Use os.UserCacheDir() once we bump to Go 1.11
	// baseCacheDir, err := os.UserCacheDir()
	// if err != nil {
	// 	return err
	// }
	baseCacheDir := filepath.Join(os.Getenv("HOME"), ".cache")

	cacheDir := filepath.Join(baseCacheDir, "openshift-install", "libvirt")
	httpCacheDir := filepath.Join(cacheDir, "http")
	err = os.MkdirAll(httpCacheDir, 0777)
	if err != nil {
		return err
	}

	cache := diskcache.New(httpCacheDir)
	transport := httpcache.NewTransport(cache)
	resp, err := transport.Client().Get(libvirt.Image)
	if err != nil {<commit_msg>tfvars/libvirt: fix installer keeping RHCOS OS Image in memory<commit_after>package libvirt

import (
	"crypto/md5"
	"encoding/hex"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"strings"

	"github.com/gregjones/httpcache"
	"github.com/gregjones/httpcache/diskcache"
	"github.com/peterbourgon/diskv"
	"github.com/sirupsen/logrus"
	"golang.org/x/sys/unix"
)

// UseCachedImage leaves non-file:// image URIs unalterered.
// Other URIs are retrieved with a local cache at
// $XDG_CACHE_HOME/openshift-install/libvirt [1].  This allows you to
// use the same remote image URI multiple times without needing to
// worry about redundant downloads, although you will want to
// periodically blow away your cache.
//
// [1]: https://standards.freedesktop.org/basedir-spec/basedir-spec-0.7.html
func (libvirt *Libvirt) UseCachedImage() (err error) {
	if strings.HasPrefix(libvirt.Image, "file://") {
		return nil
	}

	logrus.Infof("Fetching OS image: %s", filepath.Base(libvirt.Image))

	// FIXME: Use os.UserCacheDir() once we bump to Go 1.11
	// baseCacheDir, err := os.UserCacheDir()
	// if err != nil {
	// 	return err
	// }
	baseCacheDir := filepath.Join(os.Getenv("HOME"), ".cache")

	cacheDir := filepath.Join(baseCacheDir, "openshift-install", "libvirt")
	httpCacheDir := filepath.Join(cacheDir, "http")
	err = os.MkdirAll(httpCacheDir, 0777)
	if err != nil {
		return err
	}

	cache := diskcache.NewWithDiskv(diskv.New(diskv.Options{
		BasePath:     httpCacheDir,
		CacheSizeMax: 0, // This stops the diskcache from caching the resp in memory.
	}))
	transport := httpcache.NewTransport(cache)
	resp, err := transport.Client().Get(libvirt.Image)
	if err != nil {
{'fraction_non_alphanumeric': 0.14382896015549076, 'fraction_numerical': 0.008098477486232589, 'mean_word_length': 4.054009819967267, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '940112', 'n_tokens_mistral': 1150, 'n_tokens_neox': 1099, 'n_words': 269}