<commit_before>package tuf

import (
	"bytes"
	"encoding/json"
	"fmt"
	//"io"
	"path"
	"strings"
	"time"

	cjson "github.com/tent/canonical-json-go"

	"github.com/endophage/go-tuf/data"
	"github.com/endophage/go-tuf/errors"
	"github.com/endophage/go-tuf/keys"
	"github.com/endophage/go-tuf/signed"
	"github.com/endophage/go-tuf/store"
	"github.com/endophage/go-tuf/util"
)

type CompressionType uint8

const (
	CompressionTypeNone CompressionType = iota
	CompressionTypeGzip
)

// topLevelManifests determines the order signatures are verified when committing.
var topLevelManifests = []string{
	"root.json",
	"targets.json",
	"snapshot.json",
	"timestamp.json",
}

// snapshotManifests is the list of default filenames that should be included in the
// snapshots.json. If using delegated targets, additional, dynamic files should also
// be included in snapshots.
var snapshotManifests = []string{
	"root.json",
	"targets.json",
}

// Repo represents an instance of a TUF repo
type Repo struct {
	trust          *signed.Signer
	local          store.LocalStore
	hashAlgorithms []string
	meta           map[string]json.RawMessage
}

// NewRepo is a factory function for instantiating new TUF repos objects.
// If the local store is already populated, local.GetMeta() will initialise
// the Repo with the appropriate state.
func NewRepo(trust signed.TrustService, local store.LocalStore, hashAlgorithms ...string) (*Repo, error) {
	r := &Repo{trust: signed.NewSigner(trust), local: local, hashAlgorithms: hashAlgorithms}

	var err error
	r.meta, err = local.GetMeta()
	if err != nil {
		return nil, err
	}
	return r, nil
}

// Init attempts to initialize a brand new TUF repo. It will fail if
// an existing targets file is detected.
func (r *Repo) Init(consistentSnapshot bool) error {
	t, err := r.targets()
	if err != nil {
		return err
	}
	if len(t.Targets) > 0 {
		return errors.ErrInitNotAllowed
	}
	root := data.NewRoot()
	root.ConsistentSnapshot = consistentSnapshot
	err = r.setMeta("root.json", root)
	if err != nil {
		return err
	}
	t.Expires = data.DefaultExpires("targets").Round(time.Second)
	t.Version++
	err = r.setMeta("targets.json", t)
	if err != nil {
		return err
	}
	err = r.Snapshot(CompressionTypeNone)
	if err != nil {
		return err
	}
	err = r.Timestamp()
	if err != nil {
		return err
	}
	return nil
}

func (r *Repo) db() (*keys.DB, error) {
	db := keys.NewDB()<commit_msg>more init fixes<commit_after>package tuf

import (
	"bytes"
	"encoding/json"
	"fmt"
	//"io"
	"path"
	"strings"
	"time"

	cjson "github.com/tent/canonical-json-go"

	"github.com/endophage/go-tuf/data"
	"github.com/endophage/go-tuf/errors"
	"github.com/endophage/go-tuf/keys"
	"github.com/endophage/go-tuf/signed"
	"github.com/endophage/go-tuf/store"
	"github.com/endophage/go-tuf/util"
)

type CompressionType uint8

const (
	CompressionTypeNone CompressionType = iota
	CompressionTypeGzip
)

// topLevelManifests determines the order signatures are verified when committing.
var topLevelManifests = []string{
	"root.json",
	"targets.json",
	"snapshot.json",
	"timestamp.json",
}

// snapshotManifests is the list of default filenames that should be included in the
// snapshots.json. If using delegated targets, additional, dynamic files should also
// be included in snapshots.
var snapshotManifests = []string{
	"root.json",
	"targets.json",
}

// Repo represents an instance of a TUF repo
type Repo struct {
	trust          *signed.Signer
	local          store.LocalStore
	hashAlgorithms []string
	meta           map[string]json.RawMessage
}

// NewRepo is a factory function for instantiating new TUF repos objects.
// If the local store is already populated, local.GetMeta() will initialise
// the Repo with the appropriate state.
func NewRepo(trust signed.TrustService, local store.LocalStore, hashAlgorithms ...string) (*Repo, error) {
	r := &Repo{trust: signed.NewSigner(trust), local: local, hashAlgorithms: hashAlgorithms}

	var err error
	r.meta, err = local.GetMeta()
	if err != nil {
		return nil, err
	}
	return r, nil
}

// Init attempts to initialize a brand new TUF repo. It will fail if
// an existing targets file is detected.
func (r *Repo) Init(consistentSnapshot bool) error {
	t, err := r.targets()
	if err != nil {
		return err
	}
	if len(t.Targets) > 0 {
		return errors.ErrInitNotAllowed
	}
	root := data.NewRoot()
	root.ConsistentSnapshot = consistentSnapshot
	err = r.setMeta("root.json", root)
	if err != nil {
		return err
	}
	_, err = r.GenKey("root")
	if err != nil {
		return err
	}
	_, err = r.GenKey("targets")
	if err != nil {
		return err
	}
	_, err = r.GenKey("snapshot")
	if err != nil {
		return err
	}
	_, err = r.GenKey("timestamp")
	if err != nil {
		return err
	}
	t.Expires = data.DefaultExpires("targets").Round(time.Second)
	t.Version++
	err = r.setMeta("targets.json", t)
	if err != nil {
		return err
	}
	err = r.Snapshot(CompressionTypeNone)
	if err != nil {
		return err
	}
	err = r.Timestamp()
	if err != nil {
		return err
	}
	return nil
}

func (r *Repo) db() (*keys.DB, error) {
	db := keys.NewDB()
{'fraction_non_alphanumeric': 0.12470308788598575, 'fraction_numerical': 0.000791765637371338, 'mean_word_length': 3.6657433056325024, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5332757', 'n_tokens_mistral': 1827, 'n_tokens_neox': 1769, 'n_words': 501}