~welt/murse

ref: a19028ef58f0fb6f5aa0272bff58bf13cc556081 murse/upgrade.go -rw-r--r-- 2.7 KiB
a19028efwelt I'm stupid. 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
package main

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

func upgradeMain(dir string, url string, threads int, http2 bool, overwGameInfo 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 {
			debug.PrintStack()
			errPrintln(err)
			return 1
		}

		revisions = append(revisions, rev)
	}

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

	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)

	dealErr := func() bool {
		select {
		case err := <-pool.Errch:
			if err != nil {
				pool.Stop()
				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 := filepath.FromSlash(change.Path)
		fpath := filepath.Join(dir, path)
		f := func() error {
			if !overwGameInfo && isOF && change.Path == "gameinfo.txt" {
				exists, err := isExists(fpath)
				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(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

		ok := dealErr()
		if !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

}