~welt/murse

ref: cfb166c0599325f5d0c175f8686ab1f0dfa75bd1 murse/upgrade.go -rw-r--r-- 3.1 KiB
cfb166c0welt Update Makefile for new packaging. 3 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main

import (
	"fmt"
	"io"
	"os"
	"path/filepath"
	"sort"
	"strconv"
	"strings"
)

func upgradeMain(dir string, url string, threads int, http2 bool, owGameInfo bool) int {
	if !strings.HasSuffix(url, "/") {
		url = url + "/"
	}

	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 {
			errPrintln(err)
			return 1
		}

		revisions = append(revisions, rev)
	}

	changes := replayChanges(revisions)
	writes, mkdirs, dels := splitChanges(changes)

	for _, v := range dels {
		path, err := resolvePath(dir, v.Path)
		if err != nil {
			errPrintln(err)
			return 1
		}

		err = os.Remove(path)
		if err != nil && !os.IsNotExist(err) {
			errPrintln(err)
			return 1
		}

		fmt.Println("DEL", v.Path)
	}

	var mkdirpaths []string
	for _, v := range mkdirs {
		mkdirpaths = append(mkdirpaths, v.Path)
	}

	// Sort so that parents are done first,
	// then children.
	sort.Strings(mkdirpaths)

	for _, change := range mkdirpaths {
		path, err := resolvePath(dir, change)
		if err != nil {
			errPrintln(err)
			return 1
		}

		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("MKDIR", change)
	}

	pool := NewPool(threads)

	dealErr := func() bool {
		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 false
			}

		default:
		}

		return true
	}

	isOF := (filepath.Dir(dir) == "open_fortress")

	for _, v := range writes {
		change := v
		path, err := resolvePath(dir, v.Path)
		if err != nil {
			pool.Stop()
			dealErr()
			errPrintln(err)
			return 1
		}

		f := func() error {
			if !owGameInfo && isOF && change.Path == "gameinfo.txt" {
				exists, err := isExists(path)
				if err != nil {
					return err
				}

				if exists {
					fmt.Println("IGNORE", change.Path)
					return nil
				}
			}

			fmt.Println("WRITE", change.Path)
			object, err := client.GetObject(change.Object)
			if err != nil {
				return err
			}

			defer object.Close()

			err = os.RemoveAll(path)
			if err != nil {
				return err
			}
			file, err := os.Create(path)
			if err != nil {
				return err
			}

			defer file.Close()

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

		pool.Jobch <- f

		if ok := dealErr(); !ok {
			return 1
		}
	}

	pool.Stop()

	if ok := dealErr(); !ok {
		return 1
	}

	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

}