~welt/murse

ref: v0.0.1 murse/upgrade.go -rw-r--r-- 2.4 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
package main

import (
	"fmt"
	"io"
	"os"
	"path/filepath"
	"runtime/debug"
	"strconv"
)

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

	client := NewClient(url)
	if !http2 {
		client.DisableHTTP2()
	}

	latestRev, err := client.GetLatestRevision()
	if err != nil {
		errPrintln(err)
		return 1
	}

	errPrintln(instRev, "->", latestRev)

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

		revisions = append(revisions, rev)
	}

	changes := replayChanges(revisions)
	writes, mkdirs, dels := splitChanges(changes)
	err = os.RemoveAll(filepath.Join(dir, ".revision"))
	if err != nil {
		errPrintln(err)
		return 1
	}

	for _, v := range dels {
		fmt.Println("DEL", v.Path)
		path := filepath.FromSlash(v.Path)
		err := os.Remove(filepath.Join(dir, path))
		if err != nil && !os.IsNotExist(err) {
			errPrintln(err)
			return 1
		}
	}

	for _, v := range mkdirs {
		path := filepath.FromSlash(v.Path)
		err := os.RemoveAll(path)
		if err != nil {
			errPrintln(err)
			return 1
		}
		err = os.MkdirAll(filepath.Join(dir, path), 0777)
		if err != nil {
			errPrintln(err)
			return 1
		}
	}

	pool := NewPool(threads)

	for _, v := range writes {
		change := v
		f := func() error {
			fmt.Println("WRITE", change.Path)
			object, err := client.GetObject(change.Object)
			if err != nil {
				return err
			}

			defer object.Close()

			path := filepath.FromSlash(change.Path)
			fpath := filepath.Join(dir, path)
			err = os.RemoveAll(fpath)
			if err != nil {
				return err
			}
			file, err := os.Create(fpath)
			if err != nil {
				return err
			}

			defer file.Close()

			_, err = io.Copy(file, object)
			return err
		}

		pool.Jobch <- f

		select {
		case err := <-pool.Errch:
			if err != nil {
				pool.Stop()
				errPrintln(err)
				for {
					select {
					case err := <-pool.Errch:
						errPrintln(err)
					default:
						goto postloop
					}
				}
			postloop:
				return 1
			}
		default:
			continue
		}
	}

	pool.Stop()

	file, err := os.Create(filepath.Join(dir, ".revision"))
	if err != nil {
		errPrintln(err)
		return 1
	}

	defer file.Close()

	_, err = file.Write([]byte(strconv.Itoa(latestRev)))
	if err != nil {
		errPrintln(err)
		return 1
	}

	return 0

}