/
copier.go
266 lines (232 loc) · 6.23 KB
/
copier.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package steammesh
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path"
"sync"
"time"
"github.com/m1cr0man/steampump/pkg/steam"
)
var copyLock sync.Mutex = sync.Mutex{}
// GameCopier operation. Copies games between a peer and the local host
type GameCopier struct {
Status CopierStatus `json:"status"`
AppID int `json:"appid"`
BytesDone int64 `json:"bytes_done"`
BytesTotal int64 `json:"bytes_total"`
Files int `json:"files"`
Peer Peer `json:"peer"`
Dest string `json:"dest"`
Steam *steam.API
}
func (g *GameCopier) getPaths(remoteURL *url.URL, item *SyncItem) (string, *url.URL) {
localPath := path.Join(g.Dest, item.Path)
relPath, _ := url.Parse(item.Path)
remotePath := remoteURL.ResolveReference(relPath)
return localPath, remotePath
}
// DiffDirectory Get the difference between a remote path and local directory
func (g *GameCopier) DiffDirectory(remoteURL *url.URL, item *SyncItem) (items []*SyncItem, err error) {
localPath, remotePath := g.getPaths(remoteURL, item)
req, _ := http.NewRequest(http.MethodGet, remotePath.String(), nil)
req.Header.Add("Accept", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
return
}
remoteFiles := []TransferItem{}
err = json.NewDecoder(res.Body).Decode(&remoteFiles)
if err != nil {
return
}
localFiles, err := ioutil.ReadDir(localPath)
if os.IsNotExist(err) {
err = nil
localFiles = []os.FileInfo{}
}
if err != nil {
return
}
// File paths from remote are just file names relative to the requested folder
remoteMap := make(map[string]TransferItem, len(remoteFiles))
for _, file := range remoteFiles {
remoteMap[file.Path] = file
}
localMap := make(map[string]os.FileInfo, len(localFiles))
for _, file := range localFiles {
localMap[file.Name()] = file
}
for _, file := range localFiles {
// Delete
if _, ok := remoteMap[file.Name()]; !ok {
localFile := path.Join(item.Path, file.Name()) // TODO check
items = append(items, &SyncItem{
TransferItem: TransferItem{
Path: localFile,
Mtime: file.ModTime(),
Mode: file.Mode(),
Size: file.Size(),
},
Action: ActionDelete,
})
}
}
for _, ritem := range remoteFiles {
// Create or modify
if file, ok := localMap[ritem.Path]; !(ok && file.ModTime().Equal(ritem.Mtime)) {
// Make ritem.Path relative to requested directory
// It is just the file name at the moment
ritem.Path = path.Join(item.Path, ritem.Path)
items = append(items, &SyncItem{
TransferItem: ritem,
Action: ActionWrite,
})
g.BytesTotal += ritem.Size
g.Files++
}
}
return
}
// DownloadItem downloads a file from a HTTP endpoint
func (g *GameCopier) DownloadItem(remoteURL *url.URL, item *SyncItem) (err error) {
localPath, remotePath := g.getPaths(remoteURL, item)
fd, err := os.Create(localPath)
if err != nil {
return
}
defer fd.Close()
res, err := http.Get(remotePath.String())
if err != nil {
return
}
defer res.Body.Close()
buf := make([]byte, IOBufferSize)
var readerr error
var n int = IOBufferSize
for {
n, readerr = res.Body.Read(buf)
// Skip checking readerr, it might just be EOF
// Write returns an error if num bytes written != len of input
_, err = fd.Write(buf[:n])
if err != nil {
return
}
g.BytesDone += int64(n)
if readerr == io.EOF {
break
}
if readerr != nil {
return readerr
}
}
err = os.Chmod(localPath, item.Mode.Perm())
return
}
// CopyGameFrom Start copying the game using the pre configured parameters
func (g *GameCopier) CopyGameFrom() (err error) {
g.Status = StatusRunning
fmt.Printf("Starting copy: %d from %s to %s\n", g.AppID, g.Peer.Url().String(), g.Dest)
relPath, _ := url.Parse(fmt.Sprintf("games/%d/content/", g.AppID))
remoteURL := g.Peer.Url().ResolveReference(relPath)
// Create an initial sync item for the root of the game
items := []*SyncItem{
&SyncItem{
TransferItem: TransferItem{
Path: ".",
Mtime: time.Now(),
Mode: 0755 | os.ModeDir,
Size: 0,
},
Action: ActionWrite,
},
}
var newItems []*SyncItem
var item *SyncItem
for len(items) > 0 {
// Pop sync item
item = items[0]
items = items[1:]
localPath := path.Join(g.Dest, item.Path)
fmt.Println(item.Path)
// Write or delete? write covers syncing folders
switch item.Action {
case ActionWrite:
// Directory? create it and sync
if item.Mode.IsDir() {
if err = os.MkdirAll(localPath, item.Mode); err != nil {
return
}
newItems, err = g.DiffDirectory(remoteURL, item)
items = append(items, newItems...)
// File? Download it
} else {
err = g.DownloadItem(remoteURL, item)
}
if err != nil {
return
}
err = os.Chtimes(localPath, item.Mtime, item.Mtime)
case ActionDelete:
err = os.RemoveAll(localPath)
}
if err != nil {
return
}
}
return
}
func (g *GameCopier) genericCopy(src, dest string) (err error) {
relURL, _ := url.Parse(src)
mfstres, err := http.Get(g.Peer.Url().ResolveReference(relURL).String())
if err != nil {
return
}
data, err := ioutil.ReadAll(mfstres.Body)
if err != nil {
return
}
err = ioutil.WriteFile(dest, data, 0644)
if err != nil {
return
}
err = g.Steam.LoadGames()
return
}
func (g *GameCopier) CopyManifestFrom() (err error) {
err = g.genericCopy(fmt.Sprintf("games/%d/manifest", g.AppID), g.Steam.GetGameManifestPath(g.AppID))
if err != nil {
return
}
err = g.Steam.LoadGames()
return
}
func (g *GameCopier) CopyHeaderImageFrom() error {
return g.genericCopy(fmt.Sprintf("games/%d/images/header", g.AppID), g.Steam.GetGameHeaderImagePath(g.AppID))
}
// StartCopy wait for locks and start copying the game
func (g *GameCopier) StartCopy() {
g.Status = StatusQueued
copyLock.Lock()
err := g.CopyGameFrom()
copyLock.Unlock()
if err != nil {
fmt.Println("Copy", g.AppID, "failed with error:", err)
g.Status = StatusFailed
}
err = g.CopyManifestFrom()
if err != nil {
fmt.Println("Copy manifest of", g.AppID, "failed with error:", err)
g.Status = StatusFailed
} else {
// Don't really care if the header image fails
_ = g.CopyHeaderImageFrom()
g.Steam.DeleteDownloadData(g.AppID)
fmt.Println("Copy", g.AppID, "completed successfully")
g.Status = StatusSuccessful
}
}