/* GoCheese -- Python private package repository and caching proxy Copyright (C) 2019 Sergey Matveev This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package main import ( "bytes" "crypto/sha256" "fmt" "io/ioutil" "log" "os" "path/filepath" "strings" ) func goodIntegrity() bool { dirs, err := ioutil.ReadDir(*root) if err != nil { log.Fatal(err) } hasher := sha256.New() digest := make([]byte, sha256.Size) isGood := true var data []byte var pkgName string for _, dir := range dirs { files, err := ioutil.ReadDir(filepath.Join(*root, dir.Name())) if err != nil { log.Fatal(err) } for _, file := range files { if !strings.HasSuffix(file.Name(), "."+HashAlgoSHA256) { continue } pkgName = strings.TrimSuffix(file.Name(), "."+HashAlgoSHA256) data, err = ioutil.ReadFile(filepath.Join(*root, dir.Name(), pkgName)) if err != nil { if os.IsNotExist(err) { continue } log.Fatal(err) } hasher.Write(data) data, err = ioutil.ReadFile(filepath.Join(*root, dir.Name(), file.Name())) if err != nil { log.Fatal(err) } if bytes.Compare(hasher.Sum(digest[:0]), data) == 0 { fmt.Println(pkgName, "GOOD") } else { isGood = false fmt.Println(pkgName, "BAD") } hasher.Reset() } } return isGood }