]> Cypherpunks.ru repositories - gocheese.git/blob - integrity.go
f6f39e6d5749baff977e93cfa5460cb7c4be7ec3
[gocheese.git] / integrity.go
1 /*
2 GoCheese -- Python private package repository and caching proxy
3 Copyright (C) 2019-2020 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package main
19
20 import (
21         "bytes"
22         "crypto/sha256"
23         "fmt"
24         "io/ioutil"
25         "log"
26         "os"
27         "path/filepath"
28         "strings"
29 )
30
31 func goodIntegrity() bool {
32         dirs, err := ioutil.ReadDir(*root)
33         if err != nil {
34                 log.Fatal(err)
35         }
36         hasher := sha256.New()
37         digest := make([]byte, sha256.Size)
38         isGood := true
39         var data []byte
40         var pkgName string
41         for _, dir := range dirs {
42                 files, err := ioutil.ReadDir(filepath.Join(*root, dir.Name()))
43                 if err != nil {
44                         log.Fatal(err)
45                 }
46                 for _, file := range files {
47                         if !strings.HasSuffix(file.Name(), "."+HashAlgoSHA256) {
48                                 continue
49                         }
50                         pkgName = strings.TrimSuffix(file.Name(), "."+HashAlgoSHA256)
51                         data, err = ioutil.ReadFile(filepath.Join(*root, dir.Name(), pkgName))
52                         if err != nil {
53                                 if os.IsNotExist(err) {
54                                         continue
55                                 }
56                                 log.Fatal(err)
57                         }
58                         hasher.Write(data)
59                         data, err = ioutil.ReadFile(filepath.Join(*root, dir.Name(), file.Name()))
60                         if err != nil {
61                                 log.Fatal(err)
62                         }
63                         if bytes.Compare(hasher.Sum(digest[:0]), data) == 0 {
64                                 fmt.Println("GOOD", pkgName)
65                         } else {
66                                 isGood = false
67                                 fmt.Println("BAD", pkgName)
68                         }
69                         hasher.Reset()
70                 }
71         }
72         return isGood
73 }