forked from dhaivat/apt-gcs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apt.go
160 lines (134 loc) · 3.41 KB
/
apt.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
package gcs
import (
"bufio"
"bytes"
"crypto/md5"
"crypto/sha256"
"crypto/sha512"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
)
var (
messageCodes = map[int]string{
100: "Capabilities",
102: "Status",
200: "URI Start",
201: "URI Done",
400: "URI Failure",
600: "URI Acquire",
601: "Configuration",
}
)
type AptMessage struct {
Code int
Headers map[string]string
}
func (a *AptMessage) Encode() string {
var buf bytes.Buffer
buf.WriteString(fmt.Sprintf("%v %v\n", a.Code, messageCodes[a.Code]))
for key, value := range a.Headers {
buf.WriteString(fmt.Sprintf("%v: %v\n", key, value))
}
buf.WriteRune('\n')
return buf.String()
}
type AptMethod struct {
}
func (a *AptMethod) Send(code int, headers map[string]string) {
msg := AptMessage{Code: code, Headers: headers}
fmt.Fprintf(os.Stdout, msg.Encode())
}
func (a *AptMethod) SendCapabilities() {
a.Send(100, map[string]string{"Version": "1.0", "Single-Instance": "true"})
}
func (a *AptMethod) SendStatus(headers map[string]string) {
a.Send(102, headers)
}
func (a *AptMethod) SendUriStart(headers map[string]string) {
a.Send(200, headers)
}
func (a *AptMethod) SendUriDone(headers map[string]string) {
a.Send(201, headers)
}
func (a *AptMethod) SendUriFailure(headers map[string]string) {
a.Send(400, headers)
}
func (a *AptMethod) Run() (exitCode int) {
for {
message := a.readMessage()
if len(message) == 0 {
return 0
}
if message["_number"] == "600" {
a.ReadObejct(message)
} else {
return 100
}
}
}
func (a *AptMethod) readMessage() (message map[string]string) {
message = map[string]string{}
reader := bufio.NewReader(os.Stdin)
line, err := reader.ReadString('\n')
if err == io.EOF {
return message
}
for line == "\n" {
line, err = reader.ReadString('\n')
if err == io.EOF {
return message
}
}
// read the last read non blank link
s := strings.SplitN(line, " ", 2)
message["_number"] = s[0]
message["_text"] = strings.TrimSpace(s[1])
for {
line, err := reader.ReadString('\n')
if err == io.EOF || line == "\n" {
return message
}
s := strings.SplitN(line, ":", 2)
message[s[0]] = strings.TrimSpace(s[1])
}
}
func (a *AptMethod) ReadObejct(message map[string]string) {
uri := message["URI"]
filename := message["Filename"]
a.SendStatus(map[string]string{"URI": uri, "Message": "Waiting for headers"})
splits := strings.SplitN(uri, "/", 4)
bucket := splits[2]
object := splits[3]
if resp, err := oService.Get(bucket, object).Download(); err == nil {
defer resp.Body.Close()
a.SendUriStart(map[string]string{
"URI": uri,
"Size": strconv.FormatInt(resp.ContentLength, 10),
"Last-Modified": resp.Header.Get("last-modified")})
body, _ := ioutil.ReadAll(resp.Body)
ioutil.WriteFile(filename, body, 0644)
md5sum := md5.Sum(body)
sha256sum := sha256.Sum256(body)
sha512sum := sha512.Sum512(body)
a.SendUriDone(map[string]string{
"URI": uri,
"Filename": filename,
"Size": strconv.FormatInt(resp.ContentLength, 10),
"Last-Modified": resp.Header.Get("last-modified"),
"MD5-Hash": fmt.Sprintf("%x", md5sum),
"MD5Sum-Hash": fmt.Sprintf("%x", md5sum),
"SHA256-Hash": fmt.Sprintf("%x", sha256sum),
"SHA512-Hash": fmt.Sprintf("%x", sha512sum),
})
} else {
a.SendUriFailure(map[string]string{
"URI": uri,
"Message": "silly failure",
"FailReason": "really silly failure",
})
}
}