/
manager.go
169 lines (137 loc) · 3.77 KB
/
manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package manager
import (
"encoding/json"
"hash/crc32"
"io/ioutil"
"os"
lg "../logger"
ps "../parser"
)
//Entity Data Structure for holding the file name and hash of an entity
type Entity struct {
File string `json:"file"`
Hash uint32 `json:"hash"`
}
//Parent Data Structure to hold multiple Entity elements
type Parent struct {
Body struct {
Entity []Entity `json:"entity"`
} `json:"body"`
}
//Manager Data Structure to hold and operate on details.json
type Manager struct {
FileData string
NewFileTimings map[string]uint32
OldFileTimings map[string]uint32
FileList map[string]string
HashJSONold Parent
HashJSONnew Parent
Logger *lg.Logger
}
//ReadDetails Reading from the details.json file
func (man *Manager) ReadDetails() error {
jsonFile, err := os.Open("Cooking/details.json")
defer jsonFile.Close()
bytes, _ := ioutil.ReadAll(jsonFile)
err = json.Unmarshal(bytes, &man.HashJSONold)
if err != nil {
//Someone has tampered with the JSON file
os.Remove("Cooking/details.json")
man.Logger.ReportError(err.Error())
return err
}
for _, item := range man.HashJSONold.Body.Entity {
man.OldFileTimings[item.File] = item.Hash
}
man.Logger.ReportSuccess("Successfully read details.json")
return nil
}
//WriteDetails Writing the new data onto details.json
func (man *Manager) WriteDetails() error {
jsonData, err := json.MarshalIndent(man.HashJSONnew, "", " ")
if err != nil {
man.Logger.ReportError(err.Error())
return err
}
file, err := os.OpenFile("Cooking/details.json",
os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
defer file.Close()
if err != nil {
man.Logger.ReportError(err.Error())
return err
}
_, err = file.Write(jsonData)
if err != nil {
man.Logger.ReportError(err.Error())
return err
}
man.Logger.ReportSuccess("Successfully written to details.json")
return nil
}
//GenerateFileList Generating the files list which we are going to compile
func (man *Manager) GenerateFileList(par ps.Parser, tag string) error {
details := par.FileDetails[tag]
_, err := os.Stat(details.File)
if err != nil {
man.Logger.ReportError(err.Error())
return err
}
man.FileList[tag] = details.File
if details.Deps == nil {
return nil
}
for _, name := range details.Deps {
err = man.GenerateFileList(par, name)
if err != nil {
man.Logger.ReportError(err.Error())
return err
}
}
man.Logger.ReportSuccess("Successfully generated file list")
return nil
}
//GenerateList Generate a brand new details.json
func (man *Manager) GenerateList() error {
for _, value := range man.FileList {
file, err := ioutil.ReadFile(value)
if err != nil {
man.Logger.ReportError(err.Error())
return err
}
hash := HashFile(file)
man.NewFileTimings[value] = hash
man.HashJSONnew.Body.Entity = append(man.HashJSONnew.Body.Entity,
Entity{File: value, Hash: hash})
}
man.Logger.ReportSuccess("Successfully generated details.json")
return nil
}
//NewManager Helper function to create a new manager
func NewManager(log *lg.Logger) (Manager, error) {
temp, err := ioutil.ReadFile("Recipe")
if err != nil {
//Missing Recipe File
log.ReportError(err.Error())
return Manager{}, err
}
recipe := string(temp)
man := Manager{
FileData: recipe,
NewFileTimings: make(map[string]uint32),
OldFileTimings: make(map[string]uint32),
FileList: make(map[string]string),
Logger: log,
}
man.Logger.ReportSuccess("Successfully created a Manager Object")
return man, nil
}
//HashFile Obtaining the has of the passed file
func HashFile(file []byte) uint32 {
hash := crc32.ChecksumIEEE(file)
return hash
}
//CheckHash Comparing hashes of the passed file with the previous hash
func CheckHash(file []byte, hash uint32) bool {
generatedHash := crc32.ChecksumIEEE(file)
return generatedHash == hash
}