~welt/murse

ref: v0.0.1 murse/verify.go -rw-r--r-- 2.9 KiB
f34e87efwelt Fix concurrency bug, simplify upgrade function, add verify command. 5 months ago
                                                                                
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
170
171
172
173
174
175
176
177
package main

import (
	"crypto/md5"
	"encoding/hex"
	"fmt"
	"io"
	"os"
	"path/filepath"
)

func verifyMain(dir string, url string, repair bool, http2 bool) int {
	instRev, err := getInstalledRevision(dir)
	if err != nil {
		errPrintln(err)
		return 1
	}

	client := NewClient(url)

	if !http2 {
		client.DisableHTTP2()
	}

	var revisions [][]Change
	for i := 0; i != instRev+1; i++ {
		rev, err := client.GetRevision(i)
		if err != nil {
			errPrintln(err)
			return 1
		}

		revisions = append(revisions, rev)
	}

	changes := replayChanges(revisions)

	writes, mkdirs, deletes := splitChanges(changes)

	for _, v := range deletes {
		path := filepath.Join(dir, filepath.FromSlash(v.Path))
		_, err := os.Stat(path)
		if err != nil {
			if !os.IsNotExist(err) {
				errPrintln(err)
				return 1
			}
		} else {
			errPrintln("EXISTS", v.Path)
			if repair {
				err = os.Remove(path)
				if err != nil {
					errPrintln(err)
					return 1
				}
				fmt.Println("FIXDIR", v.Path)
			}
		}
	}

	for _, v := range mkdirs {
		path := filepath.Join(dir, filepath.FromSlash(v.Path))

		stat, err := os.Stat(path)
		if err != nil {
			if os.IsNotExist(err) {
				fmt.Println("MISSING", v.Path)
				if repair {
					err := os.MkdirAll(path, 0777)
					if err != nil {
						errPrintln(err)
						return 1
					}

					fmt.Println("FIXDIR", v.Path)

				}
			} else {
				errPrintln(err)
				return 1
			}
		} else {
			if !stat.IsDir() {
				fmt.Println("NOTDIR", v.Path)
				if repair {
					err := os.RemoveAll(path)
					if err != nil {
						errPrintln(err)
						return 1
					}

					err = os.MkdirAll(path, 0777)
					if err != nil {
						errPrintln(err)
						return 1
					}

					fmt.Println("FIXDIR", v.Path)

				}
			}
		}
	}

	for _, v := range writes {
		path := filepath.Join(dir, filepath.FromSlash(v.Path))
		var fix func() error

		if repair {
			fix = func() error {
				object, err := client.GetObject(v.Object)
				if err != nil {
					return err
				}

				defer object.Close()

				file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0777)
				if err != nil {
					return err
				}

				defer file.Close()

				_, err = io.Copy(file, object)
				if err == nil {
					fmt.Println("FIXWRITE", v.Path)
				}
				return err
			}
		}

		file, err := os.Open(path)
		if err != nil {
			if !os.IsNotExist(err) {
				errPrintln(err)
				return 1
			} else {
				fmt.Println("MISSING", v.Path)
				if repair {
					err := fix()
					if err != nil {
						errPrintln(err)
						return 1
					}
				}
			}
		} else {
			defer file.Close()

			hash := md5.New()
			_, err = io.Copy(hash, file)
			if err != nil {
				errPrintln(err)
				return 1
			}

			file.Close()

			md5 := hex.EncodeToString(hash.Sum(nil))
			if v.MD5 != md5 {
				fmt.Println("INCORRECT", v.Path, "EXPECTED", v.MD5, "GOT", md5)
				if repair {
					err := fix()
					if err != nil {
						errPrintln(err)
						return 1
					}
				}
			}
		}

	}

	return 0

}