Add no-internal-hashes mode

This commit is contained in:
comp500
2021-05-04 19:56:56 +01:00
parent 3462dd7f21
commit 49d7b26b2e
6 changed files with 51 additions and 26 deletions

View File

@@ -28,7 +28,7 @@ type Index struct {
type IndexFile struct {
// Files are stored in forward-slash format relative to the index file
File string `toml:"file"`
Hash string `toml:"hash"`
Hash string `toml:"hash,omitempty"`
HashFormat string `toml:"hash-format,omitempty"`
Alias string `toml:"alias,omitempty"`
MetaFile bool `toml:"metafile,omitempty"` // True when it is a .toml metadata file
@@ -121,28 +121,33 @@ func (in *Index) updateFileHashGiven(path, format, hash string, mod bool) error
// updateFile calculates the hash for a given path and updates it in the index
func (in *Index) updateFile(path string) error {
f, err := os.Open(path)
if err != nil {
return err
}
var hashString string
if viper.GetBool("no-internal-hashes") {
hashString = ""
} else {
f, err := os.Open(path)
if err != nil {
return err
}
// Hash usage strategy (may change):
// Just use SHA256, overwrite existing hash regardless of what it is
// May update later to continue using the same hash that was already being used
h, err := GetHashImpl("sha256")
if err != nil {
_ = f.Close()
return err
// Hash usage strategy (may change):
// Just use SHA256, overwrite existing hash regardless of what it is
// May update later to continue using the same hash that was already being used
h, err := GetHashImpl("sha256")
if err != nil {
_ = f.Close()
return err
}
if _, err := io.Copy(h, f); err != nil {
_ = f.Close()
return err
}
err = f.Close()
if err != nil {
return err
}
hashString = hex.EncodeToString(h.Sum(nil))
}
if _, err := io.Copy(h, f); err != nil {
_ = f.Close()
return err
}
err = f.Close()
if err != nil {
return err
}
hashString := hex.EncodeToString(h.Sum(nil))
mod := false
// If the file has an extension of toml and is in the mods folder, set mod to true
@@ -290,6 +295,9 @@ func (in Index) Write() error {
// RefreshFileWithHash updates a file in the index, given a file hash and whether it is a mod or not
func (in *Index) RefreshFileWithHash(path, format, hash string, mod bool) error {
if viper.GetBool("no-internal-hashes") {
hash = ""
}
err := in.updateFileHashGiven(path, format, hash, mod)
if err != nil {
return err
@@ -351,7 +359,7 @@ func (in Index) SaveFile(f IndexFile, dest io.Writer) error {
}
calculatedHash := hex.EncodeToString(h.Sum(nil))
if calculatedHash != f.Hash {
if calculatedHash != f.Hash && !viper.GetBool("no-internal-hashes") {
return errors.New("hash of saved file is invalid")
}

View File

@@ -20,7 +20,7 @@ type Pack struct {
// Path is stored in forward slash format relative to pack.toml
File string `toml:"file"`
HashFormat string `toml:"hash-format"`
Hash string `toml:"hash"`
Hash string `toml:"hash,omitempty"`
} `toml:"index"`
Versions map[string]string `toml:"versions"`
Client map[string]toml.Primitive `toml:"client"`
@@ -61,6 +61,12 @@ func (pack Pack) LoadIndex() (Index, error) {
// UpdateIndexHash recalculates the hash of the index file of this modpack
func (pack *Pack) UpdateIndexHash() error {
if viper.GetBool("no-internal-hashes") {
pack.Index.HashFormat = "sha256"
pack.Index.Hash = ""
return nil
}
fileNative := filepath.FromSlash(pack.Index.File)
indexFile := filepath.Join(filepath.Dir(viper.GetString("pack-file")), fileNative)
if filepath.IsAbs(pack.Index.File) {